homebridge-plugin-haier-v2 2.6.7-beta.0 → 2.6.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";let __rslib_import_meta_url__="undefined"==typeof document?new(require("url".replace("",""))).URL("file:"+__filename).href:document.currentScript&&document.currentScript.src||new URL("main.js",document.baseURI).href;var __webpack_modules__={"./package.json":function(e){e.exports=JSON.parse('{"eN":{"H":"https://github.com/zwb124/homebridge-plugin-haier-v2/issues"}}')}},__webpack_module_cache__={};function __webpack_require__(e){var t=__webpack_module_cache__[e];if(void 0!==t)return t.exports;var i=__webpack_module_cache__[e]={exports:{}};return __webpack_modules__[e](i,i.exports,__webpack_require__),i.exports}__webpack_require__.d=function(e,t){for(var i in t)__webpack_require__.o(t,i)&&!__webpack_require__.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},__webpack_require__.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},__webpack_require__.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var __webpack_exports__={};(()=>{__webpack_require__.r(__webpack_exports__),__webpack_require__.d(__webpack_exports__,{default:()=>u});let e=require("homebridge-plugin-haier-v2/shared"),t=require("haier-iot");function i(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}class r{async initializeAccessory(){await this.getDevDigitalModel();let{deviceInfo:e}=this.accessory.context;this.setupListeners(e.baseInfo.deviceId),this.setupAccessoryInformation(e),this.init()}setupListeners(e){this.platform.haierIoT.addListener("devDigitalModelUpdate",(t,i)=>{e===t&&(this.accessory.context.devDigitalModel=i,this.onDevDigitalModelUpdate())})}setupAccessoryInformation(e){var t;let{Characteristic:i,Service:r}=this.platform;null===(t=this.accessory.getService(r.AccessoryInformation))||void 0===t||t.setCharacteristic(i.Manufacturer,e.extendedInfo.brand).setCharacteristic(i.Model,e.extendedInfo.model).setCharacteristic(i.SerialNumber,e.extendedInfo.prodNo)}setServices(e,t,i){let r=this.accessory.getService(e);if(r)return this.services[e]=r,r;let a=this.accessory.displayName;i&&(a+=` - ${i}`);let s=this.accessory.addService(t,a,e);return s.setCharacteristic(this.Characteristic.Name,a),this.services[e]=s,s}get Characteristic(){return this.platform.Characteristic}get deviceInfo(){return this.accessory.context.deviceInfo}get deviceProperties(){var e;return Object.fromEntries(((null===(e=this.accessory.context.devDigitalModel)||void 0===e?void 0:e.attributes)??[]).map(e=>[e.name,e]))}getPropertyValue(t,i){var r,a;return(0,e.safeJsonParse)((null===(a=this.deviceProperties)||void 0===a?void 0:null===(r=a[t])||void 0===r?void 0:r.value)??void 0,i)}async getDevDigitalModel(){let{deviceId:e,isOnline:t}=this.deviceInfo.baseInfo;if(!t)return this.platform.log.warn(`设备 ${this.accessory.displayName} 离线`),null;if(this.isFetchingDevDigitalModel)return this.devDigitalModelPromise??this.accessory.context.devDigitalModel;this.isFetchingDevDigitalModel=!0;try{this.devDigitalModelPromise??=this.platform.haierIoT.getDevDigitalModel(e);let t=await this.devDigitalModelPromise;if(!t)return this.platform.log.error(`设备 ${this.accessory.displayName} 的数据为空`),null;return this.accessory.context.devDigitalModel={...t},t}catch(e){return this.platform.log.error(`获取设备 ${this.accessory.displayName} 数据失败`,e),this.accessory.context.devDigitalModel}finally{this.isFetchingDevDigitalModel=!1,this.devDigitalModelPromise=void 0}}async sendCommands(...e){e.forEach(e=>{Object.entries(e).forEach(([e,t])=>{let{valueRange:i,desc:r}=this.deviceProperties[e]??{},a=t;if(i){if("LIST"===i.type){var s;let e=null===(s=i.dataList)||void 0===s?void 0:s.find(e=>e.data===t);a=(null==e?void 0:e.desc)??t}this.platform.log.info("设置",this.accessory.displayName,"的",r,"为",a)}})}),await this.platform.haierIoT.sendCommands(this.deviceInfo.baseInfo.deviceId,e),this.onDevDigitalModelUpdate()}constructor(e,t){i(this,"platform",void 0),i(this,"accessory",void 0),i(this,"services",void 0),i(this,"devDigitalModelPromise",void 0),i(this,"isFetchingDevDigitalModel",void 0),this.platform=e,this.accessory=t,this.services={},this.isFetchingDevDigitalModel=!1,this.initializeAccessory().catch(e=>this.platform.log.error(`初始化设备 ${this.accessory.displayName} 时出错:`,e))}}class a extends r{init(){let e=this.setServices("thermostat",this.platform.Service.Thermostat),{CurrentHeatingCoolingState:t,TargetHeatingCoolingState:i,CurrentTemperature:r,TargetTemperature:a,TemperatureDisplayUnits:s,CurrentRelativeHumidity:o,TargetRelativeHumidity:n}=this.Characteristic;e.getCharacteristic(t).onGet(this.getCurrentHeatingCoolingState.bind(this)),e.getCharacteristic(i).onGet(this.getTargetHeatingCoolingState.bind(this)).onSet(this.setTargetHeatingCoolingState.bind(this)),e.getCharacteristic(r).onGet(this.getCurrentTemperature.bind(this)),e.getCharacteristic(a).setProps({...this.targetTemperatureProps}).onGet(this.getTargetTemperature.bind(this)).onSet(this.setTargetTemperature.bind(this)),e.getCharacteristic(s).onGet(this.getTemperatureDisplayUnits.bind(this)),this.hasIndoorHumidity&&e.getCharacteristic(o).onGet(this.getCurrentRelativeHumidity.bind(this)),this.hasTargetHumidity&&e.getCharacteristic(n).onGet(this.getTargetRelativeHumidity.bind(this))}onDevDigitalModelUpdate(){let{CurrentHeatingCoolingState:t,TargetHeatingCoolingState:i,CurrentTemperature:r,TargetTemperature:a,CurrentRelativeHumidity:s,TargetRelativeHumidity:o}=this.Characteristic,n=this.services.thermostat;n&&((0,e.isNumber)(this.targetTemperature)&&n.getCharacteristic(a).setProps({...this.targetTemperatureProps}).updateValue(this.targetTemperature),(0,e.isNumber)(this.currentTemperature)&&n.getCharacteristic(r).updateValue(this.currentTemperature),n.getCharacteristic(t).updateValue(this.getCurrentHeatingCoolingState()),n.getCharacteristic(i).updateValue(this.getTargetHeatingCoolingState()),this.hasIndoorHumidity&&n.getCharacteristic(s).updateValue(this.getCurrentRelativeHumidity()),this.hasTargetHumidity&&n.getCharacteristic(o).updateValue(this.getTargetRelativeHumidity()))}get hasIndoorHumidity(){return!!this.deviceProperties.indoorHumidity}get hasTargetHumidity(){return!!this.deviceProperties.targetHumidity}get onOffStatus(){return this.getPropertyValue("onOffStatus",!1)}get currentTemperature(){return this.getPropertyValue("indoorTemperature")}get targetTemperature(){return this.getPropertyValue("targetTemperature")}get targetTemperatureProps(){var e;let t=null===(e=this.deviceProperties.targetTemp)||void 0===e?void 0:e.valueRange;if(!t||"STEP"!==t.type||!t.dataStep)return;let i=Number.parseFloat(t.dataStep.minValue),r=Number.parseFloat(t.dataStep.maxValue);return{minValue:i,maxValue:r,minStep:Number.parseFloat(t.dataStep.step),validValueRanges:[i,r]}}getCurrentHeatingCoolingState(){var e;let{CurrentHeatingCoolingState:t}=this.Characteristic;if(!this.onOffStatus)return t.OFF;switch(null===(e=this.deviceProperties.operationMode)||void 0===e?void 0:e.value){case"1":return t.COOL;case"4":return t.HEAT;default:if(this.currentTemperature??0>(this.targetTemperature??0))return t.COOL;return t.HEAT}}getTargetHeatingCoolingState(){var e;let{TargetHeatingCoolingState:t}=this.Characteristic;if(!this.onOffStatus)return t.OFF;switch(null===(e=this.deviceProperties.operationMode)||void 0===e?void 0:e.value){case"1":return t.COOL;case"4":return t.HEAT;default:return t.AUTO}}setTargetHeatingCoolingState(e){let{TargetHeatingCoolingState:t}=this.Characteristic;if(e===t.OFF)return this.sendCommands({onOffStatus:"false"});let i={[t.AUTO]:"0",[t.COOL]:"1",[t.HEAT]:"4"}[e];if(i)return this.sendCommands({onOffStatus:"true"},{operationMode:i});this.platform.log.warn("Unsupported TargetHeatingCoolingState:",e)}getCurrentTemperature(){return this.currentTemperature}getTargetTemperature(){return this.targetTemperature}setTargetTemperature(e){let{minValue:t=10,maxValue:i=38}=this.targetTemperatureProps??{},r=[{targetTemperature:Math.min(Math.max(Number.parseFloat(e.toString()),t),i).toString()}];return this.onOffStatus||r.unshift({onOffStatus:"true"}),this.sendCommands(...r)}getTemperatureDisplayUnits(){var e;return(null===(e=this.deviceProperties.tempUnit)||void 0===e?void 0:e.value)==="2"?this.Characteristic.TemperatureDisplayUnits.FAHRENHEIT:this.Characteristic.TemperatureDisplayUnits.CELSIUS}getCurrentRelativeHumidity(){return this.getPropertyValue("indoorHumidity")}getTargetRelativeHumidity(){return this.getPropertyValue("targetHumidity")}}let s=require("homebridge");class o extends r{async init(){this.setServices("thermostat",this.platform.Service.Thermostat),this.services.thermostat.getCharacteristic(this.Characteristic.CurrentHeatingCoolingState).onGet(this.getCurrentHeatingCoolingState.bind(this)),this.services.thermostat.getCharacteristic(this.Characteristic.TargetHeatingCoolingState).setProps({validValues:[this.Characteristic.TargetHeatingCoolingState.OFF,this.Characteristic.TargetHeatingCoolingState.HEAT]}).onGet(this.getCurrentHeatingCoolingState.bind(this)).onSet(this.setTargetHeatingCoolingState.bind(this)),this.services.thermostat.getCharacteristic(this.Characteristic.CurrentTemperature).onGet(this.getTargetTemperature.bind(this)),this.services.thermostat.getCharacteristic(this.Characteristic.TargetTemperature).setProps({...this.targetTemperatureProps}).onGet(this.getTargetTemperature.bind(this)).onSet(this.setTargetTemperature.bind(this)),this.services.thermostat.getCharacteristic(this.Characteristic.TemperatureDisplayUnits).onGet(()=>this.Characteristic.TemperatureDisplayUnits.CELSIUS),this.getPropertyValue("zeroColdWaterFuncExist")&&(this.setServices("switch",this.platform.Service.Switch,"零冷水"),this.services.switch.getCharacteristic(this.Characteristic.On).onGet(this.getZeroColdWaterStatus.bind(this)).onSet(this.setZeroColdWaterStatus.bind(this))),this.deviceProperties.flowStatus&&(this.setServices("flowStatus",this.platform.Service.Valve,"水流状态"),this.services.flowStatus.getCharacteristic(this.Characteristic.Active).setProps({perms:[s.Perms.PAIRED_READ]}).onGet(()=>this.getPropertyValue("flowStatus")?this.Characteristic.Active.ACTIVE:this.Characteristic.Active.INACTIVE),this.services.flowStatus.getCharacteristic(this.Characteristic.InUse).onGet(()=>this.getPropertyValue("flowStatus")?this.Characteristic.InUse.IN_USE:this.Characteristic.InUse.NOT_IN_USE)),this.oneShotState&&this.scheduleOneShotCompletion()}onDevDigitalModelUpdate(){let t=this.services.thermostat;if(t&&((0,e.isNumber)(this.targetTemperature)&&(t.getCharacteristic(this.Characteristic.TargetTemperature).setProps({...this.targetTemperatureProps}).updateValue(this.targetTemperature),t.getCharacteristic(this.Characteristic.CurrentTemperature).updateValue(this.targetTemperature)),this.getPropertyValue("zeroColdWaterFuncExist"))){var i;null===(i=this.services.switch)||void 0===i||i.getCharacteristic(this.Characteristic.On).updateValue(this.getZeroColdWaterStatus())}}get onOffStatus(){return this.getPropertyValue("onOffStatus")}get targetTemperature(){return this.getPropertyValue("targetTemp")}get targetTemperatureProps(){var e;let t=null===(e=this.deviceProperties.targetTemp)||void 0===e?void 0:e.valueRange;if(!t||"STEP"!==t.type||!t.dataStep)return;let i=Number.parseFloat(t.dataStep.minValue),r=Number.parseFloat(t.dataStep.maxValue);return{minValue:i,maxValue:r,minStep:Number.parseFloat(t.dataStep.step),validValueRanges:[i,r]}}getCurrentHeatingCoolingState(){return this.onOffStatus?this.Characteristic.CurrentHeatingCoolingState.HEAT:this.Characteristic.CurrentHeatingCoolingState.OFF}setTargetHeatingCoolingState(e){return this.sendCommands({onOffStatus:(!!e).toString()})}getTargetTemperature(){return this.targetTemperature}setTargetTemperature(e){let{minValue:t=0,maxValue:i=100}=this.targetTemperatureProps??{},r=Math.min(Math.max(Number.parseFloat(e.toString()),t),i).toString();return this.sendCommands({targetTemp:r})}get oneShotState(){return this.accessory.context.zeroColdWaterOneShot}get oneShotDurationMs(){var e,t;let i=null===(t=this.platform.config.customConfig)||void 0===t?void 0:null===(e=t.hotWater)||void 0===e?void 0:e.zeroColdWaterDurationMinutes;return 6e4*Math.min(Math.max(Number.isFinite(i)?Number(i):15,1),120)}getZeroColdWaterStatus(){var e;return(null===(e=this.oneShotState)||void 0===e?void 0:e.phase)==="running"}async setZeroColdWaterStatus(e){if(e){if(this.oneShotState)return;this.accessory.context.zeroColdWaterOneShot={startedAt:Date.now(),durationMs:this.oneShotDurationMs,phase:"running"},this.platform.api.updatePlatformAccessories([this.accessory]);try{await this.sendCommands({zeroColdWaterStatus:"1"}),this.scheduleOneShotCompletion()}catch(e){var t;throw delete this.accessory.context.zeroColdWaterOneShot,this.platform.api.updatePlatformAccessories([this.accessory]),null===(t=this.services.switch)||void 0===t||t.getCharacteristic(this.Characteristic.On).updateValue(!1),e}return}this.oneShotTimer&&(clearTimeout(this.oneShotTimer),this.oneShotTimer=void 0),this.oneShotState&&await this.completeOneShot()}scheduleOneShotCompletion(){this.oneShotTimer&&clearTimeout(this.oneShotTimer);let e=this.oneShotState;if(!e)return;let t="restoring"===e.phase?0:e.startedAt+e.durationMs-Date.now();if(t<=0){this.completeOneShot();return}this.oneShotTimer=setTimeout(()=>{this.completeOneShot()},t)}async completeOneShot(){var e;this.oneShotTimer&&(clearTimeout(this.oneShotTimer),this.oneShotTimer=void 0);let t=this.oneShotState;if(t){t.phase="restoring",this.platform.api.updatePlatformAccessories([this.accessory]),null===(e=this.services.switch)||void 0===e||e.getCharacteristic(this.Characteristic.On).updateValue(!1);try{await this.sendCommands({zeroColdWaterStatus:"4"}),delete this.accessory.context.zeroColdWaterOneShot,this.platform.api.updatePlatformAccessories([this.accessory]),this.platform.log.info("热水器单次零冷水循环计时结束,已恢复云智慧(AI)循环模式:",this.accessory.displayName)}catch(e){this.platform.api.updatePlatformAccessories([this.accessory]),this.oneShotTimer=setTimeout(()=>void this.completeOneShot(),6e4),this.platform.log.error("恢复热水器云智慧(AI)零冷水模式失败,60 秒后重试:",this.accessory.displayName,e)}}}constructor(...e){super(...e),function(e,t,i){t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i}(this,"oneShotTimer",void 0)}}let n={refrigerator:{displayName:"冷藏室",currentTempKey:"refrigeratorTemperatureC",targetTempKey:"refrigeratorTargetTempLevel"},freezer:{displayName:"冷冻室",currentTempKey:"freezerTemperatureC",targetTempKey:"freezerTargetTempLevel"},vtRoom:{displayName:"变温室",currentTempKey:"vtRoomTemperature",targetTempKey:"vtRoomTargetTempLevel"}};class c extends r{async init(){Object.entries(n).forEach(([e,{displayName:t,currentTempKey:i,targetTempKey:r}])=>{this.createTemperatureService(e,t,i,r)})}getActive(){return this.deviceInfo.baseInfo.isOnline?this.Characteristic.Active.ACTIVE:this.Characteristic.Active.INACTIVE}getCurrentHeaterCoolerState(){return this.deviceInfo.baseInfo.isOnline?this.Characteristic.CurrentHeaterCoolerState.COOLING:this.Characteristic.CurrentHeaterCoolerState.INACTIVE}parseTemperature(e){return e?Number.parseInt(e):0}onDevDigitalModelUpdate(){Object.entries(n).forEach(([e,{currentTempKey:t,targetTempKey:i}])=>{let r=this.services[e];if(!r)return;let a=this.deviceProperties[t],s=this.deviceProperties[i];if(!a||!s)return;let o=this.extractCelsiusDataMapping(s.valueRange);r.getCharacteristic(this.Characteristic.CurrentTemperature).updateValue(this.parseTemperature(a.value??void 0)),r.getCharacteristic(this.Characteristic.CoolingThresholdTemperature).updateValue(o.get(s.value??"")??0)})}createTemperatureService(e,t,i,r){let a=this.deviceProperties[i],o=this.deviceProperties[r];if(!a||!o)return;this.setServices(e,this.platform.Service.HeaterCooler,t);let n=this.extractCelsiusDataMapping(o.valueRange),c=Array.from(n.values()),h=Math.min(...c),l=Math.max(...c),u=this.calculateStep(c),d=this.services[e];d.getCharacteristic(this.Characteristic.Active).setProps({perms:[s.Perms.PAIRED_READ]}).onGet(this.getActive.bind(this)),d.getCharacteristic(this.Characteristic.CurrentHeaterCoolerState).onGet(this.getCurrentHeaterCoolerState.bind(this)).setProps({perms:[s.Perms.PAIRED_READ]}),d.getCharacteristic(this.Characteristic.TargetHeaterCoolerState).setValue(this.Characteristic.TargetHeaterCoolerState.COOL).setProps({minValue:this.Characteristic.TargetHeaterCoolerState.COOL,maxValue:this.Characteristic.TargetHeaterCoolerState.COOL,validValues:[this.Characteristic.TargetHeaterCoolerState.COOL],perms:[s.Perms.PAIRED_READ]}).onGet(()=>this.Characteristic.TargetHeaterCoolerState.COOL),d.getCharacteristic(this.Characteristic.CurrentTemperature).onGet(()=>this.parseTemperature(a.value??void 0)),d.getCharacteristic(this.Characteristic.CoolingThresholdTemperature).setValue(n.get(o.value??"")??0).setProps({minValue:h,maxValue:l,minStep:u,validValues:c}).onGet(()=>n.get(o.value??"")??0).onSet(e=>{let t=n.getKey(Number.parseInt(e.toString()));t&&this.sendCommands({[r]:t})})}extractCelsiusDataMapping(t){let i=new e.BiMap;if(!t||"LIST"!==t.type)return i;let r=/(-?\d+)℃/;return t.dataList.forEach(e=>{var t;let a=null===(t=e.desc)||void 0===t?void 0:t.match(r);if(a){let t=a[1];i.set(e.data,Number.parseInt(t,10))}}),i}calculateStep(e){return Math.min(...e.map((e,t,i)=>0===t?i[t+1]-e:e-i[t-1]))}}function h(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}class l{configureAccessory(e){let{disabledDevices:t=[]}=this.config;if(this.log.info("从缓存加载附件:",e.displayName),t.includes(e.context.deviceInfo.baseInfo.deviceId)){this.log.info("设备",e.displayName,"已被禁用");return}this.accessories.push(e)}discoverDevices(){let{familyId:e}=this.config;if(!e){this.log.error("请在 config.json 中配置 familyId");return}this.haierIoT.getDevicesByFamilyId(e).then(async e=>{let t=(await Promise.allSettled(e.map(e=>this.handleDevice(e)))).filter(e=>"fulfilled"===e.status).map(e=>e.value).filter(e=>"string"==typeof e);t.length&&this.haierIoT.subscribeDevices(t)})}async handleDevice(t){let{disabledDevices:i=[]}=this.config;if(i.includes(t.baseInfo.deviceId)){this.log.info("设备",t.baseInfo.deviceName,"已被禁用");return}if(this.isDeviceIneligible(t))return;let r=this.getAccessoryClass(t);if(!r){this.log.warn("设备",t.baseInfo.deviceName,"暂不支持",t.extendedInfo.categoryGrouping,",可提交 issue 申请支持",__webpack_require__("./package.json").eN.H);return}let{deviceId:a}=t.baseInfo,s=this.api.hap.uuid.generate(a),o=this.accessories.find(e=>e.UUID===s);if(o)this.log.info("Restoring existing accessory from cache:",o.displayName),o.context={...o.context,deviceInfo:t},this.api.updatePlatformAccessories([o]),new r(this,o);else{let i=this.getDeviceName(t);this.log.info("Adding new accessory:",i);let a=new this.api.platformAccessory(i,s);a.context={deviceInfo:t},new r(this,a),this.api.registerPlatformAccessories(e.PLUGIN_NAME,e.PLATFORM_NAME,[a])}return a}isDeviceIneligible(e){var t,i;let r=this.getDeviceName(e);return(null===(i=e.baseInfo.permission)||void 0===i?void 0:null===(t=i.auth)||void 0===t?void 0:t.control)?!e.extendedInfo.bindType&&(this.log.warn("设备",r,"不支持云端控制"),!0):(this.log.warn("设备",r,"没有控制权限"),!0)}getAccessoryClass(e){switch(e.extendedInfo.categoryGrouping){case"空气环境":return a;case"热水卫浴":return o;case"冰箱冷储":return c;default:return}}getDeviceName(e){return`${e.extendedInfo.room} - ${e.baseInfo.deviceName}`}constructor(i,r,a){h(this,"log",void 0),h(this,"config",void 0),h(this,"api",void 0),h(this,"Service",void 0),h(this,"Characteristic",void 0),h(this,"accessories",void 0),h(this,"haierIoT",void 0),this.log=i,this.config=r,this.api=a,this.Service=this.api.hap.Service,this.Characteristic=this.api.hap.Characteristic,this.accessories=[],this.log.debug("平台初始化完成",this.config.name),this.accessories=[],this.api.on("didFinishLaunching",async()=>{let{username:r,password:a}=this.config;if(!r||!a){this.log.error("请在 config.json 中配置 username 和 password");return}this.haierIoT=new t.HaierIoT({username:r,password:a,storageDir:(0,e.generateCacheDir)(this.api.user.storagePath()),logger:i}),await this.haierIoT.connect(),this.discoverDevices()}),this.api.on("shutdown",()=>{this.log.info("Homebridge 即将关闭,断开连接")})}}let u=t=>{t.registerPlatform(e.PLATFORM_NAME,l)}})();var __webpack_export_target__=exports;for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__]=__webpack_exports__[__webpack_i__];__webpack_exports__.__esModule&&Object.defineProperty(__webpack_export_target__,"__esModule",{value:!0});
1
+ "use strict";let __rslib_import_meta_url__="undefined"==typeof document?new(require("url".replace("",""))).URL("file:"+__filename).href:document.currentScript&&document.currentScript.src||new URL("main.js",document.baseURI).href;var __webpack_modules__={"./package.json":function(e){e.exports=JSON.parse('{"eN":{"H":"https://github.com/zwb124/homebridge-plugin-haier-v2/issues"}}')}},__webpack_module_cache__={};function __webpack_require__(e){var t=__webpack_module_cache__[e];if(void 0!==t)return t.exports;var i=__webpack_module_cache__[e]={exports:{}};return __webpack_modules__[e](i,i.exports,__webpack_require__),i.exports}__webpack_require__.d=function(e,t){for(var i in t)__webpack_require__.o(t,i)&&!__webpack_require__.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},__webpack_require__.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},__webpack_require__.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var __webpack_exports__={};(()=>{__webpack_require__.r(__webpack_exports__),__webpack_require__.d(__webpack_exports__,{default:()=>u});let e=require("homebridge-plugin-haier-v2/shared"),t=require("haier-iot");function i(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}class r{async initializeAccessory(){await this.getDevDigitalModel();let{deviceInfo:e}=this.accessory.context;this.setupListeners(e.baseInfo.deviceId),this.setupAccessoryInformation(e),this.init()}setupListeners(e){this.platform.haierIoT.addListener("devDigitalModelUpdate",(t,i)=>{e===t&&(this.accessory.context.devDigitalModel=i,this.onDevDigitalModelUpdate())})}setupAccessoryInformation(e){var t;let{Characteristic:i,Service:r}=this.platform;null===(t=this.accessory.getService(r.AccessoryInformation))||void 0===t||t.setCharacteristic(i.Manufacturer,e.extendedInfo.brand).setCharacteristic(i.Model,e.extendedInfo.model).setCharacteristic(i.SerialNumber,e.extendedInfo.prodNo)}setServices(e,t,i){let r=this.accessory.getService(e);if(r)return this.services[e]=r,r;let a=this.accessory.displayName;i&&(a+=` - ${i}`);let s=this.accessory.addService(t,a,e);return s.setCharacteristic(this.Characteristic.Name,a),this.services[e]=s,s}get Characteristic(){return this.platform.Characteristic}get deviceInfo(){return this.accessory.context.deviceInfo}get deviceProperties(){var e;return Object.fromEntries(((null===(e=this.accessory.context.devDigitalModel)||void 0===e?void 0:e.attributes)??[]).map(e=>[e.name,e]))}getPropertyValue(t,i){var r,a;return(0,e.safeJsonParse)((null===(a=this.deviceProperties)||void 0===a?void 0:null===(r=a[t])||void 0===r?void 0:r.value)??void 0,i)}async getDevDigitalModel(){let{deviceId:e,isOnline:t}=this.deviceInfo.baseInfo;if(!t)return this.platform.log.warn(`设备 ${this.accessory.displayName} 离线`),null;if(this.isFetchingDevDigitalModel)return this.devDigitalModelPromise??this.accessory.context.devDigitalModel;this.isFetchingDevDigitalModel=!0;try{this.devDigitalModelPromise??=this.platform.haierIoT.getDevDigitalModel(e);let t=await this.devDigitalModelPromise;if(!t)return this.platform.log.error(`设备 ${this.accessory.displayName} 的数据为空`),null;return this.accessory.context.devDigitalModel={...t},t}catch(e){return this.platform.log.error(`获取设备 ${this.accessory.displayName} 数据失败`,e),this.accessory.context.devDigitalModel}finally{this.isFetchingDevDigitalModel=!1,this.devDigitalModelPromise=void 0}}async sendCommands(...e){e.forEach(e=>{Object.entries(e).forEach(([e,t])=>{let{valueRange:i,desc:r}=this.deviceProperties[e]??{},a=t;if(i){if("LIST"===i.type){var s;let e=null===(s=i.dataList)||void 0===s?void 0:s.find(e=>e.data===t);a=(null==e?void 0:e.desc)??t}this.platform.log.info("设置",this.accessory.displayName,"的",r,"为",a)}})}),await this.platform.haierIoT.sendCommands(this.deviceInfo.baseInfo.deviceId,e),this.onDevDigitalModelUpdate()}constructor(e,t){i(this,"platform",void 0),i(this,"accessory",void 0),i(this,"services",void 0),i(this,"devDigitalModelPromise",void 0),i(this,"isFetchingDevDigitalModel",void 0),this.platform=e,this.accessory=t,this.services={},this.isFetchingDevDigitalModel=!1,this.initializeAccessory().catch(e=>this.platform.log.error(`初始化设备 ${this.accessory.displayName} 时出错:`,e))}}class a extends r{init(){let e=this.setServices("thermostat",this.platform.Service.Thermostat),{CurrentHeatingCoolingState:t,TargetHeatingCoolingState:i,CurrentTemperature:r,TargetTemperature:a,TemperatureDisplayUnits:s,CurrentRelativeHumidity:o,TargetRelativeHumidity:n}=this.Characteristic;e.getCharacteristic(t).onGet(this.getCurrentHeatingCoolingState.bind(this)),e.getCharacteristic(i).onGet(this.getTargetHeatingCoolingState.bind(this)).onSet(this.setTargetHeatingCoolingState.bind(this)),e.getCharacteristic(r).onGet(this.getCurrentTemperature.bind(this)),e.getCharacteristic(a).setProps({...this.targetTemperatureProps}).onGet(this.getTargetTemperature.bind(this)).onSet(this.setTargetTemperature.bind(this)),e.getCharacteristic(s).onGet(this.getTemperatureDisplayUnits.bind(this)),this.hasIndoorHumidity&&e.getCharacteristic(o).onGet(this.getCurrentRelativeHumidity.bind(this)),this.hasTargetHumidity&&e.getCharacteristic(n).onGet(this.getTargetRelativeHumidity.bind(this))}onDevDigitalModelUpdate(){let{CurrentHeatingCoolingState:t,TargetHeatingCoolingState:i,CurrentTemperature:r,TargetTemperature:a,CurrentRelativeHumidity:s,TargetRelativeHumidity:o}=this.Characteristic,n=this.services.thermostat;n&&((0,e.isNumber)(this.targetTemperature)&&n.getCharacteristic(a).setProps({...this.targetTemperatureProps}).updateValue(this.targetTemperature),(0,e.isNumber)(this.currentTemperature)&&n.getCharacteristic(r).updateValue(this.currentTemperature),n.getCharacteristic(t).updateValue(this.getCurrentHeatingCoolingState()),n.getCharacteristic(i).updateValue(this.getTargetHeatingCoolingState()),this.hasIndoorHumidity&&n.getCharacteristic(s).updateValue(this.getCurrentRelativeHumidity()),this.hasTargetHumidity&&n.getCharacteristic(o).updateValue(this.getTargetRelativeHumidity()))}get hasIndoorHumidity(){return!!this.deviceProperties.indoorHumidity}get hasTargetHumidity(){return!!this.deviceProperties.targetHumidity}get onOffStatus(){return this.getPropertyValue("onOffStatus",!1)}get currentTemperature(){return this.getPropertyValue("indoorTemperature")}get targetTemperature(){return this.getPropertyValue("targetTemperature")}get targetTemperatureProps(){var e;let t=null===(e=this.deviceProperties.targetTemp)||void 0===e?void 0:e.valueRange;if(!t||"STEP"!==t.type||!t.dataStep)return;let i=Number.parseFloat(t.dataStep.minValue),r=Number.parseFloat(t.dataStep.maxValue);return{minValue:i,maxValue:r,minStep:Number.parseFloat(t.dataStep.step),validValueRanges:[i,r]}}getCurrentHeatingCoolingState(){var e;let{CurrentHeatingCoolingState:t}=this.Characteristic;if(!this.onOffStatus)return t.OFF;switch(null===(e=this.deviceProperties.operationMode)||void 0===e?void 0:e.value){case"1":return t.COOL;case"4":return t.HEAT;default:if(this.currentTemperature??0>(this.targetTemperature??0))return t.COOL;return t.HEAT}}getTargetHeatingCoolingState(){var e;let{TargetHeatingCoolingState:t}=this.Characteristic;if(!this.onOffStatus)return t.OFF;switch(null===(e=this.deviceProperties.operationMode)||void 0===e?void 0:e.value){case"1":return t.COOL;case"4":return t.HEAT;default:return t.AUTO}}setTargetHeatingCoolingState(e){let{TargetHeatingCoolingState:t}=this.Characteristic;if(e===t.OFF)return this.sendCommands({onOffStatus:"false"});let i={[t.AUTO]:"0",[t.COOL]:"1",[t.HEAT]:"4"}[e];if(i)return this.sendCommands({onOffStatus:"true"},{operationMode:i});this.platform.log.warn("Unsupported TargetHeatingCoolingState:",e)}getCurrentTemperature(){return this.currentTemperature}getTargetTemperature(){return this.targetTemperature}setTargetTemperature(e){let{minValue:t=10,maxValue:i=38}=this.targetTemperatureProps??{},r=[{targetTemperature:Math.min(Math.max(Number.parseFloat(e.toString()),t),i).toString()}];return this.onOffStatus||r.unshift({onOffStatus:"true"}),this.sendCommands(...r)}getTemperatureDisplayUnits(){var e;return(null===(e=this.deviceProperties.tempUnit)||void 0===e?void 0:e.value)==="2"?this.Characteristic.TemperatureDisplayUnits.FAHRENHEIT:this.Characteristic.TemperatureDisplayUnits.CELSIUS}getCurrentRelativeHumidity(){return this.getPropertyValue("indoorHumidity")}getTargetRelativeHumidity(){return this.getPropertyValue("targetHumidity")}}let s=require("homebridge");class o extends r{async init(){this.setServices("thermostat",this.platform.Service.Thermostat),this.services.thermostat.getCharacteristic(this.Characteristic.CurrentHeatingCoolingState).onGet(this.getCurrentHeatingCoolingState.bind(this)),this.services.thermostat.getCharacteristic(this.Characteristic.TargetHeatingCoolingState).setProps({validValues:[this.Characteristic.TargetHeatingCoolingState.OFF,this.Characteristic.TargetHeatingCoolingState.HEAT]}).onGet(this.getCurrentHeatingCoolingState.bind(this)).onSet(this.setTargetHeatingCoolingState.bind(this)),this.services.thermostat.getCharacteristic(this.Characteristic.CurrentTemperature).onGet(this.getTargetTemperature.bind(this)),this.services.thermostat.getCharacteristic(this.Characteristic.TargetTemperature).setProps({...this.targetTemperatureProps}).onGet(this.getTargetTemperature.bind(this)).onSet(this.setTargetTemperature.bind(this)),this.services.thermostat.getCharacteristic(this.Characteristic.TemperatureDisplayUnits).onGet(()=>this.Characteristic.TemperatureDisplayUnits.CELSIUS),this.getPropertyValue("zeroColdWaterFuncExist")&&(this.setServices("switch",this.platform.Service.Switch,"零冷水"),this.services.switch.getCharacteristic(this.Characteristic.On).onGet(this.getZeroColdWaterStatus.bind(this)).onSet(this.setZeroColdWaterStatus.bind(this))),this.deviceProperties.flowStatus&&(this.setServices("flowStatus",this.platform.Service.Valve,"水流状态"),this.services.flowStatus.getCharacteristic(this.Characteristic.Active).setProps({perms:[s.Perms.PAIRED_READ]}).onGet(()=>this.getPropertyValue("flowStatus")?this.Characteristic.Active.ACTIVE:this.Characteristic.Active.INACTIVE),this.services.flowStatus.getCharacteristic(this.Characteristic.InUse).onGet(()=>this.getPropertyValue("flowStatus")?this.Characteristic.InUse.IN_USE:this.Characteristic.InUse.NOT_IN_USE)),this.oneShotState&&this.scheduleOneShotCompletion()}onDevDigitalModelUpdate(){let t=this.services.thermostat;if(t&&((0,e.isNumber)(this.targetTemperature)&&(t.getCharacteristic(this.Characteristic.TargetTemperature).setProps({...this.targetTemperatureProps}).updateValue(this.targetTemperature),t.getCharacteristic(this.Characteristic.CurrentTemperature).updateValue(this.targetTemperature)),this.getPropertyValue("zeroColdWaterFuncExist"))){var i;null===(i=this.services.switch)||void 0===i||i.getCharacteristic(this.Characteristic.On).updateValue(this.getZeroColdWaterStatus())}}get onOffStatus(){return this.getPropertyValue("onOffStatus")}get targetTemperature(){return this.getPropertyValue("targetTemp")}get targetTemperatureProps(){var e;let t=null===(e=this.deviceProperties.targetTemp)||void 0===e?void 0:e.valueRange;if(!t||"STEP"!==t.type||!t.dataStep)return;let i=Number.parseFloat(t.dataStep.minValue),r=Number.parseFloat(t.dataStep.maxValue);return{minValue:i,maxValue:r,minStep:Number.parseFloat(t.dataStep.step),validValueRanges:[i,r]}}getCurrentHeatingCoolingState(){return this.onOffStatus?this.Characteristic.CurrentHeatingCoolingState.HEAT:this.Characteristic.CurrentHeatingCoolingState.OFF}setTargetHeatingCoolingState(e){return this.sendCommands({onOffStatus:(!!e).toString()})}getTargetTemperature(){return this.targetTemperature}setTargetTemperature(e){let{minValue:t=0,maxValue:i=100}=this.targetTemperatureProps??{},r=Math.min(Math.max(Number.parseFloat(e.toString()),t),i).toString();return this.sendCommands({targetTemp:r})}get oneShotState(){return this.accessory.context.zeroColdWaterOneShot}get oneShotDurationMs(){var e,t;let i=null===(t=this.platform.config.customConfig)||void 0===t?void 0:null===(e=t.hotWater)||void 0===e?void 0:e.zeroColdWaterDurationMinutes;return 6e4*Math.min(Math.max(Number.isFinite(i)?Number(i):5,1),120)}getZeroColdWaterStatus(){var e;return(null===(e=this.oneShotState)||void 0===e?void 0:e.phase)==="running"}async setZeroColdWaterStatus(e){if(e){if(this.oneShotState)return;this.accessory.context.zeroColdWaterOneShot={startedAt:Date.now(),durationMs:this.oneShotDurationMs,phase:"running"},this.platform.api.updatePlatformAccessories([this.accessory]);try{await this.sendCommands({zeroColdWaterStatus:"1"}),this.platform.log.info(`已启动单次零冷水循环,计划 ${Math.round(this.oneShotDurationMs/6e4)} 分钟后恢复云智慧(AI)模式:`,this.accessory.displayName),this.scheduleOneShotCompletion()}catch(e){var t;throw delete this.accessory.context.zeroColdWaterOneShot,this.platform.api.updatePlatformAccessories([this.accessory]),null===(t=this.services.switch)||void 0===t||t.getCharacteristic(this.Characteristic.On).updateValue(!1),e}return}this.oneShotTimer&&(clearTimeout(this.oneShotTimer),this.oneShotTimer=void 0),this.oneShotState&&await this.completeOneShot()}scheduleOneShotCompletion(){this.oneShotTimer&&clearTimeout(this.oneShotTimer);let e=this.oneShotState;if(!e)return;let t="restoring"===e.phase?0:e.startedAt+e.durationMs-Date.now();if(t<=0){this.completeOneShot();return}this.oneShotTimer=setTimeout(()=>{this.completeOneShot()},t)}async completeOneShot(){var e;this.oneShotTimer&&(clearTimeout(this.oneShotTimer),this.oneShotTimer=void 0);let t=this.oneShotState;if(t){t.phase="restoring",this.platform.log.info("单次零冷水循环计时结束,开始恢复云智慧(AI)模式:",this.accessory.displayName),this.platform.api.updatePlatformAccessories([this.accessory]),null===(e=this.services.switch)||void 0===e||e.getCharacteristic(this.Characteristic.On).updateValue(!1);try{await this.sendCommands({zeroColdWaterStatus:"4"}),delete this.accessory.context.zeroColdWaterOneShot,this.platform.api.updatePlatformAccessories([this.accessory]),this.platform.log.info("热水器单次零冷水循环计时结束,已恢复云智慧(AI)循环模式:",this.accessory.displayName)}catch(e){this.platform.api.updatePlatformAccessories([this.accessory]),this.oneShotTimer=setTimeout(()=>void this.completeOneShot(),6e4),this.platform.log.error("恢复热水器云智慧(AI)零冷水模式失败,60 秒后重试:",this.accessory.displayName,e)}}}constructor(...e){super(...e),function(e,t,i){t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i}(this,"oneShotTimer",void 0)}}let n={refrigerator:{displayName:"冷藏室",currentTempKey:"refrigeratorTemperatureC",targetTempKey:"refrigeratorTargetTempLevel"},freezer:{displayName:"冷冻室",currentTempKey:"freezerTemperatureC",targetTempKey:"freezerTargetTempLevel"},vtRoom:{displayName:"变温室",currentTempKey:"vtRoomTemperature",targetTempKey:"vtRoomTargetTempLevel"}};class c extends r{async init(){Object.entries(n).forEach(([e,{displayName:t,currentTempKey:i,targetTempKey:r}])=>{this.createTemperatureService(e,t,i,r)})}getActive(){return this.deviceInfo.baseInfo.isOnline?this.Characteristic.Active.ACTIVE:this.Characteristic.Active.INACTIVE}getCurrentHeaterCoolerState(){return this.deviceInfo.baseInfo.isOnline?this.Characteristic.CurrentHeaterCoolerState.COOLING:this.Characteristic.CurrentHeaterCoolerState.INACTIVE}parseTemperature(e){return e?Number.parseInt(e):0}onDevDigitalModelUpdate(){Object.entries(n).forEach(([e,{currentTempKey:t,targetTempKey:i}])=>{let r=this.services[e];if(!r)return;let a=this.deviceProperties[t],s=this.deviceProperties[i];if(!a||!s)return;let o=this.extractCelsiusDataMapping(s.valueRange);r.getCharacteristic(this.Characteristic.CurrentTemperature).updateValue(this.parseTemperature(a.value??void 0)),r.getCharacteristic(this.Characteristic.CoolingThresholdTemperature).updateValue(o.get(s.value??"")??0)})}createTemperatureService(e,t,i,r){let a=this.deviceProperties[i],o=this.deviceProperties[r];if(!a||!o)return;this.setServices(e,this.platform.Service.HeaterCooler,t);let n=this.extractCelsiusDataMapping(o.valueRange),c=Array.from(n.values()),h=Math.min(...c),l=Math.max(...c),u=this.calculateStep(c),d=this.services[e];d.getCharacteristic(this.Characteristic.Active).setProps({perms:[s.Perms.PAIRED_READ]}).onGet(this.getActive.bind(this)),d.getCharacteristic(this.Characteristic.CurrentHeaterCoolerState).onGet(this.getCurrentHeaterCoolerState.bind(this)).setProps({perms:[s.Perms.PAIRED_READ]}),d.getCharacteristic(this.Characteristic.TargetHeaterCoolerState).setValue(this.Characteristic.TargetHeaterCoolerState.COOL).setProps({minValue:this.Characteristic.TargetHeaterCoolerState.COOL,maxValue:this.Characteristic.TargetHeaterCoolerState.COOL,validValues:[this.Characteristic.TargetHeaterCoolerState.COOL],perms:[s.Perms.PAIRED_READ]}).onGet(()=>this.Characteristic.TargetHeaterCoolerState.COOL),d.getCharacteristic(this.Characteristic.CurrentTemperature).onGet(()=>this.parseTemperature(a.value??void 0)),d.getCharacteristic(this.Characteristic.CoolingThresholdTemperature).setValue(n.get(o.value??"")??0).setProps({minValue:h,maxValue:l,minStep:u,validValues:c}).onGet(()=>n.get(o.value??"")??0).onSet(e=>{let t=n.getKey(Number.parseInt(e.toString()));t&&this.sendCommands({[r]:t})})}extractCelsiusDataMapping(t){let i=new e.BiMap;if(!t||"LIST"!==t.type)return i;let r=/(-?\d+)℃/;return t.dataList.forEach(e=>{var t;let a=null===(t=e.desc)||void 0===t?void 0:t.match(r);if(a){let t=a[1];i.set(e.data,Number.parseInt(t,10))}}),i}calculateStep(e){return Math.min(...e.map((e,t,i)=>0===t?i[t+1]-e:e-i[t-1]))}}function h(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}class l{configureAccessory(e){let{disabledDevices:t=[]}=this.config;if(this.log.info("从缓存加载附件:",e.displayName),t.includes(e.context.deviceInfo.baseInfo.deviceId)){this.log.info("设备",e.displayName,"已被禁用");return}this.accessories.push(e)}discoverDevices(){let{familyId:e}=this.config;if(!e){this.log.error("请在 config.json 中配置 familyId");return}this.haierIoT.getDevicesByFamilyId(e).then(async e=>{let t=(await Promise.allSettled(e.map(e=>this.handleDevice(e)))).filter(e=>"fulfilled"===e.status).map(e=>e.value).filter(e=>"string"==typeof e);t.length&&this.haierIoT.subscribeDevices(t)})}async handleDevice(t){let{disabledDevices:i=[]}=this.config;if(i.includes(t.baseInfo.deviceId)){this.log.info("设备",t.baseInfo.deviceName,"已被禁用");return}if(this.isDeviceIneligible(t))return;let r=this.getAccessoryClass(t);if(!r){this.log.warn("设备",t.baseInfo.deviceName,"暂不支持",t.extendedInfo.categoryGrouping,",可提交 issue 申请支持",__webpack_require__("./package.json").eN.H);return}let{deviceId:a}=t.baseInfo,s=this.api.hap.uuid.generate(a),o=this.accessories.find(e=>e.UUID===s);if(o)this.log.info("Restoring existing accessory from cache:",o.displayName),o.context={...o.context,deviceInfo:t},this.api.updatePlatformAccessories([o]),new r(this,o);else{let i=this.getDeviceName(t);this.log.info("Adding new accessory:",i);let a=new this.api.platformAccessory(i,s);a.context={deviceInfo:t},new r(this,a),this.api.registerPlatformAccessories(e.PLUGIN_NAME,e.PLATFORM_NAME,[a])}return a}isDeviceIneligible(e){var t,i;let r=this.getDeviceName(e);return(null===(i=e.baseInfo.permission)||void 0===i?void 0:null===(t=i.auth)||void 0===t?void 0:t.control)?!e.extendedInfo.bindType&&(this.log.warn("设备",r,"不支持云端控制"),!0):(this.log.warn("设备",r,"没有控制权限"),!0)}getAccessoryClass(e){switch(e.extendedInfo.categoryGrouping){case"空气环境":return a;case"热水卫浴":return o;case"冰箱冷储":return c;default:return}}getDeviceName(e){return`${e.extendedInfo.room} - ${e.baseInfo.deviceName}`}constructor(i,r,a){h(this,"log",void 0),h(this,"config",void 0),h(this,"api",void 0),h(this,"Service",void 0),h(this,"Characteristic",void 0),h(this,"accessories",void 0),h(this,"haierIoT",void 0),this.log=i,this.config=r,this.api=a,this.Service=this.api.hap.Service,this.Characteristic=this.api.hap.Characteristic,this.accessories=[],this.log.debug("平台初始化完成",this.config.name),this.accessories=[],this.api.on("didFinishLaunching",async()=>{let{username:r,password:a}=this.config;if(!r||!a){this.log.error("请在 config.json 中配置 username 和 password");return}this.haierIoT=new t.HaierIoT({username:r,password:a,storageDir:(0,e.generateCacheDir)(this.api.user.storagePath()),logger:i}),await this.haierIoT.connect(),this.discoverDevices()}),this.api.on("shutdown",()=>{this.log.info("Homebridge 即将关闭,断开连接")})}}let u=t=>{t.registerPlatform(e.PLATFORM_NAME,l)}})();var __webpack_export_target__=exports;for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__]=__webpack_exports__[__webpack_i__];__webpack_exports__.__esModule&&Object.defineProperty(__webpack_export_target__,"__esModule",{value:!0});
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import*as e from"homebridge-plugin-haier-v2/shared";import*as t from"haier-iot";import*as i from"homebridge";var r={"./package.json":function(e){e.exports=JSON.parse('{"eN":{"H":"https://github.com/zwb124/homebridge-plugin-haier-v2/issues"}}')}},a={};function s(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}class o{async initializeAccessory(){await this.getDevDigitalModel();let{deviceInfo:e}=this.accessory.context;this.setupListeners(e.baseInfo.deviceId),this.setupAccessoryInformation(e),this.init()}setupListeners(e){this.platform.haierIoT.addListener("devDigitalModelUpdate",(t,i)=>{e===t&&(this.accessory.context.devDigitalModel=i,this.onDevDigitalModelUpdate())})}setupAccessoryInformation(e){var t;let{Characteristic:i,Service:r}=this.platform;null===(t=this.accessory.getService(r.AccessoryInformation))||void 0===t||t.setCharacteristic(i.Manufacturer,e.extendedInfo.brand).setCharacteristic(i.Model,e.extendedInfo.model).setCharacteristic(i.SerialNumber,e.extendedInfo.prodNo)}setServices(e,t,i){let r=this.accessory.getService(e);if(r)return this.services[e]=r,r;let a=this.accessory.displayName;i&&(a+=` - ${i}`);let s=this.accessory.addService(t,a,e);return s.setCharacteristic(this.Characteristic.Name,a),this.services[e]=s,s}get Characteristic(){return this.platform.Characteristic}get deviceInfo(){return this.accessory.context.deviceInfo}get deviceProperties(){var e;return Object.fromEntries(((null===(e=this.accessory.context.devDigitalModel)||void 0===e?void 0:e.attributes)??[]).map(e=>[e.name,e]))}getPropertyValue(t,i){var r,a;return(0,e.safeJsonParse)((null===(a=this.deviceProperties)||void 0===a?void 0:null===(r=a[t])||void 0===r?void 0:r.value)??void 0,i)}async getDevDigitalModel(){let{deviceId:e,isOnline:t}=this.deviceInfo.baseInfo;if(!t)return this.platform.log.warn(`设备 ${this.accessory.displayName} 离线`),null;if(this.isFetchingDevDigitalModel)return this.devDigitalModelPromise??this.accessory.context.devDigitalModel;this.isFetchingDevDigitalModel=!0;try{this.devDigitalModelPromise??=this.platform.haierIoT.getDevDigitalModel(e);let t=await this.devDigitalModelPromise;if(!t)return this.platform.log.error(`设备 ${this.accessory.displayName} 的数据为空`),null;return this.accessory.context.devDigitalModel={...t},t}catch(e){return this.platform.log.error(`获取设备 ${this.accessory.displayName} 数据失败`,e),this.accessory.context.devDigitalModel}finally{this.isFetchingDevDigitalModel=!1,this.devDigitalModelPromise=void 0}}async sendCommands(...e){e.forEach(e=>{Object.entries(e).forEach(([e,t])=>{let{valueRange:i,desc:r}=this.deviceProperties[e]??{},a=t;if(i){if("LIST"===i.type){var s;let e=null===(s=i.dataList)||void 0===s?void 0:s.find(e=>e.data===t);a=(null==e?void 0:e.desc)??t}this.platform.log.info("设置",this.accessory.displayName,"的",r,"为",a)}})}),await this.platform.haierIoT.sendCommands(this.deviceInfo.baseInfo.deviceId,e),this.onDevDigitalModelUpdate()}constructor(e,t){s(this,"platform",void 0),s(this,"accessory",void 0),s(this,"services",void 0),s(this,"devDigitalModelPromise",void 0),s(this,"isFetchingDevDigitalModel",void 0),this.platform=e,this.accessory=t,this.services={},this.isFetchingDevDigitalModel=!1,this.initializeAccessory().catch(e=>this.platform.log.error(`初始化设备 ${this.accessory.displayName} 时出错:`,e))}}class h extends o{init(){let e=this.setServices("thermostat",this.platform.Service.Thermostat),{CurrentHeatingCoolingState:t,TargetHeatingCoolingState:i,CurrentTemperature:r,TargetTemperature:a,TemperatureDisplayUnits:s,CurrentRelativeHumidity:o,TargetRelativeHumidity:h}=this.Characteristic;e.getCharacteristic(t).onGet(this.getCurrentHeatingCoolingState.bind(this)),e.getCharacteristic(i).onGet(this.getTargetHeatingCoolingState.bind(this)).onSet(this.setTargetHeatingCoolingState.bind(this)),e.getCharacteristic(r).onGet(this.getCurrentTemperature.bind(this)),e.getCharacteristic(a).setProps({...this.targetTemperatureProps}).onGet(this.getTargetTemperature.bind(this)).onSet(this.setTargetTemperature.bind(this)),e.getCharacteristic(s).onGet(this.getTemperatureDisplayUnits.bind(this)),this.hasIndoorHumidity&&e.getCharacteristic(o).onGet(this.getCurrentRelativeHumidity.bind(this)),this.hasTargetHumidity&&e.getCharacteristic(h).onGet(this.getTargetRelativeHumidity.bind(this))}onDevDigitalModelUpdate(){let{CurrentHeatingCoolingState:t,TargetHeatingCoolingState:i,CurrentTemperature:r,TargetTemperature:a,CurrentRelativeHumidity:s,TargetRelativeHumidity:o}=this.Characteristic,h=this.services.thermostat;h&&((0,e.isNumber)(this.targetTemperature)&&h.getCharacteristic(a).setProps({...this.targetTemperatureProps}).updateValue(this.targetTemperature),(0,e.isNumber)(this.currentTemperature)&&h.getCharacteristic(r).updateValue(this.currentTemperature),h.getCharacteristic(t).updateValue(this.getCurrentHeatingCoolingState()),h.getCharacteristic(i).updateValue(this.getTargetHeatingCoolingState()),this.hasIndoorHumidity&&h.getCharacteristic(s).updateValue(this.getCurrentRelativeHumidity()),this.hasTargetHumidity&&h.getCharacteristic(o).updateValue(this.getTargetRelativeHumidity()))}get hasIndoorHumidity(){return!!this.deviceProperties.indoorHumidity}get hasTargetHumidity(){return!!this.deviceProperties.targetHumidity}get onOffStatus(){return this.getPropertyValue("onOffStatus",!1)}get currentTemperature(){return this.getPropertyValue("indoorTemperature")}get targetTemperature(){return this.getPropertyValue("targetTemperature")}get targetTemperatureProps(){var e;let t=null===(e=this.deviceProperties.targetTemp)||void 0===e?void 0:e.valueRange;if(!t||"STEP"!==t.type||!t.dataStep)return;let i=Number.parseFloat(t.dataStep.minValue),r=Number.parseFloat(t.dataStep.maxValue);return{minValue:i,maxValue:r,minStep:Number.parseFloat(t.dataStep.step),validValueRanges:[i,r]}}getCurrentHeatingCoolingState(){var e;let{CurrentHeatingCoolingState:t}=this.Characteristic;if(!this.onOffStatus)return t.OFF;switch(null===(e=this.deviceProperties.operationMode)||void 0===e?void 0:e.value){case"1":return t.COOL;case"4":return t.HEAT;default:if(this.currentTemperature??0>(this.targetTemperature??0))return t.COOL;return t.HEAT}}getTargetHeatingCoolingState(){var e;let{TargetHeatingCoolingState:t}=this.Characteristic;if(!this.onOffStatus)return t.OFF;switch(null===(e=this.deviceProperties.operationMode)||void 0===e?void 0:e.value){case"1":return t.COOL;case"4":return t.HEAT;default:return t.AUTO}}setTargetHeatingCoolingState(e){let{TargetHeatingCoolingState:t}=this.Characteristic;if(e===t.OFF)return this.sendCommands({onOffStatus:"false"});let i={[t.AUTO]:"0",[t.COOL]:"1",[t.HEAT]:"4"}[e];if(i)return this.sendCommands({onOffStatus:"true"},{operationMode:i});this.platform.log.warn("Unsupported TargetHeatingCoolingState:",e)}getCurrentTemperature(){return this.currentTemperature}getTargetTemperature(){return this.targetTemperature}setTargetTemperature(e){let{minValue:t=10,maxValue:i=38}=this.targetTemperatureProps??{},r=[{targetTemperature:Math.min(Math.max(Number.parseFloat(e.toString()),t),i).toString()}];return this.onOffStatus||r.unshift({onOffStatus:"true"}),this.sendCommands(...r)}getTemperatureDisplayUnits(){var e;return(null===(e=this.deviceProperties.tempUnit)||void 0===e?void 0:e.value)==="2"?this.Characteristic.TemperatureDisplayUnits.FAHRENHEIT:this.Characteristic.TemperatureDisplayUnits.CELSIUS}getCurrentRelativeHumidity(){return this.getPropertyValue("indoorHumidity")}getTargetRelativeHumidity(){return this.getPropertyValue("targetHumidity")}}class n extends o{async init(){this.setServices("thermostat",this.platform.Service.Thermostat),this.services.thermostat.getCharacteristic(this.Characteristic.CurrentHeatingCoolingState).onGet(this.getCurrentHeatingCoolingState.bind(this)),this.services.thermostat.getCharacteristic(this.Characteristic.TargetHeatingCoolingState).setProps({validValues:[this.Characteristic.TargetHeatingCoolingState.OFF,this.Characteristic.TargetHeatingCoolingState.HEAT]}).onGet(this.getCurrentHeatingCoolingState.bind(this)).onSet(this.setTargetHeatingCoolingState.bind(this)),this.services.thermostat.getCharacteristic(this.Characteristic.CurrentTemperature).onGet(this.getTargetTemperature.bind(this)),this.services.thermostat.getCharacteristic(this.Characteristic.TargetTemperature).setProps({...this.targetTemperatureProps}).onGet(this.getTargetTemperature.bind(this)).onSet(this.setTargetTemperature.bind(this)),this.services.thermostat.getCharacteristic(this.Characteristic.TemperatureDisplayUnits).onGet(()=>this.Characteristic.TemperatureDisplayUnits.CELSIUS),this.getPropertyValue("zeroColdWaterFuncExist")&&(this.setServices("switch",this.platform.Service.Switch,"零冷水"),this.services.switch.getCharacteristic(this.Characteristic.On).onGet(this.getZeroColdWaterStatus.bind(this)).onSet(this.setZeroColdWaterStatus.bind(this))),this.deviceProperties.flowStatus&&(this.setServices("flowStatus",this.platform.Service.Valve,"水流状态"),this.services.flowStatus.getCharacteristic(this.Characteristic.Active).setProps({perms:[i.Perms.PAIRED_READ]}).onGet(()=>this.getPropertyValue("flowStatus")?this.Characteristic.Active.ACTIVE:this.Characteristic.Active.INACTIVE),this.services.flowStatus.getCharacteristic(this.Characteristic.InUse).onGet(()=>this.getPropertyValue("flowStatus")?this.Characteristic.InUse.IN_USE:this.Characteristic.InUse.NOT_IN_USE)),this.oneShotState&&this.scheduleOneShotCompletion()}onDevDigitalModelUpdate(){let t=this.services.thermostat;if(t&&((0,e.isNumber)(this.targetTemperature)&&(t.getCharacteristic(this.Characteristic.TargetTemperature).setProps({...this.targetTemperatureProps}).updateValue(this.targetTemperature),t.getCharacteristic(this.Characteristic.CurrentTemperature).updateValue(this.targetTemperature)),this.getPropertyValue("zeroColdWaterFuncExist"))){var i;null===(i=this.services.switch)||void 0===i||i.getCharacteristic(this.Characteristic.On).updateValue(this.getZeroColdWaterStatus())}}get onOffStatus(){return this.getPropertyValue("onOffStatus")}get targetTemperature(){return this.getPropertyValue("targetTemp")}get targetTemperatureProps(){var e;let t=null===(e=this.deviceProperties.targetTemp)||void 0===e?void 0:e.valueRange;if(!t||"STEP"!==t.type||!t.dataStep)return;let i=Number.parseFloat(t.dataStep.minValue),r=Number.parseFloat(t.dataStep.maxValue);return{minValue:i,maxValue:r,minStep:Number.parseFloat(t.dataStep.step),validValueRanges:[i,r]}}getCurrentHeatingCoolingState(){return this.onOffStatus?this.Characteristic.CurrentHeatingCoolingState.HEAT:this.Characteristic.CurrentHeatingCoolingState.OFF}setTargetHeatingCoolingState(e){return this.sendCommands({onOffStatus:(!!e).toString()})}getTargetTemperature(){return this.targetTemperature}setTargetTemperature(e){let{minValue:t=0,maxValue:i=100}=this.targetTemperatureProps??{},r=Math.min(Math.max(Number.parseFloat(e.toString()),t),i).toString();return this.sendCommands({targetTemp:r})}get oneShotState(){return this.accessory.context.zeroColdWaterOneShot}get oneShotDurationMs(){var e,t;let i=null===(t=this.platform.config.customConfig)||void 0===t?void 0:null===(e=t.hotWater)||void 0===e?void 0:e.zeroColdWaterDurationMinutes;return 6e4*Math.min(Math.max(Number.isFinite(i)?Number(i):15,1),120)}getZeroColdWaterStatus(){var e;return(null===(e=this.oneShotState)||void 0===e?void 0:e.phase)==="running"}async setZeroColdWaterStatus(e){if(e){if(this.oneShotState)return;this.accessory.context.zeroColdWaterOneShot={startedAt:Date.now(),durationMs:this.oneShotDurationMs,phase:"running"},this.platform.api.updatePlatformAccessories([this.accessory]);try{await this.sendCommands({zeroColdWaterStatus:"1"}),this.scheduleOneShotCompletion()}catch(e){var t;throw delete this.accessory.context.zeroColdWaterOneShot,this.platform.api.updatePlatformAccessories([this.accessory]),null===(t=this.services.switch)||void 0===t||t.getCharacteristic(this.Characteristic.On).updateValue(!1),e}return}this.oneShotTimer&&(clearTimeout(this.oneShotTimer),this.oneShotTimer=void 0),this.oneShotState&&await this.completeOneShot()}scheduleOneShotCompletion(){this.oneShotTimer&&clearTimeout(this.oneShotTimer);let e=this.oneShotState;if(!e)return;let t="restoring"===e.phase?0:e.startedAt+e.durationMs-Date.now();if(t<=0){this.completeOneShot();return}this.oneShotTimer=setTimeout(()=>{this.completeOneShot()},t)}async completeOneShot(){var e;this.oneShotTimer&&(clearTimeout(this.oneShotTimer),this.oneShotTimer=void 0);let t=this.oneShotState;if(t){t.phase="restoring",this.platform.api.updatePlatformAccessories([this.accessory]),null===(e=this.services.switch)||void 0===e||e.getCharacteristic(this.Characteristic.On).updateValue(!1);try{await this.sendCommands({zeroColdWaterStatus:"4"}),delete this.accessory.context.zeroColdWaterOneShot,this.platform.api.updatePlatformAccessories([this.accessory]),this.platform.log.info("热水器单次零冷水循环计时结束,已恢复云智慧(AI)循环模式:",this.accessory.displayName)}catch(e){this.platform.api.updatePlatformAccessories([this.accessory]),this.oneShotTimer=setTimeout(()=>void this.completeOneShot(),6e4),this.platform.log.error("恢复热水器云智慧(AI)零冷水模式失败,60 秒后重试:",this.accessory.displayName,e)}}}constructor(...e){var t,i;super(...e),i=void 0,(t="oneShotTimer")in this?Object.defineProperty(this,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):this[t]=i}}let c={refrigerator:{displayName:"冷藏室",currentTempKey:"refrigeratorTemperatureC",targetTempKey:"refrigeratorTargetTempLevel"},freezer:{displayName:"冷冻室",currentTempKey:"freezerTemperatureC",targetTempKey:"freezerTargetTempLevel"},vtRoom:{displayName:"变温室",currentTempKey:"vtRoomTemperature",targetTempKey:"vtRoomTargetTempLevel"}};class l extends o{async init(){Object.entries(c).forEach(([e,{displayName:t,currentTempKey:i,targetTempKey:r}])=>{this.createTemperatureService(e,t,i,r)})}getActive(){return this.deviceInfo.baseInfo.isOnline?this.Characteristic.Active.ACTIVE:this.Characteristic.Active.INACTIVE}getCurrentHeaterCoolerState(){return this.deviceInfo.baseInfo.isOnline?this.Characteristic.CurrentHeaterCoolerState.COOLING:this.Characteristic.CurrentHeaterCoolerState.INACTIVE}parseTemperature(e){return e?Number.parseInt(e):0}onDevDigitalModelUpdate(){Object.entries(c).forEach(([e,{currentTempKey:t,targetTempKey:i}])=>{let r=this.services[e];if(!r)return;let a=this.deviceProperties[t],s=this.deviceProperties[i];if(!a||!s)return;let o=this.extractCelsiusDataMapping(s.valueRange);r.getCharacteristic(this.Characteristic.CurrentTemperature).updateValue(this.parseTemperature(a.value??void 0)),r.getCharacteristic(this.Characteristic.CoolingThresholdTemperature).updateValue(o.get(s.value??"")??0)})}createTemperatureService(e,t,r,a){let s=this.deviceProperties[r],o=this.deviceProperties[a];if(!s||!o)return;this.setServices(e,this.platform.Service.HeaterCooler,t);let h=this.extractCelsiusDataMapping(o.valueRange),n=Array.from(h.values()),c=Math.min(...n),l=Math.max(...n),u=this.calculateStep(n),d=this.services[e];d.getCharacteristic(this.Characteristic.Active).setProps({perms:[i.Perms.PAIRED_READ]}).onGet(this.getActive.bind(this)),d.getCharacteristic(this.Characteristic.CurrentHeaterCoolerState).onGet(this.getCurrentHeaterCoolerState.bind(this)).setProps({perms:[i.Perms.PAIRED_READ]}),d.getCharacteristic(this.Characteristic.TargetHeaterCoolerState).setValue(this.Characteristic.TargetHeaterCoolerState.COOL).setProps({minValue:this.Characteristic.TargetHeaterCoolerState.COOL,maxValue:this.Characteristic.TargetHeaterCoolerState.COOL,validValues:[this.Characteristic.TargetHeaterCoolerState.COOL],perms:[i.Perms.PAIRED_READ]}).onGet(()=>this.Characteristic.TargetHeaterCoolerState.COOL),d.getCharacteristic(this.Characteristic.CurrentTemperature).onGet(()=>this.parseTemperature(s.value??void 0)),d.getCharacteristic(this.Characteristic.CoolingThresholdTemperature).setValue(h.get(o.value??"")??0).setProps({minValue:c,maxValue:l,minStep:u,validValues:n}).onGet(()=>h.get(o.value??"")??0).onSet(e=>{let t=h.getKey(Number.parseInt(e.toString()));t&&this.sendCommands({[a]:t})})}extractCelsiusDataMapping(t){let i=new e.BiMap;if(!t||"LIST"!==t.type)return i;let r=/(-?\d+)℃/;return t.dataList.forEach(e=>{var t;let a=null===(t=e.desc)||void 0===t?void 0:t.match(r);if(a){let t=a[1];i.set(e.data,Number.parseInt(t,10))}}),i}calculateStep(e){return Math.min(...e.map((e,t,i)=>0===t?i[t+1]-e:e-i[t-1]))}}function u(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}class d{configureAccessory(e){let{disabledDevices:t=[]}=this.config;if(this.log.info("从缓存加载附件:",e.displayName),t.includes(e.context.deviceInfo.baseInfo.deviceId)){this.log.info("设备",e.displayName,"已被禁用");return}this.accessories.push(e)}discoverDevices(){let{familyId:e}=this.config;if(!e){this.log.error("请在 config.json 中配置 familyId");return}this.haierIoT.getDevicesByFamilyId(e).then(async e=>{let t=(await Promise.allSettled(e.map(e=>this.handleDevice(e)))).filter(e=>"fulfilled"===e.status).map(e=>e.value).filter(e=>"string"==typeof e);t.length&&this.haierIoT.subscribeDevices(t)})}async handleDevice(t){let{disabledDevices:i=[]}=this.config;if(i.includes(t.baseInfo.deviceId)){this.log.info("设备",t.baseInfo.deviceName,"已被禁用");return}if(this.isDeviceIneligible(t))return;let s=this.getAccessoryClass(t);if(!s){this.log.warn("设备",t.baseInfo.deviceName,"暂不支持",t.extendedInfo.categoryGrouping,",可提交 issue 申请支持",function e(t){var i=a[t];if(void 0!==i)return i.exports;var s=a[t]={exports:{}};return r[t](s,s.exports,e),s.exports}("./package.json").eN.H);return}let{deviceId:o}=t.baseInfo,h=this.api.hap.uuid.generate(o),n=this.accessories.find(e=>e.UUID===h);if(n)this.log.info("Restoring existing accessory from cache:",n.displayName),n.context={...n.context,deviceInfo:t},this.api.updatePlatformAccessories([n]),new s(this,n);else{let i=this.getDeviceName(t);this.log.info("Adding new accessory:",i);let r=new this.api.platformAccessory(i,h);r.context={deviceInfo:t},new s(this,r),this.api.registerPlatformAccessories(e.PLUGIN_NAME,e.PLATFORM_NAME,[r])}return o}isDeviceIneligible(e){var t,i;let r=this.getDeviceName(e);return(null===(i=e.baseInfo.permission)||void 0===i?void 0:null===(t=i.auth)||void 0===t?void 0:t.control)?!e.extendedInfo.bindType&&(this.log.warn("设备",r,"不支持云端控制"),!0):(this.log.warn("设备",r,"没有控制权限"),!0)}getAccessoryClass(e){switch(e.extendedInfo.categoryGrouping){case"空气环境":return h;case"热水卫浴":return n;case"冰箱冷储":return l;default:return}}getDeviceName(e){return`${e.extendedInfo.room} - ${e.baseInfo.deviceName}`}constructor(i,r,a){u(this,"log",void 0),u(this,"config",void 0),u(this,"api",void 0),u(this,"Service",void 0),u(this,"Characteristic",void 0),u(this,"accessories",void 0),u(this,"haierIoT",void 0),this.log=i,this.config=r,this.api=a,this.Service=this.api.hap.Service,this.Characteristic=this.api.hap.Characteristic,this.accessories=[],this.log.debug("平台初始化完成",this.config.name),this.accessories=[],this.api.on("didFinishLaunching",async()=>{let{username:r,password:a}=this.config;if(!r||!a){this.log.error("请在 config.json 中配置 username 和 password");return}this.haierIoT=new t.HaierIoT({username:r,password:a,storageDir:(0,e.generateCacheDir)(this.api.user.storagePath()),logger:i}),await this.haierIoT.connect(),this.discoverDevices()}),this.api.on("shutdown",()=>{this.log.info("Homebridge 即将关闭,断开连接")})}}let g=t=>{t.registerPlatform(e.PLATFORM_NAME,d)};export{g as default};
1
+ import*as e from"homebridge-plugin-haier-v2/shared";import*as t from"haier-iot";import*as i from"homebridge";var r={"./package.json":function(e){e.exports=JSON.parse('{"eN":{"H":"https://github.com/zwb124/homebridge-plugin-haier-v2/issues"}}')}},a={};function s(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}class o{async initializeAccessory(){await this.getDevDigitalModel();let{deviceInfo:e}=this.accessory.context;this.setupListeners(e.baseInfo.deviceId),this.setupAccessoryInformation(e),this.init()}setupListeners(e){this.platform.haierIoT.addListener("devDigitalModelUpdate",(t,i)=>{e===t&&(this.accessory.context.devDigitalModel=i,this.onDevDigitalModelUpdate())})}setupAccessoryInformation(e){var t;let{Characteristic:i,Service:r}=this.platform;null===(t=this.accessory.getService(r.AccessoryInformation))||void 0===t||t.setCharacteristic(i.Manufacturer,e.extendedInfo.brand).setCharacteristic(i.Model,e.extendedInfo.model).setCharacteristic(i.SerialNumber,e.extendedInfo.prodNo)}setServices(e,t,i){let r=this.accessory.getService(e);if(r)return this.services[e]=r,r;let a=this.accessory.displayName;i&&(a+=` - ${i}`);let s=this.accessory.addService(t,a,e);return s.setCharacteristic(this.Characteristic.Name,a),this.services[e]=s,s}get Characteristic(){return this.platform.Characteristic}get deviceInfo(){return this.accessory.context.deviceInfo}get deviceProperties(){var e;return Object.fromEntries(((null===(e=this.accessory.context.devDigitalModel)||void 0===e?void 0:e.attributes)??[]).map(e=>[e.name,e]))}getPropertyValue(t,i){var r,a;return(0,e.safeJsonParse)((null===(a=this.deviceProperties)||void 0===a?void 0:null===(r=a[t])||void 0===r?void 0:r.value)??void 0,i)}async getDevDigitalModel(){let{deviceId:e,isOnline:t}=this.deviceInfo.baseInfo;if(!t)return this.platform.log.warn(`设备 ${this.accessory.displayName} 离线`),null;if(this.isFetchingDevDigitalModel)return this.devDigitalModelPromise??this.accessory.context.devDigitalModel;this.isFetchingDevDigitalModel=!0;try{this.devDigitalModelPromise??=this.platform.haierIoT.getDevDigitalModel(e);let t=await this.devDigitalModelPromise;if(!t)return this.platform.log.error(`设备 ${this.accessory.displayName} 的数据为空`),null;return this.accessory.context.devDigitalModel={...t},t}catch(e){return this.platform.log.error(`获取设备 ${this.accessory.displayName} 数据失败`,e),this.accessory.context.devDigitalModel}finally{this.isFetchingDevDigitalModel=!1,this.devDigitalModelPromise=void 0}}async sendCommands(...e){e.forEach(e=>{Object.entries(e).forEach(([e,t])=>{let{valueRange:i,desc:r}=this.deviceProperties[e]??{},a=t;if(i){if("LIST"===i.type){var s;let e=null===(s=i.dataList)||void 0===s?void 0:s.find(e=>e.data===t);a=(null==e?void 0:e.desc)??t}this.platform.log.info("设置",this.accessory.displayName,"的",r,"为",a)}})}),await this.platform.haierIoT.sendCommands(this.deviceInfo.baseInfo.deviceId,e),this.onDevDigitalModelUpdate()}constructor(e,t){s(this,"platform",void 0),s(this,"accessory",void 0),s(this,"services",void 0),s(this,"devDigitalModelPromise",void 0),s(this,"isFetchingDevDigitalModel",void 0),this.platform=e,this.accessory=t,this.services={},this.isFetchingDevDigitalModel=!1,this.initializeAccessory().catch(e=>this.platform.log.error(`初始化设备 ${this.accessory.displayName} 时出错:`,e))}}class h extends o{init(){let e=this.setServices("thermostat",this.platform.Service.Thermostat),{CurrentHeatingCoolingState:t,TargetHeatingCoolingState:i,CurrentTemperature:r,TargetTemperature:a,TemperatureDisplayUnits:s,CurrentRelativeHumidity:o,TargetRelativeHumidity:h}=this.Characteristic;e.getCharacteristic(t).onGet(this.getCurrentHeatingCoolingState.bind(this)),e.getCharacteristic(i).onGet(this.getTargetHeatingCoolingState.bind(this)).onSet(this.setTargetHeatingCoolingState.bind(this)),e.getCharacteristic(r).onGet(this.getCurrentTemperature.bind(this)),e.getCharacteristic(a).setProps({...this.targetTemperatureProps}).onGet(this.getTargetTemperature.bind(this)).onSet(this.setTargetTemperature.bind(this)),e.getCharacteristic(s).onGet(this.getTemperatureDisplayUnits.bind(this)),this.hasIndoorHumidity&&e.getCharacteristic(o).onGet(this.getCurrentRelativeHumidity.bind(this)),this.hasTargetHumidity&&e.getCharacteristic(h).onGet(this.getTargetRelativeHumidity.bind(this))}onDevDigitalModelUpdate(){let{CurrentHeatingCoolingState:t,TargetHeatingCoolingState:i,CurrentTemperature:r,TargetTemperature:a,CurrentRelativeHumidity:s,TargetRelativeHumidity:o}=this.Characteristic,h=this.services.thermostat;h&&((0,e.isNumber)(this.targetTemperature)&&h.getCharacteristic(a).setProps({...this.targetTemperatureProps}).updateValue(this.targetTemperature),(0,e.isNumber)(this.currentTemperature)&&h.getCharacteristic(r).updateValue(this.currentTemperature),h.getCharacteristic(t).updateValue(this.getCurrentHeatingCoolingState()),h.getCharacteristic(i).updateValue(this.getTargetHeatingCoolingState()),this.hasIndoorHumidity&&h.getCharacteristic(s).updateValue(this.getCurrentRelativeHumidity()),this.hasTargetHumidity&&h.getCharacteristic(o).updateValue(this.getTargetRelativeHumidity()))}get hasIndoorHumidity(){return!!this.deviceProperties.indoorHumidity}get hasTargetHumidity(){return!!this.deviceProperties.targetHumidity}get onOffStatus(){return this.getPropertyValue("onOffStatus",!1)}get currentTemperature(){return this.getPropertyValue("indoorTemperature")}get targetTemperature(){return this.getPropertyValue("targetTemperature")}get targetTemperatureProps(){var e;let t=null===(e=this.deviceProperties.targetTemp)||void 0===e?void 0:e.valueRange;if(!t||"STEP"!==t.type||!t.dataStep)return;let i=Number.parseFloat(t.dataStep.minValue),r=Number.parseFloat(t.dataStep.maxValue);return{minValue:i,maxValue:r,minStep:Number.parseFloat(t.dataStep.step),validValueRanges:[i,r]}}getCurrentHeatingCoolingState(){var e;let{CurrentHeatingCoolingState:t}=this.Characteristic;if(!this.onOffStatus)return t.OFF;switch(null===(e=this.deviceProperties.operationMode)||void 0===e?void 0:e.value){case"1":return t.COOL;case"4":return t.HEAT;default:if(this.currentTemperature??0>(this.targetTemperature??0))return t.COOL;return t.HEAT}}getTargetHeatingCoolingState(){var e;let{TargetHeatingCoolingState:t}=this.Characteristic;if(!this.onOffStatus)return t.OFF;switch(null===(e=this.deviceProperties.operationMode)||void 0===e?void 0:e.value){case"1":return t.COOL;case"4":return t.HEAT;default:return t.AUTO}}setTargetHeatingCoolingState(e){let{TargetHeatingCoolingState:t}=this.Characteristic;if(e===t.OFF)return this.sendCommands({onOffStatus:"false"});let i={[t.AUTO]:"0",[t.COOL]:"1",[t.HEAT]:"4"}[e];if(i)return this.sendCommands({onOffStatus:"true"},{operationMode:i});this.platform.log.warn("Unsupported TargetHeatingCoolingState:",e)}getCurrentTemperature(){return this.currentTemperature}getTargetTemperature(){return this.targetTemperature}setTargetTemperature(e){let{minValue:t=10,maxValue:i=38}=this.targetTemperatureProps??{},r=[{targetTemperature:Math.min(Math.max(Number.parseFloat(e.toString()),t),i).toString()}];return this.onOffStatus||r.unshift({onOffStatus:"true"}),this.sendCommands(...r)}getTemperatureDisplayUnits(){var e;return(null===(e=this.deviceProperties.tempUnit)||void 0===e?void 0:e.value)==="2"?this.Characteristic.TemperatureDisplayUnits.FAHRENHEIT:this.Characteristic.TemperatureDisplayUnits.CELSIUS}getCurrentRelativeHumidity(){return this.getPropertyValue("indoorHumidity")}getTargetRelativeHumidity(){return this.getPropertyValue("targetHumidity")}}class n extends o{async init(){this.setServices("thermostat",this.platform.Service.Thermostat),this.services.thermostat.getCharacteristic(this.Characteristic.CurrentHeatingCoolingState).onGet(this.getCurrentHeatingCoolingState.bind(this)),this.services.thermostat.getCharacteristic(this.Characteristic.TargetHeatingCoolingState).setProps({validValues:[this.Characteristic.TargetHeatingCoolingState.OFF,this.Characteristic.TargetHeatingCoolingState.HEAT]}).onGet(this.getCurrentHeatingCoolingState.bind(this)).onSet(this.setTargetHeatingCoolingState.bind(this)),this.services.thermostat.getCharacteristic(this.Characteristic.CurrentTemperature).onGet(this.getTargetTemperature.bind(this)),this.services.thermostat.getCharacteristic(this.Characteristic.TargetTemperature).setProps({...this.targetTemperatureProps}).onGet(this.getTargetTemperature.bind(this)).onSet(this.setTargetTemperature.bind(this)),this.services.thermostat.getCharacteristic(this.Characteristic.TemperatureDisplayUnits).onGet(()=>this.Characteristic.TemperatureDisplayUnits.CELSIUS),this.getPropertyValue("zeroColdWaterFuncExist")&&(this.setServices("switch",this.platform.Service.Switch,"零冷水"),this.services.switch.getCharacteristic(this.Characteristic.On).onGet(this.getZeroColdWaterStatus.bind(this)).onSet(this.setZeroColdWaterStatus.bind(this))),this.deviceProperties.flowStatus&&(this.setServices("flowStatus",this.platform.Service.Valve,"水流状态"),this.services.flowStatus.getCharacteristic(this.Characteristic.Active).setProps({perms:[i.Perms.PAIRED_READ]}).onGet(()=>this.getPropertyValue("flowStatus")?this.Characteristic.Active.ACTIVE:this.Characteristic.Active.INACTIVE),this.services.flowStatus.getCharacteristic(this.Characteristic.InUse).onGet(()=>this.getPropertyValue("flowStatus")?this.Characteristic.InUse.IN_USE:this.Characteristic.InUse.NOT_IN_USE)),this.oneShotState&&this.scheduleOneShotCompletion()}onDevDigitalModelUpdate(){let t=this.services.thermostat;if(t&&((0,e.isNumber)(this.targetTemperature)&&(t.getCharacteristic(this.Characteristic.TargetTemperature).setProps({...this.targetTemperatureProps}).updateValue(this.targetTemperature),t.getCharacteristic(this.Characteristic.CurrentTemperature).updateValue(this.targetTemperature)),this.getPropertyValue("zeroColdWaterFuncExist"))){var i;null===(i=this.services.switch)||void 0===i||i.getCharacteristic(this.Characteristic.On).updateValue(this.getZeroColdWaterStatus())}}get onOffStatus(){return this.getPropertyValue("onOffStatus")}get targetTemperature(){return this.getPropertyValue("targetTemp")}get targetTemperatureProps(){var e;let t=null===(e=this.deviceProperties.targetTemp)||void 0===e?void 0:e.valueRange;if(!t||"STEP"!==t.type||!t.dataStep)return;let i=Number.parseFloat(t.dataStep.minValue),r=Number.parseFloat(t.dataStep.maxValue);return{minValue:i,maxValue:r,minStep:Number.parseFloat(t.dataStep.step),validValueRanges:[i,r]}}getCurrentHeatingCoolingState(){return this.onOffStatus?this.Characteristic.CurrentHeatingCoolingState.HEAT:this.Characteristic.CurrentHeatingCoolingState.OFF}setTargetHeatingCoolingState(e){return this.sendCommands({onOffStatus:(!!e).toString()})}getTargetTemperature(){return this.targetTemperature}setTargetTemperature(e){let{minValue:t=0,maxValue:i=100}=this.targetTemperatureProps??{},r=Math.min(Math.max(Number.parseFloat(e.toString()),t),i).toString();return this.sendCommands({targetTemp:r})}get oneShotState(){return this.accessory.context.zeroColdWaterOneShot}get oneShotDurationMs(){var e,t;let i=null===(t=this.platform.config.customConfig)||void 0===t?void 0:null===(e=t.hotWater)||void 0===e?void 0:e.zeroColdWaterDurationMinutes;return 6e4*Math.min(Math.max(Number.isFinite(i)?Number(i):5,1),120)}getZeroColdWaterStatus(){var e;return(null===(e=this.oneShotState)||void 0===e?void 0:e.phase)==="running"}async setZeroColdWaterStatus(e){if(e){if(this.oneShotState)return;this.accessory.context.zeroColdWaterOneShot={startedAt:Date.now(),durationMs:this.oneShotDurationMs,phase:"running"},this.platform.api.updatePlatformAccessories([this.accessory]);try{await this.sendCommands({zeroColdWaterStatus:"1"}),this.platform.log.info(`已启动单次零冷水循环,计划 ${Math.round(this.oneShotDurationMs/6e4)} 分钟后恢复云智慧(AI)模式:`,this.accessory.displayName),this.scheduleOneShotCompletion()}catch(e){var t;throw delete this.accessory.context.zeroColdWaterOneShot,this.platform.api.updatePlatformAccessories([this.accessory]),null===(t=this.services.switch)||void 0===t||t.getCharacteristic(this.Characteristic.On).updateValue(!1),e}return}this.oneShotTimer&&(clearTimeout(this.oneShotTimer),this.oneShotTimer=void 0),this.oneShotState&&await this.completeOneShot()}scheduleOneShotCompletion(){this.oneShotTimer&&clearTimeout(this.oneShotTimer);let e=this.oneShotState;if(!e)return;let t="restoring"===e.phase?0:e.startedAt+e.durationMs-Date.now();if(t<=0){this.completeOneShot();return}this.oneShotTimer=setTimeout(()=>{this.completeOneShot()},t)}async completeOneShot(){var e;this.oneShotTimer&&(clearTimeout(this.oneShotTimer),this.oneShotTimer=void 0);let t=this.oneShotState;if(t){t.phase="restoring",this.platform.log.info("单次零冷水循环计时结束,开始恢复云智慧(AI)模式:",this.accessory.displayName),this.platform.api.updatePlatformAccessories([this.accessory]),null===(e=this.services.switch)||void 0===e||e.getCharacteristic(this.Characteristic.On).updateValue(!1);try{await this.sendCommands({zeroColdWaterStatus:"4"}),delete this.accessory.context.zeroColdWaterOneShot,this.platform.api.updatePlatformAccessories([this.accessory]),this.platform.log.info("热水器单次零冷水循环计时结束,已恢复云智慧(AI)循环模式:",this.accessory.displayName)}catch(e){this.platform.api.updatePlatformAccessories([this.accessory]),this.oneShotTimer=setTimeout(()=>void this.completeOneShot(),6e4),this.platform.log.error("恢复热水器云智慧(AI)零冷水模式失败,60 秒后重试:",this.accessory.displayName,e)}}}constructor(...e){var t,i;super(...e),i=void 0,(t="oneShotTimer")in this?Object.defineProperty(this,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):this[t]=i}}let c={refrigerator:{displayName:"冷藏室",currentTempKey:"refrigeratorTemperatureC",targetTempKey:"refrigeratorTargetTempLevel"},freezer:{displayName:"冷冻室",currentTempKey:"freezerTemperatureC",targetTempKey:"freezerTargetTempLevel"},vtRoom:{displayName:"变温室",currentTempKey:"vtRoomTemperature",targetTempKey:"vtRoomTargetTempLevel"}};class l extends o{async init(){Object.entries(c).forEach(([e,{displayName:t,currentTempKey:i,targetTempKey:r}])=>{this.createTemperatureService(e,t,i,r)})}getActive(){return this.deviceInfo.baseInfo.isOnline?this.Characteristic.Active.ACTIVE:this.Characteristic.Active.INACTIVE}getCurrentHeaterCoolerState(){return this.deviceInfo.baseInfo.isOnline?this.Characteristic.CurrentHeaterCoolerState.COOLING:this.Characteristic.CurrentHeaterCoolerState.INACTIVE}parseTemperature(e){return e?Number.parseInt(e):0}onDevDigitalModelUpdate(){Object.entries(c).forEach(([e,{currentTempKey:t,targetTempKey:i}])=>{let r=this.services[e];if(!r)return;let a=this.deviceProperties[t],s=this.deviceProperties[i];if(!a||!s)return;let o=this.extractCelsiusDataMapping(s.valueRange);r.getCharacteristic(this.Characteristic.CurrentTemperature).updateValue(this.parseTemperature(a.value??void 0)),r.getCharacteristic(this.Characteristic.CoolingThresholdTemperature).updateValue(o.get(s.value??"")??0)})}createTemperatureService(e,t,r,a){let s=this.deviceProperties[r],o=this.deviceProperties[a];if(!s||!o)return;this.setServices(e,this.platform.Service.HeaterCooler,t);let h=this.extractCelsiusDataMapping(o.valueRange),n=Array.from(h.values()),c=Math.min(...n),l=Math.max(...n),u=this.calculateStep(n),d=this.services[e];d.getCharacteristic(this.Characteristic.Active).setProps({perms:[i.Perms.PAIRED_READ]}).onGet(this.getActive.bind(this)),d.getCharacteristic(this.Characteristic.CurrentHeaterCoolerState).onGet(this.getCurrentHeaterCoolerState.bind(this)).setProps({perms:[i.Perms.PAIRED_READ]}),d.getCharacteristic(this.Characteristic.TargetHeaterCoolerState).setValue(this.Characteristic.TargetHeaterCoolerState.COOL).setProps({minValue:this.Characteristic.TargetHeaterCoolerState.COOL,maxValue:this.Characteristic.TargetHeaterCoolerState.COOL,validValues:[this.Characteristic.TargetHeaterCoolerState.COOL],perms:[i.Perms.PAIRED_READ]}).onGet(()=>this.Characteristic.TargetHeaterCoolerState.COOL),d.getCharacteristic(this.Characteristic.CurrentTemperature).onGet(()=>this.parseTemperature(s.value??void 0)),d.getCharacteristic(this.Characteristic.CoolingThresholdTemperature).setValue(h.get(o.value??"")??0).setProps({minValue:c,maxValue:l,minStep:u,validValues:n}).onGet(()=>h.get(o.value??"")??0).onSet(e=>{let t=h.getKey(Number.parseInt(e.toString()));t&&this.sendCommands({[a]:t})})}extractCelsiusDataMapping(t){let i=new e.BiMap;if(!t||"LIST"!==t.type)return i;let r=/(-?\d+)℃/;return t.dataList.forEach(e=>{var t;let a=null===(t=e.desc)||void 0===t?void 0:t.match(r);if(a){let t=a[1];i.set(e.data,Number.parseInt(t,10))}}),i}calculateStep(e){return Math.min(...e.map((e,t,i)=>0===t?i[t+1]-e:e-i[t-1]))}}function u(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}class d{configureAccessory(e){let{disabledDevices:t=[]}=this.config;if(this.log.info("从缓存加载附件:",e.displayName),t.includes(e.context.deviceInfo.baseInfo.deviceId)){this.log.info("设备",e.displayName,"已被禁用");return}this.accessories.push(e)}discoverDevices(){let{familyId:e}=this.config;if(!e){this.log.error("请在 config.json 中配置 familyId");return}this.haierIoT.getDevicesByFamilyId(e).then(async e=>{let t=(await Promise.allSettled(e.map(e=>this.handleDevice(e)))).filter(e=>"fulfilled"===e.status).map(e=>e.value).filter(e=>"string"==typeof e);t.length&&this.haierIoT.subscribeDevices(t)})}async handleDevice(t){let{disabledDevices:i=[]}=this.config;if(i.includes(t.baseInfo.deviceId)){this.log.info("设备",t.baseInfo.deviceName,"已被禁用");return}if(this.isDeviceIneligible(t))return;let s=this.getAccessoryClass(t);if(!s){this.log.warn("设备",t.baseInfo.deviceName,"暂不支持",t.extendedInfo.categoryGrouping,",可提交 issue 申请支持",function e(t){var i=a[t];if(void 0!==i)return i.exports;var s=a[t]={exports:{}};return r[t](s,s.exports,e),s.exports}("./package.json").eN.H);return}let{deviceId:o}=t.baseInfo,h=this.api.hap.uuid.generate(o),n=this.accessories.find(e=>e.UUID===h);if(n)this.log.info("Restoring existing accessory from cache:",n.displayName),n.context={...n.context,deviceInfo:t},this.api.updatePlatformAccessories([n]),new s(this,n);else{let i=this.getDeviceName(t);this.log.info("Adding new accessory:",i);let r=new this.api.platformAccessory(i,h);r.context={deviceInfo:t},new s(this,r),this.api.registerPlatformAccessories(e.PLUGIN_NAME,e.PLATFORM_NAME,[r])}return o}isDeviceIneligible(e){var t,i;let r=this.getDeviceName(e);return(null===(i=e.baseInfo.permission)||void 0===i?void 0:null===(t=i.auth)||void 0===t?void 0:t.control)?!e.extendedInfo.bindType&&(this.log.warn("设备",r,"不支持云端控制"),!0):(this.log.warn("设备",r,"没有控制权限"),!0)}getAccessoryClass(e){switch(e.extendedInfo.categoryGrouping){case"空气环境":return h;case"热水卫浴":return n;case"冰箱冷储":return l;default:return}}getDeviceName(e){return`${e.extendedInfo.room} - ${e.baseInfo.deviceName}`}constructor(i,r,a){u(this,"log",void 0),u(this,"config",void 0),u(this,"api",void 0),u(this,"Service",void 0),u(this,"Characteristic",void 0),u(this,"accessories",void 0),u(this,"haierIoT",void 0),this.log=i,this.config=r,this.api=a,this.Service=this.api.hap.Service,this.Characteristic=this.api.hap.Characteristic,this.accessories=[],this.log.debug("平台初始化完成",this.config.name),this.accessories=[],this.api.on("didFinishLaunching",async()=>{let{username:r,password:a}=this.config;if(!r||!a){this.log.error("请在 config.json 中配置 username 和 password");return}this.haierIoT=new t.HaierIoT({username:r,password:a,storageDir:(0,e.generateCacheDir)(this.api.user.storagePath()),logger:i}),await this.haierIoT.connect(),this.discoverDevices()}),this.api.on("shutdown",()=>{this.log.info("Homebridge 即将关闭,断开连接")})}}let g=t=>{t.registerPlatform(e.PLATFORM_NAME,d)};export{g as default};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "homebridge-plugin-haier-v2",
3
3
  "displayName": "Haier",
4
- "version": "2.6.7-beta.0",
4
+ "version": "2.6.7",
5
5
  "description": "海尔智家 Homebridge 插件",
6
6
  "type": "module",
7
7
  "exports": {