Skip to content

Commit

Permalink
Fake the state for a short period and skip the next update. (#12446)
Browse files Browse the repository at this point in the history
On state change the device doesn't provide the new state immediately.

Flag the device as unavailable if no communication is possible.
  • Loading branch information
syssi authored and balloob committed Feb 16, 2018
1 parent 8d48272 commit 1f041d5
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions homeassistant/components/fan/xiaomi_miio.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from homeassistant.helpers.entity import ToggleEntity
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,
ATTR_ENTITY_ID, )
from homeassistant.exceptions import PlatformNotReady
Expand Down Expand Up @@ -167,6 +167,7 @@ def __init__(self, name, air_purifier):
ATTR_AVERAGE_AIR_QUALITY_INDEX: None,
ATTR_PURIFY_VOLUME: None,
}
self._skip_update = False

@property
def supported_features(self):
Expand Down Expand Up @@ -218,23 +219,35 @@ def async_turn_on(self: ToggleEntity, speed: str = None, **kwargs) -> None:
"""Turn the fan on."""
if speed:
# If operation mode was set the device must not be turned on.
yield from self.async_set_speed(speed)
return
result = yield from self.async_set_speed(speed)
else:
result = yield from self._try_command(
"Turning the air purifier on failed.", self._air_purifier.on)

yield from self._try_command(
"Turning the air purifier on failed.", self._air_purifier.on)
if result:
self._state = True
self._skip_update = True

@asyncio.coroutine
def async_turn_off(self: ToggleEntity, **kwargs) -> None:
"""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)

if result:
self._state = False
self._skip_update = True

@asyncio.coroutine
def async_update(self):
"""Fetch state from the device."""
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:
state = yield from self.hass.async_add_job(
self._air_purifier.status)
Expand Down Expand Up @@ -262,6 +275,7 @@ def async_update(self):
ATTR_LED_BRIGHTNESS] = state.led_brightness.value

except DeviceException as ex:
self._state = None
_LOGGER.error("Got exception while fetching the state: %s", ex)

@property
Expand Down

0 comments on commit 1f041d5

Please sign in to comment.