Home Assistant Unofficial Reference 2024.12.1
fan.py
Go to the documentation of this file.
1 """Fan support for switch entities."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
8  DOMAIN as FAN_DOMAIN,
9  FanEntity,
10  FanEntityFeature,
11 )
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.const import CONF_ENTITY_ID
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers import entity_registry as er
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from .entity import BaseToggleEntity
19 
20 
22  hass: HomeAssistant,
23  config_entry: ConfigEntry,
24  async_add_entities: AddEntitiesCallback,
25 ) -> None:
26  """Initialize Fan Switch config entry."""
27  registry = er.async_get(hass)
28  entity_id = er.async_validate_entity_id(
29  registry, config_entry.options[CONF_ENTITY_ID]
30  )
31 
33  [
34  FanSwitch(
35  hass,
36  config_entry.title,
37  FAN_DOMAIN,
38  entity_id,
39  config_entry.entry_id,
40  )
41  ]
42  )
43 
44 
46  """Represents a Switch as a Fan."""
47 
48  _attr_supported_features = FanEntityFeature.TURN_OFF | FanEntityFeature.TURN_ON
49  _enable_turn_on_off_backwards_compatibility = False
50 
51  @property
52  def is_on(self) -> bool | None:
53  """Return true if the entity is on.
54 
55  Fan logic uses speed percentage or preset mode to determine
56  if it's on or off, however, when using a wrapped switch, we
57  just use the wrapped switch's state.
58  """
59  return self._attr_is_on_attr_is_on
60 
61  async def async_turn_on(
62  self,
63  percentage: int | None = None,
64  preset_mode: str | None = None,
65  **kwargs: Any,
66  ) -> None:
67  """Turn on the fan.
68 
69  Arguments of the turn_on methods fan entity differ,
70  thus we need to override them here.
71  """
72  await super().async_turn_on()
None async_turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
Definition: fan.py:66
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: fan.py:25