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
at https://home-assistant.io/components/fan.zha/
"""
import asyncio
import logging
from homeassistant.components import zha
from homeassistant.components.fan import (
@ -38,9 +37,8 @@ VALUE_TO_SPEED = {i: speed for i, speed in enumerate(SPEED_LIST)}
SPEED_TO_VALUE = {speed: i for i, speed in enumerate(SPEED_LIST)}
@asyncio.coroutine
def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up the Zigbee Home Automation fans."""
discovery_info = zha.get_discovery_info(hass, discovery_info)
if discovery_info is None:
@ -76,32 +74,34 @@ class ZhaFan(zha.Entity, FanEntity):
return False
return self._state != SPEED_OFF
@asyncio.coroutine
def async_turn_on(self, speed: str = None, **kwargs) -> None:
async def async_turn_on(self, speed: str = None, **kwargs) -> None:
"""Turn the entity on."""
if speed is None:
speed = SPEED_MEDIUM
yield from self.async_set_speed(speed)
await self.async_set_speed(speed)
@asyncio.coroutine
def async_turn_off(self, **kwargs) -> None:
async def async_turn_off(self, **kwargs) -> None:
"""Turn the entity off."""
yield from self.async_set_speed(SPEED_OFF)
await self.async_set_speed(SPEED_OFF)
@asyncio.coroutine
def async_set_speed(self, speed: str) -> None:
async def async_set_speed(self, speed: str) -> None:
"""Set the speed of the fan."""
yield from self._endpoint.fan.write_attributes({
'fan_mode': SPEED_TO_VALUE[speed]})
from zigpy.exceptions import DeliveryError
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.async_schedule_update_ha_state()
@asyncio.coroutine
def async_update(self):
async def async_update(self):
"""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)
self._state = VALUE_TO_SPEED.get(new_value, None)