-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.py
150 lines (127 loc) · 4.58 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
"""Number entities for SlxChargingController"""
from __future__ import annotations
import logging
from typing import Final
from dataclasses import dataclass
from homeassistant.components.number import NumberEntity, NumberEntityDescription
from homeassistant.const import PERCENTAGE
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import (
DOMAIN,
SOC_LIMIT_MIN,
SOC_LIMIT_MAX,
ENT_SOC_LIMIT_MIN,
ENT_SOC_LIMIT_MAX,
CMD_SOC_MIN,
CMD_SOC_MAX,
SOC_TARGET,
ENT_SOC_TARGET,
CMD_SOC_TARGET,
)
from .coordinator import SLXChgCtrlUpdateCoordinator
from .entity import SlxChgCtrlEntity
_LOGGER = logging.getLogger(__name__)
@dataclass
class SLXNumberEntityDescription(NumberEntityDescription):
"""Extend default NumberEntityDescription to include options and command which should be called in coordinator"""
command: str | None = None
data_entry: str | None = None
NUMBER_DESCRIPTIONS: Final[tuple[SLXNumberEntityDescription, ...]] = (
SLXNumberEntityDescription(
key=SOC_LIMIT_MIN,
command=CMD_SOC_MIN,
data_entry=ENT_SOC_LIMIT_MIN,
name="SOC Limit min",
icon="mdi:ev-plug-type2",
native_min_value=0,
native_max_value=100,
native_step=5,
native_unit_of_measurement=PERCENTAGE,
),
SLXNumberEntityDescription(
key=SOC_LIMIT_MAX,
command=CMD_SOC_MAX,
data_entry=ENT_SOC_LIMIT_MAX,
name="SOC Limit max",
icon="mdi:ev-plug-type2",
native_min_value=0,
native_max_value=100,
native_step=5,
native_unit_of_measurement=PERCENTAGE,
),
SLXNumberEntityDescription(
key=SOC_TARGET,
command=CMD_SOC_TARGET,
data_entry=ENT_SOC_TARGET,
name="SOC target",
icon="mdi:ev-plug-type2",
native_min_value=0,
native_max_value=100,
native_step=1,
native_unit_of_measurement=PERCENTAGE,
),
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
coordinator = hass.data[DOMAIN][config_entry.unique_id]
entities = []
for description in NUMBER_DESCRIPTIONS:
entities.append(SlxChgCtrlNumber(coordinator, description))
async_add_entities(entities)
return True
# class representing entity
class SlxChgCtrlNumber(NumberEntity, SlxChgCtrlEntity):
"""number entities for Salix Charging Controller"""
def __init__(
self,
coordinator: SLXChgCtrlUpdateCoordinator,
description: SLXNumberEntityDescription,
) -> None:
super().__init__(coordinator)
self._description = description
self._key = self._description.key
self._command = self._description.command
self._data_entry = self._description.data_entry
self._attr_unique_id = f"{DOMAIN}_{self._key}"
self._attr_icon = self._description.icon
self._attr_name = f"{self._description.name}"
self._attr_device_class = self._description.device_class
@property
def native_min_value(self):
"""Return native_min_value as reported in by the sensor"""
return self._description.native_min_value
@property
def native_max_value(self):
"""Returnnative_max_value as reported in by the sensor"""
return self._description.native_max_value
@property
def native_step(self):
"""Return step value as reported in by the sensor"""
return self._description.native_step
@property
def native_unit_of_measurement(self):
"""Return the unit the value was reported in by the sensor"""
return self._description.native_unit_of_measurement
@property
def native_value(self) -> float | None:
"""Return the entity value to represent the entity state."""
coordinator: SLXChgCtrlUpdateCoordinator = self.coordinator
data = coordinator.data
if self._data_entry in data and data is not None:
state = data[self._data_entry]
return float(state)
return None
async def async_set_native_value(self, value: float) -> None:
coordinator: SLXChgCtrlUpdateCoordinator = self.coordinator
try:
await getattr(coordinator, self._command)(value)
self.async_write_ha_state()
except (ValueError, KeyError, AttributeError) as err:
_LOGGER.warning(
"Could not set status for %s error: %s", self._attr_name, err
)