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
|
|
|
|
2022-07-14 21:53:09 +00:00
|
|
|
from typing import Any
|
2021-11-16 11:39:51 +00:00
|
|
|
|
2022-05-11 11:18:20 +00:00
|
|
|
from pydeconz.models.event import EventType
|
2022-07-14 21:53:09 +00:00
|
|
|
from pydeconz.models.light.light import Light, LightFanSpeed
|
2021-09-29 19:19:21 +00:00
|
|
|
|
2022-04-05 22:00:37 +00:00
|
|
|
from homeassistant.components.fan import DOMAIN, FanEntity, FanEntityFeature
|
2021-11-16 11:39:51 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
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
|
|
|
|
2022-07-14 21:53:09 +00:00
|
|
|
ORDERED_NAMED_FAN_SPEEDS: list[LightFanSpeed] = [
|
|
|
|
LightFanSpeed.PERCENT_25,
|
|
|
|
LightFanSpeed.PERCENT_50,
|
|
|
|
LightFanSpeed.PERCENT_75,
|
|
|
|
LightFanSpeed.PERCENT_100,
|
2021-10-02 07:09:26 +00:00
|
|
|
]
|
|
|
|
|
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
|
2022-05-11 11:18:20 +00:00
|
|
|
def async_add_fan(_: EventType, fan_id: str) -> None:
|
2020-10-13 10:57:42 +00:00
|
|
|
"""Add fan from deCONZ."""
|
2022-07-14 21:53:09 +00:00
|
|
|
fan = gateway.api.lights.lights[fan_id]
|
|
|
|
if not fan.supports_fan_speed:
|
|
|
|
return
|
2022-05-11 11:18:20 +00:00
|
|
|
async_add_entities([DeconzFan(fan, gateway)])
|
2020-10-13 10:57:42 +00:00
|
|
|
|
2022-05-25 03:48:09 +00:00
|
|
|
gateway.register_platform_add_device_callback(
|
|
|
|
async_add_fan,
|
2022-07-14 21:53:09 +00:00
|
|
|
gateway.api.lights.lights,
|
2020-10-13 10:57:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class DeconzFan(DeconzDevice, FanEntity):
|
|
|
|
"""Representation of a deCONZ fan."""
|
|
|
|
|
|
|
|
TYPE = DOMAIN
|
2022-07-14 21:53:09 +00:00
|
|
|
_device: Light
|
|
|
|
_default_on_speed = LightFanSpeed.PERCENT_50
|
2021-11-16 11:39:51 +00:00
|
|
|
|
2022-04-05 22:00:37 +00:00
|
|
|
_attr_supported_features = FanEntityFeature.SET_SPEED
|
2020-10-13 10:57:42 +00:00
|
|
|
|
2022-07-14 21:53:09 +00:00
|
|
|
def __init__(self, device: Light, gateway: DeconzGateway) -> None:
|
2020-10-13 10:57:42 +00:00
|
|
|
"""Set up fan."""
|
|
|
|
super().__init__(device, gateway)
|
|
|
|
|
2022-07-14 21:53:09 +00:00
|
|
|
if device.fan_speed in ORDERED_NAMED_FAN_SPEEDS:
|
|
|
|
self._default_on_speed = device.fan_speed
|
2020-10-13 10:57:42 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if fan is on."""
|
2022-07-14 21:53:09 +00:00
|
|
|
return self._device.fan_speed != LightFanSpeed.OFF
|
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."""
|
2022-07-14 21:53:09 +00:00
|
|
|
if self._device.fan_speed == LightFanSpeed.OFF:
|
2021-03-23 21:29:55 +00:00
|
|
|
return 0
|
2022-07-14 21:53:09 +00:00
|
|
|
if self._device.fan_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(
|
2022-07-14 21:53:09 +00:00
|
|
|
ORDERED_NAMED_FAN_SPEEDS, self._device.fan_speed
|
2021-03-23 21:29:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
@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."""
|
2022-07-14 21:53:09 +00:00
|
|
|
if self._device.fan_speed in ORDERED_NAMED_FAN_SPEEDS:
|
|
|
|
self._default_on_speed = self._device.fan_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."""
|
2022-03-09 09:38:12 +00:00
|
|
|
if percentage == 0:
|
|
|
|
return await self.async_turn_off()
|
2022-07-14 21:53:09 +00:00
|
|
|
await self.gateway.api.lights.lights.set_state(
|
|
|
|
id=self._device.resource_id,
|
|
|
|
fan_speed=percentage_to_ordered_list_item(
|
|
|
|
ORDERED_NAMED_FAN_SPEEDS, percentage
|
|
|
|
),
|
2021-03-23 21:29:55 +00:00
|
|
|
)
|
|
|
|
|
2021-01-27 23:44:36 +00:00
|
|
|
async def async_turn_on(
|
|
|
|
self,
|
2021-11-16 11:39:51 +00:00
|
|
|
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
|
|
|
if percentage is not None:
|
2022-03-09 09:38:12 +00:00
|
|
|
await self.async_set_percentage(percentage)
|
|
|
|
return
|
2022-07-14 21:53:09 +00:00
|
|
|
await self.gateway.api.lights.lights.set_state(
|
|
|
|
id=self._device.resource_id,
|
|
|
|
fan_speed=self._default_on_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."""
|
2022-07-14 21:53:09 +00:00
|
|
|
await self.gateway.api.lights.lights.set_state(
|
|
|
|
id=self._device.resource_id,
|
|
|
|
fan_speed=LightFanSpeed.OFF,
|
|
|
|
)
|