2019-02-19 13:09:06 +00:00
|
|
|
"""Support for INSTEON fans via PowerLinc Modem."""
|
2021-07-20 15:26:00 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-01-31 11:13:55 +00:00
|
|
|
import math
|
|
|
|
|
2019-02-19 13:09:06 +00:00
|
|
|
from homeassistant.components.fan import (
|
2020-08-11 23:04:44 +00:00
|
|
|
DOMAIN as FAN_DOMAIN,
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_SET_SPEED,
|
|
|
|
FanEntity,
|
|
|
|
)
|
2021-02-02 09:17:17 +00:00
|
|
|
from homeassistant.core import callback
|
2020-08-11 23:04:44 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-01-31 11:13:55 +00:00
|
|
|
from homeassistant.util.percentage import (
|
|
|
|
percentage_to_ranged_value,
|
|
|
|
ranged_value_to_percentage,
|
|
|
|
)
|
2019-02-19 13:09:06 +00:00
|
|
|
|
2020-08-11 23:04:44 +00:00
|
|
|
from .const import SIGNAL_ADD_ENTITIES
|
2020-01-30 09:47:44 +00:00
|
|
|
from .insteon_entity import InsteonEntity
|
2020-05-17 13:27:38 +00:00
|
|
|
from .utils import async_add_insteon_entities
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2021-03-12 05:35:24 +00:00
|
|
|
SPEED_RANGE = (1, 255) # off is not included
|
2018-02-25 19:13:39 +00:00
|
|
|
|
|
|
|
|
2020-08-11 23:04:44 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Insteon fans from a config entry."""
|
|
|
|
|
2021-02-02 09:17:17 +00:00
|
|
|
@callback
|
|
|
|
def async_add_insteon_fan_entities(discovery_info=None):
|
2020-08-11 23:04:44 +00:00
|
|
|
"""Add the Insteon entities for the platform."""
|
|
|
|
async_add_insteon_entities(
|
|
|
|
hass, FAN_DOMAIN, InsteonFanEntity, async_add_entities, discovery_info
|
|
|
|
)
|
|
|
|
|
|
|
|
signal = f"{SIGNAL_ADD_ENTITIES}_{FAN_DOMAIN}"
|
2021-02-02 09:17:17 +00:00
|
|
|
async_dispatcher_connect(hass, signal, async_add_insteon_fan_entities)
|
|
|
|
async_add_insteon_fan_entities()
|
2018-02-25 19:13:39 +00:00
|
|
|
|
|
|
|
|
2020-05-17 13:27:38 +00:00
|
|
|
class InsteonFanEntity(InsteonEntity, FanEntity):
|
|
|
|
"""An INSTEON fan entity."""
|
2018-02-25 19:13:39 +00:00
|
|
|
|
|
|
|
@property
|
2021-07-20 15:26:00 +00:00
|
|
|
def percentage(self) -> int | None:
|
2021-01-31 11:13:55 +00:00
|
|
|
"""Return the current speed percentage."""
|
|
|
|
if self._insteon_device_group.value is None:
|
|
|
|
return None
|
|
|
|
return ranged_value_to_percentage(SPEED_RANGE, self._insteon_device_group.value)
|
2018-02-25 19:13:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_SET_SPEED
|
|
|
|
|
2021-03-08 22:20:21 +00:00
|
|
|
@property
|
|
|
|
def speed_count(self) -> int:
|
|
|
|
"""Flag supported features."""
|
|
|
|
return 3
|
|
|
|
|
2021-01-27 23:44:36 +00:00
|
|
|
async def async_turn_on(
|
|
|
|
self,
|
|
|
|
speed: str = None,
|
|
|
|
percentage: int = None,
|
|
|
|
preset_mode: str = None,
|
|
|
|
**kwargs,
|
|
|
|
) -> None:
|
2020-05-17 13:27:38 +00:00
|
|
|
"""Turn on the fan."""
|
2021-03-08 22:20:21 +00:00
|
|
|
await self.async_set_percentage(percentage or 67)
|
2018-02-25 19:13:39 +00:00
|
|
|
|
2018-10-01 06:59:45 +00:00
|
|
|
async def async_turn_off(self, **kwargs) -> None:
|
2020-05-17 13:27:38 +00:00
|
|
|
"""Turn off the fan."""
|
|
|
|
await self._insteon_device.async_fan_off()
|
2018-02-25 19:13:39 +00:00
|
|
|
|
2021-01-31 11:13:55 +00:00
|
|
|
async def async_set_percentage(self, percentage: int) -> None:
|
|
|
|
"""Set the speed percentage of the fan."""
|
|
|
|
if percentage == 0:
|
2021-03-08 22:20:21 +00:00
|
|
|
await self.async_turn_off()
|
|
|
|
return
|
|
|
|
on_level = math.ceil(percentage_to_ranged_value(SPEED_RANGE, percentage))
|
|
|
|
await self._insteon_device.async_on(group=2, on_level=on_level)
|