Home Assistant Unofficial Reference 2024.12.1
fan.py
Go to the documentation of this file.
1 """Support for Modern Forms Fan Fans."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aiomodernforms.const import FAN_POWER_OFF, FAN_POWER_ON
8 import voluptuous as vol
9 
10 from homeassistant.components.fan import FanEntity, FanEntityFeature
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers import entity_platform
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16  percentage_to_ranged_value,
17  ranged_value_to_percentage,
18 )
19 from homeassistant.util.scaling import int_states_in_range
20 
21 from . import modernforms_exception_handler
22 from .const import (
23  ATTR_SLEEP_TIME,
24  CLEAR_TIMER,
25  DOMAIN,
26  OPT_ON,
27  OPT_SPEED,
28  SERVICE_CLEAR_FAN_SLEEP_TIMER,
29  SERVICE_SET_FAN_SLEEP_TIMER,
30 )
31 from .coordinator import ModernFormsDataUpdateCoordinator
32 from .entity import ModernFormsDeviceEntity
33 
34 
36  hass: HomeAssistant,
37  config_entry: ConfigEntry,
38  async_add_entities: AddEntitiesCallback,
39 ) -> None:
40  """Set up a Modern Forms platform from config entry."""
41 
42  coordinator: ModernFormsDataUpdateCoordinator = hass.data[DOMAIN][
43  config_entry.entry_id
44  ]
45 
46  platform = entity_platform.async_get_current_platform()
47 
48  platform.async_register_entity_service(
49  SERVICE_SET_FAN_SLEEP_TIMER,
50  {
51  vol.Required(ATTR_SLEEP_TIME): vol.All(
52  vol.Coerce(int), vol.Range(min=1, max=1440)
53  ),
54  },
55  "async_set_fan_sleep_timer",
56  )
57 
58  platform.async_register_entity_service(
59  SERVICE_CLEAR_FAN_SLEEP_TIMER,
60  None,
61  "async_clear_fan_sleep_timer",
62  )
63 
65  [ModernFormsFanEntity(entry_id=config_entry.entry_id, coordinator=coordinator)]
66  )
67 
68 
70  """Defines a Modern Forms light."""
71 
72  SPEED_RANGE = (1, 6) # off is not included
73 
74  _attr_supported_features = (
75  FanEntityFeature.DIRECTION
76  | FanEntityFeature.SET_SPEED
77  | FanEntityFeature.TURN_OFF
78  | FanEntityFeature.TURN_ON
79  )
80  _attr_translation_key = "fan"
81  _enable_turn_on_off_backwards_compatibility = False
82 
83  def __init__(
84  self, entry_id: str, coordinator: ModernFormsDataUpdateCoordinator
85  ) -> None:
86  """Initialize Modern Forms light."""
87  super().__init__(
88  entry_id=entry_id,
89  coordinator=coordinator,
90  )
91  self._attr_unique_id_attr_unique_id = f"{self.coordinator.data.info.mac_address}"
92 
93  @property
94  def percentage(self) -> int | None:
95  """Return the current speed percentage."""
96  percentage = 0
97  if bool(self.coordinator.data.state.fan_on):
98  percentage = ranged_value_to_percentage(
99  self.SPEED_RANGESPEED_RANGE, self.coordinator.data.state.fan_speed
100  )
101  return percentage
102 
103  @property
104  def current_direction(self) -> str:
105  """Return the current direction of the fan."""
106  return self.coordinator.data.state.fan_direction
107 
108  @property
109  def speed_count(self) -> int:
110  """Return the number of speeds the fan supports."""
111  return int_states_in_range(self.SPEED_RANGESPEED_RANGE)
112 
113  @property
114  def is_on(self) -> bool:
115  """Return the state of the fan."""
116  return bool(self.coordinator.data.state.fan_on)
117 
118  @modernforms_exception_handler
119  async def async_set_direction(self, direction: str) -> None:
120  """Set the direction of the fan."""
121  await self.coordinator.modern_forms.fan(direction=direction)
122 
123  @modernforms_exception_handler
124  async def async_set_percentage(self, percentage: int) -> None:
125  """Set the speed percentage of the fan."""
126  if percentage > 0:
127  await self.async_turn_onasync_turn_onasync_turn_onasync_turn_on(percentage=percentage)
128  else:
129  await self.async_turn_offasync_turn_offasync_turn_off()
130 
131  @modernforms_exception_handler
132  async def async_turn_on(
133  self,
134  percentage: int | None = None,
135  preset_mode: str | None = None,
136  **kwargs: Any,
137  ) -> None:
138  """Turn on the fan."""
139  data = {OPT_ON: FAN_POWER_ON}
140 
141  if percentage:
142  data[OPT_SPEED] = round(
143  percentage_to_ranged_value(self.SPEED_RANGESPEED_RANGE, percentage)
144  )
145  await self.coordinator.modern_forms.fan(**data)
146 
147  @modernforms_exception_handler
148  async def async_turn_off(self, **kwargs: Any) -> None:
149  """Turn the fan off."""
150  await self.coordinator.modern_forms.fan(on=FAN_POWER_OFF)
151 
152  @modernforms_exception_handler
154  self,
155  sleep_time: int,
156  ) -> None:
157  """Set a Modern Forms light sleep timer."""
158  await self.coordinator.modern_forms.fan(sleep=sleep_time * 60)
159 
160  @modernforms_exception_handler
162  self,
163  ) -> None:
164  """Clear a Modern Forms fan sleep timer."""
165  await self.coordinator.modern_forms.fan(sleep=CLEAR_TIMER)
None async_turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
Definition: __init__.py:437
None __init__(self, str entry_id, ModernFormsDataUpdateCoordinator coordinator)
Definition: fan.py:85
None async_turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
Definition: fan.py:137
None async_turn_off(self, **Any kwargs)
Definition: entity.py:1709
None async_turn_on(self, **Any kwargs)
Definition: entity.py:1701
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: fan.py:39
float percentage_to_ranged_value(tuple[float, float] low_high_range, float percentage)
Definition: percentage.py:81
int ranged_value_to_percentage(tuple[float, float] low_high_range, float value)
Definition: percentage.py:64
int int_states_in_range(tuple[float, float] low_high_range)
Definition: scaling.py:61