1 """Support for WiLight Fan."""
3 from __future__
import annotations
7 from pywilight.const
import (
17 from pywilight.wilight_device
import PyWiLightDevice
24 ordered_list_item_to_percentage,
25 percentage_to_ordered_list_item,
28 from .const
import DOMAIN
29 from .entity
import WiLightDevice
30 from .parent_device
import WiLightParent
32 ORDERED_NAMED_FAN_SPEEDS = [WL_SPEED_LOW, WL_SPEED_MEDIUM, WL_SPEED_HIGH]
36 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
38 """Set up WiLight lights from a config entry."""
39 parent: WiLightParent = hass.data[DOMAIN][entry.entry_id]
44 for item
in parent.api.items:
45 if item[
"type"] != ITEM_FAN:
48 item_name = item[
"name"]
49 if item[
"sub_type"] != FAN_V1:
51 entities.append(
WiLightFan(parent.api, index, item_name))
57 """Representation of a WiLights fan."""
60 _attr_speed_count = len(ORDERED_NAMED_FAN_SPEEDS)
61 _attr_supported_features = (
62 FanEntityFeature.SET_SPEED
63 | FanEntityFeature.DIRECTION
64 | FanEntityFeature.TURN_ON
65 | FanEntityFeature.TURN_OFF
67 _enable_turn_on_off_backwards_compatibility =
False
69 def __init__(self, api_device: PyWiLightDevice, index: str, item_name: str) ->
None:
70 """Initialize the device."""
71 super().
__init__(api_device, index, item_name)
77 """Return true if device is on."""
78 return self.
_status_status.
get(
"direction", WL_DIRECTION_OFF) != WL_DIRECTION_OFF
82 """Return the current speed percentage."""
84 "direction" in self.
_status_status
85 and self.
_status_status[
"direction"] == WL_DIRECTION_OFF
89 if (wl_speed := self.
_status_status.
get(
"speed"))
is None:
91 return ordered_list_item_to_percentage(ORDERED_NAMED_FAN_SPEEDS, wl_speed)
95 """Return the current direction of the fan."""
97 "direction" in self.
_status_status
98 and self.
_status_status[
"direction"] != WL_DIRECTION_OFF
105 percentage: int |
None =
None,
106 preset_mode: str |
None =
None,
109 """Turn on the fan."""
110 if percentage
is None:
116 """Set the speed of the fan."""
118 await self.
_client_client.set_fan_direction(self.
_index_index, WL_DIRECTION_OFF)
121 "direction" in self.
_status_status
122 and self.
_status_status[
"direction"] == WL_DIRECTION_OFF
125 wl_speed = percentage_to_ordered_list_item(ORDERED_NAMED_FAN_SPEEDS, percentage)
126 await self.
_client_client.set_fan_speed(self.
_index_index, wl_speed)
129 """Set the direction of the fan."""
130 wl_direction = WL_DIRECTION_REVERSE
131 if direction == DIRECTION_FORWARD:
132 wl_direction = WL_DIRECTION_FORWARD
133 await self.
_client_client.set_fan_direction(self.
_index_index, wl_direction)
136 """Turn the fan off."""
137 await self.
_client_client.set_fan_direction(self.
_index_index, WL_DIRECTION_OFF)
None async_set_percentage(self, int percentage)
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_direction(self, str direction)
None __init__(self, PyWiLightDevice api_device, str index, str item_name)
None async_set_percentage(self, int percentage)
str current_direction(self)
int|None percentage(self)
web.Response get(self, web.Request request, str config_key)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)