1 """Support for deCONZ fans."""
3 from __future__
import annotations
7 from pydeconz.models.event
import EventType
8 from pydeconz.models.light.light
import Light, LightFanSpeed
19 ordered_list_item_to_percentage,
20 percentage_to_ordered_list_item,
23 from .entity
import DeconzDevice
24 from .hub
import DeconzHub
26 ORDERED_NAMED_FAN_SPEEDS: list[LightFanSpeed] = [
27 LightFanSpeed.PERCENT_25,
28 LightFanSpeed.PERCENT_50,
29 LightFanSpeed.PERCENT_75,
30 LightFanSpeed.PERCENT_100,
36 config_entry: ConfigEntry,
37 async_add_entities: AddEntitiesCallback,
39 """Set up fans for deCONZ component."""
40 hub = DeconzHub.get_hub(hass, config_entry)
41 hub.entities[FAN_DOMAIN] = set()
44 def async_add_fan(_: EventType, fan_id: str) ->
None:
45 """Add fan from deCONZ."""
46 fan = hub.api.lights.lights[fan_id]
47 if not fan.supports_fan_speed:
51 hub.register_platform_add_device_callback(
53 hub.api.lights.lights,
58 """Representation of a deCONZ fan."""
61 _default_on_speed = LightFanSpeed.PERCENT_50
63 _attr_supported_features = (
64 FanEntityFeature.SET_SPEED
65 | FanEntityFeature.TURN_ON
66 | FanEntityFeature.TURN_OFF
68 _enable_turn_on_off_backwards_compatibility =
False
70 def __init__(self, device: Light, hub: DeconzHub) ->
None:
73 if device.fan_speed
in ORDERED_NAMED_FAN_SPEEDS:
78 """Return true if fan is on."""
79 return self._device.fan_speed != LightFanSpeed.OFF
83 """Return the current speed percentage."""
84 if self._device.fan_speed == LightFanSpeed.OFF:
86 if self._device.fan_speed
not in ORDERED_NAMED_FAN_SPEEDS:
88 return ordered_list_item_to_percentage(
89 ORDERED_NAMED_FAN_SPEEDS, self._device.fan_speed
94 """Store latest configured speed from the device."""
95 if self._device.fan_speed
in ORDERED_NAMED_FAN_SPEEDS:
100 """Set the speed percentage of the fan."""
104 await self.hub.api.lights.lights.set_state(
105 id=self._device.resource_id,
106 fan_speed=percentage_to_ordered_list_item(
107 ORDERED_NAMED_FAN_SPEEDS, percentage
113 percentage: int |
None =
None,
114 preset_mode: str |
None =
None,
118 if percentage
is not None:
121 await self.hub.api.lights.lights.set_state(
122 id=self._device.resource_id,
128 await self.hub.api.lights.lights.set_state(
129 id=self._device.resource_id,
130 fan_speed=LightFanSpeed.OFF,
None __init__(self, Light device, DeconzHub hub)
None async_update_callback(self)
None async_set_percentage(self, int percentage)
int|None percentage(self)
None async_turn_off(self, **Any kwargs)
None async_turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
None async_set_percentage(self, int percentage)
None async_turn_off(self, **Any kwargs)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)