Doorbell refinement, working on README

This commit is contained in:
Serge Wagener
2022-02-10 12:01:24 +01:00
parent a8c5b1b9e6
commit b95352b05b
6 changed files with 320 additions and 120 deletions
+66
View File
@@ -0,0 +1,66 @@
import {
AccessoryPlugin,
CharacteristicValue,
Service,
Nullable,
} from 'homebridge';
import { SmartHomeNGPlatform } from '../platform';
export class Doorbell implements AccessoryPlugin {
private readonly deviceService: Service;
private readonly informationService: Service;
public name: string;
//private event = this.platform.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS;
constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) {
this.name = accessory.name;
this.deviceService = new this.platform.Service.Doorbell(accessory.name);
// create handlers for required characteristics
this.deviceService.getCharacteristic(this.platform.Characteristic.ProgrammableSwitchEvent)
.onGet(this.handleProgrammableSwitchEventGet.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.singlepress);
this.platform.shng.addMonitor(accessory.singlepress, this.shngSinglePressCallback.bind(this));
this.platform.log.info('Doorbell', accessory.name, 'created!');
}
identify(): void {
this.platform.log.info('Identify!');
}
getServices(): Service[] {
return [this.informationService, this.deviceService];
}
handleProgrammableSwitchEventGet(): Nullable<CharacteristicValue> {
this.platform.log.error(
'handleProgrammableSwitchEventGet:',
this.accessory.name, '=',
0,
);
return 0;
}
shngSinglePressCallback(value: unknown): void {
this.platform.log.debug('shngSinglePressCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'boolean') {
if (value) {
this.deviceService.updateCharacteristic(
this.platform.Characteristic.ProgrammableSwitchEvent,
this.platform.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS,
);
}
} else {
this.platform.log.warn('Unknown type', typeof value, 'received for', this.accessory.name + ':', value);
}
}
}
+66
View File
@@ -0,0 +1,66 @@
import {
AccessoryPlugin,
CharacteristicValue,
Service,
Nullable,
} from 'homebridge';
import { SmartHomeNGPlatform } from '../platform';
export class Doorbell implements AccessoryPlugin {
private readonly deviceService: Service;
private readonly informationService: Service;
public name: string;
//private event = this.platform.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS;
constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) {
this.name = accessory.name;
this.deviceService = new this.platform.Service.Doorbell(accessory.name);
// create handlers for required characteristics
this.deviceService.getCharacteristic(this.platform.Characteristic.ProgrammableSwitchEvent)
.onGet(this.handleProgrammableSwitchEventGet.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.singlepress);
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];
}
handleProgrammableSwitchEventGet(): Nullable<CharacteristicValue> {
this.platform.log.debug(
'handleProgrammableSwitchEventGet:',
this.accessory.name, '=',
0,
);
return this.platform.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS;
}
shngCallback(value: unknown): void {
this.platform.log.debug('shngCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'boolean') {
if (value) {
this.deviceService.updateCharacteristic(
this.platform.Characteristic.ProgrammableSwitchEvent,
this.platform.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS,
);
}
} else {
this.platform.log.warn('Unknown type', typeof value, 'received for', this.accessory.name + ':', value);
}
}
}
+6
View File
@@ -19,6 +19,7 @@ import { TemperatureSensor } from './Accessories/TemperatureSensor';
import { Thermostat } from './Accessories/Thermostat';
import { WindowCovering } from './Accessories/WindowCovering';
import { ContactSensor } from './Accessories/ContactSensor';
import { Doorbell } from './Accessories/DoorBell';
function uncapitalizeKeys(obj): Record<string, unknown> {
function isObject(o: unknown): boolean {
@@ -95,6 +96,11 @@ export class SmartHomeNGPlatform implements StaticPlatformPlugin {
devices.push(new ContactSensor(this, accessory));
break;
// Doorbell
case 'doorbell':
devices.push(new Doorbell(this, accessory));
break;
// Switch
case 'switch':
devices.push(new Switch(this, accessory));