UNPKG

homebridge-smarthq-test

Version:

The SmartHQ plugin allows you to interact with SmartHQ Devices in HomeKit and with Siri.

142 lines 5.39 kB
import axios from 'axios'; import { deviceBase } from './device.js'; const POWER_ERD = '0x7A0F'; const TEMP_SETPOINT_ERD = '0x7003'; const FAN_SPEED_ERD = '0x7A00'; const AMBIENT_TEMP_ERD = '0x7A02'; export class SmartHQPortableAC extends deviceBase { platform; device; ACPowerServiceName = 'AC Power'; ACTempSensorServiceName = 'AC Temperature Sensor'; service; fanService; constructor(platform, accessory, device) { super(platform, accessory, device); this.platform = platform; this.device = device; this.debugLog(`Portable AC Features: ${JSON.stringify(accessory.context.device.features)}`); this.service = this.accessory.getService(this.hap.Service.HeaterCooler) || this.accessory.addService(this.hap.Service.HeaterCooler); this.service.setCharacteristic(this.hap.Characteristic.Name, 'Portable A/C'); this.setupPowerHandlers(this.hap.Characteristic); this.setupTempHandlers(this.hap.Characteristic); this.setupFanSpeedHandlers(this.hap.Characteristic); this.fanService = this.accessory.getService(this.hap.Service.Fan) || this.accessory.addService(this.hap.Service.Fan); this.setupFanService(this.hap.Characteristic); } setupPowerHandlers(Characteristic) { this.service.getCharacteristic(Characteristic.Active) .onGet(async () => { const isOn = await this.isOn(); return isOn ? Characteristic.Active.ACTIVE : Characteristic.Active.INACTIVE; }) .onSet(async (value) => { if (value === Characteristic.Active.ACTIVE) { await this.turnOn(); } else { await this.turnOff(); } }); } setupTempHandlers(Characteristic) { this.service.getCharacteristic(Characteristic.CurrentTemperature) .onGet(async () => { return await this.getAmbientTemperature(); }); this.service.getCharacteristic(Characteristic.CoolingThresholdTemperature) .setProps({ minValue: 60, maxValue: 86, minStep: 1 }) .onGet(async () => { return await this.getTemperature(); }) .onSet(async (value) => { await this.setTemperature(value); }); } setupFanSpeedHandlers(Characteristic) { this.service.getCharacteristic(Characteristic.RotationSpeed) .setProps({ minValue: 0, maxValue: 3, minStep: 1 }) .onGet(async () => { const speed = await this.getFanSpeed(); return { low: 0, medium: 1, high: 2, auto: 3 }[speed]; }) .onSet(async (value) => { const levels = ['low', 'medium', 'high', 'auto']; await this.setFanSpeed(levels[value] ?? 'low'); }); } setupFanService(Characteristic) { if (!this.fanService) { return; } this.fanService.getCharacteristic(Characteristic.On) .onGet(async () => { return await this.isOn(); }) .onSet(async (value) => { if (value) { await this.turnOn(); } else { await this.turnOff(); } }); this.fanService.getCharacteristic(Characteristic.RotationSpeed) .setProps({ minValue: 0, maxValue: 3, minStep: 1 }) .onGet(async () => { const speed = await this.getFanSpeed(); return { low: 0, medium: 1, high: 2, auto: 3 }[speed]; }) .onSet(async (value) => { const levels = ['low', 'medium', 'high', 'auto']; await this.setFanSpeed(levels[value] ?? 'low'); }); } async readErd(erd) { const res = await axios.get(`/appliance/${this.accessory.context.device.applianceId}/erd/${erd}`); return Number.parseInt(res.data.value, 16); } async writeErd(erd, value) { await axios.post(`/appliance/${this.accessory.context.device.applianceId}/erd/${erd}`, { kind: 'appliance#erdListEntry', userId: this.accessory.context.userId, applianceId: this.accessory.context.device.applianceId, erd, value: typeof value === 'boolean' ? (value ? '01' : '00') : typeof value === 'number' ? value.toString(16).padStart(2, '0') : value, }); } async turnOn() { await this.writeErd(POWER_ERD, 1); } async turnOff() { await this.writeErd(POWER_ERD, 0); } async isOn() { return (await this.readErd(POWER_ERD)) === 1; } async setTemperature(tempF) { await this.writeErd(TEMP_SETPOINT_ERD, tempF); } async getTemperature() { return await this.readErd(TEMP_SETPOINT_ERD); } async setFanSpeed(speed) { const map = { low: 0, medium: 1, high: 2, auto: 3 }; await this.writeErd(FAN_SPEED_ERD, map[speed]); } async getFanSpeed() { const reverseMap = ['low', 'medium', 'high', 'auto']; const val = await this.readErd(FAN_SPEED_ERD); return reverseMap[val] ?? 'low'; } async getAmbientTemperature() { return await this.readErd(AMBIENT_TEMP_ERD); } } //# sourceMappingURL=portableac.js.map