1 """Creates HomeWizard Energy switch entities."""
3 from __future__
import annotations
5 from collections.abc
import Awaitable, Callable
6 from dataclasses
import dataclass
9 from homewizard_energy
import HomeWizardEnergyV1
14 SwitchEntityDescription,
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
29 @dataclass(frozen=True, kw_only=True)
31 """Class describing HomeWizard switch entities."""
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]]
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),
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),
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),
72 entry: HomeWizardConfigEntry,
73 async_add_entities: AddEntitiesCallback,
75 """Set up switches."""
78 for description
in SWITCHES
79 if description.create_fn(entry.runtime_data)
84 """Representation of a HomeWizard switch."""
86 entity_description: HomeWizardSwitchEntityDescription
90 coordinator: HWEnergyDeviceUpdateCoordinator,
91 description: HomeWizardSwitchEntityDescription,
93 """Initialize the switch."""
96 self.
_attr_unique_id_attr_unique_id = f
"{coordinator.config_entry.unique_id}_{description.key}"
100 """Return if entity is available."""
102 self.coordinator.data
107 """Return state of the switch."""
110 @homewizard_exception_handler
112 """Turn the switch on."""
116 @homewizard_exception_handler
118 """Turn the switch off."""
None __init__(self, HWEnergyDeviceUpdateCoordinator coordinator, HomeWizardSwitchEntityDescription description)
None async_turn_off(self, **Any kwargs)
None async_turn_on(self, **Any kwargs)
None async_setup_entry(HomeAssistant hass, HomeWizardConfigEntry entry, AddEntitiesCallback async_add_entities)