From 01ce43ec7cc6007713ec32b8edfbe8d3a9c54665 Mon Sep 17 00:00:00 2001 From: damarco Date: Sat, 12 May 2018 14:41:44 +0200 Subject: [PATCH] Use None as initial state in zha component (#14389) * Return None if state is unknown * Use None as initial state --- homeassistant/components/binary_sensor/zha.py | 2 +- homeassistant/components/fan/zha.py | 5 ++--- homeassistant/components/light/zha.py | 3 +-- homeassistant/components/sensor/zha.py | 12 ++++++------ homeassistant/components/switch/zha.py | 2 +- homeassistant/components/zha/__init__.py | 2 +- 6 files changed, 12 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/binary_sensor/zha.py b/homeassistant/components/binary_sensor/zha.py index 4f3f824c8f9..d3b31188760 100644 --- a/homeassistant/components/binary_sensor/zha.py +++ b/homeassistant/components/binary_sensor/zha.py @@ -108,7 +108,7 @@ class BinarySensor(zha.Entity, BinarySensorDevice): @property def is_on(self) -> bool: """Return True if entity is on.""" - if self._state == 'unknown': + if self._state is None: return False return bool(self._state) diff --git a/homeassistant/components/fan/zha.py b/homeassistant/components/fan/zha.py index 3288a788e1f..01b1d0a92cf 100644 --- a/homeassistant/components/fan/zha.py +++ b/homeassistant/components/fan/zha.py @@ -10,7 +10,6 @@ from homeassistant.components import zha from homeassistant.components.fan import ( DOMAIN, FanEntity, SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH, SUPPORT_SET_SPEED) -from homeassistant.const import STATE_UNKNOWN DEPENDENCIES = ['zha'] @@ -72,7 +71,7 @@ class ZhaFan(zha.Entity, FanEntity): @property def is_on(self) -> bool: """Return true if entity is on.""" - if self._state == STATE_UNKNOWN: + if self._state is None: return False return self._state != SPEED_OFF @@ -103,7 +102,7 @@ class ZhaFan(zha.Entity, FanEntity): """Retrieve latest state.""" result = yield from zha.safe_read(self._endpoint.fan, ['fan_mode']) new_value = result.get('fan_mode', None) - self._state = VALUE_TO_SPEED.get(new_value, STATE_UNKNOWN) + self._state = VALUE_TO_SPEED.get(new_value, None) @property def should_poll(self) -> bool: diff --git a/homeassistant/components/light/zha.py b/homeassistant/components/light/zha.py index 8eb1b3dc9b6..b44bf820b23 100644 --- a/homeassistant/components/light/zha.py +++ b/homeassistant/components/light/zha.py @@ -6,7 +6,6 @@ at https://home-assistant.io/components/light.zha/ """ import logging from homeassistant.components import light, zha -from homeassistant.const import STATE_UNKNOWN import homeassistant.util.color as color_util _LOGGER = logging.getLogger(__name__) @@ -76,7 +75,7 @@ class Light(zha.Entity, light.Light): @property def is_on(self) -> bool: """Return true if entity is on.""" - if self._state == STATE_UNKNOWN: + if self._state is None: return False return bool(self._state) diff --git a/homeassistant/components/sensor/zha.py b/homeassistant/components/sensor/zha.py index 6979690708d..3ca908a679d 100644 --- a/homeassistant/components/sensor/zha.py +++ b/homeassistant/components/sensor/zha.py @@ -102,8 +102,8 @@ class TemperatureSensor(Sensor): @property def state(self): """Return the state of the entity.""" - if self._state == 'unknown': - return 'unknown' + if self._state is None: + return None celsius = round(float(self._state) / 100, 1) return convert_temperature( celsius, TEMP_CELSIUS, self.unit_of_measurement) @@ -122,8 +122,8 @@ class RelativeHumiditySensor(Sensor): @property def state(self): """Return the state of the entity.""" - if self._state == 'unknown': - return 'unknown' + if self._state is None: + return None return round(float(self._state) / 100, 1) @@ -139,7 +139,7 @@ class PressureSensor(Sensor): @property def state(self): """Return the state of the entity.""" - if self._state == 'unknown': - return 'unknown' + if self._state is None: + return None return round(float(self._state)) diff --git a/homeassistant/components/switch/zha.py b/homeassistant/components/switch/zha.py index 22eb50be86b..6109dc192f3 100644 --- a/homeassistant/components/switch/zha.py +++ b/homeassistant/components/switch/zha.py @@ -51,7 +51,7 @@ class Switch(zha.Entity, SwitchDevice): @property def is_on(self) -> bool: """Return if the switch is on based on the statemachine.""" - if self._state == 'unknown': + if self._state is None: return False return bool(self._state) diff --git a/homeassistant/components/zha/__init__.py b/homeassistant/components/zha/__init__.py index d293d4d07cd..238e89c07f0 100644 --- a/homeassistant/components/zha/__init__.py +++ b/homeassistant/components/zha/__init__.py @@ -319,7 +319,7 @@ class Entity(entity.Entity): self._endpoint = endpoint self._in_clusters = in_clusters self._out_clusters = out_clusters - self._state = ha_const.STATE_UNKNOWN + self._state = None self._unique_id = unique_id # Normally the entity itself is the listener. Sub-classes may set this