Update fan.zha platform. (#16551)

* Update fan.zha platform.

switch from asyncio to async def()
catch DeliveryError exceptions
keep previous state, if reading 'fan_mode' attribute fails

* fan.zha: Use None for unknown state.

if we fail to read 'fan_mode' attribute, use None for state
This commit is contained in:
Alexei Chetroi 2018-09-12 06:43:06 -04:00 committed by Pascal Vizeli
parent 117ea9e553
commit 501f2b0a93

View File

@ -4,7 +4,6 @@ Fans on Zigbee Home Automation networks.
For more details on this platform, please refer to the documentation For more details on this platform, please refer to the documentation
at https://home-assistant.io/components/fan.zha/ at https://home-assistant.io/components/fan.zha/
""" """
import asyncio
import logging import logging
from homeassistant.components import zha from homeassistant.components import zha
from homeassistant.components.fan import ( from homeassistant.components.fan import (
@ -38,8 +37,7 @@ VALUE_TO_SPEED = {i: speed for i, speed in enumerate(SPEED_LIST)}
SPEED_TO_VALUE = {speed: i for i, speed in enumerate(SPEED_LIST)} SPEED_TO_VALUE = {speed: i for i, speed in enumerate(SPEED_LIST)}
@asyncio.coroutine async def async_setup_platform(hass, config, async_add_entities,
def async_setup_platform(hass, config, async_add_entities,
discovery_info=None): discovery_info=None):
"""Set up the Zigbee Home Automation fans.""" """Set up the Zigbee Home Automation fans."""
discovery_info = zha.get_discovery_info(hass, discovery_info) discovery_info = zha.get_discovery_info(hass, discovery_info)
@ -76,32 +74,34 @@ class ZhaFan(zha.Entity, FanEntity):
return False return False
return self._state != SPEED_OFF return self._state != SPEED_OFF
@asyncio.coroutine async def async_turn_on(self, speed: str = None, **kwargs) -> None:
def async_turn_on(self, speed: str = None, **kwargs) -> None:
"""Turn the entity on.""" """Turn the entity on."""
if speed is None: if speed is None:
speed = SPEED_MEDIUM speed = SPEED_MEDIUM
yield from self.async_set_speed(speed) await self.async_set_speed(speed)
@asyncio.coroutine async def async_turn_off(self, **kwargs) -> None:
def async_turn_off(self, **kwargs) -> None:
"""Turn the entity off.""" """Turn the entity off."""
yield from self.async_set_speed(SPEED_OFF) await self.async_set_speed(SPEED_OFF)
@asyncio.coroutine async def async_set_speed(self, speed: str) -> None:
def async_set_speed(self, speed: str) -> None:
"""Set the speed of the fan.""" """Set the speed of the fan."""
yield from self._endpoint.fan.write_attributes({ from zigpy.exceptions import DeliveryError
'fan_mode': SPEED_TO_VALUE[speed]}) try:
await self._endpoint.fan.write_attributes(
{'fan_mode': SPEED_TO_VALUE[speed]}
)
except DeliveryError as ex:
_LOGGER.error("%s: Could not set speed: %s", self.entity_id, ex)
return
self._state = speed self._state = speed
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state()
@asyncio.coroutine async def async_update(self):
def async_update(self):
"""Retrieve latest state.""" """Retrieve latest state."""
result = yield from zha.safe_read(self._endpoint.fan, ['fan_mode']) result = await 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, None) self._state = VALUE_TO_SPEED.get(new_value, None)