2021-03-23 21:29:55 +00:00
|
|
|
"""Support for deCONZ fans."""
|
|
|
|
|
|
|
|
from typing import Optional
|
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
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 .const import FANS, NEW_LIGHT
|
|
|
|
from .deconz_device import DeconzDevice
|
|
|
|
from .gateway import get_gateway_from_config_entry
|
|
|
|
|
2021-03-23 21:29:55 +00:00
|
|
|
ORDERED_NAMED_FAN_SPEEDS = [1, 2, 3, 4]
|
2020-10-13 10:57:42 +00:00
|
|
|
|
2021-03-23 21:29:55 +00:00
|
|
|
LEGACY_SPEED_TO_DECONZ = {SPEED_OFF: 0, SPEED_LOW: 1, SPEED_MEDIUM: 2, SPEED_HIGH: 4}
|
|
|
|
LEGACY_DECONZ_TO_SPEED = {0: SPEED_OFF, 1: SPEED_LOW, 2: SPEED_MEDIUM, 4: SPEED_HIGH}
|
2020-10-13 10:57:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities) -> 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
|
2020-12-02 15:21:27 +00:00
|
|
|
def async_add_fan(lights=gateway.api.lights.values()) -> None:
|
2020-10-13 10:57:42 +00:00
|
|
|
"""Add fan from deCONZ."""
|
|
|
|
entities = []
|
|
|
|
|
|
|
|
for light in lights:
|
|
|
|
|
|
|
|
if light.type in FANS and light.uniqueid not in gateway.entities[DOMAIN]:
|
|
|
|
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
|
|
|
|
|
|
|
gateway.listeners.append(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
hass, gateway.async_signal_new_device(NEW_LIGHT), async_add_fan
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def __init__(self, device, gateway) -> None:
|
|
|
|
"""Set up fan."""
|
|
|
|
super().__init__(device, gateway)
|
|
|
|
|
2021-03-23 21:29:55 +00:00
|
|
|
self._default_on_speed = 2
|
|
|
|
if self._device.speed in ORDERED_NAMED_FAN_SPEEDS:
|
2020-10-13 10:57:42 +00:00
|
|
|
self._default_on_speed = self._device.speed
|
|
|
|
|
|
|
|
self._features = SUPPORT_SET_SPEED
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if fan is on."""
|
2021-03-23 21:29:55 +00:00
|
|
|
return self._device.speed != 0
|
2020-10-13 10:57:42 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-23 21:29:55 +00:00
|
|
|
def percentage(self) -> Optional[int]:
|
|
|
|
"""Return the current speed percentage."""
|
|
|
|
if self._device.speed == 0:
|
|
|
|
return 0
|
|
|
|
if self._device.speed not in ORDERED_NAMED_FAN_SPEEDS:
|
|
|
|
return
|
|
|
|
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
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Flag supported features."""
|
|
|
|
return self._features
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_update_callback(self, force_update=False) -> None:
|
|
|
|
"""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
|
|
|
|
super().async_update_callback(force_update)
|
|
|
|
|
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,
|
|
|
|
speed: str = None,
|
|
|
|
percentage: int = None,
|
|
|
|
preset_mode: str = None,
|
|
|
|
**kwargs,
|
|
|
|
) -> 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
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs) -> None:
|
|
|
|
"""Turn off fan."""
|
2021-03-23 21:29:55 +00:00
|
|
|
await self._device.set_speed(0)
|