From a74d6defca3a728735cf17ccf4496b2f4451ae26 Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Mon, 30 Apr 2018 16:12:12 +0200 Subject: [PATCH] Plug devices: Properly set initial power state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ‘status’ can get a value of empty string (at least in the case of the Zigbee gateway-based power plugs), which used to get translated to a 'false' power state. This would cause the triggering of the powerChanged event twice on initialisation if the device was on already, with the empty string case being the second call. This would cause users that monitor the ‘powerChanged’ event to falsely report a power plug as off. This fix explicitly allows for the empty string case, and avoids updating the power state in this case. --- lib/devices/capabilities/power.js | 2 +- lib/devices/gateway/plug.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/devices/capabilities/power.js b/lib/devices/capabilities/power.js index 328de9e..27100fa 100644 --- a/lib/devices/capabilities/power.js +++ b/lib/devices/capabilities/power.js @@ -4,7 +4,7 @@ const { Thing, SwitchablePower } = require('abstract-things'); module.exports = Thing.mixin(Parent => class extends Parent.with(SwitchablePower) { propertyUpdated(key, value) { - if(key === 'power') { + if(key === 'power' && value !== undefined) { this.updatePower(value); } diff --git a/lib/devices/gateway/plug.js b/lib/devices/gateway/plug.js index f17a565..987842d 100644 --- a/lib/devices/gateway/plug.js +++ b/lib/devices/gateway/plug.js @@ -21,7 +21,7 @@ module.exports = class Plug extends SubDevice this.defineProperty('status', { name: 'power', - mapper: v => v === 'on' + mapper: v => (v === '') ? undefined : (v === 'on') }); this.defineProperty('load_voltage', {