Home Assistant Unofficial Reference 2024.12.1
fan.py
Go to the documentation of this file.
1 """Fans on Zigbee Home Automation networks."""
2 
3 from __future__ import annotations
4 
5 import functools
6 from typing import Any
7 
8 from zha.application.platforms.fan.const import FanEntityFeature as ZHAFanEntityFeature
9 
10 from homeassistant.components.fan import FanEntity, FanEntityFeature
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import Platform
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.dispatcher import async_dispatcher_connect
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from .entity import ZHAEntity
18 from .helpers import (
19  SIGNAL_ADD_ENTITIES,
20  EntityData,
21  async_add_entities as zha_async_add_entities,
22  convert_zha_error_to_ha_error,
23  get_zha_data,
24 )
25 
26 
28  hass: HomeAssistant,
29  config_entry: ConfigEntry,
30  async_add_entities: AddEntitiesCallback,
31 ) -> None:
32  """Set up the Zigbee Home Automation fan from config entry."""
33  zha_data = get_zha_data(hass)
34  entities_to_create = zha_data.platforms[Platform.FAN]
35 
37  hass,
38  SIGNAL_ADD_ENTITIES,
39  functools.partial(
40  zha_async_add_entities, async_add_entities, ZhaFan, entities_to_create
41  ),
42  )
43  config_entry.async_on_unload(unsub)
44 
45 
47  """Representation of a ZHA fan."""
48 
49  _attr_translation_key: str = "fan"
50  _enable_turn_on_off_backwards_compatibility = False
51 
52  def __init__(self, entity_data: EntityData) -> None:
53  """Initialize the ZHA fan."""
54  super().__init__(entity_data)
55  features = FanEntityFeature(0)
56  zha_features: ZHAFanEntityFeature = self.entity_data.entity.supported_features
57 
58  if ZHAFanEntityFeature.DIRECTION in zha_features:
59  features |= FanEntityFeature.DIRECTION
60  if ZHAFanEntityFeature.OSCILLATE in zha_features:
61  features |= FanEntityFeature.OSCILLATE
62  if ZHAFanEntityFeature.PRESET_MODE in zha_features:
63  features |= FanEntityFeature.PRESET_MODE
64  if ZHAFanEntityFeature.SET_SPEED in zha_features:
65  features |= FanEntityFeature.SET_SPEED
66  if ZHAFanEntityFeature.TURN_ON in zha_features:
67  features |= FanEntityFeature.TURN_ON
68  if ZHAFanEntityFeature.TURN_OFF in zha_features:
69  features |= FanEntityFeature.TURN_OFF
70 
71  self._attr_supported_features_attr_supported_features = features
72 
73  @property
74  def preset_mode(self) -> str | None:
75  """Return the current preset mode."""
76  return self.entity_data.entity.preset_mode
77 
78  @property
79  def preset_modes(self) -> list[str]:
80  """Return the available preset modes."""
81  return self.entity_data.entity.preset_modes
82 
83  @property
84  def default_on_percentage(self) -> int:
85  """Return the default on percentage."""
86  return self.entity_data.entity.default_on_percentage
87 
88  @property
89  def speed_range(self) -> tuple[int, int]:
90  """Return the range of speeds the fan supports. Off is not included."""
91  return self.entity_data.entity.speed_range
92 
93  @property
94  def speed_count(self) -> int:
95  """Return the number of speeds the fan supports."""
96  return self.entity_data.entity.speed_count
97 
98  @convert_zha_error_to_ha_error
99  async def async_turn_on(
100  self,
101  percentage: int | None = None,
102  preset_mode: str | None = None,
103  **kwargs: Any,
104  ) -> None:
105  """Turn the entity on."""
106  await self.entity_data.entity.async_turn_on(
107  percentage=percentage, preset_mode=preset_mode
108  )
109  self.async_write_ha_stateasync_write_ha_state()
110 
111  @convert_zha_error_to_ha_error
112  async def async_turn_off(self, **kwargs: Any) -> None:
113  """Turn the entity off."""
114  await self.entity_data.entity.async_turn_off()
115  self.async_write_ha_stateasync_write_ha_state()
116 
117  @convert_zha_error_to_ha_error
118  async def async_set_percentage(self, percentage: int) -> None:
119  """Set the speed percentage of the fan."""
120  await self.entity_data.entity.async_set_percentage(percentage=percentage)
121  self.async_write_ha_stateasync_write_ha_state()
122 
123  @convert_zha_error_to_ha_error
124  async def async_set_preset_mode(self, preset_mode: str) -> None:
125  """Set the preset mode for the fan."""
126  await self.entity_data.entity.async_set_preset_mode(preset_mode=preset_mode)
127  self.async_write_ha_stateasync_write_ha_state()
128 
129  @property
130  def percentage(self) -> int | None:
131  """Return the current speed percentage."""
132  return self.entity_data.entity.percentage
None async_set_preset_mode(self, str preset_mode)
Definition: fan.py:124
tuple[int, int] speed_range(self)
Definition: fan.py:89
None async_set_percentage(self, int percentage)
Definition: fan.py:118
None __init__(self, EntityData entity_data)
Definition: fan.py:52
None async_turn_off(self, **Any kwargs)
Definition: fan.py:112
None async_turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
Definition: fan.py:104
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: fan.py:31
HAZHAData get_zha_data(HomeAssistant hass)
Definition: helpers.py:1020
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103