2019-02-14 15:01:46 +00:00
|
|
|
"""Support for ISY994 fans."""
|
2021-01-28 09:23:55 +00:00
|
|
|
import math
|
2021-03-02 07:32:24 +00:00
|
|
|
from typing import Callable, Optional
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2021-02-19 07:05:09 +00:00
|
|
|
from pyisy.constants import ISY_VALUE_UNKNOWN, PROTO_INSTEON
|
2020-05-12 02:32:19 +00:00
|
|
|
|
2021-01-28 09:23:55 +00:00
|
|
|
from homeassistant.components.fan import DOMAIN as FAN, SUPPORT_SET_SPEED, FanEntity
|
2020-05-09 19:49:00 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
2021-01-28 09:23:55 +00:00
|
|
|
from homeassistant.util.percentage import (
|
2021-02-19 07:05:09 +00:00
|
|
|
int_states_in_range,
|
2021-01-28 09:23:55 +00:00
|
|
|
percentage_to_ranged_value,
|
|
|
|
ranged_value_to_percentage,
|
|
|
|
)
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
from .const import _LOGGER, DOMAIN as ISY994_DOMAIN, ISY994_NODES, ISY994_PROGRAMS
|
2020-05-05 04:03:12 +00:00
|
|
|
from .entity import ISYNodeEntity, ISYProgramEntity
|
2020-05-09 19:49:00 +00:00
|
|
|
from .helpers import migrate_old_unique_ids
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2021-01-28 09:23:55 +00:00
|
|
|
SPEED_RANGE = (1, 255) # off is not included
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistantType,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: Callable[[list], None],
|
|
|
|
) -> bool:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the ISY994 fan platform."""
|
2020-05-09 19:49:00 +00:00
|
|
|
hass_isy_data = hass.data[ISY994_DOMAIN][entry.entry_id]
|
2016-09-11 18:18:53 +00:00
|
|
|
devices = []
|
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
for node in hass_isy_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-09 19:49:00 +00:00
|
|
|
for name, status, actions in hass_isy_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
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
await migrate_old_unique_ids(hass, FAN, devices)
|
|
|
|
async_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
|
2021-03-02 07:32:24 +00:00
|
|
|
def percentage(self) -> Optional[int]:
|
2021-01-28 09:23:55 +00:00
|
|
|
"""Return the current speed percentage."""
|
|
|
|
if self._node.status == ISY_VALUE_UNKNOWN:
|
|
|
|
return None
|
|
|
|
return ranged_value_to_percentage(SPEED_RANGE, self._node.status)
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2021-02-19 07:05:09 +00:00
|
|
|
@property
|
|
|
|
def speed_count(self) -> int:
|
|
|
|
"""Return the number of speeds the fan supports."""
|
|
|
|
if self._node.protocol == PROTO_INSTEON:
|
|
|
|
return 3
|
|
|
|
return int_states_in_range(SPEED_RANGE)
|
|
|
|
|
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."""
|
2020-05-12 02:32:19 +00:00
|
|
|
if self._node.status == ISY_VALUE_UNKNOWN:
|
|
|
|
return None
|
|
|
|
return self._node.status != 0
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2021-01-28 09:23:55 +00:00
|
|
|
def set_percentage(self, percentage: int) -> None:
|
|
|
|
"""Set node to speed percentage for the ISY994 fan device."""
|
|
|
|
if percentage == 0:
|
|
|
|
self._node.turn_off()
|
|
|
|
return
|
|
|
|
|
|
|
|
isy_speed = math.ceil(percentage_to_ranged_value(SPEED_RANGE, percentage))
|
|
|
|
|
|
|
|
self._node.turn_on(val=isy_speed)
|
|
|
|
|
2021-01-27 23:44:36 +00:00
|
|
|
def turn_on(
|
|
|
|
self,
|
|
|
|
speed: str = None,
|
|
|
|
percentage: int = None,
|
|
|
|
preset_mode: str = None,
|
|
|
|
**kwargs,
|
|
|
|
) -> None:
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Send the turn on command to the ISY994 fan device."""
|
2021-01-28 09:23:55 +00:00
|
|
|
self.set_percentage(percentage)
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs) -> None:
|
|
|
|
"""Send the turn off command to the ISY994 fan device."""
|
2020-05-08 04:15:42 +00:00
|
|
|
self._node.turn_off()
|
2016-09-11 18:18:53 +00:00
|
|
|
|
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
|
2021-03-02 07:32:24 +00:00
|
|
|
def percentage(self) -> Optional[int]:
|
2021-01-28 09:23:55 +00:00
|
|
|
"""Return the current speed percentage."""
|
|
|
|
if self._node.status == ISY_VALUE_UNKNOWN:
|
|
|
|
return None
|
|
|
|
return ranged_value_to_percentage(SPEED_RANGE, self._node.status)
|
2020-05-05 04:03:12 +00:00
|
|
|
|
2021-02-19 07:05:09 +00:00
|
|
|
@property
|
|
|
|
def speed_count(self) -> int:
|
|
|
|
"""Return the number of speeds the fan supports."""
|
|
|
|
if self._node.protocol == PROTO_INSTEON:
|
|
|
|
return 3
|
|
|
|
return int_states_in_range(SPEED_RANGE)
|
|
|
|
|
2020-05-05 04:03:12 +00:00
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Get if the fan is on."""
|
2020-05-12 02:32:19 +00:00
|
|
|
return self._node.status != 0
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs) -> None:
|
|
|
|
"""Send the turn on command to ISY994 fan program."""
|
2020-05-08 04:15:42 +00:00
|
|
|
if not self._actions.run_then():
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Unable to turn off the fan")
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2021-01-27 23:44:36 +00:00
|
|
|
def turn_on(
|
|
|
|
self,
|
|
|
|
speed: str = None,
|
|
|
|
percentage: int = None,
|
|
|
|
preset_mode: str = None,
|
|
|
|
**kwargs,
|
|
|
|
) -> None:
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Send the turn off command to ISY994 fan program."""
|
2020-05-08 04:15:42 +00:00
|
|
|
if not self._actions.run_else():
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Unable to turn on the fan")
|