homebridge-blaq
Version:
Control and view your garage door(s) remotely with real-time updates using Konnected's BlaQ hardware
67 lines • 2.83 kB
JavaScript
import { BaseBlaQAccessory } from './base.js';
export const label = 'Motion Sensor';
/**
* Platform Accessory
* An instance of this class is created for each accessory your platform registers
* Each accessory may expose multiple services of different service types.
*/
export class BlaQGarageMotionSensorAccessory extends BaseBlaQAccessory {
motionSensorService;
motionDetected;
constructor(args) {
super(args);
this.motionSensorService = this.getOrAddService(this.platform.service.MotionSensor);
// Set the service name. This is what is displayed as the name on the Home
// app. We use what we stored in `accessory.context` in `discoverDevices`.
this.motionSensorService.setCharacteristic(this.platform.characteristic.Name, this.accessory.context.device.displayName + ' ' + label);
this.motionSensorService.getCharacteristic(this.platform.characteristic.MotionDetected)
.onGet(this.getMotionDetected.bind(this));
this.logger.debug(`Initialized ${this.getSelfClassName()}!`);
}
getMotionDetected() {
return this.motionDetected || false;
}
setMotionDetected(motionDetected) {
this.motionDetected = motionDetected;
this.motionSensorService.setCharacteristic(this.platform.characteristic.MotionDetected, this.motionDetected);
}
handleStateEvent(stateEvent) {
super.handleStateEvent(stateEvent);
if (!this.synced) {
return;
}
try {
const stateInfo = JSON.parse(stateEvent.data);
if (['binary_sensor-motion'].includes(stateInfo.id)) {
const sensorEvent = stateInfo;
if (['OFF', 'ON'].includes(sensorEvent.state.toUpperCase())) {
this.setMotionDetected(sensorEvent.state.toUpperCase() === 'ON');
}
}
}
catch (e) {
this.logger.error('Cannot deserialize message:', stateEvent);
this.logger.error('Deserialization yielded:', e);
}
}
handleLogEvent(logEvent) {
super.handleLogEvent(logEvent);
if (!this.synced) {
return;
}
try {
const logStr = logEvent.data;
const lowercaseLogStr = logStr.toLowerCase();
if (lowercaseLogStr.includes('motion') && lowercaseLogStr.includes('state') && lowercaseLogStr.includes('on')) {
this.setMotionDetected(true);
}
else if (lowercaseLogStr.includes('motion') && lowercaseLogStr.includes('state') && lowercaseLogStr.includes('off')) {
this.setMotionDetected(false);
}
}
catch (e) {
this.logger.error('Log parsing error:', e);
}
}
}
//# sourceMappingURL=garage-motion-sensor.js.map