Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for HomematicIP Cloud switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homematicip.aio.device import (
8  AsyncBrandSwitch2,
9  AsyncBrandSwitchMeasuring,
10  AsyncDinRailSwitch,
11  AsyncDinRailSwitch4,
12  AsyncFullFlushInputSwitch,
13  AsyncFullFlushSwitchMeasuring,
14  AsyncHeatingSwitch2,
15  AsyncMultiIOBox,
16  AsyncOpenCollector8Module,
17  AsyncPlugableSwitch,
18  AsyncPlugableSwitchMeasuring,
19  AsyncPrintedCircuitBoardSwitch2,
20  AsyncPrintedCircuitBoardSwitchBattery,
21  AsyncWiredSwitch8,
22 )
23 from homematicip.aio.group import AsyncExtendedLinkedSwitchingGroup, AsyncSwitchingGroup
24 
25 from homeassistant.components.switch import SwitchEntity
26 from homeassistant.config_entries import ConfigEntry
27 from homeassistant.core import HomeAssistant
28 from homeassistant.helpers.entity_platform import AddEntitiesCallback
29 
30 from .const import DOMAIN
31 from .entity import ATTR_GROUP_MEMBER_UNREACHABLE, HomematicipGenericEntity
32 from .hap import HomematicipHAP
33 
34 
36  hass: HomeAssistant,
37  config_entry: ConfigEntry,
38  async_add_entities: AddEntitiesCallback,
39 ) -> None:
40  """Set up the HomematicIP switch from a config entry."""
41  hap = hass.data[DOMAIN][config_entry.unique_id]
42  entities: list[HomematicipGenericEntity] = [
43  HomematicipGroupSwitch(hap, group)
44  for group in hap.home.groups
45  if isinstance(group, (AsyncExtendedLinkedSwitchingGroup, AsyncSwitchingGroup))
46  ]
47  for device in hap.home.devices:
48  if isinstance(device, AsyncBrandSwitchMeasuring):
49  # BrandSwitchMeasuring inherits PlugableSwitchMeasuring
50  # This entity is implemented in the light platform and will
51  # not be added in the switch platform
52  pass
53  elif isinstance(
54  device, (AsyncPlugableSwitchMeasuring, AsyncFullFlushSwitchMeasuring)
55  ):
56  entities.append(HomematicipSwitchMeasuring(hap, device))
57  elif isinstance(device, AsyncWiredSwitch8):
58  entities.extend(
59  HomematicipMultiSwitch(hap, device, channel=channel)
60  for channel in range(1, 9)
61  )
62  elif isinstance(device, AsyncDinRailSwitch):
63  entities.append(HomematicipMultiSwitch(hap, device, channel=1))
64  elif isinstance(device, AsyncDinRailSwitch4):
65  entities.extend(
66  HomematicipMultiSwitch(hap, device, channel=channel)
67  for channel in range(1, 5)
68  )
69  elif isinstance(
70  device,
71  (
72  AsyncPlugableSwitch,
73  AsyncPrintedCircuitBoardSwitchBattery,
74  AsyncFullFlushInputSwitch,
75  ),
76  ):
77  entities.append(HomematicipSwitch(hap, device))
78  elif isinstance(device, AsyncOpenCollector8Module):
79  entities.extend(
80  HomematicipMultiSwitch(hap, device, channel=channel)
81  for channel in range(1, 9)
82  )
83  elif isinstance(
84  device,
85  (
86  AsyncBrandSwitch2,
87  AsyncPrintedCircuitBoardSwitch2,
88  AsyncHeatingSwitch2,
89  AsyncMultiIOBox,
90  ),
91  ):
92  entities.extend(
93  HomematicipMultiSwitch(hap, device, channel=channel)
94  for channel in range(1, 3)
95  )
96 
97  async_add_entities(entities)
98 
99 
101  """Representation of the HomematicIP multi switch."""
102 
103  def __init__(
104  self,
105  hap: HomematicipHAP,
106  device,
107  channel=1,
108  is_multi_channel=True,
109  ) -> None:
110  """Initialize the multi switch device."""
111  super().__init__(
112  hap, device, channel=channel, is_multi_channel=is_multi_channel
113  )
114 
115  @property
116  def is_on(self) -> bool:
117  """Return true if switch is on."""
118  return self._device_device.functionalChannels[self._channel_channel].on
119 
120  async def async_turn_on(self, **kwargs: Any) -> None:
121  """Turn the switch on."""
122  await self._device_device.turn_on(self._channel_channel)
123 
124  async def async_turn_off(self, **kwargs: Any) -> None:
125  """Turn the switch off."""
126  await self._device_device.turn_off(self._channel_channel)
127 
128 
130  """Representation of the HomematicIP switch."""
131 
132  def __init__(self, hap: HomematicipHAP, device) -> None:
133  """Initialize the switch device."""
134  super().__init__(hap, device, is_multi_channel=False)
135 
136 
138  """Representation of the HomematicIP switching group."""
139 
140  def __init__(self, hap: HomematicipHAP, device, post: str = "Group") -> None:
141  """Initialize switching group."""
142  device.modelType = f"HmIP-{post}"
143  super().__init__(hap, device, post)
144 
145  @property
146  def is_on(self) -> bool:
147  """Return true if group is on."""
148  return self._device_device.on
149 
150  @property
151  def available(self) -> bool:
152  """Switch-Group available."""
153  # A switch-group must be available, and should not be affected by the
154  # individual availability of group members.
155  # This allows switching even when individual group members
156  # are not available.
157  return True
158 
159  @property
160  def extra_state_attributes(self) -> dict[str, Any]:
161  """Return the state attributes of the switch-group."""
162  state_attr = super().extra_state_attributes
163 
164  if self._device_device.unreach:
165  state_attr[ATTR_GROUP_MEMBER_UNREACHABLE] = True
166 
167  return state_attr
168 
169  async def async_turn_on(self, **kwargs: Any) -> None:
170  """Turn the group on."""
171  await self._device_device.turn_on()
172 
173  async def async_turn_off(self, **kwargs: Any) -> None:
174  """Turn the group off."""
175  await self._device_device.turn_off()
176 
177 
179  """Representation of the HomematicIP measuring switch."""
None __init__(self, HomematicipHAP hap, device, str post="Group")
Definition: switch.py:140
None __init__(self, HomematicipHAP hap, device, channel=1, is_multi_channel=True)
Definition: switch.py:109
None turn_off(self, **Any kwargs)
Definition: entity.py:1705
None turn_on(self, **Any kwargs)
Definition: entity.py:1697
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:39