homebridge-virtual-accessories
Version:
Virtual HomeKit accessories for Homebridge.
88 lines • 3.87 kB
JavaScript
import { Accessory } from '../accessories/accessory.js';
import { AccessoryFactory } from '../accessoryFactory.js';
import { TriggerNotAllowedError, InvalidSensorValue } from '../errors.js';
/**
* Sensor - Abstract accessory
*/
export class Sensor extends Accessory {
static ON = true;
static OFF = false;
static NORMAL_INACTIVE = 'NORMAL-INACTIVE';
static TRIGGERED_ACTIVE = 'TRIGGERED-ACTIVE';
static NORMAL = 0;
static TRIGGERED = 1;
trigger;
eventDetected;
states = {
SensorState: Sensor.NORMAL,
};
constructor(platform, accessory, accessoryConfiguration) {
super(platform, accessory, accessoryConfiguration);
this.eventDetected = this.getEventDetectedCharacteristic();
const sensorService = this.getService();
this.service = this.accessory.getService(sensorService) || this.accessory.addService(sensorService);
this.service.setCharacteristic(this.platform.Characteristic.Name, this.accessoryConfiguration.accessoryName);
// Update the initial state of the accessory
this.log.debug(`[${this.accessoryConfiguration.accessoryName}] Setting Sensor Current State: ${Sensor.getStateName(this.states.SensorState)}`);
this.service.updateCharacteristic(this.eventDetected, (this.states.SensorState));
// register handlers
this.service.getCharacteristic(this.eventDetected)
.onGet(this.getEventDetected.bind(this));
// Create Trigger
if (this.accessoryConfiguration.sensor !== undefined && this.accessoryConfiguration.sensor.trigger !== undefined) {
this.trigger = AccessoryFactory.createTrigger(this, this.accessoryConfiguration.sensor.trigger, this.accessoryConfiguration.accessoryName + ' Trigger');
}
}
getTrigger() {
return this.trigger;
}
// Handlers
async getEventDetected() {
const sensorState = this.states.SensorState;
this.log.debug(`[${this.accessoryConfiguration.accessoryName}] Getting Sensor Current State: ${Sensor.getStateName(sensorState)}`);
return sensorState;
}
getJsonState() {
return JSON.stringify({});
}
static getStateName(state) {
let sensorStateName;
switch (state) {
case undefined: {
sensorStateName = 'undefined';
break;
}
case Sensor.NORMAL: {
sensorStateName = Sensor.NORMAL_INACTIVE;
break;
}
case Sensor.TRIGGERED: {
sensorStateName = Sensor.TRIGGERED_ACTIVE;
break;
}
default: {
sensorStateName = state.toString();
}
}
return sensorStateName;
}
/**
* This method is called by this sensor's trigger
*/
async triggerSensorState(sensorState, trigger, isLoggingDisabled = false) {
if (trigger.sensorConfig.accessoryID !== this.accessoryConfiguration.accessoryID) {
throw new TriggerNotAllowedError(`Trigger ${trigger.name} is not allowed to trigger this sensor`);
}
if (![Sensor.NORMAL, Sensor.TRIGGERED].includes(sensorState)) {
throw new InvalidSensorValue(`Sensor value ${Sensor.getStateName(sensorState)} is not a valid state`);
}
// Only update the sensor if the state has changed
if (this.states.SensorState !== sensorState) {
this.states.SensorState = sensorState;
this.service.updateCharacteristic(this.eventDetected, (this.states.SensorState));
// eslint-disable-next-line max-len
this.log.info(`[${this.accessoryConfiguration.accessoryName}] Setting Sensor Current State: ${Sensor.getStateName(this.states.SensorState)}`, isLoggingDisabled);
}
}
}
//# sourceMappingURL=sensor.js.map