Home Assistant Unofficial Reference 2024.12.1
fan.py
Go to the documentation of this file.
1 """Support for INSTEON fans via PowerLinc Modem."""
2 
3 from __future__ import annotations
4 
5 import math
6 from typing import Any
7 
8 from homeassistant.components.fan import FanEntity, FanEntityFeature
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import Platform
11 from homeassistant.core import HomeAssistant, callback
12 from homeassistant.helpers.dispatcher import async_dispatcher_connect
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15  percentage_to_ranged_value,
16  ranged_value_to_percentage,
17 )
18 
19 from .const import SIGNAL_ADD_ENTITIES
20 from .entity import InsteonEntity
21 from .utils import async_add_insteon_devices, async_add_insteon_entities
22 
23 SPEED_RANGE = (1, 255) # off is not included
24 
25 
27  hass: HomeAssistant,
28  config_entry: ConfigEntry,
29  async_add_entities: AddEntitiesCallback,
30 ) -> None:
31  """Set up the Insteon fans from a config entry."""
32 
33  @callback
34  def async_add_insteon_fan_entities(discovery_info=None):
35  """Add the Insteon entities for the platform."""
37  hass, Platform.FAN, InsteonFanEntity, async_add_entities, discovery_info
38  )
39 
40  signal = f"{SIGNAL_ADD_ENTITIES}_{Platform.FAN}"
41  async_dispatcher_connect(hass, signal, async_add_insteon_fan_entities)
43  hass,
44  Platform.FAN,
45  InsteonFanEntity,
46  async_add_entities,
47  )
48 
49 
51  """An INSTEON fan entity."""
52 
53  _attr_supported_features = (
54  FanEntityFeature.SET_SPEED
55  | FanEntityFeature.TURN_OFF
56  | FanEntityFeature.TURN_ON
57  )
58  _attr_speed_count = 3
59  _enable_turn_on_off_backwards_compatibility = False
60 
61  @property
62  def percentage(self) -> int | None:
63  """Return the current speed percentage."""
64  if self._insteon_device_group_insteon_device_group.value is None:
65  return None
66  return ranged_value_to_percentage(SPEED_RANGE, self._insteon_device_group_insteon_device_group.value)
67 
68  async def async_turn_on(
69  self,
70  percentage: int | None = None,
71  preset_mode: str | None = None,
72  **kwargs: Any,
73  ) -> None:
74  """Turn on the fan."""
75  await self.async_set_percentageasync_set_percentageasync_set_percentage(percentage or 67)
76 
77  async def async_turn_off(self, **kwargs: Any) -> None:
78  """Turn off the fan."""
79  await self._insteon_device_insteon_device.async_fan_off()
80 
81  async def async_set_percentage(self, percentage: int) -> None:
82  """Set the speed percentage of the fan."""
83  if percentage == 0:
84  await self.async_turn_offasync_turn_offasync_turn_off()
85  return
86  on_level = math.ceil(percentage_to_ranged_value(SPEED_RANGE, percentage))
87  await self._insteon_device_insteon_device.async_on(group=2, on_level=on_level)
None async_set_percentage(self, int percentage)
Definition: __init__.py:340
None async_set_percentage(self, int percentage)
Definition: fan.py:81
None async_turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
Definition: fan.py:73
None async_turn_off(self, **Any kwargs)
Definition: entity.py:1709
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: fan.py:30
None async_add_insteon_devices(HomeAssistant hass, Platform platform, type[InsteonEntity] entity_type, AddEntitiesCallback async_add_entities)
Definition: utils.py:436
None async_add_insteon_entities(HomeAssistant hass, Platform platform, type[InsteonEntity] entity_type, AddEntitiesCallback async_add_entities, dict[str, Any] discovery_info)
Definition: utils.py:420
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103
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