2021-03-23 21:29:55 +00:00
|
|
|
"""Support for deCONZ fans."""
|
2021-03-25 20:09:06 +00:00
|
|
|
from __future__ import annotations
|
2021-03-23 21:29:55 +00:00
|
|
|
|
2021-11-16 11:39:51 +00:00
|
|
|
from collections.abc import ValuesView
|
|
|
|
from typing import Any
|
|
|
|
|
2021-10-02 07:09:26 +00:00
|
|
|
from pydeconz.light import (
|
|
|
|
FAN_SPEED_25_PERCENT,
|
|
|
|
FAN_SPEED_50_PERCENT,
|
|
|
|
FAN_SPEED_75_PERCENT,
|
|
|
|
FAN_SPEED_100_PERCENT,
|
|
|
|
FAN_SPEED_OFF,
|
|
|
|
Fan,
|
|
|
|
)
|
2021-09-29 19:19:21 +00:00
|
|
|
|
2020-10-13 10:57:42 +00:00
|
|
|
from homeassistant.components.fan import (
|
|
|
|
DOMAIN,
|
|
|
|
SPEED_HIGH,
|
|
|
|
SPEED_LOW,
|
|
|
|
SPEED_MEDIUM,
|
|
|
|
SPEED_OFF,
|
|
|
|
SUPPORT_SET_SPEED,
|
|
|
|
FanEntity,
|
|
|
|
)
|
2021-11-16 11:39:51 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2020-10-13 10:57:42 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-11-16 11:39:51 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-03-23 21:29:55 +00:00
|
|
|
from homeassistant.util.percentage import (
|
|
|
|
ordered_list_item_to_percentage,
|
|
|
|
percentage_to_ordered_list_item,
|
|
|
|
)
|
2020-10-13 10:57:42 +00:00
|
|
|
|
|
|
|
from .deconz_device import DeconzDevice
|
2021-11-16 11:39:51 +00:00
|
|
|
from .gateway import DeconzGateway, get_gateway_from_config_entry
|
2020-10-13 10:57:42 +00:00
|
|
|
|
2021-10-02 07:09:26 +00:00
|
|
|
ORDERED_NAMED_FAN_SPEEDS = [
|
|
|
|
FAN_SPEED_25_PERCENT,
|
|
|
|
FAN_SPEED_50_PERCENT,
|
|
|
|
FAN_SPEED_75_PERCENT,
|
|
|
|
FAN_SPEED_100_PERCENT,
|
|
|
|
]
|
|
|
|
|
|
|
|
LEGACY_SPEED_TO_DECONZ = {
|
|
|
|
SPEED_OFF: FAN_SPEED_OFF,
|
|
|
|
SPEED_LOW: FAN_SPEED_25_PERCENT,
|
|
|
|
SPEED_MEDIUM: FAN_SPEED_50_PERCENT,
|
|
|
|
SPEED_HIGH: FAN_SPEED_100_PERCENT,
|
|
|
|
}
|
|
|
|
LEGACY_DECONZ_TO_SPEED = {
|
|
|
|
FAN_SPEED_OFF: SPEED_OFF,
|
|
|
|
FAN_SPEED_25_PERCENT: SPEED_LOW,
|
|
|
|
FAN_SPEED_50_PERCENT: SPEED_MEDIUM,
|
|
|
|
FAN_SPEED_100_PERCENT: SPEED_HIGH,
|
|
|
|
}
|
2020-10-13 10:57:42 +00:00
|
|
|
|
|
|
|
|
2021-11-16 11:39:51 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-11-23 10:37:11 +00:00
|
|
|
"""Set up fans for deCONZ component."""
|
2020-10-13 10:57:42 +00:00
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
|
|
|
gateway.entities[DOMAIN] = set()
|
|
|
|
|
|
|
|
@callback
|
2021-11-16 11:39:51 +00:00
|
|
|
def async_add_fan(
|
|
|
|
lights: list[Fan] | ValuesView[Fan] = gateway.api.lights.values(),
|
|
|
|
) -> None:
|
2020-10-13 10:57:42 +00:00
|
|
|
"""Add fan from deCONZ."""
|
|
|
|
entities = []
|
|
|
|
|
|
|
|
for light in lights:
|
|
|
|
|
2021-09-29 19:19:21 +00:00
|
|
|
if (
|
|
|
|
isinstance(light, Fan)
|
|
|
|
and light.unique_id not in gateway.entities[DOMAIN]
|
|
|
|
):
|
2020-10-13 10:57:42 +00:00
|
|
|
entities.append(DeconzFan(light, gateway))
|
|
|
|
|
|
|
|
if entities:
|
2020-10-16 15:14:26 +00:00
|
|
|
async_add_entities(entities)
|
2020-10-13 10:57:42 +00:00
|
|
|
|
2021-04-20 18:20:57 +00:00
|
|
|
config_entry.async_on_unload(
|
2020-10-13 10:57:42 +00:00
|
|
|
async_dispatcher_connect(
|
2021-10-07 10:48:27 +00:00
|
|
|
hass,
|
|
|
|
gateway.signal_new_light,
|
|
|
|
async_add_fan,
|
2020-10-13 10:57:42 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-12-02 15:21:27 +00:00
|
|
|
async_add_fan()
|
2020-10-13 10:57:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeconzFan(DeconzDevice, FanEntity):
|
|
|
|
"""Representation of a deCONZ fan."""
|
|
|
|
|
|
|
|
TYPE = DOMAIN
|
2021-11-16 11:39:51 +00:00
|
|
|
_device: Fan
|
|
|
|
|
|
|
|
_attr_supported_features = SUPPORT_SET_SPEED
|
2020-10-13 10:57:42 +00:00
|
|
|
|
2021-11-16 11:39:51 +00:00
|
|
|
def __init__(self, device: Fan, gateway: DeconzGateway) -> None:
|
2020-10-13 10:57:42 +00:00
|
|
|
"""Set up fan."""
|
|
|
|
super().__init__(device, gateway)
|
|
|
|
|
2021-10-02 07:09:26 +00:00
|
|
|
self._default_on_speed = FAN_SPEED_50_PERCENT
|
2021-03-23 21:29:55 +00:00
|
|
|
if self._device.speed in ORDERED_NAMED_FAN_SPEEDS:
|
2020-10-13 10:57:42 +00:00
|
|
|
self._default_on_speed = self._device.speed
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if fan is on."""
|
2021-11-16 11:39:51 +00:00
|
|
|
return self._device.speed != FAN_SPEED_OFF # type: ignore[no-any-return]
|
2020-10-13 10:57:42 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-25 20:09:06 +00:00
|
|
|
def percentage(self) -> int | None:
|
2021-03-23 21:29:55 +00:00
|
|
|
"""Return the current speed percentage."""
|
2021-10-02 07:09:26 +00:00
|
|
|
if self._device.speed == FAN_SPEED_OFF:
|
2021-03-23 21:29:55 +00:00
|
|
|
return 0
|
|
|
|
if self._device.speed not in ORDERED_NAMED_FAN_SPEEDS:
|
2021-03-25 19:02:17 +00:00
|
|
|
return None
|
2021-03-23 21:29:55 +00:00
|
|
|
return ordered_list_item_to_percentage(
|
|
|
|
ORDERED_NAMED_FAN_SPEEDS, self._device.speed
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def speed_count(self) -> int:
|
|
|
|
"""Return the number of speeds the fan supports."""
|
|
|
|
return len(ORDERED_NAMED_FAN_SPEEDS)
|
2020-10-13 10:57:42 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def speed_list(self) -> list:
|
2021-03-23 21:29:55 +00:00
|
|
|
"""Get the list of available speeds.
|
|
|
|
|
|
|
|
Legacy fan support.
|
|
|
|
"""
|
|
|
|
return list(LEGACY_SPEED_TO_DECONZ)
|
|
|
|
|
|
|
|
def speed_to_percentage(self, speed: str) -> int:
|
|
|
|
"""Convert speed to percentage.
|
|
|
|
|
|
|
|
Legacy fan support.
|
|
|
|
"""
|
|
|
|
if speed == SPEED_OFF:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if speed not in LEGACY_SPEED_TO_DECONZ:
|
|
|
|
speed = SPEED_MEDIUM
|
|
|
|
|
|
|
|
return ordered_list_item_to_percentage(
|
|
|
|
ORDERED_NAMED_FAN_SPEEDS, LEGACY_SPEED_TO_DECONZ[speed]
|
|
|
|
)
|
|
|
|
|
|
|
|
def percentage_to_speed(self, percentage: int) -> str:
|
|
|
|
"""Convert percentage to speed.
|
|
|
|
|
|
|
|
Legacy fan support.
|
|
|
|
"""
|
|
|
|
if percentage == 0:
|
|
|
|
return SPEED_OFF
|
|
|
|
return LEGACY_DECONZ_TO_SPEED.get(
|
|
|
|
percentage_to_ordered_list_item(ORDERED_NAMED_FAN_SPEEDS, percentage),
|
|
|
|
SPEED_MEDIUM,
|
|
|
|
)
|
2020-10-13 10:57:42 +00:00
|
|
|
|
|
|
|
@callback
|
2021-10-20 12:59:36 +00:00
|
|
|
def async_update_callback(self) -> None:
|
2020-10-13 10:57:42 +00:00
|
|
|
"""Store latest configured speed from the device."""
|
2021-03-23 21:29:55 +00:00
|
|
|
if self._device.speed in ORDERED_NAMED_FAN_SPEEDS:
|
2020-10-13 10:57:42 +00:00
|
|
|
self._default_on_speed = self._device.speed
|
2021-10-20 12:59:36 +00:00
|
|
|
super().async_update_callback()
|
2020-10-13 10:57:42 +00:00
|
|
|
|
2021-03-23 21:29:55 +00:00
|
|
|
async def async_set_percentage(self, percentage: int) -> None:
|
|
|
|
"""Set the speed percentage of the fan."""
|
|
|
|
await self._device.set_speed(
|
|
|
|
percentage_to_ordered_list_item(ORDERED_NAMED_FAN_SPEEDS, percentage)
|
|
|
|
)
|
|
|
|
|
2020-10-13 10:57:42 +00:00
|
|
|
async def async_set_speed(self, speed: str) -> None:
|
2021-03-23 21:29:55 +00:00
|
|
|
"""Set the speed of the fan.
|
|
|
|
|
|
|
|
Legacy fan support.
|
|
|
|
"""
|
|
|
|
if speed not in LEGACY_SPEED_TO_DECONZ:
|
2020-10-13 17:34:33 +00:00
|
|
|
raise ValueError(f"Unsupported speed {speed}")
|
|
|
|
|
2021-03-23 21:29:55 +00:00
|
|
|
await self._device.set_speed(LEGACY_SPEED_TO_DECONZ[speed])
|
2020-10-13 10:57:42 +00:00
|
|
|
|
2021-01-27 23:44:36 +00:00
|
|
|
async def async_turn_on(
|
|
|
|
self,
|
2021-11-16 11:39:51 +00:00
|
|
|
speed: str | None = None,
|
|
|
|
percentage: int | None = None,
|
|
|
|
preset_mode: str | None = None,
|
|
|
|
**kwargs: Any,
|
2021-01-27 23:44:36 +00:00
|
|
|
) -> None:
|
2020-10-13 10:57:42 +00:00
|
|
|
"""Turn on fan."""
|
2021-03-23 21:29:55 +00:00
|
|
|
new_speed = self._default_on_speed
|
|
|
|
|
|
|
|
if percentage is not None:
|
|
|
|
new_speed = percentage_to_ordered_list_item(
|
|
|
|
ORDERED_NAMED_FAN_SPEEDS, percentage
|
|
|
|
)
|
|
|
|
|
|
|
|
await self._device.set_speed(new_speed)
|
2020-10-13 10:57:42 +00:00
|
|
|
|
2021-11-16 11:39:51 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2020-10-13 10:57:42 +00:00
|
|
|
"""Turn off fan."""
|
2021-10-02 07:09:26 +00:00
|
|
|
await self._device.set_speed(FAN_SPEED_OFF)
|