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