@chazepps/homebridge-hejhome
Version:
The Hejhome plugin allows you to access your Hejhome device(s) from HomeKit.
77 lines • 2.9 kB
JavaScript
import { v4 as uuidv4 } from 'uuid';
import { control } from '../requests/control.js';
import { hejDevices } from '../requests/realtime.js';
import { Base } from './base.js';
const CHARACTERISTIC_MANUFACTURER = '반디통신기술(주)';
const CHARACTERISTIC_MODEL = 'BDS03G2(2구)';
export class ZigbeeSwitch2 extends Base {
platform;
accessory;
device;
services = [];
get state() {
return hejDevices[this.device.id].deviceState;
}
constructor(platform, accessory, device) {
super();
this.platform = platform;
this.accessory = accessory;
this.device = device;
const { Characteristic: { Manufacturer, Model, Name, On, SerialNumber, }, Service: { AccessoryInformation, Switch, }, } = platform;
accessory
.getService(AccessoryInformation)
.setCharacteristic(Manufacturer, CHARACTERISTIC_MANUFACTURER)
.setCharacteristic(Model, device.modelName || CHARACTERISTIC_MODEL)
.setCharacteristic(SerialNumber, device.id);
for (let i = 1; i <= 2; i++) {
const serviceName = `${device.name} ${i}`;
let service = accessory.getService(serviceName);
if (!service) {
service = accessory.addService(Switch, serviceName, uuidv4());
}
service.setCharacteristic(Name, serviceName);
service
.getCharacteristic(On)
.onSet(this.setPower.bind(this, i))
.onGet(this.getPower.bind(this, i));
this.services.push(service);
}
}
async setPower(index, value) {
const { platform, device, state } = this;
if (!state) {
return;
}
const powerKey = `power${index}`;
try {
await control(platform, device.id, {
requirments: {
[powerKey]: value,
},
});
state[powerKey] = value;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}
catch (e) {
platform.log.error(`Failed to set power state for ${device.name} (ZigbeeSwitch2) power ${index}: ${e}`);
}
platform.log.info(`Device ${device.name} (ZigbeeSwitch2) power ${index} state changed to ${value ? 'ON' : 'OFF'}`);
}
async getPower(index) {
if (!this.state) {
throw new Error('Not ready');
}
const powerKey = `power${index}`;
return this.state[powerKey];
}
updateCharacteristics() {
this.services.forEach((service, index) => {
if (!this.state) {
return;
}
const powerKey = `power${index + 1}`;
service.updateCharacteristic(this.platform.Characteristic.On, this.state[powerKey]);
});
}
}
//# sourceMappingURL=zigbee_switch2.js.map