Fake the state for a short period and skip the next update. (#12446)

On state change the device doesn't provide the new state immediately.

Flag the device as unavailable if no communication is possible.
This commit is contained in:
Sebastian Muszynski 2018-02-16 07:01:34 +01:00 committed by Paulus Schoutsen
parent 8d48272cbd
commit 1f041d54d9

View File

@ -12,7 +12,7 @@ import voluptuous as vol
from homeassistant.helpers.entity import ToggleEntity from homeassistant.helpers.entity import ToggleEntity
from homeassistant.components.fan import (FanEntity, PLATFORM_SCHEMA, from homeassistant.components.fan import (FanEntity, PLATFORM_SCHEMA,
SUPPORT_SET_SPEED, DOMAIN) SUPPORT_SET_SPEED, DOMAIN, )
from homeassistant.const import (CONF_NAME, CONF_HOST, CONF_TOKEN, from homeassistant.const import (CONF_NAME, CONF_HOST, CONF_TOKEN,
ATTR_ENTITY_ID, ) ATTR_ENTITY_ID, )
from homeassistant.exceptions import PlatformNotReady from homeassistant.exceptions import PlatformNotReady
@ -167,6 +167,7 @@ class XiaomiAirPurifier(FanEntity):
ATTR_AVERAGE_AIR_QUALITY_INDEX: None, ATTR_AVERAGE_AIR_QUALITY_INDEX: None,
ATTR_PURIFY_VOLUME: None, ATTR_PURIFY_VOLUME: None,
} }
self._skip_update = False
@property @property
def supported_features(self): def supported_features(self):
@ -218,23 +219,35 @@ class XiaomiAirPurifier(FanEntity):
"""Turn the fan on.""" """Turn the fan on."""
if speed: if speed:
# If operation mode was set the device must not be turned on. # If operation mode was set the device must not be turned on.
yield from self.async_set_speed(speed) result = yield from self.async_set_speed(speed)
return else:
result = yield from self._try_command(
yield from self._try_command(
"Turning the air purifier on failed.", self._air_purifier.on) "Turning the air purifier on failed.", self._air_purifier.on)
if result:
self._state = True
self._skip_update = True
@asyncio.coroutine @asyncio.coroutine
def async_turn_off(self: ToggleEntity, **kwargs) -> None: def async_turn_off(self: ToggleEntity, **kwargs) -> None:
"""Turn the fan off.""" """Turn the fan off."""
yield from self._try_command( result = yield from self._try_command(
"Turning the air purifier off failed.", self._air_purifier.off) "Turning the air purifier off failed.", self._air_purifier.off)
if result:
self._state = False
self._skip_update = True
@asyncio.coroutine @asyncio.coroutine
def async_update(self): def async_update(self):
"""Fetch state from the device.""" """Fetch state from the device."""
from miio import DeviceException from miio import DeviceException
# On state change the device doesn't provide the new state immediately.
if self._skip_update:
self._skip_update = False
return
try: try:
state = yield from self.hass.async_add_job( state = yield from self.hass.async_add_job(
self._air_purifier.status) self._air_purifier.status)
@ -262,6 +275,7 @@ class XiaomiAirPurifier(FanEntity):
ATTR_LED_BRIGHTNESS] = state.led_brightness.value ATTR_LED_BRIGHTNESS] = state.led_brightness.value
except DeviceException as ex: except DeviceException as ex:
self._state = None
_LOGGER.error("Got exception while fetching the state: %s", ex) _LOGGER.error("Got exception while fetching the state: %s", ex)
@property @property