Home Assistant Unofficial Reference 2024.12.1
fan.py
Go to the documentation of this file.
1 """Support for Lutron Caseta fans."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from pylutron_caseta import FAN_HIGH, FAN_LOW, FAN_MEDIUM, FAN_MEDIUM_HIGH, FAN_OFF
8 
10  DOMAIN as FAN_DOMAIN,
11  FanEntity,
12  FanEntityFeature,
13 )
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17  ordered_list_item_to_percentage,
18  percentage_to_ordered_list_item,
19 )
20 
21 from .entity import LutronCasetaUpdatableEntity
22 from .models import LutronCasetaConfigEntry
23 
24 DEFAULT_ON_PERCENTAGE = 50
25 ORDERED_NAMED_FAN_SPEEDS = [FAN_LOW, FAN_MEDIUM, FAN_MEDIUM_HIGH, FAN_HIGH]
26 
27 
29  hass: HomeAssistant,
30  config_entry: LutronCasetaConfigEntry,
31  async_add_entities: AddEntitiesCallback,
32 ) -> None:
33  """Set up the Lutron Caseta fan platform.
34 
35  Adds fan controllers from the Caseta bridge associated with the config_entry
36  as fan entities.
37  """
38  data = config_entry.runtime_data
39  bridge = data.bridge
40  fan_devices = bridge.get_devices_by_domain(FAN_DOMAIN)
41  async_add_entities(LutronCasetaFan(fan_device, data) for fan_device in fan_devices)
42 
43 
45  """Representation of a Lutron Caseta fan. Including Fan Speed."""
46 
47  _attr_supported_features = (
48  FanEntityFeature.SET_SPEED
49  | FanEntityFeature.TURN_OFF
50  | FanEntityFeature.TURN_ON
51  )
52  _attr_speed_count = len(ORDERED_NAMED_FAN_SPEEDS)
53  _enable_turn_on_off_backwards_compatibility = False
54 
55  @property
56  def percentage(self) -> int | None:
57  """Return the current speed percentage."""
58  if self._device_device_device["fan_speed"] is None:
59  return None
60  if self._device_device_device["fan_speed"] == FAN_OFF:
61  return 0
62  return ordered_list_item_to_percentage(
63  ORDERED_NAMED_FAN_SPEEDS, self._device_device_device["fan_speed"]
64  )
65 
66  async def async_turn_on(
67  self,
68  percentage: int | None = None,
69  preset_mode: str | None = None,
70  **kwargs: Any,
71  ) -> None:
72  """Turn the fan on."""
73  if percentage is None:
74  percentage = DEFAULT_ON_PERCENTAGE
75 
76  await self.async_set_percentageasync_set_percentageasync_set_percentage(percentage)
77 
78  async def async_turn_off(self, **kwargs: Any) -> None:
79  """Turn the fan off."""
80  await self.async_set_percentageasync_set_percentageasync_set_percentage(0)
81 
82  async def async_set_percentage(self, percentage: int) -> None:
83  """Set the speed of the fan."""
84  if percentage == 0:
85  named_speed = FAN_OFF
86  else:
87  named_speed = percentage_to_ordered_list_item(
88  ORDERED_NAMED_FAN_SPEEDS, percentage
89  )
90 
91  await self._smartbridge_smartbridge.set_fan(self.device_iddevice_id, named_speed)
92 
93  @property
94  def is_on(self) -> bool:
95  """Return true if device is on."""
96  return bool(self.percentagepercentagepercentage)
None async_set_percentage(self, int percentage)
Definition: __init__.py:340
None async_set_percentage(self, int percentage)
Definition: fan.py:82
None async_turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
Definition: fan.py:71
None async_setup_entry(HomeAssistant hass, LutronCasetaConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: fan.py:32