Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for SLZB-06 switches."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 import logging
8 from typing import Any
9 
10 from pysmlight import Sensors, SettingsEvent
11 from pysmlight.const import Settings
12 
14  SwitchDeviceClass,
15  SwitchEntity,
16  SwitchEntityDescription,
17 )
18 from homeassistant.const import EntityCategory
19 from homeassistant.core import HomeAssistant, callback
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 
22 from . import SmConfigEntry
23 from .coordinator import SmDataUpdateCoordinator
24 from .entity import SmEntity
25 
26 _LOGGER = logging.getLogger(__name__)
27 
28 
29 @dataclass(frozen=True, kw_only=True)
31  """Class to describe a Switch entity."""
32 
33  setting: Settings
34  state_fn: Callable[[Sensors], bool | None]
35 
36 
37 SWITCHES: list[SmSwitchEntityDescription] = [
39  key="disable_led",
40  translation_key="disable_led",
41  setting=Settings.DISABLE_LEDS,
42  state_fn=lambda x: x.disable_leds,
43  ),
45  key="night_mode",
46  translation_key="night_mode",
47  setting=Settings.NIGHT_MODE,
48  state_fn=lambda x: x.night_mode,
49  ),
51  key="auto_zigbee_update",
52  translation_key="auto_zigbee_update",
53  entity_category=EntityCategory.CONFIG,
54  setting=Settings.ZB_AUTOUPDATE,
55  entity_registry_enabled_default=False,
56  state_fn=lambda x: x.auto_zigbee,
57  ),
59  key="vpn_enabled",
60  translation_key="vpn_enabled",
61  setting=Settings.ENABLE_VPN,
62  entity_registry_enabled_default=False,
63  state_fn=lambda x: x.vpn_enabled,
64  ),
65 ]
66 
67 
69  hass: HomeAssistant,
70  entry: SmConfigEntry,
71  async_add_entities: AddEntitiesCallback,
72 ) -> None:
73  """Initialize switches for SLZB-06 device."""
74  coordinator = entry.runtime_data.data
75 
76  async_add_entities(SmSwitch(coordinator, switch) for switch in SWITCHES)
77 
78 
80  """Representation of a SLZB-06 switch."""
81 
82  coordinator: SmDataUpdateCoordinator
83  entity_description: SmSwitchEntityDescription
84  _attr_device_class = SwitchDeviceClass.SWITCH
85 
86  def __init__(
87  self,
88  coordinator: SmDataUpdateCoordinator,
89  description: SmSwitchEntityDescription,
90  ) -> None:
91  """Initialize the switch."""
92  super().__init__(coordinator)
93  self.entity_descriptionentity_description = description
94  self._attr_unique_id_attr_unique_id = f"{coordinator.unique_id}-{description.key}"
95 
96  self._page, self._toggle_toggle = description.setting.value
97 
98  async def async_added_to_hass(self) -> None:
99  """Run when entity about to be added to hass."""
100  await super().async_added_to_hass()
101  self.async_on_removeasync_on_remove(
102  self.coordinator.client.sse.register_settings_cb(
103  self.entity_descriptionentity_description.setting, self.event_callbackevent_callback
104  )
105  )
106 
107  async def set_smlight(self, state: bool) -> None:
108  """Set the state on SLZB device."""
109  await self.coordinator.client.set_toggle(self._page, self._toggle_toggle, state)
110 
111  @callback
112  def event_callback(self, event: SettingsEvent) -> None:
113  """Handle switch events from the SLZB device."""
114  if event.setting is not None:
115  self.coordinator.update_setting(
116  self.entity_descriptionentity_description.setting, event.setting[self._toggle_toggle]
117  )
118 
119  async def async_turn_on(self, **kwargs: Any) -> None:
120  """Turn the switch on."""
121  await self.set_smlightset_smlight(True)
122 
123  async def async_turn_off(self, **kwargs: Any) -> None:
124  """Turn the switch off."""
125  await self.set_smlightset_smlight(False)
126 
127  @property
128  def is_on(self) -> bool | None:
129  """Return the state of the switch."""
130  return self.entity_descriptionentity_description.state_fn(self.coordinator.data.sensors)
None __init__(self, SmDataUpdateCoordinator coordinator, SmSwitchEntityDescription description)
Definition: switch.py:90
None async_turn_off(self, **Any kwargs)
Definition: switch.py:123
None async_turn_on(self, **Any kwargs)
Definition: switch.py:119
None event_callback(self, SettingsEvent event)
Definition: switch.py:112
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_setup_entry(HomeAssistant hass, SmConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:72