-
-
Notifications
You must be signed in to change notification settings - Fork 32k
/
Copy pathnumber.py
118 lines (96 loc) · 3.79 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
"""Number platform for Plugwise integration."""
from __future__ import annotations
from dataclasses import dataclass
from homeassistant.components.number import (
NumberDeviceClass,
NumberEntity,
NumberEntityDescription,
NumberMode,
)
from homeassistant.const import EntityCategory, UnitOfTemperature
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import PlugwiseConfigEntry
from .const import NumberType
from .coordinator import PlugwiseDataUpdateCoordinator
from .entity import PlugwiseEntity
from .util import plugwise_command
PARALLEL_UPDATES = 0
@dataclass(frozen=True, kw_only=True)
class PlugwiseNumberEntityDescription(NumberEntityDescription):
"""Class describing Plugwise Number entities."""
key: NumberType
NUMBER_TYPES = (
PlugwiseNumberEntityDescription(
key="maximum_boiler_temperature",
translation_key="maximum_boiler_temperature",
device_class=NumberDeviceClass.TEMPERATURE,
entity_category=EntityCategory.CONFIG,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
PlugwiseNumberEntityDescription(
key="max_dhw_temperature",
translation_key="max_dhw_temperature",
device_class=NumberDeviceClass.TEMPERATURE,
entity_category=EntityCategory.CONFIG,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
PlugwiseNumberEntityDescription(
key="temperature_offset",
translation_key="temperature_offset",
device_class=NumberDeviceClass.TEMPERATURE,
entity_category=EntityCategory.CONFIG,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: PlugwiseConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Plugwise number platform."""
coordinator = entry.runtime_data
@callback
def _add_entities() -> None:
"""Add Entities."""
if not coordinator.new_devices:
return
async_add_entities(
PlugwiseNumberEntity(coordinator, device_id, description)
for device_id in coordinator.new_devices
for description in NUMBER_TYPES
if description.key in coordinator.data.devices[device_id]
)
_add_entities()
entry.async_on_unload(coordinator.async_add_listener(_add_entities))
class PlugwiseNumberEntity(PlugwiseEntity, NumberEntity):
"""Representation of a Plugwise number."""
entity_description: PlugwiseNumberEntityDescription
def __init__(
self,
coordinator: PlugwiseDataUpdateCoordinator,
device_id: str,
description: PlugwiseNumberEntityDescription,
) -> None:
"""Initiate Plugwise Number."""
super().__init__(coordinator, device_id)
self._attr_mode = NumberMode.BOX
self._attr_native_max_value = self.device[description.key]["upper_bound"]
self._attr_native_min_value = self.device[description.key]["lower_bound"]
self._attr_unique_id = f"{device_id}-{description.key}"
self.device_id = device_id
self.entity_description = description
native_step = self.device[description.key]["resolution"]
if description.key != "temperature_offset":
native_step = max(native_step, 0.5)
self._attr_native_step = native_step
@property
def native_value(self) -> float:
"""Return the present setpoint value."""
return self.device[self.entity_description.key]["setpoint"]
@plugwise_command
async def async_set_native_value(self, value: float) -> None:
"""Change to the new setpoint value."""
await self.coordinator.api.set_number(
self.device_id, self.entity_description.key, value
)