homebridge-eufy-security
Version:
Control Eufy Security from homebridge.
67 lines • 2.63 kB
JavaScript
import { BaseAccessory } from './BaseAccessory.js';
import { PropertyName } from 'eufy-security-client';
import { CHAR, SERV } from '../utils/utils.js';
export class DeviceAccessory extends BaseAccessory {
constructor(platform, accessory, device) {
super(platform, accessory, device);
}
/**
* Get the current value of the "propertyName" characteristic
*/
getPropertyValue(characteristic, propertyName) {
try {
const value = this.device.getPropertyValue(propertyName);
this.log.debug(`GET '${characteristic}' ${propertyName}: ${value}`);
return value;
}
catch (error) {
this.log.debug(`Error getting '${characteristic}' ${propertyName}: ${error}`);
return false;
}
}
async setPropertyValue(propertyName, value) {
await this.platform.eufyClient.setDeviceProperty(this.SN, propertyName, value);
}
onPushNotification(characteristicType, serviceType, value) {
this.log.debug(`ON '${serviceType.name}': ${value}`);
this.getService(serviceType)
.getCharacteristic(characteristicType)
.updateValue(value);
}
initSensorService() {
const propertiesToRegister = [
{
property: 'battery',
characteristicType: CHAR.BatteryLevel,
propertyName: PropertyName.DeviceBattery,
onSimpleValue: null,
fallback: 100,
},
{
property: 'batteryLow',
characteristicType: CHAR.StatusLowBattery,
propertyName: PropertyName.DeviceBatteryLow,
onSimpleValue: 'low battery',
fallback: false,
},
{
property: 'batteryIsCharging',
characteristicType: CHAR.ChargingState,
propertyName: PropertyName.DeviceBatteryIsCharging,
onSimpleValue: null,
fallback: false,
},
];
propertiesToRegister.forEach((propertyConfig) => {
if (this.device.hasProperty(propertyConfig.property)) {
this.registerCharacteristic({
serviceType: SERV.Battery,
characteristicType: propertyConfig.characteristicType,
getValue: () => this.device.getPropertyValue(propertyConfig.propertyName) || propertyConfig.fallback,
onSimpleValue: propertyConfig.onSimpleValue,
});
}
});
}
}
//# sourceMappingURL=Device.js.map