Home Assistant Unofficial Reference 2024.12.1
fan.py
Go to the documentation of this file.
1 """Support for WiLight Fan."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from pywilight.const import (
8  FAN_V1,
9  ITEM_FAN,
10  WL_DIRECTION_FORWARD,
11  WL_DIRECTION_OFF,
12  WL_DIRECTION_REVERSE,
13  WL_SPEED_HIGH,
14  WL_SPEED_LOW,
15  WL_SPEED_MEDIUM,
16 )
17 from pywilight.wilight_device import PyWiLightDevice
18 
19 from homeassistant.components.fan import DIRECTION_FORWARD, FanEntity, FanEntityFeature
20 from homeassistant.config_entries import ConfigEntry
21 from homeassistant.core import HomeAssistant
22 from homeassistant.helpers.entity_platform import AddEntitiesCallback
24  ordered_list_item_to_percentage,
25  percentage_to_ordered_list_item,
26 )
27 
28 from .const import DOMAIN
29 from .entity import WiLightDevice
30 from .parent_device import WiLightParent
31 
32 ORDERED_NAMED_FAN_SPEEDS = [WL_SPEED_LOW, WL_SPEED_MEDIUM, WL_SPEED_HIGH]
33 
34 
36  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
37 ) -> None:
38  """Set up WiLight lights from a config entry."""
39  parent: WiLightParent = hass.data[DOMAIN][entry.entry_id]
40 
41  # Handle a discovered WiLight device.
42  entities = []
43  assert parent.api
44  for item in parent.api.items:
45  if item["type"] != ITEM_FAN:
46  continue
47  index = item["index"]
48  item_name = item["name"]
49  if item["sub_type"] != FAN_V1:
50  continue
51  entities.append(WiLightFan(parent.api, index, item_name))
52 
53  async_add_entities(entities)
54 
55 
57  """Representation of a WiLights fan."""
58 
59  _attr_name = None
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
66  )
67  _enable_turn_on_off_backwards_compatibility = False
68 
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)
72  # Initialize the WiLights fan.
73  self._direction_direction = WL_DIRECTION_FORWARD
74 
75  @property
76  def is_on(self) -> bool:
77  """Return true if device is on."""
78  return self._status_status.get("direction", WL_DIRECTION_OFF) != WL_DIRECTION_OFF
79 
80  @property
81  def percentage(self) -> int | None:
82  """Return the current speed percentage."""
83  if (
84  "direction" in self._status_status
85  and self._status_status["direction"] == WL_DIRECTION_OFF
86  ):
87  return 0
88 
89  if (wl_speed := self._status_status.get("speed")) is None:
90  return None
91  return ordered_list_item_to_percentage(ORDERED_NAMED_FAN_SPEEDS, wl_speed)
92 
93  @property
94  def current_direction(self) -> str:
95  """Return the current direction of the fan."""
96  if (
97  "direction" in self._status_status
98  and self._status_status["direction"] != WL_DIRECTION_OFF
99  ):
100  self._direction_direction = self._status_status["direction"]
101  return self._direction_direction
102 
103  async def async_turn_on(
104  self,
105  percentage: int | None = None,
106  preset_mode: str | None = None,
107  **kwargs: Any,
108  ) -> None:
109  """Turn on the fan."""
110  if percentage is None:
111  await self._client_client.set_fan_direction(self._index_index, self._direction_direction)
112  else:
113  await self.async_set_percentageasync_set_percentageasync_set_percentage(percentage)
114 
115  async def async_set_percentage(self, percentage: int) -> None:
116  """Set the speed of the fan."""
117  if percentage == 0:
118  await self._client_client.set_fan_direction(self._index_index, WL_DIRECTION_OFF)
119  return
120  if (
121  "direction" in self._status_status
122  and self._status_status["direction"] == WL_DIRECTION_OFF
123  ):
124  await self._client_client.set_fan_direction(self._index_index, self._direction_direction)
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)
127 
128  async def async_set_direction(self, direction: str) -> None:
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)
134 
135  async def async_turn_off(self, **kwargs: Any) -> None:
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)
Definition: __init__.py:340
None async_turn_off(self, **Any kwargs)
Definition: fan.py:135
None async_turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
Definition: fan.py:108
None async_set_direction(self, str direction)
Definition: fan.py:128
None __init__(self, PyWiLightDevice api_device, str index, str item_name)
Definition: fan.py:69
None async_set_percentage(self, int percentage)
Definition: fan.py:115
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: fan.py:37