forked from wlcrs/huawei_solar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.py
275 lines (213 loc) · 9.92 KB
/
select.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
"""Switch entities for Huawei Solar."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from enum import IntEnum
import logging
from typing import TYPE_CHECKING, Any, Generic, TypeVar
from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.config_entries import ConfigEntry
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, register_names as rn, register_values as rv
from huawei_solar.registers import REGISTERS
from . import HuaweiSolarConfigurationUpdateCoordinator, HuaweiSolarEntity
from .const import (
CONF_ENABLE_PARAMETER_CONFIGURATION,
DATA_CONFIGURATION_UPDATE_COORDINATORS,
DATA_UPDATE_COORDINATORS,
DOMAIN,
)
if TYPE_CHECKING:
from . import HuaweiSolarUpdateCoordinator
_LOGGER = logging.getLogger(__name__)
T = TypeVar("T")
@dataclass
class HuaweiSolarSelectEntityDescription(Generic[T], SelectEntityDescription):
"""Huawei Solar Select Entity Description."""
is_available_key: str | None = None
check_is_available_func: Callable[[Any], bool] | None = None
def __post_init__(self):
"""Defaults the translation_key to the select key."""
self.translation_key = self.translation_key or self.key.replace('#','_').lower()
ENERGY_STORAGE_SWITCH_DESCRIPTIONS: tuple[HuaweiSolarSelectEntityDescription, ...] = (
HuaweiSolarSelectEntityDescription(
key=rn.STORAGE_EXCESS_PV_ENERGY_USE_IN_TOU,
icon="mdi:battery-charging-medium",
entity_category=EntityCategory.CONFIG,
),
)
CAPACITY_CONTROL_SWITCH_DESCRIPTIONS: tuple[HuaweiSolarSelectEntityDescription, ...] = (
HuaweiSolarSelectEntityDescription(
key=rn.STORAGE_CAPACITY_CONTROL_MODE,
icon="mdi:battery-arrow-up",
entity_category=EntityCategory.CONFIG,
# Active capacity control is only available is 'Charge from grid' is enabled
is_available_key=rn.STORAGE_CHARGE_FROM_GRID_FUNCTION,
check_is_available_func=lambda charge_from_grid: charge_from_grid,
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Huawei Solar Select Entities Setup."""
if not entry.data.get(CONF_ENABLE_PARAMETER_CONFIGURATION, False):
_LOGGER.info("Skipping select setup, as parameter configuration is not enabled")
return
update_coordinators: list[HuaweiSolarUpdateCoordinator] = hass.data[DOMAIN][
entry.entry_id
][DATA_UPDATE_COORDINATORS]
configuration_update_coordinators: list[
HuaweiSolarConfigurationUpdateCoordinator
] = hass.data[DOMAIN][entry.entry_id][DATA_CONFIGURATION_UPDATE_COORDINATORS]
# 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[SelectEntity] = []
for idx, (update_coordinator, configuration_update_coordinator) in enumerate(
zip(update_coordinators, configuration_update_coordinators)
):
slave_entities: list[HuaweiSolarSelectEntity | StorageModeSelectEntity] = []
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_SWITCH_DESCRIPTIONS:
slave_entities.append(
HuaweiSolarSelectEntity(
configuration_update_coordinator,
bridge,
entity_description,
device_infos["connected_energy_storage"],
)
)
slave_entities.append(
StorageModeSelectEntity(
configuration_update_coordinator,
bridge,
device_infos["connected_energy_storage"],
)
)
if bridge.supports_capacity_control:
for entity_description in CAPACITY_CONTROL_SWITCH_DESCRIPTIONS:
slave_entities.append(
HuaweiSolarSelectEntity(
configuration_update_coordinator,
bridge,
entity_description,
device_infos["connected_energy_storage"],
)
)
else:
_LOGGER.debug(
"No battery detected on slave %s. Skipping energy storage select 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 HuaweiSolarSelectEntity(CoordinatorEntity, HuaweiSolarEntity, SelectEntity):
"""Huawei Solar Select Entity."""
entity_description: HuaweiSolarSelectEntityDescription
def _friendly_format(self, value: IntEnum):
return value.name.lower()
def _to_enum(self, value: str):
return getattr(self._register_unit, value.upper())
def __init__(
self,
coordinator: HuaweiSolarConfigurationUpdateCoordinator,
bridge: HuaweiSolarBridge,
description: HuaweiSolarSelectEntityDescription,
device_info: DeviceInfo,
) -> None:
"""Huawei Solar Select Entity constructor."""
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._register_unit: IntEnum = REGISTERS[description.key].unit
self._attr_current_option = self._friendly_format(
self.coordinator.data[self.entity_description.key].value
)
self._attr_options = [
self._friendly_format(value) for value in self._register_unit
]
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._attr_current_option = self._friendly_format(
self.coordinator.data[self.entity_description.key].value
)
if self.entity_description.check_is_available_func:
is_available_register = self.coordinator.data[
self.entity_description.is_available_key
]
self._attr_available = self.entity_description.check_is_available_func(
is_available_register.value if is_available_register else None
)
self.async_write_ha_state()
async def async_select_option(self, option) -> None:
"""Change the selected option."""
await self.bridge.set(self.entity_description.key, self._to_enum(option))
self._attr_current_option = option
await self.coordinator.async_request_refresh()
@property
def available(self) -> bool:
"""Override available property (from CoordinatorEntity) to take into account the custom check_is_available_func result."""
available = super().available
if self.entity_description.check_is_available_func and available:
return self._attr_available
return available
class StorageModeSelectEntity(CoordinatorEntity, HuaweiSolarEntity, SelectEntity):
"""Huawei Solar Storage Mode Select Entity.
The available options depend on the type of battery used, so it needs
separate logic.
"""
entity_description: HuaweiSolarSelectEntityDescription
def __init__(
self,
coordinator: HuaweiSolarConfigurationUpdateCoordinator,
bridge: HuaweiSolarBridge,
device_info: DeviceInfo,
) -> None:
"""Huawei Solar Storage Mode Select Entity constructor.
Do not use directly. Use `.create` instead!
"""
super().__init__(coordinator)
self.coordinator = coordinator
self.bridge = bridge
self.entity_description = HuaweiSolarSelectEntityDescription(
key=rn.STORAGE_WORKING_MODE_SETTINGS,
entity_category=EntityCategory.CONFIG,
)
self._attr_device_info = device_info
self._attr_unique_id = f"{bridge.serial_number}_{self.entity_description.key}"
self._attr_current_option = self.coordinator.data[self.entity_description.key].value.name.lower()
# The options depend on the type of battery
available_options = [swm.name for swm in rv.StorageWorkingModesC]
if bridge.battery_type == rv.StorageProductModel.HUAWEI_LUNA2000:
available_options.remove(rv.StorageWorkingModesC.TIME_OF_USE_LG.name)
elif bridge.battery_type == rv.StorageProductModel.LG_RESU:
available_options.remove(rv.StorageWorkingModesC.TIME_OF_USE_LUNA2000.name)
self._attr_options = [option.lower() for option in available_options]
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._attr_current_option = self.coordinator.data[self.entity_description.key].value.name.lower()
self.async_write_ha_state()
async def async_select_option(self, option) -> None:
"""Change the selected option."""
await self.bridge.set(
rn.STORAGE_WORKING_MODE_SETTINGS, getattr(rv.StorageWorkingModesC, option.upper())
)
self._attr_current_option = option
await self.coordinator.async_request_refresh()