@ubreu/homebridge-eufy-security
Version:
Control Eufy Security from homebridge.
175 lines • 8.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseAccessory = void 0;
const eufy_security_client_1 = require("eufy-security-client");
const events_1 = require("events");
function isServiceInstance(serviceType) {
// eslint-disable-next-line
return typeof serviceType === 'object';
}
class BaseAccessory extends events_1.EventEmitter {
constructor(platform, accessory,
// eslint-disable-next-line
device) {
super();
this.platform = platform;
this.accessory = accessory;
this.device = device;
this.servicesInUse = [];
this.platform = platform;
this.accessory = accessory;
this.device = device;
this.SN = this.device.getSerial();
this.registerCharacteristic({
serviceType: this.platform.Service.AccessoryInformation,
characteristicType: this.platform.Characteristic.Manufacturer,
getValue: (data) => 'Eufy',
});
this.registerCharacteristic({
serviceType: this.platform.Service.AccessoryInformation,
characteristicType: this.platform.Characteristic.Name,
getValue: (data) => this.accessory.displayName || 'Unknowm',
});
this.registerCharacteristic({
serviceType: this.platform.Service.AccessoryInformation,
characteristicType: this.platform.Characteristic.Model,
getValue: (data) => eufy_security_client_1.DeviceType[this.device.getDeviceType()] || 'Unknowm',
});
this.registerCharacteristic({
serviceType: this.platform.Service.AccessoryInformation,
characteristicType: this.platform.Characteristic.SerialNumber,
getValue: (data) => this.SN || 'Unknowm',
});
this.registerCharacteristic({
serviceType: this.platform.Service.AccessoryInformation,
characteristicType: this.platform.Characteristic.FirmwareRevision,
getValue: (data) => this.device.getSoftwareVersion() || 'Unknowm',
});
this.registerCharacteristic({
serviceType: this.platform.Service.AccessoryInformation,
characteristicType: this.platform.Characteristic.HardwareRevision,
getValue: (data) => this.device.getHardwareVersion() || 'Unknowm',
});
if (this.platform.config.enableDetailedLogging) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.device.on('raw property changed', (device, type, value) => this.handleRawPropertyChange(device, type, value));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.device.on('property changed', (device, name, value) => this.handlePropertyChange(device, name, value));
}
this.logPropertyKeys();
}
// Function to extract and log keys
logPropertyKeys() {
const properties = this.device.getProperties();
const keys = Object.keys(properties).join(', ');
this.platform.log.debug(`${this.accessory.displayName} Property Keys: ${keys}`);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
handleRawPropertyChange(device, type, value) {
this.platform.log.debug(`${this.accessory.displayName} Raw Property Changes: ${type} ${value}`);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
handlePropertyChange(device, name, value) {
this.platform.log.debug(`${this.accessory.displayName} Property Changes: ${name} ${value}`);
}
registerCharacteristic({ characteristicType, serviceType, getValue, setValue, onValue, onSimpleValue, onMultipleValue, name, serviceSubType, setValueDebounceTime = 0, }) {
const service = this.getService(serviceType, name, serviceSubType);
const characteristic = service.getCharacteristic(characteristicType);
// eslint-disable-next-line max-len
this.platform.log.debug(`${this.accessory.displayName} REGISTER CHARACTERISTIC '${serviceType.name} (${service.UUID}) / ${characteristic.displayName} (${characteristic.UUID})`);
if (getValue) {
characteristic.onGet(async (data) => {
const value = getValue(data, characteristic, service);
this.platform.log.debug(`${this.accessory.displayName} GET '${serviceType.name} / ${characteristicType.name}': ${value}`);
return value;
});
}
if (setValue && setValueDebounceTime) {
let timeoutId = null;
characteristic.onSet(async (value) => {
if (timeoutId) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(() => {
timeoutId = null;
setValue(value, characteristic, service);
}, setValueDebounceTime);
});
}
else if (setValue) {
characteristic.onSet(async (value) => {
Promise.resolve(setValue(value, characteristic, service));
});
}
if (onSimpleValue) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.device.on(onSimpleValue, (device, value) => {
// eslint-disable-next-line max-len
this.platform.log.info(`${this.accessory.displayName} ON '${serviceType.name} / ${characteristicType.name} / ${onSimpleValue}': ${value}`);
characteristic.updateValue(value);
});
}
if (onValue) {
this.platform.log.debug(`${this.accessory.displayName} ON '${serviceType.name} / ${characteristicType.name}'`);
onValue(service, characteristic);
}
if (onMultipleValue) {
// Attach the common event handler to each event type
onMultipleValue.forEach(eventType => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.device.on(eventType, (device, value) => {
// eslint-disable-next-line max-len
this.platform.log.info(`${this.accessory.displayName} ON '${serviceType.name} / ${characteristicType.name} / ${eventType}': ${value}`);
characteristic.updateValue(value);
});
});
}
}
getService(serviceType, name = this.accessory.displayName, subType) {
if (isServiceInstance(serviceType)) {
return serviceType;
}
const existingService = subType ? this.accessory.getServiceById(serviceType, subType) : this.accessory.getService(serviceType);
const service = existingService ||
this.accessory.addService(serviceType, name, subType);
if (existingService &&
existingService.displayName &&
name !== existingService.displayName) {
throw new Error(`Overlapping services for device ${this.accessory.displayName} - ${name} != ${existingService.displayName} - ${serviceType}`);
}
if (!this.servicesInUse.includes(service)) {
this.servicesInUse.push(service);
}
return service;
}
pruneUnusedServices() {
const safeServiceUUIDs = [
this.platform.Service.CameraRTPStreamManagement.UUID,
];
this.accessory.services.forEach((service) => {
if (!this.servicesInUse.includes(service) &&
!safeServiceUUIDs.includes(service.UUID)) {
// eslint-disable-next-line max-len
this.platform.log.debug(`${this.accessory.displayName} Pruning unused service ${service.UUID} ${service.displayName || service.name}`);
this.accessory.removeService(service);
}
});
}
handleDummyEventGet(serviceName) {
const characteristicValues = {
'EventSnapshotsActive': this.platform.Characteristic.EventSnapshotsActive.DISABLE,
'HomeKitCameraActive': this.platform.Characteristic.HomeKitCameraActive.OFF,
};
const currentValue = characteristicValues[serviceName];
if (currentValue === undefined) {
throw new Error(`Invalid serviceName: ${serviceName}`);
}
this.platform.log.debug(`${this.accessory.displayName} IGNORE GET ${serviceName}: ${currentValue}`);
return Promise.resolve(currentValue);
}
handleDummyEventSet(serviceName, value) {
this.platform.log.debug(`${this.accessory.displayName} IGNORE SET ${serviceName}: ${value}`);
}
}
exports.BaseAccessory = BaseAccessory;
//# sourceMappingURL=BaseAccessory.js.map