-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber.py
More file actions
493 lines (463 loc) · 15.8 KB
/
number.py
File metadata and controls
493 lines (463 loc) · 15.8 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
"""SEM Solar Energy Management number entities for settings control."""
import logging
from typing import Any
from homeassistant.components.number import (
NumberEntity,
NumberEntityDescription,
NumberMode,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.const import (
UnitOfPower,
UnitOfEnergy,
UnitOfElectricCurrent,
UnitOfTemperature,
UnitOfTime,
PERCENTAGE,
)
from homeassistant.helpers.entity import EntityCategory
from .const import DOMAIN
from .coordinator import SEMCoordinator
_LOGGER = logging.getLogger(__name__)
PARALLEL_UPDATES = 0 # Coordinator handles all updates
NUMBER_TYPES = [
# Delta Thresholds
NumberEntityDescription(
key="update_interval",
native_unit_of_measurement=UnitOfTime.SECONDS,
native_min_value=10,
native_max_value=60,
native_step=5,
mode=NumberMode.SLIDER,
),
NumberEntityDescription(
key="power_delta",
native_unit_of_measurement=UnitOfPower.WATT,
native_min_value=50,
native_max_value=3000,
native_step=50,
mode=NumberMode.SLIDER,
),
NumberEntityDescription(
key="current_delta",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
native_min_value=1,
native_max_value=10,
native_step=1,
mode=NumberMode.SLIDER,
),
NumberEntityDescription(
key="soc_delta",
native_unit_of_measurement=PERCENTAGE,
native_min_value=1,
native_max_value=20,
native_step=1,
mode=NumberMode.SLIDER,
),
# Battery Management
NumberEntityDescription(
# 4-zone Zone 1 floor: below this, all solar → battery, EV blocked.
# Range widened from 50–100 (legacy 3-zone meaning) to 5–60 to match
# the 4-zone semantics documented in docs/ARCHITECTURE.md.
key="battery_priority_soc",
native_unit_of_measurement=PERCENTAGE,
native_min_value=5,
native_max_value=60,
native_step=5,
mode=NumberMode.SLIDER,
),
NumberEntityDescription(
# Hard stop: SOC below this halts EV charging entirely (safety).
key="battery_minimum_soc",
native_unit_of_measurement=PERCENTAGE,
native_min_value=5,
native_max_value=50,
native_step=5,
mode=NumberMode.SLIDER,
),
NumberEntityDescription(
key="battery_resume_soc",
native_unit_of_measurement=PERCENTAGE,
native_min_value=30,
native_max_value=80,
native_step=5,
mode=NumberMode.SLIDER,
),
# SOC Zone Thresholds
NumberEntityDescription(
key="battery_buffer_soc",
native_unit_of_measurement=PERCENTAGE,
native_min_value=50,
native_max_value=95,
native_step=5,
mode=NumberMode.SLIDER,
),
NumberEntityDescription(
key="battery_auto_start_soc",
native_unit_of_measurement=PERCENTAGE,
native_min_value=70,
native_max_value=100,
native_step=5,
mode=NumberMode.SLIDER,
),
NumberEntityDescription(
key="battery_assist_floor_soc",
native_unit_of_measurement=PERCENTAGE,
native_min_value=30,
native_max_value=80,
native_step=5,
mode=NumberMode.SLIDER,
),
NumberEntityDescription(
key="battery_capacity",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
native_min_value=5,
native_max_value=100,
native_step=5,
mode=NumberMode.BOX,
),
NumberEntityDescription(
key="battery_max_discharge_power",
native_unit_of_measurement=UnitOfPower.WATT,
native_min_value=500,
native_max_value=10000,
native_step=500,
mode=NumberMode.SLIDER,
),
# Solar & Power
NumberEntityDescription(
key="minimum_solar_power",
native_unit_of_measurement=UnitOfPower.WATT,
native_min_value=0,
native_max_value=5000,
native_step=100,
mode=NumberMode.SLIDER,
),
NumberEntityDescription(
key="maximum_grid_import",
native_unit_of_measurement=UnitOfPower.WATT,
native_min_value=0,
native_max_value=2000,
native_step=100,
mode=NumberMode.SLIDER,
),
# EV Charging
NumberEntityDescription(
key="daily_ev_target",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
native_min_value=0,
native_max_value=100,
native_step=0.5,
mode=NumberMode.SLIDER,
),
NumberEntityDescription(
key="ev_km_per_kwh",
native_unit_of_measurement="km/kWh",
native_min_value=3,
native_max_value=10,
native_step=0.5,
mode=NumberMode.BOX,
entity_category=EntityCategory.CONFIG,
),
NumberEntityDescription(
key="public_charging_rate",
native_unit_of_measurement="CHF/kWh",
native_min_value=0,
native_max_value=2,
native_step=0.01,
mode=NumberMode.BOX,
entity_category=EntityCategory.CONFIG,
),
NumberEntityDescription(
key="battery_assist_max_power",
native_unit_of_measurement=UnitOfPower.WATT,
native_min_value=1000,
native_max_value=10000,
native_step=500,
mode=NumberMode.SLIDER,
),
# EV Charging Parameters
NumberEntityDescription(
key="ev_night_initial_current",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
native_min_value=6,
native_max_value=32,
native_step=1,
mode=NumberMode.SLIDER,
),
NumberEntityDescription(
key="ev_minimum_current",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
native_min_value=6,
native_max_value=16,
native_step=1,
mode=NumberMode.SLIDER,
),
NumberEntityDescription(
key="ev_stall_cooldown",
native_unit_of_measurement=UnitOfTime.SECONDS,
native_min_value=30,
native_max_value=300,
native_step=10,
mode=NumberMode.SLIDER,
),
NumberEntityDescription(
key="ev_phases",
native_min_value=1,
native_max_value=3,
native_step=1,
mode=NumberMode.SLIDER,
),
# Tariff rates (previously only in OptionsFlow)
NumberEntityDescription(
key="electricity_import_rate",
native_unit_of_measurement="CHF/kWh", # CHF replaced dynamically with HA currency
native_min_value=0.01,
native_max_value=1.0,
native_step=0.01,
mode=NumberMode.BOX,
),
NumberEntityDescription(
key="electricity_export_rate",
native_unit_of_measurement="CHF/kWh", # CHF replaced dynamically with HA currency
native_min_value=0.01,
native_max_value=0.50,
native_step=0.005,
mode=NumberMode.BOX,
),
# Phase 0: Surplus controller
NumberEntityDescription(
key="regulation_offset",
native_unit_of_measurement=UnitOfPower.WATT,
native_min_value=0,
native_max_value=500,
native_step=10,
mode=NumberMode.SLIDER,
),
# Phase 1: Demand charge
NumberEntityDescription(
key="demand_charge_rate",
native_unit_of_measurement="CHF/kW/Mt",
native_min_value=0.0,
native_max_value=20.0,
native_step=0.5,
mode=NumberMode.BOX,
),
# Phase 1: Price thresholds
NumberEntityDescription(
key="cheap_price_threshold",
native_unit_of_measurement="CHF/kWh",
native_min_value=0.0,
native_max_value=1.0,
native_step=0.01,
mode=NumberMode.BOX,
),
NumberEntityDescription(
key="expensive_price_threshold",
native_unit_of_measurement="CHF/kWh",
native_min_value=0.0,
native_max_value=1.0,
native_step=0.01,
mode=NumberMode.BOX,
),
# Phase 2: Heat pump
NumberEntityDescription(
key="heat_pump_boost_offset",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
native_min_value=0,
native_max_value=5,
native_step=0.5,
mode=NumberMode.SLIDER,
),
NumberEntityDescription(
key="hot_water_max_temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
native_min_value=40,
native_max_value=80,
native_step=1,
mode=NumberMode.SLIDER,
),
# Phase 5: PV system
NumberEntityDescription(
key="system_size_kwp",
native_unit_of_measurement="kWp",
native_min_value=1,
native_max_value=100,
native_step=0.5,
mode=NumberMode.BOX,
),
NumberEntityDescription(
key="system_investment_cost",
native_min_value=0,
native_max_value=200000,
native_step=100,
mode=NumberMode.BOX,
),
NumberEntityDescription(
key="system_install_year",
native_min_value=2015,
native_max_value=2035.12,
native_step=0.01,
mode=NumberMode.BOX,
),
# Night charging schedule
NumberEntityDescription(
key="night_earliest_start",
native_unit_of_measurement="h",
native_min_value=18.0,
native_max_value=23.0,
native_step=0.5,
mode=NumberMode.SLIDER,
),
NumberEntityDescription(
key="night_latest_end",
native_unit_of_measurement="h",
native_min_value=5.0,
native_max_value=9.0,
native_step=0.5,
mode=NumberMode.SLIDER,
),
]
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up EMS Solar Optimizer number entities."""
coordinator = hass.data[DOMAIN][entry.entry_id]
entities = [
EMSSolarNumber(coordinator, description, entry)
for description in NUMBER_TYPES
]
async_add_entities(entities)
class EMSSolarNumber(CoordinatorEntity, NumberEntity):
"""EMS Solar Optimizer number entity."""
_attr_has_entity_name = True
_attr_entity_category = EntityCategory.CONFIG
# All number entities are enabled by default
DISABLED_BY_DEFAULT: set = set()
def __init__(
self,
coordinator: SEMCoordinator,
description: NumberEntityDescription,
entry: ConfigEntry,
) -> None:
"""Initialize the number entity."""
super().__init__(coordinator)
self.entity_description = description
# Keep entry_id-based unique_id for backward compatibility with existing entities
self._attr_unique_id = f"{entry.entry_id}_{description.key}"
self._attr_translation_key = description.key
self._attr_suggested_object_id = f"sem_{description.key}"
self._entry = entry
if description.key in self.DISABLED_BY_DEFAULT:
self._attr_entity_registry_enabled_default = False
# Use HA configured currency for monetary number entities
uom = description.native_unit_of_measurement or ""
if "CHF" in uom:
currency = coordinator.hass.config.currency
self._attr_native_unit_of_measurement = uom.replace("CHF", currency)
# Set initial value from config.
# Some entity keys differ from config keys for dashboard compatibility.
_CONFIG_KEY_MAP = {
"battery_capacity": "battery_capacity_kwh",
"ev_minimum_current": "ev_min_current",
}
config = {**entry.data, **entry.options}
config_key = _CONFIG_KEY_MAP.get(description.key, description.key)
self._attr_native_value = config.get(
config_key,
config.get(description.key, self._get_default_value(description.key))
)
def _get_default_value(self, key: str) -> float:
"""Get default value for a setting."""
from .const import (
DEFAULT_UPDATE_INTERVAL,
DEFAULT_POWER_DELTA,
DEFAULT_CURRENT_DELTA,
DEFAULT_SOC_DELTA,
DEFAULT_BATTERY_PRIORITY_SOC,
DEFAULT_BATTERY_MINIMUM_SOC,
DEFAULT_BATTERY_RESUME_SOC,
DEFAULT_MIN_SOLAR_POWER,
DEFAULT_MAX_GRID_IMPORT,
DEFAULT_DAILY_EV_TARGET,
DEFAULT_BATTERY_ASSIST_MAX_POWER,
DEFAULT_REGULATION_OFFSET,
DEFAULT_DEMAND_CHARGE_RATE,
DEFAULT_CHEAP_PRICE_THRESHOLD,
DEFAULT_EXPENSIVE_PRICE_THRESHOLD,
DEFAULT_HEAT_PUMP_BOOST_OFFSET,
DEFAULT_HOT_WATER_MAX_TEMP,
DEFAULT_SYSTEM_SIZE_KWP,
DEFAULT_EV_NIGHT_INITIAL_CURRENT,
DEFAULT_EV_MIN_CURRENT,
DEFAULT_EV_STALL_COOLDOWN,
DEFAULT_BATTERY_CAPACITY_KWH,
)
defaults = {
"update_interval": DEFAULT_UPDATE_INTERVAL,
"power_delta": DEFAULT_POWER_DELTA,
"current_delta": DEFAULT_CURRENT_DELTA,
"soc_delta": DEFAULT_SOC_DELTA,
"battery_priority_soc": DEFAULT_BATTERY_PRIORITY_SOC,
"battery_minimum_soc": DEFAULT_BATTERY_MINIMUM_SOC,
"battery_resume_soc": DEFAULT_BATTERY_RESUME_SOC,
"minimum_solar_power": DEFAULT_MIN_SOLAR_POWER,
"maximum_grid_import": DEFAULT_MAX_GRID_IMPORT,
"daily_ev_target": DEFAULT_DAILY_EV_TARGET,
"battery_assist_max_power": DEFAULT_BATTERY_ASSIST_MAX_POWER,
"regulation_offset": DEFAULT_REGULATION_OFFSET,
"demand_charge_rate": DEFAULT_DEMAND_CHARGE_RATE,
"cheap_price_threshold": DEFAULT_CHEAP_PRICE_THRESHOLD,
"expensive_price_threshold": DEFAULT_EXPENSIVE_PRICE_THRESHOLD,
"heat_pump_boost_offset": DEFAULT_HEAT_PUMP_BOOST_OFFSET,
"hot_water_max_temperature": DEFAULT_HOT_WATER_MAX_TEMP,
"system_size_kwp": DEFAULT_SYSTEM_SIZE_KWP,
"ev_night_initial_current": DEFAULT_EV_NIGHT_INITIAL_CURRENT,
"ev_minimum_current": DEFAULT_EV_MIN_CURRENT,
"ev_stall_cooldown": DEFAULT_EV_STALL_COOLDOWN,
"ev_phases": 3,
"ev_km_per_kwh": 5.5,
"public_charging_rate": 0.55,
"electricity_import_rate": 0.3387,
"electricity_export_rate": 0.075,
"battery_buffer_soc": 70,
"battery_auto_start_soc": 90,
"battery_assist_floor_soc": 60,
"battery_capacity": DEFAULT_BATTERY_CAPACITY_KWH,
"night_earliest_start": 20.5,
"night_latest_end": 7.0,
"battery_max_discharge_power": 5000,
}
return defaults.get(key, 0)
@property
def available(self) -> bool:
"""Return if entity is available."""
return self.coordinator.last_update_success
@property
def device_info(self):
"""Return device information."""
return self.coordinator.device_info
async def async_set_native_value(self, value: float) -> None:
"""Update the setting value."""
self._attr_native_value = value
# Map entity key back to config key if they differ
_CONFIG_KEY_MAP = {
"battery_capacity": "battery_capacity_kwh",
"ev_minimum_current": "ev_min_current",
}
config_key = _CONFIG_KEY_MAP.get(self.entity_description.key, self.entity_description.key)
# Update the config entry options
new_options = {**self._entry.options}
new_options[config_key] = value
self.hass.config_entries.async_update_entry(
self._entry,
options=new_options
)
# Update coordinator config
await self.coordinator.async_update_config({config_key: value})
# Force coordinator refresh
await self.coordinator.async_request_refresh()
_LOGGER.info(f"Updated {self.entity_description.key} to {value}")