Added ContactSensor

This commit is contained in:
Serge Wagener 2022-02-09 20:21:42 +01:00
parent 9c360dff2d
commit a8c5b1b9e6
2 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,66 @@
import {
AccessoryPlugin,
CharacteristicValue,
Service,
Nullable,
} from 'homebridge';
import { SmartHomeNGPlatform } from '../platform';
export class ContactSensor implements AccessoryPlugin {
private readonly deviceService: Service;
private readonly informationService: Service;
public name: string;
private contactState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) {
this.name = accessory.name;
this.deviceService = new this.platform.Service.ContactSensor(accessory.name);
// create handlers for required characteristics
this.deviceService.getCharacteristic(this.platform.Characteristic.MotionDetected)
.onGet(this.handleContactSensorStateGet.bind(this));
this.informationService =
new this.platform.Service.AccessoryInformation()
.setCharacteristic(this.platform.Characteristic.Manufacturer, accessory.manufacturer)
.setCharacteristic(this.platform.Characteristic.Model, accessory.model)
.setCharacteristic(this.platform.Characteristic.SerialNumber, accessory.contactstate);
this.platform.shng.addMonitor(accessory.contactstate, this.shngCallback.bind(this));
this.platform.log.info('ContactSensor', accessory.name, 'created!');
}
identify(): void {
this.platform.log.info('Identify!');
}
getServices(): Service[] {
return [this.informationService, this.deviceService];
}
handleContactSensorStateGet(): Nullable<CharacteristicValue> {
this.platform.log.debug(
'handleContactSensorStateGet:',
this.accessory.name, '=',
this.contactState === this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED ? 'True' : 'False',
);
return this.contactState;
}
shngCallback(value: unknown): void {
this.platform.log.debug('shngCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'boolean') {
if (value) {
this.contactState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
} else {
this.contactState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
}
} else {
this.platform.log.warn('Unknown type', typeof value, 'received for', this.accessory.name + ':', value);
}
this.deviceService.updateCharacteristic(this.platform.Characteristic.ContactSensorState, this.contactState);
}
}

View File

@ -18,6 +18,7 @@ import { Lightbulb } from './Accessories/Lightbulb';
import { TemperatureSensor } from './Accessories/TemperatureSensor'; import { TemperatureSensor } from './Accessories/TemperatureSensor';
import { Thermostat } from './Accessories/Thermostat'; import { Thermostat } from './Accessories/Thermostat';
import { WindowCovering } from './Accessories/WindowCovering'; import { WindowCovering } from './Accessories/WindowCovering';
import { ContactSensor } from './Accessories/ContactSensor';
function uncapitalizeKeys(obj): Record<string, unknown> { function uncapitalizeKeys(obj): Record<string, unknown> {
function isObject(o: unknown): boolean { function isObject(o: unknown): boolean {
@ -89,6 +90,11 @@ export class SmartHomeNGPlatform implements StaticPlatformPlugin {
devices.push(new MotionSensor(this, accessory)); devices.push(new MotionSensor(this, accessory));
break; break;
// Contact sensor
case 'contactsensor':
devices.push(new ContactSensor(this, accessory));
break;
// Switch // Switch
case 'switch': case 'switch':
devices.push(new Switch(this, accessory)); devices.push(new Switch(this, accessory));