Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Big Ass Fans switch."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any, cast
8 
9 from aiobafi6 import Device
10 
11 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
12 from homeassistant.const import EntityCategory
13 from homeassistant.core import HomeAssistant, callback
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from . import BAFConfigEntry
17 from .entity import BAFDescriptionEntity
18 
19 
20 @dataclass(frozen=True, kw_only=True)
22  SwitchEntityDescription,
23 ):
24  """Class describing BAF switch entities."""
25 
26  value_fn: Callable[[Device], bool | None]
27 
28 
29 BASE_SWITCHES = [
31  key="legacy_ir_remote_enable",
32  translation_key="legacy_ir_remote_enable",
33  entity_category=EntityCategory.CONFIG,
34  value_fn=lambda device: cast(bool | None, device.legacy_ir_remote_enable),
35  ),
37  key="led_indicators_enable",
38  translation_key="led_indicators_enable",
39  entity_category=EntityCategory.CONFIG,
40  value_fn=lambda device: cast(bool | None, device.led_indicators_enable),
41  ),
42 ]
43 
44 AUTO_COMFORT_SWITCHES = [
46  key="comfort_heat_assist_enable",
47  translation_key="comfort_heat_assist_enable",
48  entity_category=EntityCategory.CONFIG,
49  value_fn=lambda device: cast(bool | None, device.comfort_heat_assist_enable),
50  ),
51 ]
52 
53 FAN_SWITCHES = [
55  key="fan_beep_enable",
56  translation_key="fan_beep_enable",
57  entity_category=EntityCategory.CONFIG,
58  value_fn=lambda device: cast(bool | None, device.fan_beep_enable),
59  ),
61  key="eco_enable",
62  translation_key="eco_enable",
63  entity_category=EntityCategory.CONFIG,
64  value_fn=lambda device: cast(bool | None, device.eco_enable),
65  ),
67  key="motion_sense_enable",
68  translation_key="motion_sense_enable",
69  entity_category=EntityCategory.CONFIG,
70  value_fn=lambda device: cast(bool | None, device.motion_sense_enable),
71  ),
73  key="return_to_auto_enable",
74  translation_key="return_to_auto_enable",
75  entity_category=EntityCategory.CONFIG,
76  value_fn=lambda device: cast(bool | None, device.return_to_auto_enable),
77  ),
79  key="whoosh_enable",
80  translation_key="whoosh_enable",
81  # Not a configuration switch
82  value_fn=lambda device: cast(bool | None, device.whoosh_enable),
83  ),
84 ]
85 
86 
87 LIGHT_SWITCHES = [
89  key="light_dim_to_warm_enable",
90  translation_key="light_dim_to_warm_enable",
91  entity_category=EntityCategory.CONFIG,
92  value_fn=lambda device: cast(bool | None, device.light_dim_to_warm_enable),
93  ),
95  key="light_return_to_auto_enable",
96  translation_key="light_return_to_auto_enable",
97  entity_category=EntityCategory.CONFIG,
98  value_fn=lambda device: cast(bool | None, device.light_return_to_auto_enable),
99  ),
100 ]
101 
102 
104  hass: HomeAssistant,
105  entry: BAFConfigEntry,
106  async_add_entities: AddEntitiesCallback,
107 ) -> None:
108  """Set up BAF fan switches."""
109  device = entry.runtime_data
110  descriptions: list[BAFSwitchDescription] = []
111  descriptions.extend(BASE_SWITCHES)
112  if device.has_fan:
113  descriptions.extend(FAN_SWITCHES)
114  if device.has_light:
115  descriptions.extend(LIGHT_SWITCHES)
116  if device.has_auto_comfort:
117  descriptions.extend(AUTO_COMFORT_SWITCHES)
118  async_add_entities(BAFSwitch(device, description) for description in descriptions)
119 
120 
122  """BAF switch component."""
123 
124  entity_description: BAFSwitchDescription
125 
126  @callback
127  def _async_update_attrs(self) -> None:
128  """Update attrs from device."""
129  self._attr_is_on_attr_is_on = self.entity_descriptionentity_description.value_fn(self._device_device)
130 
131  async def async_turn_on(self, **kwargs: Any) -> None:
132  """Turn on the switch."""
133  setattr(self._device_device, self.entity_descriptionentity_description.key, True)
134 
135  async def async_turn_off(self, **kwargs: Any) -> None:
136  """Turn off the switch."""
137  setattr(self._device_device, self.entity_descriptionentity_description.key, False)
None async_turn_on(self, **Any kwargs)
Definition: switch.py:131
None async_turn_off(self, **Any kwargs)
Definition: switch.py:135
None async_setup_entry(HomeAssistant hass, BAFConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:107