-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.py
98 lines (81 loc) · 3.3 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
from .const import DOMAIN
from .modbus_data_coordinator import ModbusDataCoordinator, ModbusInfo
from homeassistant.components.number import ConfigType, NumberEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import DiscoveryInfoType
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.helpers import device_registry as dr
from homeassistant.const import UnitOfTime
from homeassistant.helpers.entity import EntityCategory
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities) -> None:
device: dr.DeviceEntry = hass.data[DOMAIN][entry.entry_id].get("device")
coordinator: ModbusDataCoordinator = hass.data[DOMAIN][entry.entry_id].get("coordinator")
sensors = [
SleepFuctionDurationSensor(coordinator, device),
]
async_add_entities(sensors, update_before_add=False)
class PowerboxNumber(CoordinatorEntity, NumberEntity, ModbusInfo):
def __init__(self, coordinator: ModbusDataCoordinator, device: dr.DeviceEntry):
self._device = device
self._attr_unique_id = f"{self._device.name.lower()}_{self.id}"
self._attr_name = self.name
self.entity_id = f"number.{self._attr_unique_id}"
super().__init__(coordinator)
@property
def device_info(self) -> DeviceInfo:
return {
"name": self._device.name,
"manufacturer": self._device.manufacturer,
"model": self._device.model,
"identifiers": self._device.identifiers,
"connections": self._device.connections,
"sw_version": self._device.sw_version,
"hw_version": self._device.hw_version,
}
@property
def unique_id(self):
return f"{self.id}_{self._device.id}"
@property
def state(self):
if self.coordinator.data is not None and self.address in self.coordinator.data.keys():
value = self.coordinator.data[self.address]
return value * self.scale if value is not None else None
else:
return None
class SleepFuctionDurationSensor(PowerboxNumber):
def __init__(self, coordinator: ModbusDataCoordinator, device: dr.DeviceEntry):
self._attr_entity_category = EntityCategory.CONFIG
self._attr_native_unit_of_measurement = UnitOfTime.MINUTES
super().__init__(coordinator, device)
@property
def min_value(self) -> float:
return 5
@property
def maxn_value(self) -> float:
return 90
@property
def step(self) -> float:
return 5
@property
def id(self):
return "sleep_function_duration"
@property
def name(self):
return "Dauer Einschlaffunktion"
@property
def icon(self):
return "mdi:clock-start"
@property
def address(self) -> int:
return 160
@property
def state(self):
if self.coordinator.data is not None and self.address in self.coordinator.data.keys():
value = self.coordinator.data[self.address]
return value if value is not None else None
else:
return None
def set_value(self, value: float) -> None:
self.coordinator.write(self.address, int(value))