Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
Merge pull request #291 from home-assistant/accomodate-null-states
Browse files Browse the repository at this point in the history
Check for null states from event listener
  • Loading branch information
schmittx authored Feb 13, 2018
2 parents 10689e0 + c15aa96 commit 1cd2beb
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 89 deletions.
36 changes: 19 additions & 17 deletions accessories/alarm_control_panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
6 changes: 4 additions & 2 deletions accessories/binary_sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
20 changes: 11 additions & 9 deletions accessories/climate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 7 additions & 5 deletions accessories/cover.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions accessories/device_tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions accessories/fan.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
52 changes: 27 additions & 25 deletions accessories/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 7 additions & 5 deletions accessories/lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
16 changes: 9 additions & 7 deletions accessories/media_player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
28 changes: 15 additions & 13 deletions accessories/sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions accessories/switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 1cd2beb

Please sign in to comment.