Skip to content

Commit

Permalink
Supprt for “plug” accerssory
Browse files Browse the repository at this point in the history
  • Loading branch information
lprhodes committed Feb 9, 2018
1 parent d71003e commit 263ee7e
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 11 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Turn the switch on and the "on" hex code is sent, turn it off and the "off" hex

key | description | example | default
--- | ----------- | ------- | -------
disableAutomaticOff (optional) | Prevent the window-covering from turning off automatically after a given amount of time. | false | true
disableAutomaticOff (optional) | Prevent the switch from turning off automatically after a given amount of time. | false | true
onDuration (optional) | The amount of time before the switch automatically turns itself off (used in conjunction with disableAutomaticOff). | 5 | 2
pingIPAddress (optional) | When an IP address is provided, it is pinged every second. If a response is received then the switch turns on, otherwise it turns off. | "192.167.1.77" | -

Expand All @@ -85,7 +85,7 @@ off | A hex code string to be sent when the switch is changed to the off positio

### switch-multi

Turn the switch on and the switch will send each hex code in the provided array until. It then turns itself off automatically. You can also set the interval between each send.
Turn the switch on and the switch will send each hex code in the provided array. It then turns itself off automatically. You can also set the interval between each send.

key | description | example | default
--- | ----------- | ------- | -------
Expand All @@ -107,6 +107,22 @@ offSendCount (optional) | If you set separate "on" and "off" codes you can use t
interval (optional) | The amount of time between each send of a hex code in seconds. | 0.3 | 1
disableAutomaticOff (optional) | Prevent the switch from turning off automatically when complete. | true | false

### outlet

Turn the outlet on and the "on" hex code is sent, turn it off and the "off" hex code is sent.

key | description | example | default
--- | ----------- | ------- | -------
disableAutomaticOff (optional) | Prevent the switch from turning off automatically after a given amount of time. | false | true
onDuration (optional) | The amount of time before the switch automatically turns itself off (used in conjunction with disableAutomaticOff). | 5 | 2
pingIPAddress (optional) | When an IP address is provided, it is pinged every second. If a response is received then the outlet's "Outlet In Use" shows as "Yes", otherwise it shows as "No". | "192.167.1.77" | -

#### "data" key-value object
key | description
--- | -----------
on | A hex code string to be sent when the plug is changed to the on position.
off | A hex code string to be sent when the plug is changed to the off position.


### fan

Expand Down
2 changes: 2 additions & 0 deletions accessories/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const AirCon = require('./aircon');
const Channel = require('./channel');
const LearnCode = require('./learnCode');
const Outlet = require('./outlet');
const Switch = require('./switch');
const SwitchMulti = require('./switchMulti');
const SwitchMultiRepeat = require('./switchMultiRepeat');
Expand All @@ -19,6 +20,7 @@ module.exports = {
Switch,
SwitchMulti,
SwitchMultiRepeat,
Outlet,
SwitchRepeat,
UpDown,
Fan,
Expand Down
92 changes: 92 additions & 0 deletions accessories/outlet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const sendData = require('../helpers/sendData');
const delayForDuration = require('../helpers/delayForDuration')
const BroadlinkRMAccessory = require('./accessory');

class SwitchAccessory extends BroadlinkRMAccessory {

constructor (log, config) {
super(log, config);

this.manufacturer = 'Broadlink';
this.model = 'RM Mini or Pro';
this.serialNumber = this.host;

config.resendDataAfterReload = config.resendHexAfterReload;

if (config.pingIPAddress) checkStateWithPing()
}

checkStateWithPing () {
const { config, debug, log } = this;
let { pingIPAddress } = config;

const pingFrequency = 1000;

setInterval(() => {
ping.sys.probe(pingIPAddress, (active) => {
if (debug) log(`${name} ping "${host}": ${active ? 'active' : 'inactive'}`);

if (active) {
this.switchService.setCharacteristic(Characteristic.On, 1);
} else {
this.switchService.setCharacteristic(Characteristic.On, 0);
}
})
}, pingFrequency);
}

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

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

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

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

if (state.switchState && !disableAutomaticOff) {
this.autoOffTimeout = setTimeout(() => {
this.switchService.setCharacteristic(Characteristic.On, 0);
}, onDuration * 1000);
}
}

setOutletInUse (value, callback) {
callback(null, callback)
}

getServices () {
const services = super.getServices();

const { data, name } = this;
const { on, off } = data || { };

const service = new Service.Switch(name);
this.addNameService(service);

this.createToggleCharacteristic({
service,
characteristicType: Characteristic.On,
propertyName: 'switchState',
onData: on,
offData: off,
setValuePromise: this.setSwitchState.bind(this)
});

service.getCharacteristic(Characteristic.OutletInUse)
.on('set', this.setOutletInUse)
.on('get', this.getCharacteristicValue.bind(this, { propertyName: 'outletInUse' }));


this.switchService = service;

services.push(service);

return services;
}
}

module.exports = SwitchAccessory;
6 changes: 3 additions & 3 deletions accessories/switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class SwitchAccessory extends BroadlinkRMAccessory {
if (debug) log(`${name} ping "${host}": ${active ? 'active' : 'inactive'}`);

if (active) {
this.switchService.setCharacteristic(Characteristic.On, 1);
this.switchService.setCharacteristic(Characteristic.OutletInUse, 1);
} else {
this.switchService.setCharacteristic(Characteristic.On, 0);
this.switchService.setCharacteristic(Characteristic.OutletInUse, 0);
}
})
}, pingFrequency);
Expand Down Expand Up @@ -60,7 +60,7 @@ class SwitchAccessory extends BroadlinkRMAccessory {
const { data, name } = this;
const { on, off } = data || { };

const service = new Service.Switch(name);
const service = new Service.Outlet(name);
this.addNameService(service);

this.createToggleCharacteristic({
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const classTypes = {
'switch-multi-repeat': Accessory.SwitchMultiRepeat,
'switch-repeat': Accessory.SwitchRepeat,
'fan': Accessory.Fan,
'outlet': Accessory.Outlet,
'light': Accessory.Light,
'window-covering': Accessory.WindowCovering,
}
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homebridge-broadlink-rm",
"version": "2.6.0",
"version": "2.6.1",
"description": "Broadlink RM plugin (including the mini and pro) for homebridge: https://github.com/nfarina/homebridge",
"license": "ISC",
"keywords": [
Expand All @@ -22,7 +22,7 @@
},
"dependencies": {
"broadlinkjs-rm": "^0.2.2",
"homebridge-platform-helper": "0.0.5",
"homebridge-platform-helper": "0.0.6",
"ping": "^0.2.2"
},
"devDependencies": {
Expand Down

0 comments on commit 263ee7e

Please sign in to comment.