-
-
Notifications
You must be signed in to change notification settings - Fork 28.6k
/
switch.py
138 lines (116 loc) · 4.76 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
126
127
128
129
130
131
132
133
134
135
136
137
138
"""Support for Overkiz switches."""
from __future__ import annotations
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
from typing import Any
from pyoverkiz.enums import OverkizCommand, OverkizCommandParam, OverkizState
from pyoverkiz.enums.ui import UIClass, UIWidget
from pyoverkiz.types import StateType as OverkizStateType
from homeassistant.components.switch import (
SwitchDeviceClass,
SwitchEntity,
SwitchEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import HomeAssistantOverkizData
from .const import DOMAIN
from .entity import OverkizDescriptiveEntity
@dataclass
class OverkizSwitchDescriptionMixin:
"""Define an entity description mixin for switch entities."""
turn_on: Callable[[Callable[..., Awaitable[None]]], Awaitable[None]]
turn_off: Callable[[Callable[..., Awaitable[None]]], Awaitable[None]]
is_on: Callable[[Callable[[str], OverkizStateType]], bool]
@dataclass
class OverkizSwitchDescription(SwitchEntityDescription, OverkizSwitchDescriptionMixin):
"""Class to describe an Overkiz switch."""
SWITCH_DESCRIPTIONS: list[OverkizSwitchDescription] = [
OverkizSwitchDescription(
key=UIWidget.DOMESTIC_HOT_WATER_TANK,
turn_on=lambda execute_command: execute_command(
OverkizCommand.SET_FORCE_HEATING, OverkizCommandParam.ON
),
turn_off=lambda execute_command: execute_command(
OverkizCommand.SET_FORCE_HEATING, OverkizCommandParam.OFF
),
is_on=lambda select_state: (
select_state(OverkizState.IO_FORCE_HEATING) == OverkizCommandParam.ON
),
icon="mdi:water-boiler",
),
OverkizSwitchDescription(
key=UIClass.ON_OFF,
turn_on=lambda execute_command: execute_command(OverkizCommand.ON),
turn_off=lambda execute_command: execute_command(OverkizCommand.OFF),
is_on=lambda select_state: (
select_state(OverkizState.CORE_ON_OFF) == OverkizCommandParam.ON
),
device_class=SwitchDeviceClass.OUTLET,
),
OverkizSwitchDescription(
key=UIClass.SWIMMING_POOL,
turn_on=lambda execute_command: execute_command(OverkizCommand.ON),
turn_off=lambda execute_command: execute_command(OverkizCommand.OFF),
is_on=lambda select_state: (
select_state(OverkizState.CORE_ON_OFF) == OverkizCommandParam.ON
),
icon="mdi:pool",
),
OverkizSwitchDescription(
key=UIWidget.RTD_INDOOR_SIREN,
turn_on=lambda execute_command: execute_command(OverkizCommand.ON),
turn_off=lambda execute_command: execute_command(OverkizCommand.OFF),
is_on=lambda select_state: (
select_state(OverkizState.CORE_ON_OFF) == OverkizCommandParam.ON
),
icon="mdi:bell",
),
OverkizSwitchDescription(
key=UIWidget.RTD_OUTDOOR_SIREN,
turn_on=lambda execute_command: execute_command(OverkizCommand.ON),
turn_off=lambda execute_command: execute_command(OverkizCommand.OFF),
is_on=lambda select_state: (
select_state(OverkizState.CORE_ON_OFF) == OverkizCommandParam.ON
),
icon="mdi:bell",
),
]
SUPPORTED_DEVICES = {
description.key: description for description in SWITCH_DESCRIPTIONS
}
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Overkiz switch from a config entry."""
data: HomeAssistantOverkizData = hass.data[DOMAIN][entry.entry_id]
entities: list[OverkizSwitch] = []
for device in data.platforms[Platform.SWITCH]:
if description := SUPPORTED_DEVICES.get(device.widget) or SUPPORTED_DEVICES.get(
device.ui_class
):
entities.append(
OverkizSwitch(
device.device_url,
data.coordinator,
description,
)
)
async_add_entities(entities)
class OverkizSwitch(OverkizDescriptiveEntity, SwitchEntity):
"""Representation of an Overkiz Switch."""
entity_description: OverkizSwitchDescription
@property
def is_on(self) -> bool:
"""Return True if entity is on."""
return self.entity_description.is_on(self.executor.select_state)
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the entity on."""
await self.entity_description.turn_on(self.executor.async_execute_command)
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the entity off."""
await self.entity_description.turn_off(self.executor.async_execute_command)