Home Assistant Unofficial Reference 2024.12.1
fan.py
Go to the documentation of this file.
1 """Support for Freedompro fan."""
2 
3 from __future__ import annotations
4 
5 import json
6 from typing import Any
7 
8 from pyfreedompro import put_state
9 
10 from homeassistant.components.fan import FanEntity, FanEntityFeature
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import CONF_API_KEY
13 from homeassistant.core import HomeAssistant, callback
14 from homeassistant.helpers import aiohttp_client
15 from homeassistant.helpers.device_registry import DeviceInfo
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 from homeassistant.helpers.update_coordinator import CoordinatorEntity
18 
19 from .const import DOMAIN
20 from .coordinator import FreedomproDataUpdateCoordinator
21 
22 
24  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
25 ) -> None:
26  """Set up Freedompro fan."""
27  api_key: str = entry.data[CONF_API_KEY]
28  coordinator: FreedomproDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
30  FreedomproFan(hass, api_key, device, coordinator)
31  for device in coordinator.data
32  if device["type"] == "fan"
33  )
34 
35 
36 class FreedomproFan(CoordinatorEntity[FreedomproDataUpdateCoordinator], FanEntity):
37  """Representation of a Freedompro fan."""
38 
39  _attr_has_entity_name = True
40  _attr_name = None
41  _attr_is_on = False
42  _attr_percentage = 0
43  _enable_turn_on_off_backwards_compatibility = False
44 
45  def __init__(
46  self,
47  hass: HomeAssistant,
48  api_key: str,
49  device: dict[str, Any],
50  coordinator: FreedomproDataUpdateCoordinator,
51  ) -> None:
52  """Initialize the Freedompro fan."""
53  super().__init__(coordinator)
54  self._session_session = aiohttp_client.async_get_clientsession(hass)
55  self._api_key_api_key_api_key = api_key
56  self._attr_unique_id_attr_unique_id = device["uid"]
57  self._characteristics_characteristics = device["characteristics"]
58  self._attr_device_info_attr_device_info = DeviceInfo(
59  identifiers={
60  (DOMAIN, device["uid"]),
61  },
62  manufacturer="Freedompro",
63  model=device["type"],
64  name=device["name"],
65  )
66  self._attr_supported_features_attr_supported_features = (
67  FanEntityFeature.TURN_OFF | FanEntityFeature.TURN_ON
68  )
69  if "rotationSpeed" in self._characteristics_characteristics:
70  self._attr_supported_features_attr_supported_features |= FanEntityFeature.SET_SPEED
71 
72  @property
73  def is_on(self) -> bool | None:
74  """Return True if entity is on."""
75  return self._attr_is_on_attr_is_on_attr_is_on
76 
77  @callback
78  def _handle_coordinator_update(self) -> None:
79  """Handle updated data from the coordinator."""
80  device = next(
81  (
82  device
83  for device in self.coordinator.data
84  if device["uid"] == self.unique_idunique_id
85  ),
86  None,
87  )
88  if device is not None and "state" in device:
89  state = device["state"]
90  self._attr_is_on_attr_is_on_attr_is_on = state["on"]
91  if "rotationSpeed" in state:
92  self._attr_percentage_attr_percentage_attr_percentage = state["rotationSpeed"]
94 
95  async def async_added_to_hass(self) -> None:
96  """When entity is added to hass."""
97  await super().async_added_to_hass()
98  self._handle_coordinator_update_handle_coordinator_update()
99 
100  async def async_turn_on(
101  self,
102  percentage: int | None = None,
103  preset_mode: str | None = None,
104  **kwargs: Any,
105  ) -> None:
106  """Async function to turn on the fan."""
107  payload = {"on": True}
108  await put_state(
109  self._session_session,
110  self._api_key_api_key_api_key,
111  self.unique_idunique_id,
112  json.dumps(payload),
113  )
114  await self.coordinator.async_request_refresh()
115 
116  async def async_turn_off(self, **kwargs: Any) -> None:
117  """Async function to turn off the fan."""
118  payload = {"on": False}
119  await put_state(
120  self._session_session,
121  self._api_key_api_key_api_key,
122  self.unique_idunique_id,
123  json.dumps(payload),
124  )
125  await self.coordinator.async_request_refresh()
126 
127  async def async_set_percentage(self, percentage: int) -> None:
128  """Set the speed percentage of the fan."""
129  payload = {"rotationSpeed": percentage}
130  await put_state(
131  self._session_session,
132  self._api_key_api_key_api_key,
133  self.unique_idunique_id,
134  json.dumps(payload),
135  )
136  await self.coordinator.async_request_refresh()
None async_set_percentage(self, int percentage)
Definition: fan.py:127
None __init__(self, HomeAssistant hass, str api_key, dict[str, Any] device, FreedomproDataUpdateCoordinator coordinator)
Definition: fan.py:51
None async_turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
Definition: fan.py:105
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: fan.py:25