Home Assistant Unofficial Reference 2024.12.1
fan.py
Go to the documentation of this file.
1 """Support for Netatmo/Bubendorff fans."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Final
7 
8 from pyatmo import modules as NaModules
9 
10 from homeassistant.components.fan import FanEntity, FanEntityFeature
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant, callback
13 from homeassistant.helpers.dispatcher import async_dispatcher_connect
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .const import CONF_URL_CONTROL, NETATMO_CREATE_FAN
17 from .data_handler import HOME, SIGNAL_NAME, NetatmoDevice
18 from .entity import NetatmoModuleEntity
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 DEFAULT_PERCENTAGE: Final = 50
23 
24 PRESET_MAPPING = {"slow": 1, "fast": 2}
25 PRESETS = {v: k for k, v in PRESET_MAPPING.items()}
26 
27 
29  hass: HomeAssistant,
30  entry: ConfigEntry,
31  async_add_entities: AddEntitiesCallback,
32 ) -> None:
33  """Set up the Netatmo fan platform."""
34 
35  @callback
36  def _create_entity(netatmo_device: NetatmoDevice) -> None:
37  entity = NetatmoFan(netatmo_device)
38  _LOGGER.debug("Adding cover %s", entity)
39  async_add_entities([entity])
40 
41  entry.async_on_unload(
42  async_dispatcher_connect(hass, NETATMO_CREATE_FAN, _create_entity)
43  )
44 
45 
47  """Representation of a Netatmo fan."""
48 
49  _attr_preset_modes = ["slow", "fast"]
50  _attr_supported_features = FanEntityFeature.PRESET_MODE
51  _attr_configuration_url = CONF_URL_CONTROL
52  _attr_name = None
53  device: NaModules.Fan
54  _enable_turn_on_off_backwards_compatibility = False
55 
56  def __init__(self, netatmo_device: NetatmoDevice) -> None:
57  """Initialize of Netatmo fan."""
58  super().__init__(netatmo_device)
59  self._publishers.extend(
60  [
61  {
62  "name": HOME,
63  "home_id": self.homehome.entity_id,
64  SIGNAL_NAME: f"{HOME}-{self.home.entity_id}",
65  },
66  ]
67  )
68 
69  self._attr_unique_id_attr_unique_id = f"{self.device.entity_id}-{self.device_type}"
70 
71  async def async_set_preset_mode(self, preset_mode: str) -> None:
72  """Set the preset mode of the fan."""
73  await self.devicedevice.async_set_fan_speed(PRESET_MAPPING[preset_mode])
74 
75  @callback
76  def async_update_callback(self) -> None:
77  """Update the entity's state."""
78  if self.devicedevice.fan_speed is None:
79  self._attr_preset_mode_attr_preset_mode = None
80  return
81  self._attr_preset_mode_attr_preset_mode = PRESETS.get(self.devicedevice.fan_speed)
None async_set_preset_mode(self, str preset_mode)
Definition: fan.py:71
None __init__(self, NetatmoDevice netatmo_device)
Definition: fan.py:56
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: fan.py:32
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103