UNPKG

@homebridge-plugins/homebridge-smarthq

Version:

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

93 lines 4.46 kB
import axios from 'axios'; import { interval, skipWhile } from 'rxjs'; import { ERD_TYPES } from '../settings.js'; import { deviceBase } from './device.js'; export class SmartHQDishWasher extends deviceBase { platform; device; // Updates SensorUpdateInProgress; deviceStatus; constructor(platform, accessory, device) { super(platform, accessory, device); this.platform = platform; this.device = device; this.debugLog(`Dishwasher Features: ${JSON.stringify(accessory.context.device.features)}`); // Dishwasher Running State (Valve for active/inactive) const dishwasherValve = this.accessory.getService('Dishwasher') ?? this.accessory.addService(this.platform.Service.Valve, 'Dishwasher', 'Dishwasher'); dishwasherValve.setCharacteristic(this.platform.Characteristic.Name, 'Dishwasher'); dishwasherValve.setCharacteristic(this.platform.Characteristic.ValveType, this.platform.Characteristic.ValveType.GENERIC_VALVE); dishwasherValve .getCharacteristic(this.platform.Characteristic.Active) .onGet(async () => { try { return await this.readErd(ERD_TYPES.DISHWASHER_CYCLE).then(r => Number.parseInt(r) !== 0 ? this.platform.Characteristic.Active.ACTIVE : this.platform.Characteristic.Active.INACTIVE); } catch (error) { this.warnLog?.(`Dishwasher Active error: ${error?.message ?? error}`); return this.platform.Characteristic.Active.INACTIVE; } }) .onSet(async (value) => { try { await this.writeErd(ERD_TYPES.DISHWASHER_CYCLE, value === this.platform.Characteristic.Active.ACTIVE); } catch (error) { this.warnLog?.(`Dishwasher Active set error: ${error?.message ?? error}`); } }); dishwasherValve .getCharacteristic(this.platform.Characteristic.InUse) .onGet(async () => { try { return await this.readErd(ERD_TYPES.DISHWASHER_CYCLE).then(r => Number.parseInt(r) !== 0 ? this.platform.Characteristic.InUse.IN_USE : this.platform.Characteristic.InUse.NOT_IN_USE); } catch (error) { this.warnLog?.(`Dishwasher InUse error: ${error?.message ?? error}`); return this.platform.Characteristic.InUse.NOT_IN_USE; } }); // Dishwasher Door Sensor const doorSensor = this.accessory.getService('Dishwasher Door') ?? this.accessory.addService(this.platform.Service.ContactSensor, 'Dishwasher Door', 'DishwasherDoor'); doorSensor.setCharacteristic(this.platform.Characteristic.Name, 'Dishwasher Door'); doorSensor .getCharacteristic(this.platform.Characteristic.ContactSensorState) .onGet(async () => { try { // TODO: Use actual door status ERD when available return this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED; } catch (error) { this.warnLog?.(`Dishwasher Door error: ${error?.message ?? error}`); return this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED; } }); // this is subject we use to track when we need to POST changes to the SmartHQ API this.SensorUpdateInProgress = false; // Retrieve initial values and updateHomekit // this.refreshStatus() // Start an update interval interval(this.deviceRefreshRate * 10000) .pipe(skipWhile(() => this.SensorUpdateInProgress)) .subscribe(async () => { // await this.refreshStatus() }); } async readErd(erd) { const d = await axios .get(`/appliance/${this.accessory.context.device.applianceId}/erd/${erd}`); return String(d.data.value); } 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') : value, }); return undefined; } } //# sourceMappingURL=dishwasher.js.map