Skip to content

Commit

Permalink
Better logging, fixes for restoring values after restart, updated res…
Browse files Browse the repository at this point in the history
…endHexAfterReload defaults to always be false
  • Loading branch information
lprhodes committed Apr 27, 2017
1 parent 97489a9 commit d797731
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 88 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ key | description | example | default
name (required) | A descriptor for the accessory that will show in HomeKit apps. | "TV" | -
type (required) | The type of accessory. | "switch" | -
persistState (optional) | Determines whether the state of accessory persists after homebridge has been restarted. | false | true
resendHexAfterReload (optional) | When persistState is true (it is by default) this will resend the hex code for the last known state when homebridge is restarted restored. | true | false
resendHexAfterReload (optional) | When persistState is true (it is by default) this will resend the hex code for the last known state if homebridge is restarted. | true | false
disableLogs (optional) | Disables the log output for this accessory. | true | false

## Accessory Types
Expand All @@ -55,6 +55,9 @@ disableLogs (optional) | Disables the log output for this accessory. | true | fa

You shouldn't need to add this accessory type yourself as we add one automatically however if you have multiple Broadlink RM devices then it may be useful to specify multiple devices with the "learn-code" type along with a host so that you can learn from each of them.

key | description | example | default
--- | ----------- | ------- | -------
disableAutomaticOff (optional) | Prevent the learn-code accessory from turning off automatically after a given amount of time or when a hex code has been received. | false | true

### switch

Expand Down
31 changes: 18 additions & 13 deletions accessories/accessory.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const sendData = require('../helpers/sendData');
const persistentState = require('../helpers/persistentState');

