Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Platform for switch integration."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import Any
7 
8 from boschshcpy import (
9  SHCCamera360,
10  SHCCameraEyes,
11  SHCLightSwitch,
12  SHCSession,
13  SHCSmartPlug,
14  SHCSmartPlugCompact,
15 )
16 from boschshcpy.device import SHCDevice
17 
19  SwitchDeviceClass,
20  SwitchEntity,
21  SwitchEntityDescription,
22 )
23 from homeassistant.config_entries import ConfigEntry
24 from homeassistant.const import EntityCategory
25 from homeassistant.core import HomeAssistant
26 from homeassistant.helpers.entity_platform import AddEntitiesCallback
27 from homeassistant.helpers.typing import StateType
28 
29 from .const import DATA_SESSION, DOMAIN
30 from .entity import SHCEntity
31 
32 
33 @dataclass(frozen=True, kw_only=True)
35  """Class describing SHC switch entities."""
36 
37  on_key: str
38  on_value: StateType
39  should_poll: bool
40 
41 
42 SWITCH_TYPES: dict[str, SHCSwitchEntityDescription] = {
43  "smartplug": SHCSwitchEntityDescription(
44  key="smartplug",
45  device_class=SwitchDeviceClass.OUTLET,
46  on_key="switchstate",
47  on_value=SHCSmartPlug.PowerSwitchService.State.ON,
48  should_poll=False,
49  ),
50  "smartplugcompact": SHCSwitchEntityDescription(
51  key="smartplugcompact",
52  device_class=SwitchDeviceClass.OUTLET,
53  on_key="switchstate",
54  on_value=SHCSmartPlugCompact.PowerSwitchService.State.ON,
55  should_poll=False,
56  ),
57  "lightswitch": SHCSwitchEntityDescription(
58  key="lightswitch",
59  device_class=SwitchDeviceClass.SWITCH,
60  on_key="switchstate",
61  on_value=SHCLightSwitch.PowerSwitchService.State.ON,
62  should_poll=False,
63  ),
64  "cameraeyes": SHCSwitchEntityDescription(
65  key="cameraeyes",
66  device_class=SwitchDeviceClass.SWITCH,
67  on_key="cameralight",
68  on_value=SHCCameraEyes.CameraLightService.State.ON,
69  should_poll=True,
70  ),
71  "camera360": SHCSwitchEntityDescription(
72  key="camera360",
73  device_class=SwitchDeviceClass.SWITCH,
74  on_key="privacymode",
75  on_value=SHCCamera360.PrivacyModeService.State.DISABLED,
76  should_poll=True,
77  ),
78 }
79 
80 
82  hass: HomeAssistant,
83  config_entry: ConfigEntry,
84  async_add_entities: AddEntitiesCallback,
85 ) -> None:
86  """Set up the SHC switch platform."""
87  session: SHCSession = hass.data[DOMAIN][config_entry.entry_id][DATA_SESSION]
88 
89  entities: list[SwitchEntity] = [
90  SHCSwitch(
91  device=switch,
92  parent_id=session.information.unique_id,
93  entry_id=config_entry.entry_id,
94  description=SWITCH_TYPES["smartplug"],
95  )
96  for switch in session.device_helper.smart_plugs
97  ]
98 
99  entities.extend(
101  device=switch,
102  parent_id=session.information.unique_id,
103  entry_id=config_entry.entry_id,
104  )
105  for switch in session.device_helper.smart_plugs
106  )
107 
108  entities.extend(
109  SHCSwitch(
110  device=switch,
111  parent_id=session.information.unique_id,
112  entry_id=config_entry.entry_id,
113  description=SWITCH_TYPES["lightswitch"],
114  )
115  for switch in session.device_helper.light_switches_bsm
116  )
117 
118  entities.extend(
119  SHCSwitch(
120  device=switch,
121  parent_id=session.information.unique_id,
122  entry_id=config_entry.entry_id,
123  description=SWITCH_TYPES["smartplugcompact"],
124  )
125  for switch in session.device_helper.smart_plugs_compact
126  )
127 
128  entities.extend(
129  SHCSwitch(
130  device=switch,
131  parent_id=session.information.unique_id,
132  entry_id=config_entry.entry_id,
133  description=SWITCH_TYPES["cameraeyes"],
134  )
135  for switch in session.device_helper.camera_eyes
136  )
137 
138  entities.extend(
139  SHCSwitch(
140  device=switch,
141  parent_id=session.information.unique_id,
142  entry_id=config_entry.entry_id,
143  description=SWITCH_TYPES["camera360"],
144  )
145  for switch in session.device_helper.camera_360
146  )
147 
148  async_add_entities(entities)
149 
150 
152  """Representation of a SHC switch."""
153 
154  entity_description: SHCSwitchEntityDescription
155 
156  def __init__(
157  self,
158  device: SHCDevice,
159  parent_id: str,
160  entry_id: str,
161  description: SHCSwitchEntityDescription,
162  ) -> None:
163  """Initialize a SHC switch."""
164  super().__init__(device, parent_id, entry_id)
165  self.entity_descriptionentity_description = description
166 
167  @property
168  def is_on(self) -> bool:
169  """Return the state of the switch."""
170  return (
171  getattr(self._device_device, self.entity_descriptionentity_description.on_key)
172  == self.entity_descriptionentity_description.on_value
173  )
174 
175  def turn_on(self, **kwargs: Any) -> None:
176  """Turn the switch on."""
177  setattr(self._device_device, self.entity_descriptionentity_description.on_key, True)
178 
179  def turn_off(self, **kwargs: Any) -> None:
180  """Turn the switch off."""
181  setattr(self._device_device, self.entity_descriptionentity_description.on_key, False)
182 
183  @property
184  def should_poll(self) -> bool:
185  """Switch needs polling."""
186  return self.entity_descriptionentity_description.should_poll
187 
188  def update(self) -> None:
189  """Trigger an update of the device."""
190  self._device_device.update()
191 
192 
194  """Representation of a SHC routing switch."""
195 
196  _attr_translation_key = "routing"
197  _attr_entity_category = EntityCategory.CONFIG
198 
199  def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
200  """Initialize an SHC communication quality reporting sensor."""
201  super().__init__(device, parent_id, entry_id)
202  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{device.serial}_routing"
203 
204  @property
205  def is_on(self) -> bool:
206  """Return the state of the switch."""
207  return self._device_device.routing.name == "ENABLED"
208 
209  def turn_on(self, **kwargs: Any) -> None:
210  """Turn the switch on."""
211  self._device_device.routing = True
212 
213  def turn_off(self, **kwargs: Any) -> None:
214  """Turn the switch off."""
215  self._device_device.routing = False
None __init__(self, SHCDevice device, str parent_id, str entry_id)
Definition: switch.py:199
None __init__(self, SHCDevice device, str parent_id, str entry_id, SHCSwitchEntityDescription description)
Definition: switch.py:162
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:85