Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Creates HomeWizard Energy switch entities."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Awaitable, Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from homewizard_energy import HomeWizardEnergyV1
10 
12  SwitchDeviceClass,
13  SwitchEntity,
14  SwitchEntityDescription,
15 )
16 from homeassistant.const import EntityCategory
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from . import HomeWizardConfigEntry
21 from .const import DeviceResponseEntry
22 from .coordinator import HWEnergyDeviceUpdateCoordinator
23 from .entity import HomeWizardEntity
24 from .helpers import homewizard_exception_handler
25 
26 PARALLEL_UPDATES = 1
27 
28 
29 @dataclass(frozen=True, kw_only=True)
31  """Class describing HomeWizard switch entities."""
32 
33  available_fn: Callable[[DeviceResponseEntry], bool]
34  create_fn: Callable[[HWEnergyDeviceUpdateCoordinator], bool]
35  is_on_fn: Callable[[DeviceResponseEntry], bool | None]
36  set_fn: Callable[[HomeWizardEnergyV1, bool], Awaitable[Any]]
37 
38 
39 SWITCHES = [
41  key="power_on",
42  name=None,
43  device_class=SwitchDeviceClass.OUTLET,
44  create_fn=lambda coordinator: coordinator.supports_state(),
45  available_fn=lambda data: data.state is not None and not data.state.switch_lock,
46  is_on_fn=lambda data: data.state.power_on if data.state else None,
47  set_fn=lambda api, active: api.state_set(power_on=active),
48  ),
50  key="switch_lock",
51  translation_key="switch_lock",
52  entity_category=EntityCategory.CONFIG,
53  create_fn=lambda coordinator: coordinator.supports_state(),
54  available_fn=lambda data: data.state is not None,
55  is_on_fn=lambda data: data.state.switch_lock if data.state else None,
56  set_fn=lambda api, active: api.state_set(switch_lock=active),
57  ),
59  key="cloud_connection",
60  translation_key="cloud_connection",
61  entity_category=EntityCategory.CONFIG,
62  create_fn=lambda _: True,
63  available_fn=lambda data: data.system is not None,
64  is_on_fn=lambda data: data.system.cloud_enabled if data.system else None,
65  set_fn=lambda api, active: api.system_set(cloud_enabled=active),
66  ),
67 ]
68 
69 
71  hass: HomeAssistant,
72  entry: HomeWizardConfigEntry,
73  async_add_entities: AddEntitiesCallback,
74 ) -> None:
75  """Set up switches."""
77  HomeWizardSwitchEntity(entry.runtime_data, description)
78  for description in SWITCHES
79  if description.create_fn(entry.runtime_data)
80  )
81 
82 
84  """Representation of a HomeWizard switch."""
85 
86  entity_description: HomeWizardSwitchEntityDescription
87 
88  def __init__(
89  self,
90  coordinator: HWEnergyDeviceUpdateCoordinator,
91  description: HomeWizardSwitchEntityDescription,
92  ) -> None:
93  """Initialize the switch."""
94  super().__init__(coordinator)
95  self.entity_descriptionentity_description = description
96  self._attr_unique_id_attr_unique_id = f"{coordinator.config_entry.unique_id}_{description.key}"
97 
98  @property
99  def available(self) -> bool:
100  """Return if entity is available."""
101  return super().available and self.entity_descriptionentity_description.available_fn(
102  self.coordinator.data
103  )
104 
105  @property
106  def is_on(self) -> bool | None:
107  """Return state of the switch."""
108  return self.entity_descriptionentity_description.is_on_fn(self.coordinator.data)
109 
110  @homewizard_exception_handler
111  async def async_turn_on(self, **kwargs: Any) -> None:
112  """Turn the switch on."""
113  await self.entity_descriptionentity_description.set_fn(self.coordinator.api, True)
114  await self.coordinator.async_refresh()
115 
116  @homewizard_exception_handler
117  async def async_turn_off(self, **kwargs: Any) -> None:
118  """Turn the switch off."""
119  await self.entity_descriptionentity_description.set_fn(self.coordinator.api, False)
120  await self.coordinator.async_refresh()
None __init__(self, HWEnergyDeviceUpdateCoordinator coordinator, HomeWizardSwitchEntityDescription description)
Definition: switch.py:92
None async_setup_entry(HomeAssistant hass, HomeWizardConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:74