-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnumber.py
More file actions
741 lines (689 loc) · 28.7 KB
/
number.py
File metadata and controls
741 lines (689 loc) · 28.7 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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
"""SEM Solar Energy Management number entities for settings control."""
from __future__ import annotations
import logging
from typing import Any
from homeassistant.components.number import (
NumberEntity,
NumberEntityDescription,
NumberMode,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers import entity_registry as er
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
type SEMConfigEntry = ConfigEntry[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 charge-target / consumption settings (daily_ev_target[_max], ev_target_soc[_max],
# ev_kwh_per_100km) are PER-CHARGER only (#255) — global duplicates removed; see the
# per-charger descriptions in async_setup_entry. Stale registry entities are
# auto-removed by _cleanup_stale_entities; values were seeded per-charger by the v3→v4
# migration so nothing resets.
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 — global night_initial_current + minimum_current removed as
# per-charger duplicates (#255); ev_stall_cooldown stays a global tuning constant.
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,
),
# ev_phases is PER-CHARGER only (#255) — it's a charger hardware property. Global
# entity removed (seeded per-charger by the v3→v4 migration; stale entity auto-removed).
# 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.0,
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.0,
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,
),
# Hot water solar boost + Legionella prevention (#92)
NumberEntityDescription(
key="hot_water_solar_target",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
native_min_value=40,
native_max_value=80,
native_step=5,
mode=NumberMode.SLIDER,
),
NumberEntityDescription(
key="legionella_target_temp",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
native_min_value=60,
native_max_value=80,
native_step=5,
mode=NumberMode.SLIDER,
),
NumberEntityDescription(
key="legionella_interval_hours",
native_unit_of_measurement="h",
native_min_value=24,
native_max_value=168,
native_step=24,
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,
),
# system_install_year removed — auto-detected from recorder statistics
# 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: SEMConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up EMS Solar Optimizer number entities."""
coordinator: SEMCoordinator = entry.runtime_data
entities = [
SEMNumberEntity(coordinator, description, entry)
for description in NUMBER_TYPES
]
# Per-charger number entities (#193)
full_config = {**entry.data, **entry.options}
ev_chargers = full_config.get("ev_chargers", [])
per_charger_descriptions = []
if len(ev_chargers) >= 1:
for charger_cfg in ev_chargers:
cid = charger_cfg.get("id", "ev_charger")
cname = charger_cfg.get("name", "EV Charger")
for base_desc, config_key, default_val in [
(NumberEntityDescription(
key=f"charger_{cid}_daily_ev_target",
name=f"{cname} Night Target",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
native_min_value=0, native_max_value=100, native_step=0.5,
mode=NumberMode.SLIDER,
), "daily_ev_target", full_config.get("daily_ev_target", 10)),
(NumberEntityDescription(
key=f"charger_{cid}_night_initial_current",
name=f"{cname} Start Amps",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
native_min_value=6, native_max_value=32, native_step=1,
mode=NumberMode.SLIDER,
), "ev_night_initial_current", full_config.get("ev_night_initial_current", 10)),
(NumberEntityDescription(
key=f"charger_{cid}_minimum_current",
name=f"{cname} Min Amps",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
native_min_value=6, native_max_value=16, native_step=1,
mode=NumberMode.SLIDER,
), "ev_min_current", full_config.get("ev_min_current", 6)),
(NumberEntityDescription(
key=f"charger_{cid}_target_soc",
name=f"{cname} Target SOC",
native_unit_of_measurement=PERCENTAGE,
native_min_value=50, native_max_value=100, native_step=5,
mode=NumberMode.SLIDER,
icon="mdi:battery-charging-80",
), "ev_target_soc", full_config.get("ev_target_soc", 80)),
# Solar ceiling (Max) = the Max handle of the EV-card range slider;
# defaults to full (charge freely from sun) until the user caps it (#245).
(NumberEntityDescription(
key=f"charger_{cid}_daily_ev_target_max",
name=f"{cname} Solar Max",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
native_min_value=0, native_max_value=100, native_step=0.5,
mode=NumberMode.SLIDER,
icon="mdi:solar-power-variant",
), "daily_ev_target_max",
charger_cfg.get("daily_ev_target_max", 100)),
(NumberEntityDescription(
key=f"charger_{cid}_target_soc_max",
name=f"{cname} Solar Max SOC",
native_unit_of_measurement=PERCENTAGE,
native_min_value=50, native_max_value=100, native_step=5,
mode=NumberMode.SLIDER,
icon="mdi:battery-charging-high",
), "ev_target_soc_max",
charger_cfg.get("ev_target_soc_max", 100)),
# Car battery capacity (kWh) — feeds the SOC/range math; editable
# from the EV card so users don't have to open the options flow (#245).
(NumberEntityDescription(
key=f"charger_{cid}_ev_battery_capacity_kwh",
name=f"{cname} Battery Capacity",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
native_min_value=10, native_max_value=120, native_step=1,
mode=NumberMode.BOX,
icon="mdi:car-battery",
entity_category=EntityCategory.CONFIG,
), "ev_battery_capacity_kwh",
charger_cfg.get("ev_battery_capacity_kwh",
full_config.get("ev_battery_capacity_kwh", 40))),
# Per-car consumption (kWh/100km) — feeds the driving-range estimate.
# Individual per car, hence per charger (one car per charger). (#245)
(NumberEntityDescription(
key=f"charger_{cid}_ev_kwh_per_100km",
name=f"{cname} Consumption",
native_unit_of_measurement="kWh/100km",
native_min_value=8, native_max_value=50, native_step=0.5,
mode=NumberMode.BOX,
icon="mdi:map-marker-distance",
entity_category=EntityCategory.CONFIG,
), "ev_kwh_per_100km",
charger_cfg.get("ev_kwh_per_100km",
full_config.get("ev_kwh_per_100km", 18))),
# Per-charger phase count (#255) — a charger hardware property (1 or 3).
(NumberEntityDescription(
key=f"charger_{cid}_ev_phases",
name=f"{cname} Phases",
native_min_value=1, native_max_value=3, native_step=1,
mode=NumberMode.SLIDER,
entity_category=EntityCategory.CONFIG,
), "ev_phases",
charger_cfg.get("ev_phases", full_config.get("ev_phases", 3))),
]:
per_charger_descriptions.append(base_desc)
entities.append(SEMPerChargerNumber(
coordinator, base_desc, entry, cid, config_key,
charger_cfg.get(config_key, default_val),
))
if per_charger_descriptions:
_LOGGER.info(
"Created %d per-charger number entities for %d charger(s)",
len(per_charger_descriptions), len(ev_chargers),
)
async_add_entities(entities)
# Fix entity_ids from pre-translation installs and clean up stale entities
all_descriptions = list(NUMBER_TYPES) + per_charger_descriptions
_fix_entity_ids(hass, entry, all_descriptions, "number")
_cleanup_stale_entities(hass, entry, all_descriptions, "number")
def _fix_entity_ids(hass, entry, descriptions, platform):
"""Fix entity_ids from pre-translation installs."""
try:
registry = er.async_get(hass)
expected = {}
for desc in descriptions:
# Numbers use {entry_id}_{key} unique_id, with legacy map
_LEGACY = {"battery_capacity": "battery_capacity_kwh"}
uid_key = _LEGACY.get(desc.key, desc.key)
uid = f"{entry.entry_id}_{uid_key}"
expected[uid] = f"{platform}.sem_{desc.key}"
for entity_entry in er.async_entries_for_config_entry(registry, entry.entry_id):
if entity_entry.domain != platform:
continue
uid = entity_entry.unique_id or ""
if uid in expected:
correct_eid = expected[uid]
if entity_entry.entity_id != correct_eid:
existing = registry.async_get(correct_eid)
if existing is None:
registry.async_update_entity(
entity_entry.entity_id, new_entity_id=correct_eid,
)
_LOGGER.info("Fixed entity_id: %s → %s", entity_entry.entity_id, correct_eid)
except Exception as e:
_LOGGER.debug("Entity ID fix skipped: %s", e)
def _cleanup_stale_entities(hass, entry, descriptions, platform):
"""Remove orphaned entities from previous SEM versions."""
try:
registry = er.async_get(hass)
# Valid keys: both description keys AND legacy UID mapped keys
valid_keys = {d.key for d in descriptions}
_LEGACY_UID_MAP = {"battery_capacity": "battery_capacity_kwh"}
valid_keys.update(_LEGACY_UID_MAP.values())
for entity_entry in er.async_entries_for_config_entry(registry, entry.entry_id):
if entity_entry.domain != platform:
continue
unique_id = entity_entry.unique_id or ""
key = unique_id.replace(f"{entry.entry_id}_", "", 1)
if key and key not in valid_keys:
_LOGGER.info("Removing stale entity %s (key '%s' removed)", entity_entry.entity_id, key)
registry.async_remove(entity_entry.entity_id)
except Exception as e:
_LOGGER.debug("Stale entity cleanup skipped: %s", e)
class SEMNumberEntity(CoordinatorEntity, NumberEntity):
"""EMS Solar Optimizer number entity."""
_attr_has_entity_name = True
_attr_entity_category = EntityCategory.CONFIG
# Solar-ceiling (Max) entities are enabled: they are the Max handle of the
# dual-handle range slider on the EV card (#245), and default to full
# (100% / 100 kWh) = "charge freely from sun" until the user caps surplus.
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
# Backward-compatible unique_id: old versions used the config key
# (e.g. battery_capacity_kwh), not the description key (battery_capacity).
_LEGACY_UID_MAP = {
"battery_capacity": "battery_capacity_kwh",
}
uid_key = _LEGACY_UID_MAP.get(description.key, description.key)
self._attr_unique_id = f"{entry.entry_id}_{uid_key}"
self._attr_translation_key = description.key
self._attr_suggested_object_id = f"sem_{description.key}"
# Force stable entity ID regardless of HA language
self.entity_id = f"number.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)
# Null-safe: config may store None explicitly, which makes the
# entity unavailable. Fall through to the default in that case.
# Note: don't use `or` — 0 is a valid value (e.g. max_grid_import=0).
value = config.get(config_key)
if value is None:
value = config.get(description.key)
if value is None:
value = self._get_default_value(description.key)
self._attr_native_value = value
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,
# Ceiling defaults to full (charge freely from sun until capped) (#245)
"daily_ev_target_max": 100,
"ev_target_soc_max": 100,
"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,
"hot_water_solar_target": 50.0,
"legionella_target_temp": 65.0,
"legionella_interval_hours": 72,
"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_kwh_per_100km": 18,
"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 coordinator config (immediate, in-memory)
await self.coordinator.async_update_config({config_key: value})
# Persist to config entry options WITHOUT triggering integration reload.
# async_update_entry fires the update listener which normally calls
# async_reload — destroying all 255 entities for ~1s and causing card
# flashes. The _skip_options_reload flag tells the listener to skip.
new_options = {**self._entry.options}
new_options[config_key] = value
# Skip reload only for THIS exact payload (snapshot) — see async_update_options.
self.coordinator._skip_options_reload = new_options
self.hass.config_entries.async_update_entry(
self._entry,
options=new_options
)
# Publish state immediately — no full coordinator refresh needed.
# The entity already has the correct value via _attr_native_value.
# Derived recalculations (charging strategy) happen on the next 10s cycle.
self.async_write_ha_state()
_LOGGER.info(f"Updated {self.entity_description.key} to {value}")
class SEMPerChargerNumber(CoordinatorEntity, NumberEntity):
"""Per-charger number entity that stores its value in the charger's config dict (#193)."""
_attr_has_entity_name = True
_attr_entity_category = EntityCategory.CONFIG
def __init__(
self,
coordinator: SEMCoordinator,
description: NumberEntityDescription,
entry: ConfigEntry,
charger_id: str,
config_key: str,
initial_value: float,
) -> None:
"""Initialize per-charger number entity."""
super().__init__(coordinator)
self.entity_description = description
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.entity_id = f"number.sem_{description.key}"
self._entry = entry
self._charger_id = charger_id
self._config_key = config_key
self._attr_native_value = initial_value
@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 per-charger setting value."""
self._attr_native_value = value
# Keep coordinator's in-memory config in sync immediately
new_options = {**self._entry.options}
# Copy each charger dict — in-place mutation leaves entry.options unchanged,
# so async_update_entry skips persisting and the value reverts on restart (#245).
ev_chargers = [dict(c) for c in new_options.get("ev_chargers", [])]
for charger in ev_chargers:
if charger.get("id") == self._charger_id:
charger[self._config_key] = value
break
new_options["ev_chargers"] = ev_chargers
if hasattr(self.coordinator, "config") and isinstance(self.coordinator.config, dict):
self.coordinator.config.update({**self._entry.data, **new_options})
# Persist without triggering integration reload (snapshot-keyed skip)
self.coordinator._skip_options_reload = new_options
self.hass.config_entries.async_update_entry(
self._entry,
options=new_options,
)
self.async_write_ha_state()
_LOGGER.info(
"Updated per-charger %s.%s to %s",
self._charger_id, self._config_key, value,
)