Home Assistant Unofficial Reference 2024.12.1
fan.py
Go to the documentation of this file.
1 """Support for Big Ass Fans fan."""
2 
3 from __future__ import annotations
4 
5 import math
6 from typing import Any
7 
8 from aiobafi6 import OffOnAuto
9 
10 from homeassistant.components.fan import (
11  DIRECTION_FORWARD,
12  DIRECTION_REVERSE,
13  FanEntity,
14  FanEntityFeature,
15 )
16 from homeassistant.core import HomeAssistant, callback
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19  percentage_to_ranged_value,
20  ranged_value_to_percentage,
21 )
22 
23 from . import BAFConfigEntry
24 from .const import PRESET_MODE_AUTO, SPEED_COUNT, SPEED_RANGE
25 from .entity import BAFEntity
26 
27 
29  hass: HomeAssistant,
30  entry: BAFConfigEntry,
31  async_add_entities: AddEntitiesCallback,
32 ) -> None:
33  """Set up SenseME fans."""
34  device = entry.runtime_data
35  if device.has_fan:
36  async_add_entities([BAFFan(device)])
37 
38 
40  """BAF ceiling fan component."""
41 
42  _attr_supported_features = (
43  FanEntityFeature.SET_SPEED
44  | FanEntityFeature.DIRECTION
45  | FanEntityFeature.PRESET_MODE
46  | FanEntityFeature.TURN_OFF
47  | FanEntityFeature.TURN_ON
48  )
49  _enable_turn_on_off_backwards_compatibility = False
50  _attr_preset_modes = [PRESET_MODE_AUTO]
51  _attr_speed_count = SPEED_COUNT
52  _attr_name = None
53  _attr_translation_key = "baf"
54 
55  @callback
56  def _async_update_attrs(self) -> None:
57  """Update attrs from device."""
58  self._attr_is_on_attr_is_on = self._device_device.fan_mode == OffOnAuto.ON
59  self._attr_current_direction_attr_current_direction = DIRECTION_FORWARD
60  if self._device_device.reverse_enable:
61  self._attr_current_direction_attr_current_direction = DIRECTION_REVERSE
62  if self._device_device.speed is not None:
64  SPEED_RANGE, self._device_device.speed
65  )
66  else:
67  self._attr_percentage_attr_percentage = None
68  auto = self._device_device.fan_mode == OffOnAuto.AUTO
69  self._attr_preset_mode_attr_preset_mode = PRESET_MODE_AUTO if auto else None
70  super()._async_update_attrs()
71 
72  async def async_set_percentage(self, percentage: int) -> None:
73  """Set the speed of the fan, as a percentage."""
74  device = self._device_device
75  if device.fan_mode != OffOnAuto.ON:
76  device.fan_mode = OffOnAuto.ON
77  device.speed = math.ceil(percentage_to_ranged_value(SPEED_RANGE, percentage))
78 
79  async def async_turn_on(
80  self,
81  percentage: int | None = None,
82  preset_mode: str | None = None,
83  **kwargs: Any,
84  ) -> None:
85  """Turn the fan on with a percentage or preset mode."""
86  if preset_mode is not None:
87  await self.async_set_preset_modeasync_set_preset_modeasync_set_preset_mode(preset_mode)
88  return
89  if percentage is None:
90  self._device_device.fan_mode = OffOnAuto.ON
91  return
92  await self.async_set_percentageasync_set_percentageasync_set_percentage(percentage)
93 
94  async def async_turn_off(self, **kwargs: Any) -> None:
95  """Turn the fan off."""
96  self._device_device.fan_mode = OffOnAuto.OFF
97 
98  async def async_set_preset_mode(self, preset_mode: str) -> None:
99  """Set the preset mode of the fan."""
100  self._device_device.fan_mode = OffOnAuto.AUTO
101 
102  async def async_set_direction(self, direction: str) -> None:
103  """Set the direction of the fan."""
104  self._device_device.reverse_enable = direction == DIRECTION_REVERSE
None async_set_preset_mode(self, str preset_mode)
Definition: fan.py:98
None async_turn_off(self, **Any kwargs)
Definition: fan.py:94
None async_set_percentage(self, int percentage)
Definition: fan.py:72
None async_turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
Definition: fan.py:84
None async_set_direction(self, str direction)
Definition: fan.py:102
None async_set_percentage(self, int percentage)
Definition: __init__.py:340
None async_set_preset_mode(self, str preset_mode)
Definition: __init__.py:385
None async_setup_entry(HomeAssistant hass, BAFConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: fan.py:32
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