mirror of
https://github.com/dtwardow/homebridge-multi-tap-switch.git
synced 2026-11-01 20:18:30 +00:00
Configuration parameter to one location and configurable logger
This commit is contained in:
1 parent
a396e49c8e
commit
505b8b0a4a
5 files changed
+214
-80
No files matched your search
+64
-54
@@ -1,10 +1,7 @@
|
||||
import {CharacteristicValue, PlatformAccessory, Service} from 'homebridge';
|
||||
import {MultiTapSwitchPlatform} from './platform';
|
||||
|
||||
interface AccessoryState {
|
||||
NumConfiguredScenes: number;
|
||||
SecondsToKeepState: number;
|
||||
}
|
||||
import {AccessoryState, DeviceConfig} from './config';
|
||||
import {Logging} from './helper/logger';
|
||||
|
||||
/**
|
||||
* Platform Accessory
|
||||
@@ -22,57 +19,55 @@ export class DeviceAccessory {
|
||||
};
|
||||
|
||||
private readonly State: AccessoryState;
|
||||
private readonly Config: DeviceConfig;
|
||||
|
||||
private readonly TriggerButtons: Service[] = [];
|
||||
private SwitchResetTimer: NodeJS.Timeout | undefined;
|
||||
private SwitchOn = false;
|
||||
private NextItemToTrigger = 0;
|
||||
|
||||
private Log: Logging;
|
||||
|
||||
constructor(
|
||||
private readonly platform: MultiTapSwitchPlatform,
|
||||
private readonly accessory: PlatformAccessory,
|
||||
) {
|
||||
if (this.accessory.context.persistent) {
|
||||
this.platform.log.debug('Restore previous state');
|
||||
this.State = this.accessory.context.persistent;
|
||||
} else {
|
||||
this.platform.log.debug('First run, use configuration data');
|
||||
this.State = {
|
||||
NumConfiguredScenes: accessory.context.config.MaxNumScenes,
|
||||
SecondsToKeepState: accessory.context.config.SecondsBeforeReset,
|
||||
};
|
||||
}
|
||||
this.Config = new DeviceConfig(accessory.context.config);
|
||||
this.State = this.accessory.context.persistent;
|
||||
|
||||
this.Log = new Logging(platform.log, this.Config.Name(), this.Config.isLogging());
|
||||
this.Log.log('Initialize Accessory', this.Config.Name());
|
||||
|
||||
// Set accessory information
|
||||
this.accessory.getService(this.platform.Service.AccessoryInformation)!
|
||||
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'TippyTapper Solutions')
|
||||
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'TippyTapper')
|
||||
.setCharacteristic(this.platform.Characteristic.Model, 'SceneSwitch')
|
||||
.setCharacteristic(this.platform.Characteristic.SerialNumber, 'DE:AD:BE:EF:00:00');
|
||||
.setCharacteristic(this.platform.Characteristic.SerialNumber, accessory.UUID);
|
||||
|
||||
|
||||
// Get or Create the input switch service
|
||||
this.serviceIn = this.accessory.getService(this.platform.Service.Switch)
|
||||
|| this.accessory.addService(this.platform.Service.Switch);
|
||||
|
||||
// register handlers for the On/Off Characteristic
|
||||
// Register handlers for the On/Off Characteristic
|
||||
this.serviceIn.getCharacteristic(this.platform.Characteristic.On)
|
||||
.onSet(this.setOn.bind(this))
|
||||
.onGet(this.getOn.bind(this));
|
||||
|
||||
// Register handlers for the NumScenes characteristic & restore previous value
|
||||
if (this.State.NumConfiguredScenes > this.accessory.context.config.MaxNumScenes) {
|
||||
this.State.NumConfiguredScenes = this.accessory.context.config.MaxNumScenes;
|
||||
if (this.State.numberConfiguredScenes > this.Config.NumOfScenes()) {
|
||||
this.State.numberConfiguredScenes = this.Config.NumOfScenes();
|
||||
}
|
||||
(this.serviceIn.getCharacteristic(this.platform.Characteristic.ConfiguredScenes)
|
||||
|| this.serviceIn.addOptionalCharacteristic(this.platform.Characteristic.ConfiguredScenes))
|
||||
.setProps({
|
||||
'minValue': 0,
|
||||
'maxValue': this.accessory.context.config.MaxNumScenes,
|
||||
'maxValue': this.Config.NumOfScenes(),
|
||||
'minStep': 1,
|
||||
})
|
||||
.onSet(this.setItemsToHandle.bind(this))
|
||||
.onGet(this.getItemsToHandle.bind(this))
|
||||
.updateValue(this.State.NumConfiguredScenes);
|
||||
.updateValue(this.State.numberConfiguredScenes);
|
||||
|
||||
// Register handlers for the TriggerTimeout characteristic & restore previous value
|
||||
(this.serviceIn.getCharacteristic(this.platform.Characteristic.TriggerTimeout)
|
||||
@@ -84,7 +79,10 @@ export class DeviceAccessory {
|
||||
})
|
||||
.onSet(this.setTriggerTimeout.bind(this))
|
||||
.onGet(this.getTriggerTimeout.bind(this))
|
||||
.updateValue(this.State.SecondsToKeepState);
|
||||
.updateValue(this.State.triggerTimeout);
|
||||
|
||||
// Set the service name, this is what is displayed as the default name on the Home app
|
||||
this.serviceIn.setCharacteristic(this.platform.Characteristic.Name, this.Config.Name());
|
||||
|
||||
// Get or Create the output service (excluding the buttons)
|
||||
this.serviceOut = this.accessory.getService(this.platform.Service.ServiceLabel)
|
||||
@@ -92,12 +90,12 @@ export class DeviceAccessory {
|
||||
this.serviceOut.setCharacteristic(this.platform.Characteristic.ServiceLabelNamespace, 1);
|
||||
|
||||
// Create max configured number of scene buttons
|
||||
for (let i = 1; i <= this.accessory.context.config.MaxNumScenes; i++) {
|
||||
this.platform.log.debug('Handle Scene Button -> ', i);
|
||||
for (let i = 1; i <= this.Config.NumOfScenes(); i++) {
|
||||
this.Log.debug('Handle Scene Button -> ', i);
|
||||
|
||||
let newButton = this.accessory.getService('Scene ' + i);
|
||||
if (!newButton) {
|
||||
this.platform.log.debug('Add new Button');
|
||||
this.Log.debug('Add new Button');
|
||||
newButton = this.accessory.addService(this.platform.Service.StatelessProgrammableSwitch, 'Scene ' + i, 'scene' + i);
|
||||
|
||||
this.serviceOut.addLinkedService(newButton);
|
||||
@@ -113,7 +111,7 @@ export class DeviceAccessory {
|
||||
for (let i = this.TriggerButtons.length + 1;; i++) {
|
||||
const exButton = this.accessory.getService('Scene ' + i);
|
||||
if (exButton) {
|
||||
this.platform.log.debug('Remove Scene Button -> ', i);
|
||||
this.Log.debug('Remove Scene Button -> ', i);
|
||||
this.serviceOut.removeLinkedService(exButton);
|
||||
accessory.removeService(exButton);
|
||||
} else {
|
||||
@@ -122,48 +120,60 @@ export class DeviceAccessory {
|
||||
}
|
||||
|
||||
// Set the service name, this is what is displayed as the default name on the Home app
|
||||
this.serviceOut.setCharacteristic(this.platform.Characteristic.Name, accessory.context.config.Name);
|
||||
|
||||
// Store persistent data
|
||||
this.accessory.context.persistent = this.State;
|
||||
this.serviceOut.setCharacteristic(this.platform.Characteristic.Name, this.Config.Name());
|
||||
}
|
||||
|
||||
triggerNextItem() {
|
||||
if (this.NextItemToTrigger >= this.State.NumConfiguredScenes) {
|
||||
/**
|
||||
* Trigger the next event
|
||||
*/
|
||||
triggerNext() {
|
||||
if (this.NextItemToTrigger >= this.State.numberConfiguredScenes) {
|
||||
this.NextItemToTrigger = 0;
|
||||
}
|
||||
|
||||
// Trigger Scene Button
|
||||
this.platform.log.debug('Trigger Scene -> ', this.NextItemToTrigger);
|
||||
this.Log.log('Trigger Scene -> ', this.NextItemToTrigger);
|
||||
this.TriggerButtons[this.NextItemToTrigger++].updateCharacteristic(this.platform.Characteristic.ProgrammableSwitchEvent, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the trigger mechanism
|
||||
*/
|
||||
resetTrigger() {
|
||||
this.Log.log('Reset Scene Trigger');
|
||||
this.serviceIn.updateCharacteristic(this.platform.Characteristic.On,
|
||||
this.SwitchOn = false);
|
||||
this.NextItemToTrigger = 0;
|
||||
}
|
||||
|
||||
|
||||
// Switch Handlers
|
||||
async setOn(value: CharacteristicValue) {
|
||||
// implement your own code to turn your device on/off
|
||||
this.SwitchOn = value as boolean;
|
||||
|
||||
if (this.SwitchResetTimer) {
|
||||
clearTimeout(this.SwitchResetTimer);
|
||||
}
|
||||
if (this.SwitchOn) {
|
||||
this.SwitchResetTimer = setTimeout(() => {
|
||||
this.platform.log.debug('Reset Scene Trigger');
|
||||
this.serviceIn.updateCharacteristic(this.platform.Characteristic.On,
|
||||
this.SwitchOn = false);
|
||||
this.NextItemToTrigger = 0;
|
||||
}, this.State.SecondsToKeepState * 1000);
|
||||
if (this.SwitchResetTimer) {
|
||||
clearTimeout(this.SwitchResetTimer);
|
||||
}
|
||||
// Start the reset timer
|
||||
this.SwitchResetTimer = setTimeout(this.resetTrigger.bind(this), this.State.triggerTimeout * 1000);
|
||||
|
||||
// Trigger Scene
|
||||
this.triggerNextItem();
|
||||
this.triggerNext();
|
||||
} else if (this.Config.isResetWhenOff() && !this.SwitchOn) {
|
||||
if (this.SwitchResetTimer) {
|
||||
clearTimeout(this.SwitchResetTimer);
|
||||
}
|
||||
this.resetTrigger();
|
||||
}
|
||||
|
||||
this.platform.log.debug('Set Characteristic On ->', value);
|
||||
this.Log.debug('Set Characteristic On ->', value);
|
||||
}
|
||||
|
||||
async getOn(): Promise<CharacteristicValue> {
|
||||
const isOn = this.SwitchOn;
|
||||
this.platform.log.debug('Get Characteristic On ->', isOn);
|
||||
this.Log.debug('Get Characteristic On ->', isOn);
|
||||
// if you need to return an error to show the device as "Not Responding" in the Home app:
|
||||
// throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
|
||||
return isOn;
|
||||
@@ -172,30 +182,30 @@ export class DeviceAccessory {
|
||||
// NumScenes Handlers
|
||||
async setItemsToHandle(value: CharacteristicValue) {
|
||||
const numItemsToHandle = value as number;
|
||||
this.platform.log.debug('Set Characteristic NumScenes ->', numItemsToHandle);
|
||||
this.State.NumConfiguredScenes = numItemsToHandle;
|
||||
this.Log.debug('Set Characteristic NumScenes ->', numItemsToHandle);
|
||||
this.State.numberConfiguredScenes = numItemsToHandle;
|
||||
|
||||
//this.updatePersistentData();
|
||||
}
|
||||
|
||||
async getItemsToHandle(): Promise<CharacteristicValue> {
|
||||
const numItemsToHandle = this.State.NumConfiguredScenes;
|
||||
this.platform.log.debug('Get Characteristic NumScenes ->', numItemsToHandle);
|
||||
const numItemsToHandle = this.State.numberConfiguredScenes;
|
||||
this.Log.debug('Get Characteristic NumScenes ->', numItemsToHandle);
|
||||
return numItemsToHandle;
|
||||
}
|
||||
|
||||
// TriggerTimeout Handlers
|
||||
async setTriggerTimeout(value: CharacteristicValue) {
|
||||
const triggerTimeout = value as number;
|
||||
this.platform.log.debug('Set Characteristic TriggerTimeout ->', triggerTimeout);
|
||||
this.State.SecondsToKeepState = triggerTimeout;
|
||||
this.Log.debug('Set Characteristic TriggerTimeout ->', triggerTimeout);
|
||||
this.State.triggerTimeout = triggerTimeout;
|
||||
|
||||
//this.updatePersistentData();
|
||||
}
|
||||
|
||||
async getTriggerTimeout(): Promise<CharacteristicValue> {
|
||||
const triggerTimeout = this.State.SecondsToKeepState;
|
||||
this.platform.log.debug('Get Characteristic TriggerTimeout ->', triggerTimeout);
|
||||
const triggerTimeout = this.State.triggerTimeout;
|
||||
this.Log.debug('Get Characteristic TriggerTimeout ->', triggerTimeout);
|
||||
return triggerTimeout;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user