Skip to content

Commit

Permalink
Trying banboobees changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwi-cam committed Jun 17, 2023
1 parent bc4eab9 commit e85bcfc
Show file tree
Hide file tree
Showing 8 changed files with 567 additions and 79 deletions.
12 changes: 5 additions & 7 deletions accessories/accessory.js
Expand Up @@ -88,7 +88,7 @@ class BroadlinkRMAccessory extends HomebridgeAccessory {
const device = getDevice({ host, log });

if (!host || !device) { // Error reporting
sendData({ host, hexData: data, log, name, logLevel });
await sendData({ host, hexData: data, log, name, logLevel });

return;
}
Expand All @@ -107,9 +107,8 @@ class BroadlinkRMAccessory extends HomebridgeAccessory {
await this.performRepeatSend(data[index], actionCallback);

if (pause) {
// this.pauseTimeoutPromise = delayForDuration(pause);
// await this.pauseTimeoutPromise;
await new Promise(resolve => setTimeout(resolve, pause * 1000));
log(`${name} pause (${device.host.address}; ${device.host.macAddress}) ${pause * 1000} ms`);
}
}
});
Expand All @@ -126,10 +125,9 @@ class BroadlinkRMAccessory extends HomebridgeAccessory {
for (let index = 0; data && index < sendCount; index++) {
await sendData({ host, hexData: data, log, name, logLevel });

if (interval && index < sendCount - 1) {
// this.intervalTimeoutPromise = delayForDuration(interval);
// await this.intervalTimeoutPromise;
await new Promise(resolve => setTimeout(resolve, interval * 1000));
if (interval && index < sendCount) {
await new Promise(resolve => setTimeout(resolve, interval * 1000));

Check failure on line 129 in accessories/accessory.js

View workflow job for this annotation

GitHub Actions / Build and Test on Node 16 and ubuntu-latest

Expected indentation of 8 spaces but found 1 tab

Check failure on line 129 in accessories/accessory.js

View workflow job for this annotation

GitHub Actions / Build and Test on Node 20 and ubuntu-latest

Expected indentation of 8 spaces but found 1 tab

Check failure on line 129 in accessories/accessory.js

View workflow job for this annotation

GitHub Actions / Build and Test on Node 18 and ubuntu-latest

Expected indentation of 8 spaces but found 1 tab
log(`${name} interval (${host}) ${interval * 1000} ms`);

Check failure on line 130 in accessories/accessory.js

View workflow job for this annotation

GitHub Actions / Build and Test on Node 16 and ubuntu-latest

Expected indentation of 8 spaces but found 1 tab

Check failure on line 130 in accessories/accessory.js

View workflow job for this annotation

GitHub Actions / Build and Test on Node 20 and ubuntu-latest

Expected indentation of 8 spaces but found 1 tab

Check failure on line 130 in accessories/accessory.js

View workflow job for this annotation

GitHub Actions / Build and Test on Node 18 and ubuntu-latest

Expected indentation of 8 spaces but found 1 tab
}
}
}
Expand Down
364 changes: 321 additions & 43 deletions accessories/aircon.js

Large diffs are not rendered by default.

79 changes: 71 additions & 8 deletions accessories/light.js
@@ -1,3 +1,4 @@
// -*- js-indent-level : 2 -*-
const { assert } = require('chai');
const ServiceManagerTypes = require('../helpers/serviceManagerTypes');
const delayForDuration = require('../helpers/delayForDuration');
Expand All @@ -6,15 +7,15 @@ const catchDelayCancelError = require('../helpers/catchDelayCancelError')
const SwitchAccessory = require('./switch');

class LightAccessory extends SwitchAccessory {

setDefaults () {
super.setDefaults();

const { config } = this;

config.onDelay = config.onDelay || 0.1;
config.defaultBrightness = config.defaultBrightness || 100;
config.defaultColorTemperature = config.defaultColorTemperature || 140;
config.defaultColorTemperature = config.defaultColorTemperature || 500;
}

reset () {
Expand Down Expand Up @@ -82,6 +83,21 @@ class LightAccessory extends SwitchAccessory {
}
}

async setExclusivesOFF () {
const { log, name, logLevel } = this;
if (this.exclusives) {
this.exclusives.forEach(x => {
if (x.state.switchState) {
this.log(`${name} setSwitchState: (${x.name} is configured to be turned off)`);
x.reset();
x.state.switchState = false;
x.lastBrightness = undefined;
x.serviceManager.refreshCharacteristicUI(Characteristic.On);
}
});
}
}

async setSwitchState (hexData, previousValue) {
const { config, data, host, log, name, state, logLevel, serviceManager } = this;
let { defaultBrightness, useLastKnownBrightness } = config;
Expand All @@ -93,25 +109,29 @@ class LightAccessory extends SwitchAccessory {
this.setExclusivesOFF();
const brightness = (useLastKnownBrightness && state.brightness > 0) ? state.brightness : defaultBrightness;
const colorTemperature = useLastKnownColorTemperature ? state.colorTemperature : defaultColorTemperature;
if (brightness !== state.brightness || previousValue !== state.switchState) {
if (brightness !== state.brightness || previousValue !== state.switchState || colorTemperature !== state.colorTemperature) {
log(`${name} setSwitchState: (brightness: ${brightness})`);

state.switchState = false;
state.brightness = brightness;
serviceManager.setCharacteristic(Characteristic.Brightness, brightness);
serviceManager.refreshCharacteristicUI(Characteristic.Brightness);
if (this.dataKeys('colorTemperature').length > 0) {
state.colorTemperature = colorTemperature;
serviceManager.setCharacteristic(Characteristic.ColorTemperature, colorTemperature);
serviceManager.refreshCharacteristicUI(Characteristic.ColorTemperature);
}
} else {
if (hexData) {await this.performSend(hexData);}
await this.mqttpublish('On', 'true');

this.checkAutoOnOff();
}
} else {
this.lastBrightness = undefined;

if (hexData) {await this.performSend(hexData);}
await this.mqttpublish('On', 'false');

this.checkAutoOnOff();
}
Expand Down Expand Up @@ -142,6 +162,7 @@ class LightAccessory extends SwitchAccessory {
this.onDelayTimeoutPromise = delayForDuration(onDelay);
await this.onDelayTimeoutPromise;
}
await this.mqttpublish('On', 'true');
}

// Find hue closest to the one requested
Expand Down Expand Up @@ -170,6 +191,7 @@ class LightAccessory extends SwitchAccessory {

if (state.brightness > 0) {
state.switchState = true;
// await this.mqttpublish('On', 'true');
}

await this.checkAutoOnOff();
Expand All @@ -191,10 +213,10 @@ class LightAccessory extends SwitchAccessory {
log(`${name} setBrightness: (turn on, wait ${onDelay}s)`);
await this.performSend(on);

log(`${name} setHue: (wait ${onDelay}s then send data)`);
this.onDelayTimeoutPromise = delayForDuration(onDelay);
await this.onDelayTimeoutPromise;
}
await this.mqttpublish('On', 'true');
}

if (data['brightness+'] || data['brightness-'] || data['availableBrightnessSteps']) {
Expand Down Expand Up @@ -231,6 +253,7 @@ class LightAccessory extends SwitchAccessory {
} else {
log(`${name} setBrightness: (off)`);
await this.performSend(off);
await this.mqttpublish('On', 'false');
}

await this.checkAutoOnOff();
Expand All @@ -256,6 +279,7 @@ class LightAccessory extends SwitchAccessory {
this.onDelayTimeoutPromise = delayForDuration(onDelay);
await this.onDelayTimeoutPromise;
}
await this.mqttpublish('On', 'true');
}
if (data['colorTemperature+'] || data['colorTemperature-'] || data['availableColorTemperatureSteps']) {
assert(data['colorTemperature+'] && data['colorTemperature-'] && data['availableColorTemperatureSteps'], `\x1b[31m[CONFIG ERROR] \x1b[33mcolorTemperature+, colorTemperature- and availableColorTemperatureSteps\x1b[0m need to be set.`);
Expand Down Expand Up @@ -313,12 +337,51 @@ class LightAccessory extends SwitchAccessory {
return foundValues
}

async getLastActivation(callback) {
const lastActivation = this.state.lastActivation ?
Math.max(0, this.state.lastActivation - this.historyService.getInitialTime()) : 0;

callback(null, lastActivation);
}

localCharacteristic(key, uuid, props) {
let characteristic = class extends Characteristic {
constructor() {
super(key, uuid);
this.setProps(props);
}
}
characteristic.UUID = uuid;

return characteristic;
}

setupServiceManager () {
const { data, name, config, serviceManagerType } = this;
const { on, off } = data || { };
const history = config.history === true || config.noHistory === false;

this.serviceManager = new ServiceManagerTypes[serviceManagerType](name, Service.Lightbulb, this.log);

//this.serviceManager = new ServiceManagerTypes[serviceManagerType](name, Service.Lightbulb, this.log);
this.serviceManager = new ServiceManagerTypes[serviceManagerType](name, history ? Service.Switch : Service.Lightbulb, this.log);

if (history) {
const LastActivationCharacteristic = this.localCharacteristic(
'LastActivation', 'E863F11A-079E-48FF-8F27-9C2605A29F52',
{format: Characteristic.Formats.UINT32,
unit: Characteristic.Units.SECONDS,
perms: [
Characteristic.Perms.READ,
Characteristic.Perms.NOTIFY
]});

this.serviceManager.addGetCharacteristic({
name: 'LastActivation',
type: LastActivationCharacteristic,
method: this.getLastActivation,
bind: this
});
}

this.serviceManager.addToggleCharacteristic({
name: 'switchState',
type: Characteristic.On,
Expand Down Expand Up @@ -353,7 +416,7 @@ class LightAccessory extends SwitchAccessory {
bind: this,
props: {
setValuePromise: this.setColorTemperature.bind(this),
ignorePreviousValue: true
ignorePreviousValue: true // TODO: Check what this does and test it
}
});
}
Expand Down
94 changes: 92 additions & 2 deletions accessories/switch.js
@@ -1,3 +1,4 @@
// -*- js-indent-level : 2 -*-
const ServiceManagerTypes = require('../helpers/serviceManagerTypes');
const delayForDuration = require('../helpers/delayForDuration');
const catchDelayCancelError = require('../helpers/catchDelayCancelError');
Expand All @@ -10,8 +11,37 @@ class SwitchAccessory extends BroadlinkRMAccessory {
constructor (log, config = {}, serviceManagerType) {
super(log, config, serviceManagerType);

if (!config.isUnitTest) {this.checkPing(ping)}

// Fakegato setup
if (config.history === true || config.noHistory === false) {
this.historyService = new HistoryService('switch', { displayName: config.name, log: log }, { storage: 'fs', filename: 'RMPro_' + config.name.replace(' ','-') + '_persist.json'});
this.historyService.addEntry(
{time: Math.round(new Date().valueOf()/1000),
status: this.state.switchState ? 1 : 0})

let state2 = this.state;
this.state = new Proxy(state2, {
set: async function(target, key, value) {
if (target[key] != value) {
Reflect.set(target, key, value);
if (this.historyService) {
if (key == `switchState`) {
//this.log(`adding history of switchState.`, value);
const time = Math.round(new Date().valueOf()/1000);
//if (value) {
this.state.lastActivation = time;
//}
this.historyService.addEntry(
{time: time, status: value ? 1 : 0})
// await this.mqttpublish('On', value ? 'true' : 'false')
}
}
}
return true
}.bind(this)
})

if (!config.isUnitTest) {this.checkPing(ping)}
}
}

setDefaults () {
Expand Down Expand Up @@ -113,10 +143,12 @@ class SwitchAccessory extends BroadlinkRMAccessory {
this.reset();

if (hexData) {await this.performSend(hexData);}
await this.mqttpublish('On', state.switchState ? 'true' : 'false')

if (config.stateless === true) {
state.switchState = false;
serviceManager.refreshCharacteristicUI(Characteristic.On);
await this.mqttpublish('On', 'false')
} else {
this.checkAutoOnOff();
}
Expand Down Expand Up @@ -169,12 +201,70 @@ class SwitchAccessory extends BroadlinkRMAccessory {
});
}

async getLastActivation(callback) {
const lastActivation = this.state.lastActivation ?
Math.max(0, this.state.lastActivation - this.historyService.getInitialTime()) : 0;

callback(null, lastActivation);
}

localCharacteristic(key, uuid, props) {
let characteristic = class extends Characteristic {
constructor() {
super(key, uuid);
this.setProps(props);
}
}
characteristic.UUID = uuid;

return characteristic;
}

// MQTT
onMQTTMessage (identifier, message) {
const { state, logLevel, log, name, config } = this;
const mqttStateOnly = config.mqttStateOnly === false ? false : true;

super.onMQTTMessage(identifier, message);

if (identifier.toLowerCase() === 'on') {
const on = this.mqttValuesTemp[identifier] === 'true' ? true : false;
this.reset();
if (mqttStateOnly) {
this.state.switchState = on;
this.serviceManager.refreshCharacteristicUI(Characteristic.On);
} else {
this.serviceManager.setCharacteristic(Characteristic.On, on)
}
log(`${name} onMQTTMessage (set switchState to ${this.state.switchState}).`);
}
}

setupServiceManager () {
const { data, name, config, serviceManagerType } = this;
const { on, off } = data || { };
const history = config.history === true || config.noHistory === false;

this.serviceManager = new ServiceManagerTypes[serviceManagerType](name, Service.Switch, this.log);

if (history) {
const LastActivationCharacteristic = this.localCharacteristic(
'LastActivation', 'E863F11A-079E-48FF-8F27-9C2605A29F52',
{format: Characteristic.Formats.UINT32,
unit: Characteristic.Units.SECONDS,
perms: [
Characteristic.Perms.READ,
Characteristic.Perms.NOTIFY
]});

this.serviceManager.addGetCharacteristic({
name: 'LastActivation',
type: LastActivationCharacteristic,
method: this.getLastActivation,
bind: this
});
}

this.serviceManager.addToggleCharacteristic({
name: 'switchState',
type: Characteristic.On,
Expand Down

0 comments on commit e85bcfc

Please sign in to comment.