Home Assistant Unofficial Reference 2024.12.1
fan.py
Go to the documentation of this file.
1 """Platform to control a Salda Smarty XP/XV ventilation unit."""
2 
3 from __future__ import annotations
4 
5 import logging
6 import math
7 from typing import Any
8 
9 from homeassistant.components.fan import FanEntity, FanEntityFeature
10 from homeassistant.core import HomeAssistant, callback
11 from homeassistant.exceptions import HomeAssistantError
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14  percentage_to_ranged_value,
15  ranged_value_to_percentage,
16 )
17 from homeassistant.util.scaling import int_states_in_range
18 
19 from . import SmartyConfigEntry
20 from .coordinator import SmartyCoordinator
21 from .entity import SmartyEntity
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 DEFAULT_ON_PERCENTAGE = 66
26 SPEED_RANGE = (1, 3) # off is not included
27 
28 
30  hass: HomeAssistant,
31  entry: SmartyConfigEntry,
32  async_add_entities: AddEntitiesCallback,
33 ) -> None:
34  """Set up the Smarty Fan Platform."""
35 
36  coordinator = entry.runtime_data
37 
38  async_add_entities([SmartyFan(coordinator)])
39 
40 
42  """Representation of a Smarty Fan."""
43 
44  _attr_name = None
45  _attr_translation_key = "fan"
46  _attr_supported_features = (
47  FanEntityFeature.SET_SPEED
48  | FanEntityFeature.TURN_OFF
49  | FanEntityFeature.TURN_ON
50  )
51  _enable_turn_on_off_backwards_compatibility = False
52 
53  def __init__(self, coordinator: SmartyCoordinator) -> None:
54  """Initialize the entity."""
55  super().__init__(coordinator)
56  self._smarty_fan_speed_smarty_fan_speed = 0
57  self._smarty_smarty = coordinator.client
58  self._attr_unique_id_attr_unique_id = coordinator.config_entry.entry_id
59 
60  @property
61  def is_on(self) -> bool:
62  """Return state of the fan."""
63  return bool(self._smarty_fan_speed_smarty_fan_speed)
64 
65  @property
66  def speed_count(self) -> int:
67  """Return the number of speeds the fan supports."""
68  return int_states_in_range(SPEED_RANGE)
69 
70  @property
71  def percentage(self) -> int:
72  """Return speed percentage of the fan."""
73  if self._smarty_fan_speed_smarty_fan_speed == 0:
74  return 0
75  return ranged_value_to_percentage(SPEED_RANGE, self._smarty_fan_speed_smarty_fan_speed)
76 
77  def set_percentage(self, percentage: int) -> None:
78  """Set the speed percentage of the fan."""
79  _LOGGER.debug("Set the fan percentage to %s", percentage)
80  if percentage == 0:
81  self.turn_offturn_offturn_off()
82  return
83 
84  fan_speed = math.ceil(percentage_to_ranged_value(SPEED_RANGE, percentage))
85  if not self._smarty_smarty.set_fan_speed(fan_speed):
86  raise HomeAssistantError(
87  f"Failed to set the fan speed percentage to {percentage}"
88  )
89 
90  self._smarty_fan_speed_smarty_fan_speed = fan_speed
91  self.schedule_update_ha_stateschedule_update_ha_state()
92 
93  def turn_on(
94  self,
95  percentage: int | None = None,
96  preset_mode: str | None = None,
97  **kwargs: Any,
98  ) -> None:
99  """Turn on the fan."""
100  _LOGGER.debug("Turning on fan. percentage is %s", percentage)
101  self.set_percentageset_percentageset_percentage(percentage or DEFAULT_ON_PERCENTAGE)
102 
103  def turn_off(self, **kwargs: Any) -> None:
104  """Turn off the fan."""
105  _LOGGER.debug("Turning off fan")
106  if not self._smarty_smarty.turn_off():
107  raise HomeAssistantError("Failed to turn off the fan")
108 
109  self._smarty_fan_speed_smarty_fan_speed = 0
110  self.schedule_update_ha_stateschedule_update_ha_state()
111 
112  @callback
113  def _handle_coordinator_update(self) -> None:
114  """Call update method."""
115  self._smarty_fan_speed_smarty_fan_speed = self._smarty_smarty.fan_speed
None set_percentage(self, int percentage)
Definition: __init__.py:336
None set_percentage(self, int percentage)
Definition: fan.py:77
None turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
Definition: fan.py:98
None __init__(self, SmartyCoordinator coordinator)
Definition: fan.py:53
None turn_off(self, **Any kwargs)
Definition: fan.py:103
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
None turn_off(self, **Any kwargs)
Definition: entity.py:1705
None async_setup_entry(HomeAssistant hass, SmartyConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: fan.py:33
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