Skip to content

Commit

Permalink
Added support for temperatureFilePath to the “air-conditioner” acce…
Browse files Browse the repository at this point in the history
…ssory.
  • Loading branch information
lprhodes committed Mar 14, 2018
1 parent e7b50e5 commit adc26ea
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ key | description | example | default | required | unit tested
`autoHeatTemperature` | When the temperature is below this value, the heat mode will be enabled. | 18 | - | No | Yes
`autoCoolTemperature` | When the temperature is above this value, the cool mode will enabled. | 27 | - | No | Yes
`minimumAutoOnOffDuration` | The minimum amount of time that the air-conditioner must remain on or off after having been automatically turned on or off. | 300 | 120 | No | Yes
`temperatureFilePath` | A path to a file containing a number that represents the current temperature reported to the accessory. | "/Users/username/HomeKit/temperature/" | - | No | No
`temperatureAdjustment` | The number of degrees that the reported temperature should be offset by. | 3 | 0 | No | Yes
`turnOnWhenOff` | If the air-con unit state is off then an `on` code shall be sent before sending the temperature code. | true | false | No | Yes
`preventResendHex` | Determines whether the temperature hex code should be resent when requesting the same temperature as the last requested temperature. | true | false | No | Yes
Expand Down
92 changes: 68 additions & 24 deletions accessories/aircon.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { assert } = require('chai');
const uuid = require('uuid');
const fs = require('fs');

const sendData = require('../helpers/sendData');
const delayForDuration = require('../helpers/delayForDuration');
Expand Down Expand Up @@ -300,52 +301,59 @@ class AirConAccessory extends BroadlinkRMAccessory {

async monitorTemperature () {
const { config, host, log, name, state } = this;
const { pseudoDeviceTemperature, minTemperature, maxTemperature, temperatureAdjustment } = config;
const { temperatureFilePath } = config;

if (temperatureFilePath) return;

const device = getDevice({ host, log });

// Try again in a second if we don't have a device yet
if (!device) {
await delayForDuration(1);

this.monitorTemperature();

return;
}

const onTemperature = (temperature) => {
temperature += temperatureAdjustment

state.currentTemperature = temperature;

log(`${name} onTemperature (${temperature})`);
log(`${name} monitorTemperature`);

if (temperature > maxTemperature) {
log(`${name} getCurrentTemperature (reported temperature too high, settings to 0: ${temperature})`);
device.on('temperature', this.onTemperature.bind(this));
device.checkTemperature();

temperature = 0
}
this.updateTemperatureUI();
if (!config.isUnitTest) setInterval(this.updateTemperatureUI.bind(this), config.temperatureUpdateFrequency * 1000)
}

if (temperature < minTemperature) {
log(`${name} getCurrentTemperature (reported temperature too low, setting to 0: ${temperature})`);
onTemperature (temperature) {
const { config, host, log, name, state } = this;
const { pseudoDeviceTemperature, minTemperature, maxTemperature, temperatureAdjustment } = config;

temperature = 0
}
temperature += temperatureAdjustment

state.currentTemperature = temperature;

log(`${name} onTemperature (${temperature})`);

this.processQueuedTemperatureCallbacks(temperature);
if (temperature > maxTemperature) {
log(`${name} getCurrentTemperature (reported temperature too high, settings to 0: ${temperature})`);

temperature = 0
}

log(`${name} monitorTemperature`);
if (temperature < minTemperature) {

log(`${name} getCurrentTemperature (reported temperature too low, setting to 0: ${temperature})`);

device.on('temperature', onTemperature);
device.checkTemperature();
temperature = 0
}

this.updateTemperatureUI();
if (!config.isUnitTest) setInterval(this.updateTemperatureUI.bind(this), config.temperatureUpdateFrequency * 1000)
this.processQueuedTemperatureCallbacks(temperature);
}

addTemperatureCallbackToQueue (callback) {
const { host, log, name } = this;
const { config, host, log, name } = this;
const { temperatureFilePath } = config;

const callbackIdentifier = uuid.v4();

Expand All @@ -358,6 +366,12 @@ class AirConAccessory extends BroadlinkRMAccessory {
return;
}

if (temperatureFilePath) {
this.updateTemperatureFromFile();

return;
}

const device = getDevice({ host, log });

if (!device) {
Expand All @@ -367,7 +381,37 @@ class AirConAccessory extends BroadlinkRMAccessory {
}

device.checkTemperature();
log(`${name} getCurrentTemperature (requested temperature from device, waiting)`);
log(`${name} addTemperatureCallbackToQueue (requested temperature from device, waiting)`);
}

updateTemperatureFromFile () {
const { config, debug, host, log, name } = this;
const { temperatureFilePath } = config;

if (debug) log(`${name} updateTemperatureFromFile reading file: ${temperatureFilePath}`);

fs.readFile(temperatureFilePath, 'utf8', (err, temperature) => {

if (err) {
log(`\x1b[31m[ERROR] \x1b[30m${name} updateTemperatureFromFile\n\n${err.message}`);

return;
}

if (!temperature || temperature.trim().length === 0) {
log(`\x1b[31m[ERROR] \x1b[30m${name} updateTemperatureFromFile (no temperature found)`);

return;
}

if (debug) log(`${name} updateTemperatureFromFile (file content: ${temperature.trim()})`);

temperature = parseFloat(temperature);

if (debug) log(`${name} updateTemperatureFromFile (parsed temperature: ${temperature})`);

this.onTemperature(temperature);
});
}

processQueuedTemperatureCallbacks (temperature) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homebridge-broadlink-rm",
"version": "3.0.4",
"version": "3.0.5",
"description": "Broadlink RM plugin (including the mini and pro) for homebridge: https://github.com/nfarina/homebridge",
"license": "ISC",
"keywords": [
Expand Down

0 comments on commit adc26ea

Please sign in to comment.