From 9d44e98d01de1a54ed1c4d1d1d62d922ea019d75 Mon Sep 17 00:00:00 2001 From: Luke Rhodes Date: Wed, 28 Mar 2018 12:03:39 +1100 Subject: [PATCH] Fix for mqtt not working for the `air-conditioner` temperature. --- accessories/aircon.js | 24 ++++++++++++++++++------ package.json | 3 ++- yarn.lock | 26 +++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/accessories/aircon.js b/accessories/aircon.js index cb56d03a..dd007f1f 100644 --- a/accessories/aircon.js +++ b/accessories/aircon.js @@ -1,6 +1,7 @@ const { assert } = require('chai'); const uuid = require('uuid'); const fs = require('fs'); +const findKey = require('find-key'); const delayForDuration = require('../helpers/delayForDuration'); const ServiceManagerTypes = require('../helpers/serviceManagerTypes'); @@ -369,15 +370,15 @@ class AirConAccessory extends BroadlinkRMAccessory { log(`${name} onTemperature (${temperature})`); if (temperature > config.maxTemperature) { + log(`\x1b[35m[INFO]\x1b[0m Reported temperature (${temperature}) is too high, setting to \x1b[33mmaxTemperature\x1b[0m (${maxTemperature}).`) temperature = config.maxTemperature - - log(`\x1b[35m[INFO]\x1b[0m Reported temperature (${temperature}) is too high, setting to \x1b[33mmaxTemperature\x1b[0m`) } if (temperature < config.minTemperature) { - temperature = config.minTemperature - log(`\x1b[35m[INFO]\x1b[0m Reported temperature (${temperature}) is too low, setting to \x1b[33mminTemperature\x1b[0m`) + log(`\x1b[35m[INFO]\x1b[0m Reported temperature (${temperature}) is too low, setting to \x1b[33mminTemperature\x1b[0m (${minTemperature}).`) + temperature = config.minTemperature + } assert.isBelow(temperature, config.maxTemperature + 1, `\x1b[31m[CONFIG ERROR] \x1b[33mmaxTemperature\x1b[0m (${config.maxTemperature}) must be more than the reported temperature (${temperature})`) @@ -571,7 +572,7 @@ class AirConAccessory extends BroadlinkRMAccessory { super.onMQTTMessage(identifier, message); - let temperature = this.mqttValues[identifier]; + let temperature = this.mqttValuesTemp[identifier]; if (debug) log(`\x1b[33m[DEBUG]\x1b[0m ${name} onMQTTMessage (raw value: ${temperature})`); @@ -579,7 +580,16 @@ class AirConAccessory extends BroadlinkRMAccessory { const temperatureJSON = JSON.parse(temperature); if (typeof temperatureJSON === 'object') { - temperature = temperatureJSON.temp || temperatureJSON.Temp || temperatureJSON.temperature || temperatureJSON.Temperature + let values = findKey(temperatureJSON, 'temp'); + if (values.length === 0) values = findKey(temperatureJSON, 'Temp'); + if (values.length === 0) values = findKey(temperatureJSON, 'temperature'); + if (values.length === 0) values = findKey(temperatureJSON, 'Temperature'); + + if (values.length > 0) { + temperature = values[0]; + } else { + temperature = undefined; + } } } catch (err) {} @@ -598,6 +608,8 @@ class AirConAccessory extends BroadlinkRMAccessory { this.mqttValues[identifier] = temperature; this.updateTemperatureUI(); } + + // Service Manager Setup diff --git a/package.json b/package.json index c8ba7174..07164b87 100644 --- a/package.json +++ b/package.json @@ -26,8 +26,9 @@ "dependencies": { "broadlinkjs-rm": "^0.6.0", "chai": "^4.1.2", + "find-key": "^2.0.1", "github-version-checker": "^1.2.0", - "homebridge-platform-helper": "1.1.0", + "homebridge-platform-helper": "1.1.1", "ping": "^0.2.2", "uuid": "^3.2.1" }, diff --git a/yarn.lock b/yarn.lock index cc7e4f89..55f541b6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -613,6 +613,14 @@ base@^0.11.1: mixin-deep "^1.2.0" pascalcase "^0.1.1" +basic-utils@^1.6.0: + version "1.7.4" + resolved "https://registry.yarnpkg.com/basic-utils/-/basic-utils-1.7.4.tgz#b710a47db43daff50724c0e1ea4247e1e6d49946" + dependencies: + deep-freeze "0.0.1" + mkdirp "^0.5.1" + rimraf "^2.6.1" + before-after-hook@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/before-after-hook/-/before-after-hook-1.1.0.tgz#83165e15a59460d13702cb8febd6a1807896db5a" @@ -1204,6 +1212,10 @@ deep-extend@~0.4.0: version "0.4.2" resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.4.2.tgz#48b699c27e334bf89f10892be432f6e4c7d34a7f" +deep-freeze@0.0.1: + version "0.0.1" + resolved "https://registry.yarnpkg.com/deep-freeze/-/deep-freeze-0.0.1.tgz#3a0b0005de18672819dfd38cd31f91179c893e84" + defaults@^1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/defaults/-/defaults-1.0.3.tgz#c656051e9817d9ff08ed881477f3fe4019f3ef7d" @@ -1397,6 +1409,12 @@ fill-range@^4.0.0: repeat-string "^1.6.1" to-regex-range "^2.1.0" +find-key@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/find-key/-/find-key-2.0.1.tgz#e985c95e0089787290d9a0512e139888c13f102b" + dependencies: + basic-utils "^1.6.0" + find-up@^1.0.0: version "1.1.2" resolved "https://registry.yarnpkg.com/find-up/-/find-up-1.1.2.tgz#6b2e9822b1a2ce0a60ab64d610eccad53cb24d0f" @@ -1522,7 +1540,7 @@ glob-to-regexp@^0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.3.0.tgz#8c5a1494d2066c570cc3bfe4496175acc4d502ab" -glob@7.1.2, glob@^7.0.0, glob@^7.0.3, glob@^7.1.1, glob@^7.1.2: +glob@7.1.2, glob@^7.0.0, glob@^7.0.3, glob@^7.0.5, glob@^7.1.1, glob@^7.1.2: version "7.1.2" resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" dependencies: @@ -3018,6 +3036,12 @@ right-align@^0.1.1: dependencies: align-text "^0.1.1" +rimraf@^2.6.1: + version "2.6.2" + resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.2.tgz#2ed8150d24a16ea8651e6d6ef0f47c4158ce7a36" + dependencies: + glob "^7.0.5" + run-async@^2.2.0: version "2.3.0" resolved "https://registry.yarnpkg.com/run-async/-/run-async-2.3.0.tgz#0371ab4ae0bdd720d4166d7dfda64ff7a445a6c0"