diff --git a/accessories/alarm_control_panel.js b/accessories/alarm_control_panel.js index 1556083..f412078 100644 --- a/accessories/alarm_control_panel.js +++ b/accessories/alarm_control_panel.js @@ -37,24 +37,26 @@ function HomeAssistantAlarmControlPanel(log, data, client) { HomeAssistantAlarmControlPanel.prototype = { onEvent(oldState, newState) { - let alarmState; - if (newState.state === 'armed_home') { - alarmState = 0; - } else if (newState.state === 'armed_away') { - alarmState = 1; - } else if (newState.state === 'armed_night') { - alarmState = 2; - } else if (newState.state === 'disarmed') { - alarmState = 3; - } else if (newState.state === 'triggered') { - alarmState = 4; - } else { - alarmState = 3; + if (newState.state) { + let alarmState; + if (newState.state === 'armed_home') { + alarmState = 0; + } else if (newState.state === 'armed_away') { + alarmState = 1; + } else if (newState.state === 'armed_night') { + alarmState = 2; + } else if (newState.state === 'disarmed') { + alarmState = 3; + } else if (newState.state === 'triggered') { + alarmState = 4; + } else { + alarmState = 3; + } + this.alarmService.getCharacteristic(Characteristic.SecuritySystemCurrentState) + .setValue(alarmState, null, 'internal'); + this.alarmService.getCharacteristic(Characteristic.SecuritySystemTargetState) + .setValue(alarmState, null, 'internal'); } - this.alarmService.getCharacteristic(Characteristic.SecuritySystemCurrentState) - .setValue(alarmState, null, 'internal'); - this.alarmService.getCharacteristic(Characteristic.SecuritySystemTargetState) - .setValue(alarmState, null, 'internal'); }, getAlarmState(callback) { this.client.fetchState(this.entity_id, (data) => { diff --git a/accessories/binary_sensor.js b/accessories/binary_sensor.js index 33c9c14..ecbce50 100644 --- a/accessories/binary_sensor.js +++ b/accessories/binary_sensor.js @@ -46,8 +46,10 @@ class HomeAssistantBinarySensor { } onEvent(oldState, newState) { - this.sensorService.getCharacteristic(this.characteristic) - .setValue(newState.state === 'on' ? this.onValue : this.offValue, null, 'internal'); + if (newState.state) { + this.sensorService.getCharacteristic(this.characteristic) + .setValue(newState.state === 'on' ? this.onValue : this.offValue, null, 'internal'); + } } identify(callback) { this.log(`identifying: ${this.name}`); diff --git a/accessories/climate.js b/accessories/climate.js index 2874be5..aa66071 100644 --- a/accessories/climate.js +++ b/accessories/climate.js @@ -52,15 +52,17 @@ function HomeAssistantClimate(log, data, client) { } HomeAssistantClimate.prototype = { onEvent: function (oldState, newState) { - const list = { - idle: 0, heat: 1, cool: 2, auto: 3, off: 0 - }; - this.ThermostatService.getCharacteristic(Characteristic.CurrentTemperature) - .setValue(newState.attributes.current_temperature || newState.attributes.temperature, null, 'internal'); - this.ThermostatService.getCharacteristic(Characteristic.TargetTemperature) - .setValue(newState.attributes.temperature, null, 'internal'); - this.ThermostatService.getCharacteristic(Characteristic.TargetHeatingCoolingState) - .setValue(list[newState.state], null, 'internal'); + if (newState.state) { + const list = { + idle: 0, heat: 1, cool: 2, auto: 3, off: 0 + }; + this.ThermostatService.getCharacteristic(Characteristic.CurrentTemperature) + .setValue(newState.attributes.current_temperature || newState.attributes.temperature, null, 'internal'); + this.ThermostatService.getCharacteristic(Characteristic.TargetTemperature) + .setValue(newState.attributes.temperature, null, 'internal'); + this.ThermostatService.getCharacteristic(Characteristic.TargetHeatingCoolingState) + .setValue(list[newState.state], null, 'internal'); + } }, getCurrentTemp: function (callback) { this.client.fetchState(this.entity_id, function (data) { diff --git a/accessories/cover.js b/accessories/cover.js index 9545b7f..a272186 100644 --- a/accessories/cover.js +++ b/accessories/cover.js @@ -31,12 +31,14 @@ class HomeAssistantCover { } onEvent(oldState, newState) { - const state = this.transformData(newState); + if (newState.state) { + const state = this.transformData(newState); - this.service.getCharacteristic(this.stateCharacteristic) - .setValue(state, null, 'internal'); - this.service.getCharacteristic(this.targetCharacteristic) - .setValue(state, null, 'internal'); + this.service.getCharacteristic(this.stateCharacteristic) + .setValue(state, null, 'internal'); + this.service.getCharacteristic(this.targetCharacteristic) + .setValue(state, null, 'internal'); + } } getState(callback) { diff --git a/accessories/device_tracker.js b/accessories/device_tracker.js index 6def5c9..4688bc2 100644 --- a/accessories/device_tracker.js +++ b/accessories/device_tracker.js @@ -42,8 +42,10 @@ class HomeAssistantDeviceTracker { } onEvent(oldState, newState) { - this.sensorService.getCharacteristic(this.characteristic) - .setValue(newState.state === 'home' ? this.onValue : this.offValue, null, 'internal'); + if (newState.state) { + this.sensorService.getCharacteristic(this.characteristic) + .setValue(newState.state === 'home' ? this.onValue : this.offValue, null, 'internal'); + } } identify(callback) { this.log('identifying: ' + this.name); diff --git a/accessories/fan.js b/accessories/fan.js index b0cd700..09c41ec 100644 --- a/accessories/fan.js +++ b/accessories/fan.js @@ -42,8 +42,10 @@ function HomeAssistantFan(log, data, client) { HomeAssistantFan.prototype = { onEvent(oldState, newState) { - this.fanService.getCharacteristic(Characteristic.On) - .setValue(newState.state === 'on', null, 'internal'); + if (newState.state) { + this.fanService.getCharacteristic(Characteristic.On) + .setValue(newState.state === 'on', null, 'internal'); + } }, getPowerState(callback) { this.client.fetchState(this.entity_id, (data) => { diff --git a/accessories/light.js b/accessories/light.js index c793625..82fbf48 100644 --- a/accessories/light.js +++ b/accessories/light.js @@ -147,38 +147,40 @@ HomeAssistantLight.prototype = { return (this.data.attributes.supported_features & feature) > 0; }, onEvent(oldState, newState) { - this.lightbulbService.getCharacteristic(Characteristic.On) - .setValue(newState.state === 'on', null, 'internal'); - if (this.is_supported(this.features.BRIGHTNESS)) { - const brightness = Math.round(((newState.attributes.brightness || 0) / 255) * 100); + if (newState.state) { + this.lightbulbService.getCharacteristic(Characteristic.On) + .setValue(newState.state === 'on', null, 'internal'); + if (this.is_supported(this.features.BRIGHTNESS)) { + const brightness = Math.round(((newState.attributes.brightness || 0) / 255) * 100); - this.lightbulbService.getCharacteristic(Characteristic.Brightness) - .setValue(brightness, null, 'internal'); + this.lightbulbService.getCharacteristic(Characteristic.Brightness) + .setValue(brightness, null, 'internal'); - this.data.attributes.brightness = newState.attributes.brightness; - } + this.data.attributes.brightness = newState.attributes.brightness; + } - if (this.is_supported(this.features.RGB_COLOR) && - newState.attributes.rgb_color !== undefined) { - const rgbColor = newState.attributes.rgb_color; - const hsv = LightUtil.rgbToHsv(rgbColor[0], rgbColor[1], rgbColor[2]); - const hue = hsv.h * 360; - const saturation = hsv.s * 100; + if (this.is_supported(this.features.RGB_COLOR) && + newState.attributes.rgb_color !== undefined) { + const rgbColor = newState.attributes.rgb_color; + const hsv = LightUtil.rgbToHsv(rgbColor[0], rgbColor[1], rgbColor[2]); + const hue = hsv.h * 360; + const saturation = hsv.s * 100; - this.lightbulbService.getCharacteristic(Characteristic.Hue) - .setValue(hue, null, 'internal'); - this.lightbulbService.getCharacteristic(Characteristic.Saturation) - .setValue(saturation, null, 'internal'); + this.lightbulbService.getCharacteristic(Characteristic.Hue) + .setValue(hue, null, 'internal'); + this.lightbulbService.getCharacteristic(Characteristic.Saturation) + .setValue(saturation, null, 'internal'); - this.data.attributes.hue = hue; - this.data.attributes.saturation = saturation; - } + this.data.attributes.hue = hue; + this.data.attributes.saturation = saturation; + } - if (this.is_supported(this.features.COLOR_TEMP)) { - const colorTemperature = Math.round(newState.attributes.color_temp) || this.minTemp; + if (this.is_supported(this.features.COLOR_TEMP)) { + const colorTemperature = Math.round(newState.attributes.color_temp) || this.minTemp; - this.lightbulbService.getCharacteristic(Characteristic.ColorTemperature) - .setValue(colorTemperature, null, 'internal'); + this.lightbulbService.getCharacteristic(Characteristic.ColorTemperature) + .setValue(colorTemperature, null, 'internal'); + } } }, identify(callback) { diff --git a/accessories/lock.js b/accessories/lock.js index 14713af..96d0c68 100644 --- a/accessories/lock.js +++ b/accessories/lock.js @@ -39,11 +39,13 @@ function HomeAssistantLock(log, data, client) { HomeAssistantLock.prototype = { onEvent(oldState, newState) { - const lockState = newState.state === 'unlocked' ? 0 : 1; - this.lockService.getCharacteristic(Characteristic.LockCurrentState) - .setValue(lockState, null, 'internal'); - this.lockService.getCharacteristic(Characteristic.LockTargetState) - .setValue(lockState, null, 'internal'); + if (newState.state) { + const lockState = newState.state === 'unlocked' ? 0 : 1; + this.lockService.getCharacteristic(Characteristic.LockCurrentState) + .setValue(lockState, null, 'internal'); + this.lockService.getCharacteristic(Characteristic.LockTargetState) + .setValue(lockState, null, 'internal'); + } }, getLockState(callback) { this.client.fetchState(this.entity_id, (data) => { diff --git a/accessories/media_player.js b/accessories/media_player.js index 797c9f9..4396fe3 100644 --- a/accessories/media_player.js +++ b/accessories/media_player.js @@ -85,14 +85,16 @@ function HomeAssistantMediaPlayer(log, data, client) { HomeAssistantMediaPlayer.prototype = { onEvent(oldState, newState) { - let powerState; - if (this.stateLogicCompareWithOn) { - powerState = newState.state === this.onState; - } else { - powerState = newState.state !== this.offState; + if (newState.state) { + let powerState; + if (this.stateLogicCompareWithOn) { + powerState = newState.state === this.onState; + } else { + powerState = newState.state !== this.offState; + } + this.switchService.getCharacteristic(Characteristic.On) + .setValue(powerState, null, 'internal'); } - this.switchService.getCharacteristic(Characteristic.On) - .setValue(powerState, null, 'internal'); }, getPowerState(callback) { this.log(`fetching power state for: ${this.name}`); diff --git a/accessories/sensor.js b/accessories/sensor.js index 4a41124..6ac29e1 100644 --- a/accessories/sensor.js +++ b/accessories/sensor.js @@ -47,19 +47,21 @@ class HomeAssistantSensor { } onEvent(oldState, newState) { - if (this.service === Service.CarbonDioxideSensor) { - const transformed = this.transformData(newState); - this.sensorService.getCharacteristic(this.characteristic) - .setValue(transformed, null, 'internal'); - - const abnormal = Characteristic.CarbonDioxideDetected.CO2_LEVELS_ABNORMAL; - const normal = Characteristic.CarbonDioxideDetected.CO2_LEVELS_NORMAL; - const detected = (transformed > 1000 ? abnormal : normal); - this.sensorService.getCharacteristic(Characteristic.CarbonDioxideDetected) - .setValue(detected, null, 'internal'); - } else { - this.sensorService.getCharacteristic(this.characteristic) - .setValue(this.transformData(newState), null, 'internal'); + if (newState.state) { + if (this.service === Service.CarbonDioxideSensor) { + const transformed = this.transformData(newState); + this.sensorService.getCharacteristic(this.characteristic) + .setValue(transformed, null, 'internal'); + + const abnormal = Characteristic.CarbonDioxideDetected.CO2_LEVELS_ABNORMAL; + const normal = Characteristic.CarbonDioxideDetected.CO2_LEVELS_NORMAL; + const detected = (transformed > 1000 ? abnormal : normal); + this.sensorService.getCharacteristic(Characteristic.CarbonDioxideDetected) + .setValue(detected, null, 'internal'); + } else { + this.sensorService.getCharacteristic(this.characteristic) + .setValue(this.transformData(newState), null, 'internal'); + } } } diff --git a/accessories/switch.js b/accessories/switch.js index 98a5a1f..be85fce 100644 --- a/accessories/switch.js +++ b/accessories/switch.js @@ -31,8 +31,10 @@ function HomeAssistantSwitch(log, data, client, type) { HomeAssistantSwitch.prototype = { onEvent(oldState, newState) { - this.service.getCharacteristic(Characteristic.On) - .setValue(newState.state === 'on', null, 'internal'); + if (newState.state) { + this.service.getCharacteristic(Characteristic.On) + .setValue(newState.state === 'on', null, 'internal'); + } }, getPowerState(callback) { this.client.fetchState(this.entity_id, (data) => {