2019-04-03 15:40:03 +00:00
|
|
|
"""Fans on Zigbee Home Automation networks."""
|
2019-12-31 16:09:58 +00:00
|
|
|
import functools
|
2020-12-05 23:24:49 +00:00
|
|
|
from typing import List, Optional
|
2020-03-27 02:19:48 +00:00
|
|
|
|
2020-04-11 16:01:49 +00:00
|
|
|
from zigpy.exceptions import ZigbeeException
|
2020-03-27 02:19:48 +00:00
|
|
|
import zigpy.zcl.clusters.hvac as hvac
|
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,
|
|
|
|
)
|
2020-03-27 02:19:48 +00:00
|
|
|
from homeassistant.const import STATE_UNAVAILABLE
|
2020-03-29 00:38:48 +00:00
|
|
|
from homeassistant.core import State, callback
|
2018-12-04 10:38:57 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2019-08-02 10:05:23 +00:00
|
|
|
|
2020-02-21 23:06:57 +00:00
|
|
|
from .core import discovery
|
2019-01-26 13:54:49 +00:00
|
|
|
from .core.const import (
|
2019-08-02 14:37:21 +00:00
|
|
|
CHANNEL_FAN,
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_ZHA,
|
|
|
|
DATA_ZHA_DISPATCHERS,
|
2020-02-21 23:06:57 +00:00
|
|
|
SIGNAL_ADD_ENTITIES,
|
2019-07-31 19:25:30 +00:00
|
|
|
SIGNAL_ATTR_UPDATED,
|
2019-02-06 18:33:21 +00:00
|
|
|
)
|
2019-12-31 16:09:58 +00:00
|
|
|
from .core.registries import ZHA_ENTITIES
|
2020-03-29 00:38:48 +00:00
|
|
|
from .entity import ZhaEntity, ZhaGroupEntity
|
2018-03-18 16:17:56 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
]
|
|
|
|
|
2019-10-07 15:17:39 +00:00
|
|
|
VALUE_TO_SPEED = dict(enumerate(SPEED_LIST))
|
2018-03-18 16:17:56 +00:00
|
|
|
SPEED_TO_VALUE = {speed: i for i, speed in enumerate(SPEED_LIST)}
|
2019-12-31 16:09:58 +00:00
|
|
|
STRICT_MATCH = functools.partial(ZHA_ENTITIES.strict_match, DOMAIN)
|
2020-03-27 02:19:48 +00:00
|
|
|
GROUP_MATCH = functools.partial(ZHA_ENTITIES.group_match, DOMAIN)
|
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."""
|
2020-03-21 17:25:43 +00:00
|
|
|
entities_to_create = hass.data[DATA_ZHA][DOMAIN]
|
2018-11-27 20:21:25 +00:00
|
|
|
|
|
|
|
unsub = async_dispatcher_connect(
|
2020-02-21 23:06:57 +00:00
|
|
|
hass,
|
|
|
|
SIGNAL_ADD_ENTITIES,
|
|
|
|
functools.partial(
|
2020-12-05 23:24:49 +00:00
|
|
|
discovery.async_add_entities,
|
|
|
|
async_add_entities,
|
|
|
|
entities_to_create,
|
|
|
|
update_before_add=False,
|
2020-02-21 23:06:57 +00:00
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-11-27 20:21:25 +00:00
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_DISPATCHERS].append(unsub)
|
|
|
|
|
2019-12-31 16:09:58 +00:00
|
|
|
|
2020-03-29 00:38:48 +00:00
|
|
|
class BaseFan(FanEntity):
|
2020-03-27 02:19:48 +00:00
|
|
|
"""Base representation of a ZHA fan."""
|
2019-03-10 04:09:09 +00:00
|
|
|
|
2020-03-27 02:19:48 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
"""Initialize the fan."""
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self._state = None
|
|
|
|
self._fan_channel = None
|
2018-03-18 16:17:56 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
2019-02-06 18:33:21 +00:00
|
|
|
@property
|
2020-03-27 02:19:48 +00:00
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_SET_SPEED
|
2019-02-06 18:33:21 +00:00
|
|
|
|
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])
|
2020-03-04 18:11:53 +00:00
|
|
|
self.async_set_state(0, "fan_mode", speed)
|
2019-03-10 04:09:09 +00:00
|
|
|
|
2020-03-29 00:38:48 +00:00
|
|
|
@callback
|
|
|
|
def async_set_state(self, attr_id, attr_name, value):
|
|
|
|
"""Handle state update from channel."""
|
|
|
|
|
2020-03-27 02:19:48 +00:00
|
|
|
|
|
|
|
@STRICT_MATCH(channel_names=CHANNEL_FAN)
|
2020-03-29 00:38:48 +00:00
|
|
|
class ZhaFan(BaseFan, ZhaEntity):
|
2020-03-27 02:19:48 +00:00
|
|
|
"""Representation of a ZHA fan."""
|
|
|
|
|
|
|
|
def __init__(self, unique_id, zha_device, channels, **kwargs):
|
|
|
|
"""Init this sensor."""
|
|
|
|
super().__init__(unique_id, zha_device, channels, **kwargs)
|
|
|
|
self._fan_channel = self.cluster_channels.get(CHANNEL_FAN)
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Run when about to be added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
2020-07-20 14:04:57 +00:00
|
|
|
self.async_accept_signal(
|
2020-03-27 02:19:48 +00:00
|
|
|
self._fan_channel, SIGNAL_ATTR_UPDATED, self.async_set_state
|
|
|
|
)
|
|
|
|
|
2020-12-05 23:24:49 +00:00
|
|
|
@property
|
|
|
|
def speed(self) -> Optional[str]:
|
|
|
|
"""Return the current speed."""
|
|
|
|
return VALUE_TO_SPEED.get(self._fan_channel.fan_mode)
|
2020-03-27 02:19:48 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_set_state(self, attr_id, attr_name, value):
|
|
|
|
"""Handle state update from channel."""
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
|
|
|
|
@GROUP_MATCH()
|
2020-03-29 00:38:48 +00:00
|
|
|
class FanGroup(BaseFan, ZhaGroupEntity):
|
2020-03-27 02:19:48 +00:00
|
|
|
"""Representation of a fan group."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, entity_ids: List[str], unique_id: str, group_id: int, zha_device, **kwargs
|
|
|
|
) -> None:
|
|
|
|
"""Initialize a fan group."""
|
2020-03-29 00:38:48 +00:00
|
|
|
super().__init__(entity_ids, unique_id, group_id, zha_device, **kwargs)
|
2020-03-27 02:19:48 +00:00
|
|
|
self._available: bool = False
|
|
|
|
group = self.zha_device.gateway.get_group(self._group_id)
|
|
|
|
self._fan_channel = group.endpoint[hvac.Fan.cluster_id]
|
|
|
|
|
|
|
|
# what should we do with this hack?
|
|
|
|
async def async_set_speed(value) -> None:
|
|
|
|
"""Set the speed of the fan."""
|
|
|
|
try:
|
|
|
|
await self._fan_channel.write_attributes({"fan_mode": value})
|
2020-04-11 16:01:49 +00:00
|
|
|
except ZigbeeException as ex:
|
2020-03-27 02:19:48 +00:00
|
|
|
self.error("Could not set speed: %s", ex)
|
|
|
|
return
|
|
|
|
|
|
|
|
self._fan_channel.async_set_speed = async_set_speed
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Attempt to retrieve on off state from the fan."""
|
|
|
|
all_states = [self.hass.states.get(x) for x in self._entity_ids]
|
|
|
|
states: List[State] = list(filter(None, all_states))
|
|
|
|
on_states: List[State] = [state for state in states if state.state != SPEED_OFF]
|
2020-12-05 23:24:49 +00:00
|
|
|
|
2020-03-27 02:19:48 +00:00
|
|
|
self._available = any(state.state != STATE_UNAVAILABLE for state in states)
|
|
|
|
# for now just use first non off state since its kind of arbitrary
|
|
|
|
if not on_states:
|
|
|
|
self._state = SPEED_OFF
|
|
|
|
else:
|
2020-12-05 23:24:49 +00:00
|
|
|
self._state = on_states[0].state
|
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Run when about to be added to hass."""
|
|
|
|
await self.async_update()
|
|
|
|
await super().async_added_to_hass()
|