UNPKG

homebridge-better-miot

Version:
279 lines 13.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AirPurifierProAccessory = void 0; const AirPurifierPro_1 = require("../devices/AirPurifierPro"); const PM25_LEVELS = [7, 15, 30, 55]; const Format = { toPercent: (value, maxValue) => { if (typeof value !== 'number' || Number.isNaN(value)) { return 0; } return Math.round(value / maxValue * 100); }, fromPercent: (value, maxValue) => { if (typeof value !== 'number' || Number.isNaN(value)) { return 0; } return Math.round(value / 100 * maxValue); }, toAirQuality: (value) => { if (value <= PM25_LEVELS[0]) { return Characteristic.AirQuality.EXCELLENT; } if (value <= PM25_LEVELS[1]) { return Characteristic.AirQuality.GOOD; } if (value <= PM25_LEVELS[2]) { return Characteristic.AirQuality.FAIR; } if (value <= PM25_LEVELS[3]) { return Characteristic.AirQuality.INFERIOR; } if (value > PM25_LEVELS[3]) { return Characteristic.AirQuality.POOR; } return Characteristic.AirQuality.UNKNOWN; }, toFilterChangeIndication: (value) => { if (value < 5) { return Characteristic.FilterChangeIndication.CHANGE_FILTER; } return Characteristic.FilterChangeIndication.FILTER_OK; }, toTargetAirPurifierState: (value) => { return value === AirPurifierPro_1.EOperationMode.FAVORITE ? Characteristic.TargetAirPurifierState.MANUAL : Characteristic.TargetAirPurifierState.AUTO; }, toCurrentAirPurifierState: (power, mode) => { if (power === false) { return Characteristic.CurrentAirPurifierState.INACTIVE; } return mode === AirPurifierPro_1.EOperationMode.SILENT ? Characteristic.CurrentAirPurifierState.IDLE : Characteristic.CurrentAirPurifierState.PURIFYING_AIR; }, toActive: (value) => { return value ? Characteristic.Active.ACTIVE : Characteristic.Active.INACTIVE; }, toSwitchableMode: (value) => { let isOn = false; if (typeof value === 'string') { isOn = value === '1'; } else if (typeof value === 'number') { isOn = value === 1; } else if (typeof value === 'boolean') { isOn = value; } return isOn ? AirPurifierPro_1.ESwitchableMode.ON : AirPurifierPro_1.ESwitchableMode.OFF; }, }; // TODO: fix it let Characteristic; var EServiceName; (function (EServiceName) { EServiceName["AIR_PURIFIER_SERVICE"] = "AIR_PURIFIER_SERVICE"; EServiceName["LIGHT_SENSOR_SERVICE"] = "LIGHT_SENSOR_SERVICE"; EServiceName["AIR_QUALITY_SERVICE"] = "AIR_QUALITY_SERVICE"; EServiceName["FILTER_MAINTENANCE_SERVICE"] = "FILTER_MAINTENANCE_SERVICE"; EServiceName["SILENT_MODE_SWITCH_SERVICE"] = "SILENT_MODE_SWITCH_SERVICE"; EServiceName["BUZZER_SWITCH_SERVICE"] = "BUZZER_SWITCH_SERVICE"; EServiceName["LED_SWITCH_STATE"] = "LED_SWITCH_STATE"; })(EServiceName || (EServiceName = {})); var EServiceSubType; (function (EServiceSubType) { EServiceSubType["AIR_PURIFIER"] = "AIR_PURIFIER"; EServiceSubType["LIGHT_SENSOR"] = "LIGHT_SENSOR"; EServiceSubType["AIR_QUALITY"] = "AIR_QUALITY"; EServiceSubType["FILTER_MAINTENANCE"] = "FILTER_MAINTENANCE"; EServiceSubType["SILENT_MODE_SWITCH"] = "SILENT_MODE_SWITCH"; EServiceSubType["BUZZER_SWITCH"] = "BUZZER_SWITCH"; EServiceSubType["LED_SWITCH"] = "LED_SWITCH"; })(EServiceSubType || (EServiceSubType = {})); class AirPurifierProAccessory { constructor(platform, accessory) { this.platform = platform; this.accessory = accessory; Characteristic = this.platform.Characteristic; this.accessory.getService(this.platform.Service.AccessoryInformation) .setCharacteristic(Characteristic.Manufacturer, 'Xiaomi') .setCharacteristic(Characteristic.Model, 'Air Purifier Pro') .setCharacteristic(Characteristic.SerialNumber, accessory.context.address); this.device = new AirPurifierPro_1.AirPurifierPro({ address: accessory.context.address, token: accessory.context.token, pollingIntervalMS: accessory.context.updateIntervalMS, }); this.service = this.accessory.getService(EServiceName.AIR_PURIFIER_SERVICE); if (!this.service) { this.service = this.accessory.addService(this.platform.Service.AirPurifier, EServiceName.AIR_PURIFIER_SERVICE, EServiceSubType.AIR_PURIFIER); } this.silentModeSwitchService = this.accessory.getService(EServiceName.SILENT_MODE_SWITCH_SERVICE); if (!this.silentModeSwitchService) { this.silentModeSwitchService = this.accessory.addService(this.platform.Service.Switch, EServiceName.SILENT_MODE_SWITCH_SERVICE, EServiceSubType.SILENT_MODE_SWITCH); } this.service .setCharacteristic(Characteristic.Name, accessory.context.displayName); this.silentModeSwitchService .setCharacteristic(Characteristic.Name, `${accessory.context.displayName} Silent Mode`); this.service .getCharacteristic(Characteristic.Active) .onGet(() => Format.toActive(this.device.properties.power)) .onSet((active) => this.device.setPower(Format.toSwitchableMode(active))); this.service .getCharacteristic(Characteristic.CurrentAirPurifierState) .onGet(() => { return Format.toCurrentAirPurifierState(this.device.properties.power, this.device.properties.mode); }); this.service .getCharacteristic(Characteristic.TargetAirPurifierState) .onGet(() => { return Format.toTargetAirPurifierState(this.device.properties.mode); }) .onSet((state) => { if (state === Characteristic.TargetAirPurifierState.MANUAL) { this.device.setMode(AirPurifierPro_1.EOperationMode.FAVORITE); } else { const isSilentModeEnabled = this.silentModeSwitchService .getCharacteristic(Characteristic.On) .value === true; this.device.setMode(isSilentModeEnabled ? AirPurifierPro_1.EOperationMode.SILENT : AirPurifierPro_1.EOperationMode.AUTO); } }); this.silentModeSwitchService .getCharacteristic(Characteristic.On) .onGet(() => this.device.properties.mode === AirPurifierPro_1.EOperationMode.SILENT) .onSet((isOn) => { if (isOn) { this.device.setMode(AirPurifierPro_1.EOperationMode.SILENT); } else { const isAutoModeEnabled = this.service .getCharacteristic(Characteristic.TargetAirPurifierState) .value === Characteristic.TargetAirPurifierState.AUTO; this.device.setMode(isAutoModeEnabled ? AirPurifierPro_1.EOperationMode.AUTO : AirPurifierPro_1.EOperationMode.FAVORITE); } }); this.service .getCharacteristic(Characteristic.RotationSpeed) .onGet(() => Format.toPercent(this.device.properties.favoriteLevel, 17)) .onSet((value) => this.device.setFavoriteLevel(Format.fromPercent(Number(value), 17))); this.device.on('power', (isOn) => { this.service .getCharacteristic(Characteristic.Active) .updateValue(Format.toActive(isOn)); if (!isOn) { this.service .getCharacteristic(Characteristic.CurrentAirPurifierState) .updateValue(Format.toCurrentAirPurifierState(isOn)); } }); this.device.on('mode', (mode) => { this.service .getCharacteristic(Characteristic.TargetAirPurifierState) .updateValue(Format.toTargetAirPurifierState(mode)); this.service .getCharacteristic(Characteristic.CurrentAirPurifierState) .updateValue(Format.toCurrentAirPurifierState(true, mode)); this.silentModeSwitchService .getCharacteristic(Characteristic.On) .updateValue(mode === AirPurifierPro_1.EOperationMode.SILENT); }); this.device.on('favoriteLevel', (favoriteLevel) => { this.service .getCharacteristic(Characteristic.RotationSpeed) .updateValue(Format.toPercent(favoriteLevel, 17)); }); this.buzzerSwitchService = this.accessory.getService(EServiceName.BUZZER_SWITCH_SERVICE); if (!this.buzzerSwitchService) { this.buzzerSwitchService = this.accessory.addService(this.platform.Service.Switch, EServiceName.BUZZER_SWITCH_SERVICE, EServiceSubType.BUZZER_SWITCH); } this.buzzerSwitchService .setCharacteristic(Characteristic.Name, `${accessory.context.displayName} Buzzer`); this.buzzerSwitchService .getCharacteristic(Characteristic.On) .onGet(() => this.device.properties.volume === 100) .onSet((isON) => this.device.setVolume(isON ? 100 : 0)); this.device.on('volume', (volume) => { this.buzzerSwitchService .getCharacteristic(Characteristic.On) .updateValue(volume === 100); }); this.ledSwitchService = this.accessory.getService(EServiceName.LED_SWITCH_STATE); if (!this.ledSwitchService) { this.ledSwitchService = this.accessory.addService(this.platform.Service.Switch, EServiceName.LED_SWITCH_STATE, EServiceSubType.LED_SWITCH); } this.ledSwitchService .setCharacteristic(Characteristic.Name, `${accessory.context.displayName} LED`); this.ledSwitchService .getCharacteristic(Characteristic.On) .onGet(() => this.device.properties.led || false) .onSet((isON) => this.device.setLed(Format.toSwitchableMode(isON))); this.device.on('led', (isOn) => { this.ledSwitchService .getCharacteristic(Characteristic.On) .updateValue(isOn); }); this.lightSensorService = this.accessory.getService(EServiceName.LIGHT_SENSOR_SERVICE); if (!this.lightSensorService) { this.lightSensorService = this.accessory.addService(this.platform.Service.LightSensor, EServiceName.LIGHT_SENSOR_SERVICE, EServiceSubType.LIGHT_SENSOR); } this.lightSensorService .setCharacteristic(Characteristic.Name, `${accessory.context.displayName} Light Sensor`); this.lightSensorService .getCharacteristic(Characteristic.CurrentAmbientLightLevel) .onGet(() => this.device.properties.illuminance || null); this.device.on('illuminance', (value) => { this.lightSensorService .getCharacteristic(Characteristic.CurrentAmbientLightLevel) .updateValue(value); }); this.airQualityService = this.accessory.getService(EServiceName.AIR_QUALITY_SERVICE); if (!this.airQualityService) { this.airQualityService = this.accessory.addService(this.platform.Service.AirQualitySensor, EServiceName.AIR_QUALITY_SERVICE, EServiceSubType.AIR_QUALITY); } this.airQualityService .setCharacteristic(Characteristic.Name, `${accessory.context.displayName} Air Quality Sensor`); this.airQualityService .getCharacteristic(Characteristic.AirQuality) .onGet(() => Format.toAirQuality(this.device.properties.pm25)); this.airQualityService .getCharacteristic(Characteristic.PM2_5Density) .onGet(() => this.device.properties.pm25 || null); this.device.on('pm25', (value) => { this.airQualityService .getCharacteristic(Characteristic.AirQuality) .updateValue(Format.toAirQuality(value)); this.airQualityService .getCharacteristic(Characteristic.PM2_5Density) .updateValue(value); }); this.filterMaintenanceService = this.accessory.getService(EServiceName.FILTER_MAINTENANCE_SERVICE); if (!this.filterMaintenanceService) { this.filterMaintenanceService = this.accessory.addService(this.platform.Service.FilterMaintenance, EServiceName.FILTER_MAINTENANCE_SERVICE, EServiceSubType.FILTER_MAINTENANCE); } this.filterMaintenanceService .setCharacteristic(Characteristic.Name, `${accessory.context.displayName} Filter Maintenance`); this.filterMaintenanceService.getCharacteristic(Characteristic.FilterChangeIndication) .onGet(() => Format.toFilterChangeIndication(this.device.properties.filterLifeRemaining)); this.filterMaintenanceService.getCharacteristic(Characteristic.FilterLifeLevel) .onGet(() => this.device.properties.filterLifeRemaining || null); this.device.on('filterLifeRemaining', (value) => { this.filterMaintenanceService .getCharacteristic(Characteristic.FilterChangeIndication) .updateValue(Format.toFilterChangeIndication(value)); this.filterMaintenanceService .getCharacteristic(Characteristic.FilterLifeLevel) .updateValue(value); }); } } exports.AirPurifierProAccessory = AirPurifierProAccessory; //# sourceMappingURL=AirPurifierProAccessory.js.map