-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
switch.py
125 lines (96 loc) · 4.01 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
121
122
123
124
125
from typing import Optional
from typing import cast
from homeassistant.components.switch import SwitchDeviceClass
from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from plugp100.new.child.tapohubchildren import SwitchChildDevice, KE100Device
from custom_components.tapo.const import DOMAIN
from custom_components.tapo.coordinators import HassTapoDeviceData, TapoDataCoordinator
from custom_components.tapo.entity import CoordinatedTapoEntity
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
):
data = cast(HassTapoDeviceData, hass.data[DOMAIN][entry.entry_id])
for child_coordinator in data.child_coordinators:
device = child_coordinator.device
if isinstance(device, SwitchChildDevice):
async_add_entities([
SwitchTapoChild(child_coordinator, device)
], True)
elif isinstance(device, KE100Device):
async_add_entities([
TRVFrostProtection(child_coordinator, device),
TRVChildLock(child_coordinator, device)
], True)
class SwitchTapoChild(CoordinatedTapoEntity, SwitchEntity):
_attr_device_class = SwitchDeviceClass.OUTLET
device: SwitchChildDevice
def __init__(
self,
coordinator: TapoDataCoordinator,
device: SwitchChildDevice
) -> None:
super().__init__(coordinator, device)
@property
def is_on(self) -> Optional[bool]:
return self.device.is_on
async def async_turn_on(self, **kwargs):
(await self.device.on()).get_or_raise()
await self.coordinator.async_request_refresh()
async def async_turn_off(self, **kwargs):
(await self.device.off()).get_or_raise()
await self.coordinator.async_request_refresh()
# Perhaps unneeded as Climate Entity provides this functionality
# These can be extracted into common functionality
class TRVFrostProtection(CoordinatedTapoEntity, SwitchEntity):
_attr_has_entity_name = True
_attr_name = "Frost Protection"
device: KE100Device
def __init__(
self,
coordinator: TapoDataCoordinator,
device: KE100Device
) -> None:
super().__init__(coordinator, device)
@property
def unique_id(self):
return super().unique_id + "_" + self._attr_name.replace(" ", "_")
@property
def is_on(self) -> Optional[bool]:
return self.device.is_frost_protection_on == 1
@property
def device_class(self) -> Optional[str]:
return SwitchDeviceClass.SWITCH
async def async_turn_on(self, **kwargs):
(await self.device.set_frost_protection_on()).get_or_raise()
await self.coordinator.async_request_refresh()
async def async_turn_off(self, **kwargs):
(await self.device.set_frost_protection_off()).get_or_raise()
await self.coordinator.async_request_refresh()
class TRVChildLock(CoordinatedTapoEntity, SwitchEntity):
_attr_has_entity_name = True
_attr_name = "Child Lock"
device: KE100Device
def __init__(
self,
coordinator: TapoDataCoordinator,
device: KE100Device
) -> None:
super().__init__(coordinator, device)
@property
def unique_id(self):
return super().unique_id + "_" + self._attr_name.replace(" ", "_")
@property
def device_class(self) -> Optional[str]:
return SwitchDeviceClass.SWITCH
@property
def is_on(self) -> Optional[bool]:
return self.device.is_child_protection_on == 1
async def async_turn_on(self, **kwargs):
(await self.device.set_child_protection_on()).get_or_raise()
await self.coordinator.async_request_refresh()
async def async_turn_off(self, **kwargs):
(await self.device.set_child_protection_off()).get_or_raise()
await self.coordinator.async_request_refresh()