-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.py
158 lines (130 loc) · 4.8 KB
/
number.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""CleverTouch number entities"""
from typing import Optional, Callable, Any, Awaitable
import logging
from math import ceil
from homeassistant.components.number import (
NumberEntityDescription,
NumberEntity,
NumberDeviceClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from clevertouch.devices import Radiator, Device
from clevertouch.devices.radiator import Temperature
from .const import (
DOMAIN,
TEMP_HA_UNIT,
TEMP_NATIVE_UNIT,
TEMP_NATIVE_STEP,
TEMP_NATIVE_MIN,
TEMP_NATIVE_MAX,
)
from .coordinator import CleverTouchUpdateCoordinator, CleverTouchEntity
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up CleverTouch number entities."""
coordinator: CleverTouchUpdateCoordinator = hass.data[DOMAIN].get(entry.entry_id)
entities: list[NumberEntity] = []
entities.extend(
[
TemperatureNumberEntity(coordinator, device, temp.name)
for home in coordinator.homes.values()
for device in home.devices.values()
if isinstance(device, Radiator)
for temp in device.temperatures.values()
if temp.is_writable and temp.name
]
)
def _get_boost_time(dev: Device) -> Optional[int]:
return (
ceil(dev.boost_time / (60.0 * 60.0)) if isinstance(dev, Radiator) else None
)
async def _set_boost_time(dev: Device, value: int) -> None:
if isinstance(dev, Radiator):
await dev.set_boost_time(value * 60 * 60)
entities.extend(
[
CleverNumberEntity(
coordinator,
device,
NumberEntityDescription(
name="Boost time preset",
key="boost_time_preset",
native_min_value=0,
native_step=1,
native_unit_of_measurement="h",
),
_get_boost_time,
_set_boost_time,
)
for home in coordinator.homes.values()
for device in home.devices.values()
if isinstance(device, Radiator)
]
)
async_add_entities(entities)
class TemperatureNumberEntity(CleverTouchEntity, NumberEntity):
"""Representation of a CleverTouch configurable temperature."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: CleverTouchUpdateCoordinator,
radiator: Radiator,
temp_name: str,
) -> None:
super().__init__(coordinator, radiator)
self._temp_name = temp_name
self._radiator = radiator
self.entity_description = NumberEntityDescription(
icon="mdi:thermometer",
name=f"{self._temp_name} temperature",
key=f"temp_{self._temp_name}",
device_class=NumberDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_HA_UNIT,
native_step=TEMP_NATIVE_STEP,
native_max_value=TEMP_NATIVE_MAX,
native_min_value=TEMP_NATIVE_MIN,
)
@property
def native_value(self) -> Optional[float]:
temp = self._radiator.temperatures[self._temp_name].as_unit(TEMP_NATIVE_UNIT)
if isinstance(temp, float):
temp = round(temp, 1)
return temp
async def async_set_native_value(self, value: float) -> None:
if value == self.native_value:
return
await self._radiator.set_temperature(self._temp_name, value, TEMP_NATIVE_UNIT)
self._radiator.temperatures[self._temp_name] = Temperature(
value,
TEMP_NATIVE_UNIT,
is_writable=True,
name=self._temp_name,
)
await self.coordinator.async_request_delayed_refresh()
class CleverNumberEntity(CleverTouchEntity, NumberEntity):
"""Representation of a CleverTouch read-only duration."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: CleverTouchUpdateCoordinator,
device: Device,
description: NumberEntityDescription,
getter: Callable[[Device], Any],
setter: Callable[[Device, Any], Awaitable[None]],
) -> None:
super().__init__(coordinator, device)
self._get_value = getter
self._set_value = setter
self.entity_description = description
@property
def native_value(self) -> Any:
return self._get_value(self.device)
async def async_set_native_value(self, value: Any) -> None:
if value == self.native_value:
return
await self._set_value(self.device, value)
await self.coordinator.async_request_delayed_refresh()