homebridge-eufy-security
Version:
Control Eufy Security from homebridge.
146 lines • 5.69 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LockAccessory = void 0;
const Device_1 = require("./Device");
const eufy_security_client_1 = require("eufy-security-client");
const utils_1 = require("../utils/utils");
/**
* LockAccessory Class
*
* This class represents a lock accessory within a home automation system. It is designed to
* integrate smart locks into the system, register appropriate HomeKit characteristics, and provide
* functionality for controlling and monitoring the lock's status.
*
* @class LockAccessory
* @extends DeviceAccessory
*/
class LockAccessory extends Device_1.DeviceAccessory {
/**
* Constructor for LockAccessory.
*
* @param {EufySecurityPlatform} platform - The platform instance managing accessories.
* @param {PlatformAccessory} accessory - The platform-specific accessory.
* @param {Lock} device - The lock device being represented.
*/
constructor(platform, accessory, device) {
// Call the constructor of the parent class DeviceAccessory.
super(platform, accessory, device);
// Log that the LockAccessory is constructed.
this.log.debug(`Constructed Lock`);
// Check if the device has the 'locked' property.
if (this.device.hasProperty('locked')) {
// Initialize Lock Management Service characteristics.
this.initLockManagementService();
// Initialize Lock Mechanism Service characteristics.
this.initLockMechanismService();
}
else {
// Log an error if the device has no lock.
this.log.error(`has no lock`);
}
// Prune any unused services.
this.pruneUnusedServices();
}
/**
* Initializes characteristics for the Lock Management Service.
*/
initLockManagementService() {
// Register Lock Management Service characteristics.
// Version characteristic (always returns '1.0').
this.registerCharacteristic({
serviceType: utils_1.SERV.LockManagement,
characteristicType: utils_1.CHAR.Version,
getValue: () => '1.0',
});
// LockManagementAutoSecurityTimeout characteristic (always returns 3 seconds).
this.registerCharacteristic({
serviceType: utils_1.SERV.LockManagement,
characteristicType: utils_1.CHAR.LockManagementAutoSecurityTimeout,
getValue: () => 3,
});
// AdministratorOnlyAccess characteristic (always returns true).
this.registerCharacteristic({
serviceType: utils_1.SERV.LockManagement,
characteristicType: utils_1.CHAR.AdministratorOnlyAccess,
getValue: () => true,
});
// LockControlPoint characteristic (no initial value).
this.registerCharacteristic({
serviceType: utils_1.SERV.LockManagement,
characteristicType: utils_1.CHAR.LockControlPoint,
});
}
/**
* Initializes characteristics for the Lock Mechanism Service.
*/
initLockMechanismService() {
// Register Lock Mechanism Service characteristics.
// LockCurrentState and LockTargetState characteristics.
this.registerCharacteristic({
serviceType: utils_1.SERV.LockMechanism,
characteristicType: utils_1.CHAR.LockCurrentState,
getValue: () => this.getLockStatus(),
onValue: (service, characteristic) => {
this.device.on('locked', () => {
characteristic.updateValue(this.getLockStatus());
});
this.device.on('jammed', () => {
characteristic.updateValue(utils_1.CHAR.LockCurrentState.JAMMED);
});
},
});
this.registerCharacteristic({
serviceType: utils_1.SERV.LockMechanism,
characteristicType: utils_1.CHAR.LockTargetState,
getValue: () => this.getLockStatus(),
setValue: async (value) => {
try {
await this.setLockTargetState(value);
}
catch (error) {
this.log.error(`Lock target state could not be set: ${error}`);
}
},
onValue: (service, characteristic) => {
this.device.on('locked', () => {
characteristic.updateValue(this.getLockStatus());
});
},
});
// Initialize the sensor service.
this.initSensorService();
}
/**
* Gets the lock status and maps it to HomeKit lock states.
*/
getLockStatus() {
const lockStatus = this.device.isLocked();
this.log.debug(`getLockStatus: ${lockStatus}`);
return this.convertLockStatusCode(lockStatus);
}
/**
* Sets the lock target state asynchronously.
*/
async setLockTargetState(state) {
try {
await this.setPropertyValue(eufy_security_client_1.PropertyName.DeviceLocked, !!state);
this.log.info(`Lock target state set to: ${state}`);
}
catch (error) {
this.log.error(`Error setting lock target state: ${error}`);
}
}
/**
* Converts lock status codes to corresponding HomeKit lock states.
*/
convertLockStatusCode(lockStatus) {
if (lockStatus) {
return utils_1.CHAR.LockCurrentState.SECURED;
}
else {
return utils_1.CHAR.LockCurrentState.UNSECURED;
}
}
}
exports.LockAccessory = LockAccessory;
//# sourceMappingURL=LockAccessory.js.map