2019-04-03 15:40:03 +00:00
|
|
|
"""Fans on Zigbee Home Automation networks."""
|
2018-03-18 16:17:56 +00:00
|
|
|
import logging
|
2018-12-04 10:38:57 +00:00
|
|
|
|
2019-03-10 04:09:09 +00:00
|
|
|
from homeassistant.core import callback
|
2018-12-04 10:38:57 +00:00
|
|
|
from homeassistant.components.fan import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
|
|
|
SPEED_HIGH,
|
|
|
|
SPEED_LOW,
|
|
|
|
SPEED_MEDIUM,
|
|
|
|
SPEED_OFF,
|
|
|
|
SUPPORT_SET_SPEED,
|
|
|
|
FanEntity,
|
|
|
|
)
|
2018-12-04 10:38:57 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2019-01-26 13:54:49 +00:00
|
|
|
from .core.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_ZHA,
|
|
|
|
DATA_ZHA_DISPATCHERS,
|
|
|
|
ZHA_DISCOVERY_NEW,
|
|
|
|
FAN_CHANNEL,
|
|
|
|
SIGNAL_ATTR_UPDATED,
|
2019-02-06 18:33:21 +00:00
|
|
|
)
|
2019-01-26 13:54:49 +00:00
|
|
|
from .entity import ZhaEntity
|
2018-03-18 16:17:56 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
# Additional speeds in zigbee's ZCL
|
|
|
|
# Spec is unclear as to what this value means. On King Of Fans HBUniversal
|
|
|
|
# receiver, this means Very High.
|
2019-07-31 19:25:30 +00:00
|
|
|
SPEED_ON = "on"
|
2018-03-18 16:17:56 +00:00
|
|
|
# The fan speed is self-regulated
|
2019-07-31 19:25:30 +00:00
|
|
|
SPEED_AUTO = "auto"
|
2018-03-18 16:17:56 +00:00
|
|
|
# When the heated/cooled space is occupied, the fan is always on
|
2019-07-31 19:25:30 +00:00
|
|
|
SPEED_SMART = "smart"
|
2018-03-18 16:17:56 +00:00
|
|
|
|
|
|
|
SPEED_LIST = [
|
|
|
|
SPEED_OFF,
|
|
|
|
SPEED_LOW,
|
|
|
|
SPEED_MEDIUM,
|
|
|
|
SPEED_HIGH,
|
|
|
|
SPEED_ON,
|
|
|
|
SPEED_AUTO,
|
2019-07-31 19:25:30 +00:00
|
|
|
SPEED_SMART,
|
2018-03-18 16:17:56 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
VALUE_TO_SPEED = {i: speed for i, speed in enumerate(SPEED_LIST)}
|
|
|
|
SPEED_TO_VALUE = {speed: i for i, speed in enumerate(SPEED_LIST)}
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2018-11-27 20:21:25 +00:00
|
|
|
"""Old way of setting up Zigbee Home Automation fans."""
|
|
|
|
pass
|
2018-03-18 16:17:56 +00:00
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Zigbee Home Automation fan from config entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
async def async_discover(discovery_info):
|
2019-07-31 19:25:30 +00:00
|
|
|
await _async_setup_entities(
|
|
|
|
hass, config_entry, async_add_entities, [discovery_info]
|
|
|
|
)
|
2018-11-27 20:21:25 +00:00
|
|
|
|
|
|
|
unsub = async_dispatcher_connect(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, ZHA_DISCOVERY_NEW.format(DOMAIN), async_discover
|
|
|
|
)
|
2018-11-27 20:21:25 +00:00
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_DISPATCHERS].append(unsub)
|
|
|
|
|
|
|
|
fans = hass.data.get(DATA_ZHA, {}).get(DOMAIN)
|
|
|
|
if fans is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
await _async_setup_entities(
|
|
|
|
hass, config_entry, async_add_entities, fans.values()
|
|
|
|
)
|
2018-11-27 20:21:25 +00:00
|
|
|
del hass.data[DATA_ZHA][DOMAIN]
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def _async_setup_entities(
|
|
|
|
hass, config_entry, async_add_entities, discovery_infos
|
|
|
|
):
|
2018-11-27 20:21:25 +00:00
|
|
|
"""Set up the ZHA fans."""
|
|
|
|
entities = []
|
|
|
|
for discovery_info in discovery_infos:
|
2018-12-23 19:47:06 +00:00
|
|
|
entities.append(ZhaFan(**discovery_info))
|
2018-11-27 20:21:25 +00:00
|
|
|
|
|
|
|
async_add_entities(entities, update_before_add=True)
|
2018-03-18 16:17:56 +00:00
|
|
|
|
|
|
|
|
2018-11-22 18:00:46 +00:00
|
|
|
class ZhaFan(ZhaEntity, FanEntity):
|
2018-03-18 16:17:56 +00:00
|
|
|
"""Representation of a ZHA fan."""
|
|
|
|
|
|
|
|
_domain = DOMAIN
|
2018-12-19 13:52:20 +00:00
|
|
|
|
2019-02-19 17:58:22 +00:00
|
|
|
def __init__(self, unique_id, zha_device, channels, **kwargs):
|
2019-02-06 18:33:21 +00:00
|
|
|
"""Init this sensor."""
|
2019-02-19 17:58:22 +00:00
|
|
|
super().__init__(unique_id, zha_device, channels, **kwargs)
|
|
|
|
self._fan_channel = self.cluster_channels.get(FAN_CHANNEL)
|
2018-12-19 13:52:20 +00:00
|
|
|
|
2019-02-06 18:33:21 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Run when about to be added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
await self.async_accept_signal(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._fan_channel, SIGNAL_ATTR_UPDATED, self.async_set_state
|
|
|
|
)
|
2018-03-18 16:17:56 +00:00
|
|
|
|
2019-03-10 04:09:09 +00:00
|
|
|
@callback
|
|
|
|
def async_restore_last_state(self, last_state):
|
|
|
|
"""Restore previous state."""
|
|
|
|
self._state = VALUE_TO_SPEED.get(last_state.state, last_state.state)
|
|
|
|
|
2018-03-18 16:17:56 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_SET_SPEED
|
|
|
|
|
|
|
|
@property
|
|
|
|
def speed_list(self) -> list:
|
|
|
|
"""Get the list of available speeds."""
|
|
|
|
return SPEED_LIST
|
|
|
|
|
|
|
|
@property
|
|
|
|
def speed(self) -> str:
|
|
|
|
"""Return the current speed."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if entity is on."""
|
2018-05-12 12:41:44 +00:00
|
|
|
if self._state is None:
|
2018-03-18 16:17:56 +00:00
|
|
|
return False
|
|
|
|
return self._state != SPEED_OFF
|
|
|
|
|
2019-02-06 18:33:21 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return state attributes."""
|
|
|
|
return self.state_attributes
|
|
|
|
|
|
|
|
def async_set_state(self, state):
|
2019-02-19 17:58:22 +00:00
|
|
|
"""Handle state update from channel."""
|
2019-02-06 18:33:21 +00:00
|
|
|
self._state = VALUE_TO_SPEED.get(state, self._state)
|
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
2018-09-12 10:43:06 +00:00
|
|
|
async def async_turn_on(self, speed: str = None, **kwargs) -> None:
|
2018-03-18 16:17:56 +00:00
|
|
|
"""Turn the entity on."""
|
|
|
|
if speed is None:
|
|
|
|
speed = SPEED_MEDIUM
|
|
|
|
|
2018-09-12 10:43:06 +00:00
|
|
|
await self.async_set_speed(speed)
|
2018-03-18 16:17:56 +00:00
|
|
|
|
2018-09-12 10:43:06 +00:00
|
|
|
async def async_turn_off(self, **kwargs) -> None:
|
2018-03-18 16:17:56 +00:00
|
|
|
"""Turn the entity off."""
|
2018-09-12 10:43:06 +00:00
|
|
|
await self.async_set_speed(SPEED_OFF)
|
2018-03-18 16:17:56 +00:00
|
|
|
|
2018-09-12 10:43:06 +00:00
|
|
|
async def async_set_speed(self, speed: str) -> None:
|
2018-03-18 16:17:56 +00:00
|
|
|
"""Set the speed of the fan."""
|
2019-02-19 17:58:22 +00:00
|
|
|
await self._fan_channel.async_set_speed(SPEED_TO_VALUE[speed])
|
2019-02-06 18:33:21 +00:00
|
|
|
self.async_set_state(speed)
|
2019-03-10 04:09:09 +00:00
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Attempt to retrieve on off state from the fan."""
|
|
|
|
await super().async_update()
|
|
|
|
if self._fan_channel:
|
2019-07-31 19:25:30 +00:00
|
|
|
state = await self._fan_channel.get_attribute_value("fan_mode")
|
2019-03-10 04:09:09 +00:00
|
|
|
if state is not None:
|
|
|
|
self._state = VALUE_TO_SPEED.get(state, self._state)
|