const addSaveProxy = (target, saveFunc) => {
const addSaveProxy = (name, target, saveFunc) => {
const handler = {
set (target, key, value) {
target[key] = value;

// console.log(`${name} save ${key} ${value}`, target)
saveFunc(target);

return true
Expand All @@ -18,9 +19,10 @@ const addSaveProxy = (target, saveFunc) => {
class BroadlinkRMAccessory {

constructor (log, config = {}) {
let { host, name, data, persistState } = config;
let { disableLogs, host, name, data, persistState } = config;

this.log = !config.disableLogs ? log : () => {};

this.log = !disableLogs ? log : () => {};
this.config = config;

this.host = host;
Expand All @@ -31,18 +33,21 @@ class BroadlinkRMAccessory {
if (persistState === undefined) persistState = true;

if (persistState) {
this.isReloadingState = true;

const restoreStateOrder = this.restoreStateOrder();

const state = persistentState.load({ host, name }) || {};
this.correctReloadedState(state);

this.state = addSaveProxy(state, (state) => {
this.state = addSaveProxy(name, state, (state) => {
persistentState.save({ host, name, state });
});

this.correctReloadedState();
setTimeout(() => {
this.isReloadingState = false;
}, 2300);
} else {
persistentState.clear({ host, name });

this.state = {};
}
}
Expand Down Expand Up @@ -78,26 +83,26 @@ class BroadlinkRMAccessory {
const { resendHexAfterReload } = config;

const capitalizedPropertyName = propertyName.charAt(0).toUpperCase() + propertyName.slice(1);
log(`${name} set${capitalizedPropertyName}: ${value}`);
log(`${name} set${capitalizedPropertyName}: ${value} (isReloadingState: ${this.isReloadingState})`);

if (!ignorePreviousValue && this.state[propertyName] === value && !resendHexAfterReload) {
const previousValue = this.state[propertyName];
this.state[propertyName] = value;

if ((!ignorePreviousValue && this.state[propertyName] === value && !this.isReloadingState) || (this.isReloadingState && !resendHexAfterReload)) {
log(`${name} set${capitalizedPropertyName}: already ${value}`);

callback(null, value);

return;
}

const previousValue = this.state[propertyName];
this.state[propertyName] = value;

// Set toggle data if this is a toggle
const hexData = value ? onHex : offHex;

if (setValuePromise) {
await setValuePromise(hexData, previousValue);
} else if (hexData) {
sendData({ host, hexData, log });
sendData({ host, hexData, log, name });
}

callback(null, this.state[propertyName]);
Expand Down
22 changes: 10 additions & 12 deletions accessories/aircon.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ const sendData = require('../helpers/sendData');

class AirConAccessory extends BroadlinkRMAccessory {

correctReloadedState () {
this.state.lastUsedHeatingCoolingState = undefined;
this.state.lastUsedTemperature = undefined;
correctReloadedState (state) {
state.lastUsedHeatingCoolingState = undefined;
state.lastUsedTemperature = undefined;

if (this.state.currentHeatingCoolingState === 0) {
this.state.targetTemperature = undefined
if (state.currentHeatingCoolingState === 0) {
state.targetTemperature = undefined
}

if (this.state.currentTemperature > this.config.maxTemperature) this.state.currentTemperature = this.config.maxTemperature;
if (state.currentTemperature > this.config.maxTemperature) state.currentTemperature = this.config.maxTemperature;

this.state.targetHeatingCoolingState = this.state.currentHeatingCoolingState;
state.targetHeatingCoolingState = state.currentHeatingCoolingState;
}

constructor (log, config) {
Expand All @@ -23,9 +23,7 @@ class AirConAccessory extends BroadlinkRMAccessory {
const { state } = this;
const { defaultCoolTemperature, defaultHeatTemperature, heatTemperature, minTemperature, maxTemperature, pseudoDeviceTemperature, replaceAutoMode, units } = config

this.log = log;

if (config.resendHexAfterReload === undefined) config.resendHexAfterReload = true;
// if (config.resendHexAfterReload === undefined) config.resendHexAfterReload = true;

if (state.currentHeatingCoolingState === undefined) state.currentHeatingCoolingState = Characteristic.CurrentHeatingCoolingState.OFF;
if (state.targetHeatingCoolingState === undefined) state.targetHeatingCoolingState = Characteristic.CurrentHeatingCoolingState.OFF;
Expand Down Expand Up @@ -247,7 +245,7 @@ class AirConAccessory extends BroadlinkRMAccessory {
state.lastUsedTemperature = state.targetTemperature;
state.lastUsedHeatingCoolingState = state.currentHeatingCoolingState;

sendData({ host, hexData: hexData.data, log });
sendData({ host, hexData: hexData.data, log, name });
}

getCurrentHeatingCoolingState () {
Expand Down Expand Up @@ -298,7 +296,7 @@ class AirConAccessory extends BroadlinkRMAccessory {
this.resetAutoOnTimeout();

this.updateServiceHeatingCoolingState(state.targetHeatingCoolingState);
sendData({ host, hexData: data.off, log });
sendData({ host, hexData: data.off, log, name });
} else {
this.sendTemperature(temperature);
}
Expand Down
4 changes: 2 additions & 2 deletions accessories/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const BroadlinkRMAccessory = require('./accessory');
class ChannelAccessory extends BroadlinkRMAccessory {

setChannel (hexData, channel, callback) {
const { host, log } = this;
const { host, log, name } = this;

log(`setChannel: ${channel}`);
sendData({ host, hexData: hexData[channel], callback, log });
sendData({ host, hexData: hexData[channel], callback, log, name });
}

getServices () {
Expand Down
6 changes: 3 additions & 3 deletions accessories/fan.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const BroadlinkRMAccessory = require('./accessory');
class FanAccessory extends BroadlinkRMAccessory {

async setFanSpeed (hexData) {
const { data, host, log, state } = this;
const { data, host, log, state , name} = this;

const allHexKeys = Object.keys(data);

Expand All @@ -21,12 +21,12 @@ class FanAccessory extends BroadlinkRMAccessory {

// Find speed closest to the one requested
const closest = foundSpeeds.reduce((prev, curr) => Math.abs(curr - state.fanSpeed) < Math.abs(prev - state.fanSpeed) ? curr : prev);
log(`setFanSpeed: (closest: ${closest})`);
log(`${name} setFanSpeed: (closest: ${closest})`);

// Get the closest speed's hex data
hexData = data[`fanSpeed${closest}`];

sendData({ host, hexData, log });
sendData({ host, hexData, log, name });
}

getServices () {
Expand Down
26 changes: 16 additions & 10 deletions accessories/garageDoorOpener.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,47 @@ const BroadlinkRMAccessory = require('./accessory');

class GarageDoorOpenerAccessory extends BroadlinkRMAccessory {

correctReloadedState () {
this.state.targetDoorState = this.state.currentDoorState;
correctReloadedState (state) {
state.targetDoorState = state.currentDoorState;
}

async setTargetDoorState (hexData) {
const { config, data, host, log, state } = this;
const { config, data, host, log, name, state } = this;
let { openCloseDuration } = config;

if (!openCloseDuration) openCloseDuration = 8;

sendData({ host, hexData, log });
sendData({ host, hexData, log, name });

if (!state.targetDoorState) {
if (this.finishedClosingTimeout) clearTimeout(this.finishedClosingTimeout);

this.finishedOpeningTimeout = setTimeout(() => {
log('setCurrentDoorState: open')
log(`${name} setCurrentDoorState: open`)

this.garageDoorOpenerService.setCharacteristic(Characteristic.CurrentDoorState, 0);
}, openCloseDuration * 1000)
} else {
if (this.garageDoorOpenerService) clearTimeout(this.garageDoorOpenerService);

this.finishedClosingTimeout = setTimeout(() => {
log('setCurrentDoorState: closed')
log(`${name} setCurrentDoorState: closed`)

this.garageDoorOpenerService.setCharacteristic(Characteristic.CurrentDoorState, 1);
}, openCloseDuration * 1000)
}
}

async setLockTargetState (hexData) {
const { config, data, host, log, state } = this;
const { config, data, host, log, name, state } = this;

sendData({ host, hexData, log });
sendData({ host, hexData, log, name });

if (!state.lockTargetState) {
log('setCurrentDoorState: unlocked')
log(`${name} setCurrentDoorState: unlocked`)
this.garageDoorOpenerService.setCharacteristic(Characteristic.LockCurrentState, Characteristic.LockCurrentState.UNSECURED);
} else {
log('setCurrentDoorState: locked')
log(`${name} setCurrentDoorState: locked`)
this.garageDoorOpenerService.setCharacteristic(Characteristic.LockCurrentState, Characteristic.LockCurrentState.SECURED);
}
}
Expand All @@ -58,6 +58,12 @@ class GarageDoorOpenerAccessory extends BroadlinkRMAccessory {
const service = new Service.GarageDoorOpener(name);
this.addNameService(service);

this.createToggleCharacteristic({
service,
characteristicType: Characteristic.CurrentDoorState,
propertyName: 'currentDoorState',
});

this.createToggleCharacteristic({
service,
characteristicType: Characteristic.TargetDoorState,
Expand Down
9 changes: 7 additions & 2 deletions accessories/learnCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ class LearnIRAccessory extends BroadlinkRMAccessory {

toggleLearning (on, callback) {
const { config } = this;
const { scanRF } = config;
const { disableAutomaticOff, scanRF } = config;

const turnOffCallback = () => {
this.learnService.setCharacteristic(Characteristic.On, false);

if (disableAutomaticOff) {
learnData.start(this.host, null, turnOffCallback, this.log, true);
} else {
this.learnService.setCharacteristic(Characteristic.On, false);
}
}

const scanRFCallback = (success) => {
Expand Down
10 changes: 5 additions & 5 deletions accessories/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class LightAccessory extends BroadlinkRMAccessory {
} else {
this.stopAutoOffTimeout();

sendData({ host, hexData, log });
sendData({ host, hexData, log, name });
}
}

Expand Down Expand Up @@ -78,25 +78,25 @@ class LightAccessory extends BroadlinkRMAccessory {

if (on) {
log(`${name} setBrightness: (turn on, wait ${onDelay}s)`);
sendData({ host, hexData: on, log });
sendData({ host, hexData: on, log, name });

setTimeout(() => {
log(`${name} setBrightness: (closest: ${closest})`);
sendData({ host, hexData, log });
sendData({ host, hexData, log, name });

this.resetAutoOffTimeout();
}, onDelay * 1000);
} else {
log(`setBrightness: (closest: ${closest})`);
sendData({ host, hexData, log });
sendData({ host, hexData, log, name });

this.resetAutoOffTimeout();
}
} else {
log(`${name} setBrightness: (off)`);

this.stopAutoOffTimeout();
sendData({ host, hexData: off, log });
sendData({ host, hexData: off, log, name });
}
}

Expand Down
4 changes: 2 additions & 2 deletions accessories/switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ const BroadlinkRMAccessory = require('./accessory');
class SwitchAccessory extends BroadlinkRMAccessory {

async setSwitchState (hexData) {
const { config, data, host, log, state } = this;
const { config, data, host, log, name, state } = this;
let { disableAutomaticOff, onDuration } = config;

// Set defaults
if (disableAutomaticOff === undefined) disableAutomaticOff = true;
if (!onDuration) onDuration = 60;

if (hexData) sendData({ host, hexData, log });
if (hexData) sendData({ host, hexData, log, name });

if (this.autoOffTimeout) clearTimeout(this.autoOffTimeout);

Expand Down
4 changes: 2 additions & 2 deletions accessories/switchMulti.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class SwitchMultiAccessory extends BroadlinkRMAccessory {
}

async performSend () {
const { config, data, host, log } = this;
const { config, data, host, log, name } = this;
let { disableAutomaticOff, interval } = config;

// Itterate through each hex config in the array
for (let index = 0; index < data.length; index++) {
const hexData = data[index]

sendData({ host, hexData, log });
sendData({ host, hexData, log, name });

if (index < data.length - 1) await delayForDuration(interval);
}
Expand Down
4 changes: 2 additions & 2 deletions accessories/switchMultiRepeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ class SwitchMultiAccessory extends BroadlinkRMAccessory {
}

async performRepeatSend (hexConfig) {
const { host, log } = this;
const { host, log, name } = this;
let { data, interval, sendCount } = hexConfig;

interval = interval || 1;

// Itterate through each hex config in the array
for (let index = 0; index < sendCount; index++) {
sendData({ host, hexData: data, log });
sendData({ host, hexData: data, log, name });

if (index < sendCount - 1) await delayForDuration(interval);
}
Expand Down
4 changes: 2 additions & 2 deletions accessories/switchRepeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ class SwitchRepeatAccessory extends BroadlinkRMAccessory {
}

async performSend () {
const { config, data, host, log } = this;
const { config, data, host, log, name } = this;
let { disableAutomaticOff, interval, sendCount } = config;

interval = interval || 1;

// Itterate through each hex config in the array
for (let index = 0; index < sendCount; index++) {
sendData({ host, hexData: data, log });
sendData({ host, hexData: data, log, name });

if (index < sendCount - 1) await delayForDuration(interval);
}
Expand Down
Loading

0 comments on commit d797731

Please sign in to comment.