-
-
Notifications
You must be signed in to change notification settings - Fork 32k
/
Copy pathswitch.py
123 lines (100 loc) · 3.51 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
"""Plugwise Switch component for HomeAssistant."""
from __future__ import annotations
from dataclasses import dataclass
from typing import Any
from plugwise.constants import SwitchType
from homeassistant.components.switch import (
SwitchDeviceClass,
SwitchEntity,
SwitchEntityDescription,
)
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import PlugwiseConfigEntry
from .coordinator import PlugwiseDataUpdateCoordinator
from .entity import PlugwiseEntity
from .util import plugwise_command
PARALLEL_UPDATES = 0
@dataclass(frozen=True)
class PlugwiseSwitchEntityDescription(SwitchEntityDescription):
"""Describes Plugwise switch entity."""
key: SwitchType
SWITCHES: tuple[PlugwiseSwitchEntityDescription, ...] = (
PlugwiseSwitchEntityDescription(
key="dhw_cm_switch",
translation_key="dhw_cm_switch",
entity_category=EntityCategory.CONFIG,
),
PlugwiseSwitchEntityDescription(
key="lock",
translation_key="lock",
entity_category=EntityCategory.CONFIG,
),
PlugwiseSwitchEntityDescription(
key="relay",
translation_key="relay",
device_class=SwitchDeviceClass.SWITCH,
),
PlugwiseSwitchEntityDescription(
key="cooling_ena_switch",
translation_key="cooling_ena_switch",
entity_category=EntityCategory.CONFIG,
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: PlugwiseConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Smile switches from a config entry."""
coordinator = entry.runtime_data
@callback
def _add_entities() -> None:
"""Add Entities."""
if not coordinator.new_devices:
return
async_add_entities(
PlugwiseSwitchEntity(coordinator, device_id, description)
for device_id in coordinator.new_devices
if (switches := coordinator.data.devices[device_id].get("switches"))
for description in SWITCHES
if description.key in switches
)
_add_entities()
entry.async_on_unload(coordinator.async_add_listener(_add_entities))
class PlugwiseSwitchEntity(PlugwiseEntity, SwitchEntity):
"""Representation of a Plugwise plug."""
entity_description: PlugwiseSwitchEntityDescription
def __init__(
self,
coordinator: PlugwiseDataUpdateCoordinator,
device_id: str,
description: PlugwiseSwitchEntityDescription,
) -> None:
"""Set up the Plugwise API."""
super().__init__(coordinator, device_id)
self._attr_unique_id = f"{device_id}-{description.key}"
self.entity_description = description
@property
def is_on(self) -> bool:
"""Return True if entity is on."""
return self.device["switches"][self.entity_description.key]
@plugwise_command
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the device on."""
await self.coordinator.api.set_switch_state(
self._dev_id,
self.device.get("members"),
self.entity_description.key,
"on",
)
@plugwise_command
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the device off."""
await self.coordinator.api.set_switch_state(
self._dev_id,
self.device.get("members"),
self.entity_description.key,
"off",
)