2019-02-14 15:01:46 +00:00
|
|
|
"""Support for ISY994 fans."""
|
2016-09-11 18:18:53 +00:00
|
|
|
from typing import Callable
|
|
|
|
|
2019-02-14 15:01:46 +00:00
|
|
|
from homeassistant.components.fan import (
|
2020-05-05 00:21:40 +00:00
|
|
|
DOMAIN as FAN,
|
2019-07-31 19:25:30 +00:00
|
|
|
SPEED_HIGH,
|
|
|
|
SPEED_LOW,
|
|
|
|
SPEED_MEDIUM,
|
|
|
|
SPEED_OFF,
|
|
|
|
SUPPORT_SET_SPEED,
|
|
|
|
FanEntity,
|
|
|
|
)
|
2016-09-11 18:18:53 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
|
2020-05-05 04:03:12 +00:00
|
|
|
from . import ISY994_NODES, ISY994_PROGRAMS
|
2020-05-05 00:21:40 +00:00
|
|
|
from .const import _LOGGER
|
2020-05-05 04:03:12 +00:00
|
|
|
from .entity import ISYNodeEntity, ISYProgramEntity
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
VALUE_TO_STATE = {
|
|
|
|
0: SPEED_OFF,
|
|
|
|
63: SPEED_LOW,
|
|
|
|
64: SPEED_LOW,
|
2017-01-21 06:21:28 +00:00
|
|
|
190: SPEED_MEDIUM,
|
|
|
|
191: SPEED_MEDIUM,
|
2016-09-11 18:18:53 +00:00
|
|
|
255: SPEED_HIGH,
|
|
|
|
}
|
|
|
|
|
|
|
|
STATE_TO_VALUE = {}
|
|
|
|
for key in VALUE_TO_STATE:
|
|
|
|
STATE_TO_VALUE[VALUE_TO_STATE[key]] = key
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass, config: ConfigType, add_entities: Callable[[list], None], discovery_info=None
|
|
|
|
):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the ISY994 fan platform."""
|
2016-09-11 18:18:53 +00:00
|
|
|
devices = []
|
|
|
|
|
2020-05-05 00:21:40 +00:00
|
|
|
for node in hass.data[ISY994_NODES][FAN]:
|
2020-05-05 04:03:12 +00:00
|
|
|
devices.append(ISYFanEntity(node))
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2020-05-05 00:21:40 +00:00
|
|
|
for name, status, actions in hass.data[ISY994_PROGRAMS][FAN]:
|
2020-05-05 04:03:12 +00:00
|
|
|
devices.append(ISYFanProgramEntity(name, status, actions))
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(devices)
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
|
2020-05-05 04:03:12 +00:00
|
|
|
class ISYFanEntity(ISYNodeEntity, FanEntity):
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Representation of an ISY994 fan device."""
|
|
|
|
|
2017-01-14 06:08:13 +00:00
|
|
|
@property
|
|
|
|
def speed(self) -> str:
|
|
|
|
"""Return the current speed."""
|
2017-10-12 07:36:24 +00:00
|
|
|
return VALUE_TO_STATE.get(self.value)
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
@property
|
2017-12-26 08:26:37 +00:00
|
|
|
def is_on(self) -> bool:
|
2017-10-12 07:36:24 +00:00
|
|
|
"""Get if the fan is on."""
|
|
|
|
return self.value != 0
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
def set_speed(self, speed: str) -> None:
|
|
|
|
"""Send the set speed command to the ISY994 fan device."""
|
2017-10-12 07:36:24 +00:00
|
|
|
self._node.on(val=STATE_TO_VALUE.get(speed, 255))
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def turn_on(self, speed: str = None, **kwargs) -> None:
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Send the turn on command to the ISY994 fan device."""
|
|
|
|
self.set_speed(speed)
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs) -> None:
|
|
|
|
"""Send the turn off command to the ISY994 fan device."""
|
2017-10-12 07:36:24 +00:00
|
|
|
self._node.off()
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2017-08-19 19:17:47 +00:00
|
|
|
@property
|
|
|
|
def speed_list(self) -> list:
|
|
|
|
"""Get the list of available speeds."""
|
|
|
|
return [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
|
|
|
|
|
2017-12-26 08:26:37 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_SET_SPEED
|
|
|
|
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2020-05-05 04:03:12 +00:00
|
|
|
class ISYFanProgramEntity(ISYProgramEntity, FanEntity):
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Representation of an ISY994 fan program."""
|
|
|
|
|
2020-05-05 04:03:12 +00:00
|
|
|
@property
|
|
|
|
def speed(self) -> str:
|
|
|
|
"""Return the current speed."""
|
|
|
|
# TEMPORARY: Cast value to int until PyISYv2.
|
|
|
|
return VALUE_TO_STATE.get(int(self.value))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Get if the fan is on."""
|
|
|
|
# TEMPORARY: Cast value to int until PyISYv2.
|
|
|
|
return int(self.value) != 0
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs) -> None:
|
|
|
|
"""Send the turn on command to ISY994 fan program."""
|
|
|
|
if not self._actions.runThen():
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Unable to turn off the fan")
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def turn_on(self, speed: str = None, **kwargs) -> None:
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Send the turn off command to ISY994 fan program."""
|
|
|
|
if not self._actions.runElse():
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Unable to turn on the fan")
|