Home Assistant Unofficial Reference 2024.12.1
fan.py
Go to the documentation of this file.
1 """Demo fan platform that has a fake fan."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.fan import FanEntity, FanEntityFeature
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 PRESET_MODE_AUTO = "auto"
13 PRESET_MODE_SMART = "smart"
14 PRESET_MODE_SLEEP = "sleep"
15 PRESET_MODE_ON = "on"
16 
17 FULL_SUPPORT = (
18  FanEntityFeature.SET_SPEED
19  | FanEntityFeature.OSCILLATE
20  | FanEntityFeature.DIRECTION
21  | FanEntityFeature.TURN_OFF
22  | FanEntityFeature.TURN_ON
23 )
24 LIMITED_SUPPORT = (
25  FanEntityFeature.SET_SPEED | FanEntityFeature.TURN_OFF | FanEntityFeature.TURN_ON
26 )
27 
28 
30  hass: HomeAssistant,
31  config_entry: ConfigEntry,
32  async_add_entities: AddEntitiesCallback,
33 ) -> None:
34  """Set up the Demo config entry."""
36  [
38  hass,
39  "fan1",
40  "Living Room Fan",
41  FULL_SUPPORT,
42  [
43  PRESET_MODE_AUTO,
44  PRESET_MODE_SMART,
45  PRESET_MODE_SLEEP,
46  PRESET_MODE_ON,
47  ],
48  ),
50  hass,
51  "fan2",
52  "Ceiling Fan",
53  LIMITED_SUPPORT,
54  None,
55  ),
57  hass,
58  "fan3",
59  "Percentage Full Fan",
60  FULL_SUPPORT,
61  [
62  PRESET_MODE_AUTO,
63  PRESET_MODE_SMART,
64  PRESET_MODE_SLEEP,
65  PRESET_MODE_ON,
66  ],
67  ),
69  hass,
70  "fan4",
71  "Percentage Limited Fan",
72  LIMITED_SUPPORT,
73  [
74  PRESET_MODE_AUTO,
75  PRESET_MODE_SMART,
76  PRESET_MODE_SLEEP,
77  PRESET_MODE_ON,
78  ],
79  ),
81  hass,
82  "fan5",
83  "Preset Only Limited Fan",
84  FanEntityFeature.PRESET_MODE
85  | FanEntityFeature.TURN_OFF
86  | FanEntityFeature.TURN_ON,
87  [
88  PRESET_MODE_AUTO,
89  PRESET_MODE_SMART,
90  PRESET_MODE_SLEEP,
91  PRESET_MODE_ON,
92  ],
93  ),
94  ]
95  )
96 
97 
99  """A demonstration fan component that uses legacy fan speeds."""
100 
101  _attr_should_poll = False
102  _attr_translation_key = "demo"
103  _enable_turn_on_off_backwards_compatibility = False
104 
105  def __init__(
106  self,
107  hass: HomeAssistant,
108  unique_id: str,
109  name: str,
110  supported_features: FanEntityFeature,
111  preset_modes: list[str] | None,
112  ) -> None:
113  """Initialize the entity."""
114  self.hasshasshass = hass
115  self._unique_id_unique_id = unique_id
116  self._attr_supported_features_attr_supported_features = supported_features
117  self._percentage: int | None = None
118  self._preset_modes_preset_modes = preset_modes
119  self._preset_mode: str | None = None
120  self._oscillating_oscillating: bool | None = None
121  self._direction_direction: str | None = None
122  self._attr_name_attr_name = name
123  if supported_features & FanEntityFeature.OSCILLATE:
124  self._oscillating_oscillating = False
125  if supported_features & FanEntityFeature.DIRECTION:
126  self._direction_direction = "forward"
127 
128  @property
129  def unique_id(self) -> str:
130  """Return the unique id."""
131  return self._unique_id_unique_id
132 
133  @property
134  def current_direction(self) -> str | None:
135  """Fan direction."""
136  return self._direction_direction
137 
138  @property
139  def oscillating(self) -> bool | None:
140  """Oscillating."""
141  return self._oscillating_oscillating
142 
143 
145  """A demonstration fan component that uses percentages."""
146 
147  @property
148  def percentage(self) -> int | None:
149  """Return the current speed."""
150  return self._percentage_percentage
151 
152  @property
153  def speed_count(self) -> int:
154  """Return the number of speeds the fan supports."""
155  return 3
156 
157  def set_percentage(self, percentage: int) -> None:
158  """Set the speed of the fan, as a percentage."""
159  self._percentage_percentage = percentage
160  self._preset_mode_preset_mode = None
161  self.schedule_update_ha_stateschedule_update_ha_state()
162 
163  @property
164  def preset_mode(self) -> str | None:
165  """Return the current preset mode, e.g., auto, smart, interval, favorite."""
166  return self._preset_mode_preset_mode
167 
168  @property
169  def preset_modes(self) -> list[str] | None:
170  """Return a list of available preset modes."""
171  return self._preset_modes_preset_modes
172 
173  def set_preset_mode(self, preset_mode: str) -> None:
174  """Set new preset mode."""
175  self._preset_mode_preset_mode = preset_mode
176  self._percentage_percentage = None
177  self.schedule_update_ha_stateschedule_update_ha_state()
178 
179  def turn_on(
180  self,
181  percentage: int | None = None,
182  preset_mode: str | None = None,
183  **kwargs: Any,
184  ) -> None:
185  """Turn on the entity."""
186  if preset_mode:
187  self.set_preset_modeset_preset_modeset_preset_mode(preset_mode)
188  return
189 
190  if percentage is None:
191  percentage = 67
192 
193  self.set_percentageset_percentageset_percentage(percentage)
194 
195  def turn_off(self, **kwargs: Any) -> None:
196  """Turn off the entity."""
197  self.set_percentageset_percentageset_percentage(0)
198 
199  def set_direction(self, direction: str) -> None:
200  """Set the direction of the fan."""
201  self._direction_direction_direction = direction
202  self.schedule_update_ha_stateschedule_update_ha_state()
203 
204  def oscillate(self, oscillating: bool) -> None:
205  """Set oscillation."""
206  self._oscillating_oscillating_oscillating = oscillating
207  self.schedule_update_ha_stateschedule_update_ha_state()
208 
209 
211  """An async demonstration fan component that uses percentages."""
212 
213  @property
214  def percentage(self) -> int | None:
215  """Return the current speed."""
216  return self._percentage_percentage
217 
218  @property
219  def speed_count(self) -> int:
220  """Return the number of speeds the fan supports."""
221  return 3
222 
223  async def async_set_percentage(self, percentage: int) -> None:
224  """Set the speed of the fan, as a percentage."""
225  self._percentage_percentage = percentage
226  self._preset_mode_preset_mode = None
227  self.async_write_ha_stateasync_write_ha_state()
228 
229  @property
230  def preset_mode(self) -> str | None:
231  """Return the current preset mode, e.g., auto, smart, interval, favorite."""
232  return self._preset_mode_preset_mode
233 
234  @property
235  def preset_modes(self) -> list[str] | None:
236  """Return a list of available preset modes."""
237  return self._preset_modes_preset_modes
238 
239  async def async_set_preset_mode(self, preset_mode: str) -> None:
240  """Set new preset mode."""
241  self._preset_mode_preset_mode = preset_mode
242  self._percentage_percentage = None
243  self.async_write_ha_stateasync_write_ha_state()
244 
245  async def async_turn_on(
246  self,
247  percentage: int | None = None,
248  preset_mode: str | None = None,
249  **kwargs: Any,
250  ) -> None:
251  """Turn on the entity."""
252  if preset_mode:
253  await self.async_set_preset_modeasync_set_preset_modeasync_set_preset_mode(preset_mode)
254  return
255 
256  if percentage is None:
257  percentage = 67
258 
259  await self.async_set_percentageasync_set_percentageasync_set_percentage(percentage)
260 
261  async def async_turn_off(self, **kwargs: Any) -> None:
262  """Turn off the entity."""
263  await self.async_oscillateasync_oscillateasync_oscillate(False)
264  await self.async_set_percentageasync_set_percentageasync_set_percentage(0)
265 
266  async def async_set_direction(self, direction: str) -> None:
267  """Set the direction of the fan."""
268  self._direction_direction_direction = direction
269  self.async_write_ha_stateasync_write_ha_state()
270 
271  async def async_oscillate(self, oscillating: bool) -> None:
272  """Set oscillation."""
273  self._oscillating_oscillating_oscillating = oscillating
274  self.async_write_ha_stateasync_write_ha_state()
None async_set_preset_mode(self, str preset_mode)
Definition: fan.py:239
None async_oscillate(self, bool oscillating)
Definition: fan.py:271
None async_set_percentage(self, int percentage)
Definition: fan.py:223
None async_turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
Definition: fan.py:250
None __init__(self, HomeAssistant hass, str unique_id, str name, FanEntityFeature supported_features, list[str]|None preset_modes)
Definition: fan.py:112
None turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
Definition: fan.py:184
None set_percentage(self, int percentage)
Definition: fan.py:157
None oscillate(self, bool oscillating)
Definition: fan.py:204
None set_preset_mode(self, str preset_mode)
Definition: fan.py:173
None set_direction(self, str direction)
Definition: fan.py:199
None set_preset_mode(self, str preset_mode)
Definition: __init__.py:375
None set_percentage(self, int percentage)
Definition: __init__.py:336
None async_set_percentage(self, int percentage)
Definition: __init__.py:340
None async_set_preset_mode(self, str preset_mode)
Definition: __init__.py:385
None async_oscillate(self, bool oscillating)
Definition: __init__.py:452
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: fan.py:33