forked from wlcrs/huawei_solar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.py
306 lines (255 loc) · 10.6 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""This component provides number entities for Huawei Solar."""
from __future__ import annotations
import logging
from dataclasses import dataclass
from homeassistant.components.number import (
NumberEntity,
NumberEntityDescription,
NumberMode,
)
from homeassistant.components.number.const import DEFAULT_MIN_VALUE, DEFAULT_MAX_VALUE
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, POWER_WATT
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from huawei_solar import HuaweiSolarBridge
from huawei_solar import register_names as rn
from huawei_solar import register_values as rv
from . import (
HuaweiSolarConfigurationUpdateCoordinator,
HuaweiSolarEntity,
HuaweiSolarUpdateCoordinator,
)
from .const import (
CONF_ENABLE_PARAMETER_CONFIGURATION,
DATA_CONFIGURATION_UPDATE_COORDINATORS,
DATA_UPDATE_COORDINATORS,
DOMAIN,
)
_LOGGER = logging.getLogger(__name__)
@dataclass
class HuaweiSolarNumberEntityDescription(NumberEntityDescription):
"""Huawei Solar Number Entity Description."""
# Used when the min/max cannot dynamically change
static_minimum_key: str | None = None
static_maximum_key: str | None = None
# Used when the min/max is influenced by other parameters
dynamic_minimum_key: str | None = None
dynamic_maximum_key: str | None = None
ENERGY_STORAGE_NUMBER_DESCRIPTIONS: tuple[HuaweiSolarNumberEntityDescription, ...] = (
HuaweiSolarNumberEntityDescription(
key=rn.STORAGE_MAXIMUM_CHARGING_POWER,
native_min_value=0,
static_maximum_key=rn.STORAGE_MAXIMUM_CHARGE_POWER,
name="Maximum charging power",
icon="mdi:battery-positive",
native_unit_of_measurement=POWER_WATT,
entity_category=EntityCategory.CONFIG,
),
HuaweiSolarNumberEntityDescription(
key=rn.STORAGE_MAXIMUM_DISCHARGING_POWER,
native_min_value=0,
static_maximum_key=rn.STORAGE_MAXIMUM_DISCHARGE_POWER,
name="Maximum discharging power",
icon="mdi:battery-negative",
native_unit_of_measurement=POWER_WATT,
entity_category=EntityCategory.CONFIG,
),
HuaweiSolarNumberEntityDescription(
key=rn.STORAGE_CHARGING_CUTOFF_CAPACITY,
native_min_value=90,
native_max_value=100,
native_step=0.1,
name="End-of-charge SOC",
icon="mdi:battery-positive",
native_unit_of_measurement=PERCENTAGE,
entity_category=EntityCategory.CONFIG,
),
HuaweiSolarNumberEntityDescription(
key=rn.STORAGE_DISCHARGING_CUTOFF_CAPACITY,
native_min_value=0,
native_max_value=20,
dynamic_maximum_key=rn.STORAGE_CAPACITY_CONTROL_SOC_PEAK_SHAVING,
native_step=0.1,
name="End-of-discharge SOC",
icon="mdi:battery-negative",
native_unit_of_measurement=PERCENTAGE,
entity_category=EntityCategory.CONFIG,
),
HuaweiSolarNumberEntityDescription(
key=rn.STORAGE_GRID_CHARGE_CUTOFF_STATE_OF_CHARGE,
native_min_value=20,
native_max_value=100,
native_step=0.1,
name="Grid charge cutoff SOC",
icon="mdi:battery-charging-50",
native_unit_of_measurement=PERCENTAGE,
entity_category=EntityCategory.CONFIG,
),
HuaweiSolarNumberEntityDescription(
key=rn.STORAGE_POWER_OF_CHARGE_FROM_GRID,
native_min_value=0,
dynamic_maximum_key=rn.STORAGE_MAXIMUM_POWER_OF_CHARGE_FROM_GRID,
name="Grid charge maximum power",
icon="mdi:battery-negative",
native_unit_of_measurement=POWER_WATT,
entity_category=EntityCategory.CONFIG,
),
)
CAPACITY_CONTROL_NUMBER_DESCRIPTIONS: tuple[HuaweiSolarNumberEntityDescription, ...] = (
HuaweiSolarNumberEntityDescription(
key=rn.STORAGE_CAPACITY_CONTROL_SOC_PEAK_SHAVING,
dynamic_minimum_key=rn.STORAGE_DISCHARGING_CUTOFF_CAPACITY,
native_max_value=100,
native_step=0.1,
name="Peak Shaving SOC",
icon="mdi:battery-arrow-up",
native_unit_of_measurement=PERCENTAGE,
entity_category=EntityCategory.CONFIG,
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Huawei Solar Number entities Setup."""
if not entry.data.get(CONF_ENABLE_PARAMETER_CONFIGURATION):
_LOGGER.info("Skipping number setup, as parameter configuration is not enabled")
return
update_coordinators = hass.data[DOMAIN][entry.entry_id][
DATA_UPDATE_COORDINATORS
] # type: list[HuaweiSolarUpdateCoordinator]
configuration_update_coordinators = hass.data[DOMAIN][entry.entry_id][
DATA_CONFIGURATION_UPDATE_COORDINATORS
] # type: list[HuaweiSolarConfigurationUpdateCoordinator]
# When more than one inverter is present, then we suffix all sensors with '#1', '#2', ...
# The order for these suffixes is the order in which the user entered the slave-ids.
must_append_inverter_suffix = len(update_coordinators) > 1
entities_to_add: list[NumberEntity] = []
for idx, (update_coordinator, configuration_update_coordinator) in enumerate(
zip(update_coordinators, configuration_update_coordinators)
):
slave_entities: list[HuaweiSolarNumberEntity] = []
bridge = update_coordinator.bridge
device_infos = update_coordinator.device_infos
if bridge.battery_type != rv.StorageProductModel.NONE:
assert device_infos["connected_energy_storage"]
for entity_description in ENERGY_STORAGE_NUMBER_DESCRIPTIONS:
slave_entities.append(
await HuaweiSolarNumberEntity.create(
configuration_update_coordinator,
bridge,
entity_description,
device_infos["connected_energy_storage"],
)
)
if bridge.supports_capacity_control:
for entity_description in CAPACITY_CONTROL_NUMBER_DESCRIPTIONS:
slave_entities.append(
await HuaweiSolarNumberEntity.create(
configuration_update_coordinator,
bridge,
entity_description,
device_infos["connected_energy_storage"],
)
)
else:
_LOGGER.debug(
"No battery detected on slave %s. Skipping energy storage number entities",
bridge.slave_id,
)
# Add suffix if multiple inverters are present
if must_append_inverter_suffix:
for entity in slave_entities:
entity.add_name_suffix(f" #{idx+1}")
entities_to_add.extend(slave_entities)
async_add_entities(entities_to_add)
class HuaweiSolarNumberEntity(CoordinatorEntity, HuaweiSolarEntity, NumberEntity):
"""Huawei Solar Number Entity."""
entity_description: HuaweiSolarNumberEntityDescription
def __init__(
self,
coordinator: HuaweiSolarConfigurationUpdateCoordinator,
bridge: HuaweiSolarBridge,
description: HuaweiSolarNumberEntityDescription,
device_info: DeviceInfo,
) -> None:
"""Huawei Solar Number Entity constructor.
Do not use directly. Use `.create` instead!
"""
super().__init__(coordinator)
self.coordinator = coordinator
self.bridge = bridge
self.entity_description = description
self._attr_device_info = device_info
self._attr_unique_id = f"{bridge.serial_number}_{description.key}"
self._attr_mode = NumberMode.BOX # Always allow a precise number
self._dynamic_min_value: float | None = None
self._dynamic_max_value: float | None = None
@classmethod
async def create(
cls,
coordinator: HuaweiSolarConfigurationUpdateCoordinator,
bridge: HuaweiSolarBridge,
description: HuaweiSolarNumberEntityDescription,
device_info: DeviceInfo,
):
"""Huawei Solar Number Entity constructor.
This async constructor fills in the necessary min/max values
"""
if description.static_minimum_key:
description.native_min_value = (
await bridge.client.get(description.static_minimum_key)
).value
if description.static_maximum_key:
description.native_max_value = (
await bridge.client.get(description.static_maximum_key)
).value
return cls(coordinator, bridge, description, device_info)
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._attr_native_value = self.coordinator.data[
self.entity_description.key
].value
if self.entity_description.dynamic_minimum_key:
min_register = self.coordinator.data.get(
self.entity_description.dynamic_minimum_key
)
if min_register:
self._dynamic_min_value = min_register.value
if self.entity_description.dynamic_maximum_key:
max_register = self.coordinator.data.get(
self.entity_description.dynamic_maximum_key
)
if max_register:
self._dynamic_max_value = max_register.value
self.async_write_ha_state()
async def async_set_native_value(self, value: float) -> None:
"""Set a new value."""
if await self.bridge.set(self.entity_description.key, int(value)):
self._attr_native_value = int(value)
await self.coordinator.async_request_refresh()
@property
def native_max_value(self) -> float:
native_max_value = self.entity_description.native_max_value
if self._dynamic_max_value:
if native_max_value:
return min(self._dynamic_max_value, native_max_value)
return self._dynamic_max_value
if native_max_value:
return native_max_value
return DEFAULT_MAX_VALUE
@property
def native_min_value(self) -> float:
native_min_value = self.entity_description.native_min_value
if self._dynamic_min_value:
if native_min_value:
return max(self._dynamic_min_value, native_min_value)
return self._dynamic_min_value
if native_min_value:
return native_min_value
return DEFAULT_MIN_VALUE