|
| 1 | +import debounce from 'debounce'; |
| 2 | +import BaseAccessory from './BaseAccessory'; |
| 3 | + |
| 4 | +const POWER_OFF = 0; |
| 5 | +const POWER_ON = 1; |
| 6 | + |
| 7 | +const AC_MODE_COOL = 0; |
| 8 | +const AC_MODE_HEAT = 1; |
| 9 | +const AC_MODE_AUTO = 2; |
| 10 | +const AC_MODE_FAN = 3; |
| 11 | +const AC_MODE_DEHUMIDIFIER = 4; |
| 12 | + |
| 13 | +// const FAN_SPEED_AUTO = 0; |
| 14 | +// const FAN_SPEED_LOW = 1; |
| 15 | +// const FAN_SPEED_MEDIUM = 2; |
| 16 | +// const FAN_SPEED_HIGH = 3; |
| 17 | + |
| 18 | +export default class IRAirConditionerAccessory extends BaseAccessory { |
| 19 | + |
| 20 | + configureServices() { |
| 21 | + this.configureAirConditioner(); |
| 22 | + this.configureDehumidifier(); |
| 23 | + this.configureFan(); |
| 24 | + } |
| 25 | + |
| 26 | + configureAirConditioner() { |
| 27 | + |
| 28 | + const service = this.mainService(); |
| 29 | + const { INACTIVE, ACTIVE } = this.Characteristic.Active; |
| 30 | + |
| 31 | + // Required Characteristics |
| 32 | + service.getCharacteristic(this.Characteristic.Active) |
| 33 | + .onSet(value => { |
| 34 | + if (value === ACTIVE) { |
| 35 | + // Turn off Dehumidifier & Fan |
| 36 | + this.supportDehumidifier() && this.dehumidifierService().setCharacteristic(this.Characteristic.Active, INACTIVE); |
| 37 | + this.supportFan() && this.fanService().setCharacteristic(this.Characteristic.Active, INACTIVE); |
| 38 | + } |
| 39 | + this.debounceSendACCommands(); |
| 40 | + }); |
| 41 | + |
| 42 | + const { IDLE } = this.Characteristic.CurrentHeaterCoolerState; |
| 43 | + service.setCharacteristic(this.Characteristic.CurrentHeaterCoolerState, IDLE); |
| 44 | + |
| 45 | + this.configureTargetState(); |
| 46 | + |
| 47 | + // Optional Characteristics |
| 48 | + this.configureRotationSpeed(service); |
| 49 | + |
| 50 | + const key_range = this.device.remote_keys.key_range; |
| 51 | + if (key_range.find(item => item.mode === AC_MODE_HEAT)) { |
| 52 | + const range = this.getTempRange(AC_MODE_HEAT)!; |
| 53 | + service.getCharacteristic(this.Characteristic.HeatingThresholdTemperature) |
| 54 | + .onSet(() => { |
| 55 | + this.debounceSendACCommands(); |
| 56 | + }) |
| 57 | + .setProps({ minValue: range[0], maxValue: range[1], minStep: 1 }); |
| 58 | + } |
| 59 | + if (key_range.find(item => item.mode === AC_MODE_COOL)) { |
| 60 | + const range = this.getTempRange(AC_MODE_COOL)!; |
| 61 | + service.getCharacteristic(this.Characteristic.CoolingThresholdTemperature) |
| 62 | + .onSet(() => { |
| 63 | + this.debounceSendACCommands(); |
| 64 | + }) |
| 65 | + .setProps({ minValue: range[0], maxValue: range[1], minStep: 1 }); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + configureDehumidifier() { |
| 70 | + if (!this.supportDehumidifier()) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const service = this.dehumidifierService(); |
| 75 | + const { INACTIVE, ACTIVE } = this.Characteristic.Active; |
| 76 | + |
| 77 | + // Required Characteristics |
| 78 | + service.getCharacteristic(this.Characteristic.Active) |
| 79 | + .onSet(value => { |
| 80 | + if (value === ACTIVE) { |
| 81 | + // Turn off AC & Fan |
| 82 | + this.mainService().setCharacteristic(this.Characteristic.Active, INACTIVE); |
| 83 | + this.supportFan() && this.fanService().setCharacteristic(this.Characteristic.Active, INACTIVE); |
| 84 | + } |
| 85 | + this.debounceSendACCommands(); |
| 86 | + }); |
| 87 | + |
| 88 | + const { DEHUMIDIFYING } = this.Characteristic.CurrentHumidifierDehumidifierState; |
| 89 | + service.setCharacteristic(this.Characteristic.CurrentHumidifierDehumidifierState, DEHUMIDIFYING); |
| 90 | + |
| 91 | + const { DEHUMIDIFIER } = this.Characteristic.TargetHumidifierDehumidifierState; |
| 92 | + service.getCharacteristic(this.Characteristic.TargetHumidifierDehumidifierState) |
| 93 | + .updateValue(DEHUMIDIFIER) |
| 94 | + .setProps({ validValues: [DEHUMIDIFIER] }); |
| 95 | + |
| 96 | + service.setCharacteristic(this.Characteristic.CurrentRelativeHumidity, 0); |
| 97 | + |
| 98 | + // Optional Characteristics |
| 99 | + this.configureRotationSpeed(service); |
| 100 | + } |
| 101 | + |
| 102 | + configureFan() { |
| 103 | + if (!this.supportFan()) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + const service = this.fanService(); |
| 108 | + const { INACTIVE, ACTIVE } = this.Characteristic.Active; |
| 109 | + |
| 110 | + // Required Characteristics |
| 111 | + service.getCharacteristic(this.Characteristic.Active) |
| 112 | + .onSet(value => { |
| 113 | + if (value === ACTIVE) { |
| 114 | + // Turn off AC & Fan |
| 115 | + this.mainService().setCharacteristic(this.Characteristic.Active, INACTIVE); |
| 116 | + this.supportDehumidifier() && this.dehumidifierService().setCharacteristic(this.Characteristic.Active, INACTIVE); |
| 117 | + } |
| 118 | + this.debounceSendACCommands(); |
| 119 | + }); |
| 120 | + |
| 121 | + // Optional Characteristics |
| 122 | + this.configureRotationSpeed(service); |
| 123 | + } |
| 124 | + |
| 125 | + mainService() { |
| 126 | + return this.accessory.getService(this.Service.HeaterCooler) |
| 127 | + || this.accessory.addService(this.Service.HeaterCooler); |
| 128 | + } |
| 129 | + |
| 130 | + dehumidifierService() { |
| 131 | + return this.accessory.getService(this.Service.HumidifierDehumidifier) |
| 132 | + || this.accessory.addService(this.Service.HumidifierDehumidifier, this.accessory.displayName + ' Dehumidifier'); |
| 133 | + } |
| 134 | + |
| 135 | + fanService() { |
| 136 | + return this.accessory.getService(this.Service.Fanv2) |
| 137 | + || this.accessory.addService(this.Service.Fanv2, this.accessory.displayName + ' Fan'); |
| 138 | + } |
| 139 | + |
| 140 | + getKeyRangeItem(mode: number) { |
| 141 | + return this.device.remote_keys.key_range.find(item => item.mode === mode); |
| 142 | + } |
| 143 | + |
| 144 | + supportDehumidifier() { |
| 145 | + return this.getKeyRangeItem(AC_MODE_DEHUMIDIFIER) !== undefined; |
| 146 | + } |
| 147 | + |
| 148 | + supportFan() { |
| 149 | + return this.getKeyRangeItem(AC_MODE_FAN) !== undefined; |
| 150 | + } |
| 151 | + |
| 152 | + getTempRange(mode: number) { |
| 153 | + const keyRangeItem = this.getKeyRangeItem(mode); |
| 154 | + if (!keyRangeItem || !keyRangeItem.temp_list || keyRangeItem.temp_list.length === 0) { |
| 155 | + return undefined; |
| 156 | + } |
| 157 | + |
| 158 | + const min = keyRangeItem.temp_list[0].temp; |
| 159 | + const max = keyRangeItem.temp_list[keyRangeItem.temp_list.length - 1].temp; |
| 160 | + return [min, max]; |
| 161 | + } |
| 162 | + |
| 163 | + configureTargetState() { |
| 164 | + const { AUTO, HEAT, COOL } = this.Characteristic.TargetHeaterCoolerState; |
| 165 | + |
| 166 | + const validValues: number[] = []; |
| 167 | + const key_range = this.device.remote_keys.key_range; |
| 168 | + if (key_range.find(item => item.mode === AC_MODE_AUTO)) { |
| 169 | + validValues.push(AUTO); |
| 170 | + } |
| 171 | + if (key_range.find(item => item.mode === AC_MODE_HEAT)) { |
| 172 | + validValues.push(HEAT); |
| 173 | + } |
| 174 | + if (key_range.find(item => item.mode === AC_MODE_COOL)) { |
| 175 | + validValues.push(COOL); |
| 176 | + } |
| 177 | + |
| 178 | + if (validValues.length === 0) { |
| 179 | + this.log.warn('Invalid mode range for TargetHeaterCoolerState:', key_range); |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + this.mainService().getCharacteristic(this.Characteristic.TargetHeaterCoolerState) |
| 184 | + .onSet(() => { |
| 185 | + this.debounceSendACCommands(); |
| 186 | + }) |
| 187 | + .setProps({ validValues }); |
| 188 | + } |
| 189 | + |
| 190 | + configureRotationSpeed(service) { |
| 191 | + service.getCharacteristic(this.Characteristic.RotationSpeed) |
| 192 | + .onSet(() => { |
| 193 | + this.debounceSendACCommands(); |
| 194 | + }) |
| 195 | + .setProps({ minValue: 0, maxValue: 3, minStep: 1, unit: 'speed' }); |
| 196 | + } |
| 197 | + |
| 198 | + debounceSendACCommands = debounce(this.sendACCommands, 100); |
| 199 | + |
| 200 | + async sendACCommands() { |
| 201 | + |
| 202 | + let power = POWER_ON; |
| 203 | + let mode = -1; |
| 204 | + let temp = -1; |
| 205 | + let wind = -1; |
| 206 | + |
| 207 | + // Determine AC mode |
| 208 | + const { ACTIVE } = this.Characteristic.Active; |
| 209 | + if (this.mainService().getCharacteristic(this.Characteristic.Active).value === ACTIVE) { |
| 210 | + const { HEAT, COOL } = this.Characteristic.TargetHeaterCoolerState; |
| 211 | + const value = this.mainService().getCharacteristic(this.Characteristic.TargetHeaterCoolerState) |
| 212 | + .value as number; |
| 213 | + if (value === HEAT) { |
| 214 | + mode = AC_MODE_HEAT; |
| 215 | + } else if (value === COOL) { |
| 216 | + mode = AC_MODE_COOL; |
| 217 | + } else { |
| 218 | + mode = AC_MODE_AUTO; |
| 219 | + } |
| 220 | + } else if (this.supportDehumidifier() && this.dehumidifierService().getCharacteristic(this.Characteristic.Active).value === ACTIVE) { |
| 221 | + mode = AC_MODE_DEHUMIDIFIER; |
| 222 | + } else if (this.supportFan() && this.fanService().getCharacteristic(this.Characteristic.Active).value === ACTIVE) { |
| 223 | + mode = AC_MODE_FAN; |
| 224 | + } else { |
| 225 | + // No mode |
| 226 | + power = POWER_OFF; |
| 227 | + } |
| 228 | + |
| 229 | + if (mode === AC_MODE_AUTO) { |
| 230 | + temp = this.mainService().getCharacteristic(this.Characteristic.CoolingThresholdTemperature).value as number; |
| 231 | + wind = this.mainService().getCharacteristic(this.Characteristic.RotationSpeed).value as number; |
| 232 | + } else if (mode === AC_MODE_HEAT) { |
| 233 | + temp = this.mainService().getCharacteristic(this.Characteristic.HeatingThresholdTemperature).value as number; |
| 234 | + wind = this.mainService().getCharacteristic(this.Characteristic.RotationSpeed).value as number; |
| 235 | + } else if (mode === AC_MODE_COOL) { |
| 236 | + temp = this.mainService().getCharacteristic(this.Characteristic.CoolingThresholdTemperature).value as number; |
| 237 | + wind = this.mainService().getCharacteristic(this.Characteristic.RotationSpeed).value as number; |
| 238 | + } else if (mode === AC_MODE_DEHUMIDIFIER) { |
| 239 | + temp = this.mainService().getCharacteristic(this.Characteristic.CoolingThresholdTemperature).value as number; |
| 240 | + wind = this.dehumidifierService().getCharacteristic(this.Characteristic.RotationSpeed).value as number; |
| 241 | + } else if (mode === AC_MODE_FAN) { |
| 242 | + temp = this.mainService().getCharacteristic(this.Characteristic.CoolingThresholdTemperature).value as number; |
| 243 | + wind = this.fanService().getCharacteristic(this.Characteristic.RotationSpeed).value as number; |
| 244 | + } |
| 245 | + |
| 246 | + (power === POWER_ON) && this.mainService().setCharacteristic(this.Characteristic.CurrentTemperature, temp); |
| 247 | + |
| 248 | + const { parent_id, id } = this.device; |
| 249 | + await this.deviceManager.sendInfraredACCommands(parent_id, id, power, mode, temp, wind); |
| 250 | + |
| 251 | + } |
| 252 | +} |
0 commit comments