Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Plugwise Switch component for HomeAssistant."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import Any
7 
8 from plugwise.constants import SwitchType
9 
11  SwitchDeviceClass,
12  SwitchEntity,
13  SwitchEntityDescription,
14 )
15 from homeassistant.const import EntityCategory
16 from homeassistant.core import HomeAssistant, callback
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from . import PlugwiseConfigEntry
20 from .coordinator import PlugwiseDataUpdateCoordinator
21 from .entity import PlugwiseEntity
22 from .util import plugwise_command
23 
24 
25 @dataclass(frozen=True)
27  """Describes Plugwise switch entity."""
28 
29  key: SwitchType
30 
31 
32 SWITCHES: tuple[PlugwiseSwitchEntityDescription, ...] = (
34  key="dhw_cm_switch",
35  translation_key="dhw_cm_switch",
36  entity_category=EntityCategory.CONFIG,
37  ),
39  key="lock",
40  translation_key="lock",
41  entity_category=EntityCategory.CONFIG,
42  ),
44  key="relay",
45  translation_key="relay",
46  device_class=SwitchDeviceClass.SWITCH,
47  ),
49  key="cooling_ena_switch",
50  translation_key="cooling_ena_switch",
51  name="Cooling",
52  entity_category=EntityCategory.CONFIG,
53  ),
54 )
55 
56 
58  hass: HomeAssistant,
59  entry: PlugwiseConfigEntry,
60  async_add_entities: AddEntitiesCallback,
61 ) -> None:
62  """Set up the Smile switches from a config entry."""
63  coordinator = entry.runtime_data
64 
65  @callback
66  def _add_entities() -> None:
67  """Add Entities."""
68  if not coordinator.new_devices:
69  return
70 
72  PlugwiseSwitchEntity(coordinator, device_id, description)
73  for device_id in coordinator.new_devices
74  if (switches := coordinator.data.devices[device_id].get("switches"))
75  for description in SWITCHES
76  if description.key in switches
77  )
78 
79  _add_entities()
80  entry.async_on_unload(coordinator.async_add_listener(_add_entities))
81 
82 
84  """Representation of a Plugwise plug."""
85 
86  entity_description: PlugwiseSwitchEntityDescription
87 
88  def __init__(
89  self,
90  coordinator: PlugwiseDataUpdateCoordinator,
91  device_id: str,
92  description: PlugwiseSwitchEntityDescription,
93  ) -> None:
94  """Set up the Plugwise API."""
95  super().__init__(coordinator, device_id)
96  self._attr_unique_id_attr_unique_id = f"{device_id}-{description.key}"
97  self.entity_descriptionentity_description = description
98 
99  @property
100  def is_on(self) -> bool:
101  """Return True if entity is on."""
102  return self.devicedevice["switches"][self.entity_descriptionentity_description.key]
103 
104  @plugwise_command
105  async def async_turn_on(self, **kwargs: Any) -> None:
106  """Turn the device on."""
107  await self.coordinator.api.set_switch_state(
108  self._dev_id_dev_id,
109  self.devicedevice.get("members"),
110  self.entity_descriptionentity_description.key,
111  "on",
112  )
113 
114  @plugwise_command
115  async def async_turn_off(self, **kwargs: Any) -> None:
116  """Turn the device off."""
117  await self.coordinator.api.set_switch_state(
118  self._dev_id_dev_id,
119  self.devicedevice.get("members"),
120  self.entity_descriptionentity_description.key,
121  "off",
122  )
None __init__(self, PlugwiseDataUpdateCoordinator coordinator, str device_id, PlugwiseSwitchEntityDescription description)
Definition: switch.py:93
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, PlugwiseConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:61