Use None as initial state in zha component (#14389)

* Return None if state is unknown

* Use None as initial state
This commit is contained in:
damarco 2018-05-12 14:41:44 +02:00 committed by Martin Hjelmare
parent b903bbc042
commit 01ce43ec7c
6 changed files with 12 additions and 14 deletions

View File

@ -108,7 +108,7 @@ class BinarySensor(zha.Entity, BinarySensorDevice):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return True if entity is on.""" """Return True if entity is on."""
if self._state == 'unknown': if self._state is None:
return False return False
return bool(self._state) return bool(self._state)

View File

@ -10,7 +10,6 @@ from homeassistant.components import zha
from homeassistant.components.fan import ( from homeassistant.components.fan import (
DOMAIN, FanEntity, SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH, DOMAIN, FanEntity, SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH,
SUPPORT_SET_SPEED) SUPPORT_SET_SPEED)
from homeassistant.const import STATE_UNKNOWN
DEPENDENCIES = ['zha'] DEPENDENCIES = ['zha']
@ -72,7 +71,7 @@ class ZhaFan(zha.Entity, FanEntity):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if entity is on.""" """Return true if entity is on."""
if self._state == STATE_UNKNOWN: if self._state is None:
return False return False
return self._state != SPEED_OFF return self._state != SPEED_OFF
@ -103,7 +102,7 @@ class ZhaFan(zha.Entity, FanEntity):
"""Retrieve latest state.""" """Retrieve latest state."""
result = yield from zha.safe_read(self._endpoint.fan, ['fan_mode']) result = yield from zha.safe_read(self._endpoint.fan, ['fan_mode'])
new_value = result.get('fan_mode', None) 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 @property
def should_poll(self) -> bool: def should_poll(self) -> bool:

View File

@ -6,7 +6,6 @@ at https://home-assistant.io/components/light.zha/
""" """
import logging import logging
from homeassistant.components import light, zha from homeassistant.components import light, zha
from homeassistant.const import STATE_UNKNOWN
import homeassistant.util.color as color_util import homeassistant.util.color as color_util
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -76,7 +75,7 @@ class Light(zha.Entity, light.Light):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if entity is on.""" """Return true if entity is on."""
if self._state == STATE_UNKNOWN: if self._state is None:
return False return False
return bool(self._state) return bool(self._state)

View File

@ -102,8 +102,8 @@ class TemperatureSensor(Sensor):
@property @property
def state(self): def state(self):
"""Return the state of the entity.""" """Return the state of the entity."""
if self._state == 'unknown': if self._state is None:
return 'unknown' return None
celsius = round(float(self._state) / 100, 1) celsius = round(float(self._state) / 100, 1)
return convert_temperature( return convert_temperature(
celsius, TEMP_CELSIUS, self.unit_of_measurement) celsius, TEMP_CELSIUS, self.unit_of_measurement)
@ -122,8 +122,8 @@ class RelativeHumiditySensor(Sensor):
@property @property
def state(self): def state(self):
"""Return the state of the entity.""" """Return the state of the entity."""
if self._state == 'unknown': if self._state is None:
return 'unknown' return None
return round(float(self._state) / 100, 1) return round(float(self._state) / 100, 1)
@ -139,7 +139,7 @@ class PressureSensor(Sensor):
@property @property
def state(self): def state(self):
"""Return the state of the entity.""" """Return the state of the entity."""
if self._state == 'unknown': if self._state is None:
return 'unknown' return None
return round(float(self._state)) return round(float(self._state))

View File

@ -51,7 +51,7 @@ class Switch(zha.Entity, SwitchDevice):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return if the switch is on based on the statemachine.""" """Return if the switch is on based on the statemachine."""
if self._state == 'unknown': if self._state is None:
return False return False
return bool(self._state) return bool(self._state)

View File

@ -319,7 +319,7 @@ class Entity(entity.Entity):
self._endpoint = endpoint self._endpoint = endpoint
self._in_clusters = in_clusters self._in_clusters = in_clusters
self._out_clusters = out_clusters self._out_clusters = out_clusters
self._state = ha_const.STATE_UNKNOWN self._state = None
self._unique_id = unique_id self._unique_id = unique_id
# Normally the entity itself is the listener. Sub-classes may set this # Normally the entity itself is the listener. Sub-classes may set this