EV-03: car charger seam, Alfen HA driver, read-only Car card
Read-only: nothing in this change writes to the charger. - app/chargers.py: ChargerDriver, AlfenHaDriver, UnavailableCharger, ChargerReading, CarTracker - options ev_charger/ev_current_entity/ev_max_a/ev_phases (default off), Settings group, validator, census - Car card on Home, unreachable row and notification, two-writer info row, MQTT sensors - car out of the House tile and the house profile - tools/chargersim.py charger model, test_ev03.py Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -332,7 +332,7 @@ import math
|
||||
import re
|
||||
from typing import NamedTuple
|
||||
|
||||
from . import dayplan, messages, pv, solarfc
|
||||
from . import chargers, dayplan, messages, pv, solarfc
|
||||
from .arbitrage import enabled as arbitrage_on
|
||||
from .control import SLEW_RAIL_FRAC, LOOP_TICK_S, PLANT_SETTLE_S
|
||||
from . import drivers
|
||||
@@ -667,6 +667,34 @@ def _labels(keys, joiner: str = "and") -> str:
|
||||
return f" {joiner} ".join(S(k) for k in keys)
|
||||
|
||||
|
||||
def _charger_problems(opts: dict, writers: dict) -> list[str]:
|
||||
"""EV-03: the car charger options. A charger whose entities are missing is
|
||||
refused at run time (the Car card says so), not here: it never stops the
|
||||
add-on. What stops it is a setpoint that an inverter also writes."""
|
||||
out: list[str] = []
|
||||
raw = opts.get("ev_charger", chargers.OFF)
|
||||
if raw not in (None, "") and str(raw).strip() not in chargers.SELECTABLE:
|
||||
out.append(f"{S('ev_charger')} is {messages.shown(raw)}. Choose "
|
||||
f"{', '.join(chargers.SELECTABLE)}.")
|
||||
return out
|
||||
if chargers.mode_of(opts) == chargers.OFF:
|
||||
return out
|
||||
ph = opts.get("ev_phases", chargers.EV_OPTION_DEFAULTS["ev_phases"])
|
||||
if str(ph).strip() not in chargers.PHASES_CHOICES:
|
||||
out.append(f"{S('ev_phases')} is {messages.shown(ph)}. Choose 1, 3 or "
|
||||
"auto.")
|
||||
amps = chargers.max_a(opts)
|
||||
if amps is None or not chargers.MIN_A <= amps <= chargers.ENTITY_MAX_A:
|
||||
out.append(f"{S('ev_max_a')} is {messages.shown(opts.get('ev_max_a'))}"
|
||||
f". Set it from {chargers.MIN_A:.0f} to "
|
||||
f"{chargers.ENTITY_MAX_A:.0f} A.")
|
||||
entity = chargers.census_entity(opts)
|
||||
if entity and drivers.census_key(entity) in writers:
|
||||
out.append(f"{S('ev_current_entity')} is {entity}, which an inverter "
|
||||
"also uses. Give the charger its own entity.")
|
||||
return out
|
||||
|
||||
|
||||
def _roster_problems(opts: dict) -> list[str]:
|
||||
"""Rule 9 (CFG-04): the `inverters:` roster, refused where it cannot mean
|
||||
what it says.
|
||||
@@ -956,6 +984,12 @@ def validate_startup_config(opts: dict, dev_min=None, dev_max=None,
|
||||
f"{actuator}: {', '.join(who)}. Give each inverter its own "
|
||||
"setpoint entity.")
|
||||
|
||||
# --- EV-03: the car charger's setpoint is a writer too ---------------------
|
||||
# Counted in the census beside the inverters', and kept apart from
|
||||
# `actuator_writers()` so setup-only mode still reads "no inverter". An
|
||||
# inverter's entity named as the charger is two writers on one actuator.
|
||||
problems.extend(_charger_problems(opts, writers))
|
||||
|
||||
# --- 9. CFG-04's roster, graded BEFORE anything reads a per-unit number --
|
||||
# ⚠️ FIRST, THOUGH IT IS THE NEWEST RULE. Rules 5 and 6 below now ask the
|
||||
# roster what one unit may be commanded, so a roster that is nonsense has to
|
||||
@@ -1921,6 +1955,7 @@ def config_notes(opts: dict, *, entities: dict | None = None,
|
||||
notes: list[Note] = []
|
||||
if entities is not None:
|
||||
_entity_notes(notes, opts, entities)
|
||||
_charger_notes(notes, opts, entities)
|
||||
cap = _number(opts, "peak_cap_w", None)
|
||||
if month_peak_w is not None and cap is not None and cap < month_peak_w:
|
||||
_advise(notes, "warn", ("peak_cap_w",),
|
||||
@@ -1932,6 +1967,94 @@ def config_notes(opts: dict, *, entities: dict | None = None,
|
||||
return notes
|
||||
|
||||
|
||||
def charger_probe_rows(opts: dict, entities: dict | None) -> dict:
|
||||
"""EV-03: the settings panel's read-only look at the charger. `entities` is
|
||||
Home Assistant's list by id (None: it did not answer). Nothing is written.
|
||||
`{"ok", "found": [[role, entity]], "missing": [[role, entity]], "limit_a",
|
||||
"state"}`; `state` is `off`, `no_name` (the setpoint is not named the way
|
||||
the charger's integration names it), `unread`, `ok` or `missing`."""
|
||||
if chargers.mode_of(opts) == chargers.OFF:
|
||||
return {"ok": False, "found": [], "missing": [], "limit_a": None,
|
||||
"state": "off"}
|
||||
setpoint = str(opts.get("ev_current_entity") or "").strip()
|
||||
if not chargers.entities_for(setpoint):
|
||||
return {"ok": False, "found": [], "missing": [], "limit_a": None,
|
||||
"state": "no_name" if setpoint else "empty"}
|
||||
if entities is None:
|
||||
return {"ok": False, "found": [], "missing": [], "limit_a": None,
|
||||
"state": "unread"}
|
||||
def state_obj(e):
|
||||
row = entities.get(e)
|
||||
return None if row is None else {
|
||||
"state": row.get("state"),
|
||||
"attributes": {"unit_of_measurement": row.get("unit")}}
|
||||
pr = chargers.probe_from(setpoint, state_obj)
|
||||
return {"ok": pr.ok, "found": [list(x) for x in pr.found],
|
||||
"missing": [list(x) for x in pr.missing], "limit_a": pr.station_a,
|
||||
"state": "ok" if pr.ok else "missing"}
|
||||
|
||||
|
||||
# The roles the "Found:" note names, in words (EV-03).
|
||||
_CHARGER_WORDS = (
|
||||
("state", "car state", "autostatus"), ("power", "power", "vermogen"),
|
||||
("energy", "energy", "energie"), ("readback", "read-back", "terugmelding"),
|
||||
("phases_read", "phases", "fasen"),
|
||||
("station", "the charger limit", "de laadpaalgrens"))
|
||||
|
||||
|
||||
def charger_probe_for(setpoint: str, entities: dict | None) -> dict:
|
||||
"""`charger_probe_rows()` for a setpoint entity typed on the page."""
|
||||
return charger_probe_rows({"ev_charger": chargers.ALFEN_HA,
|
||||
"ev_current_entity": setpoint}, entities)
|
||||
|
||||
|
||||
def _charger_notes(notes, opts: dict, entities: dict) -> None:
|
||||
"""EV-03: a note for each charger entity Home Assistant does not have, and
|
||||
one when the limit is above the charger's own. Never a refusal."""
|
||||
look = charger_probe_rows(opts, entities)
|
||||
key = ("ev_current_entity",)
|
||||
if look["state"] == "no_name":
|
||||
_advise(notes, "warn", key,
|
||||
f"{S('ev_current_entity')} must be named like the Alfen "
|
||||
"integration's current setting, so the charger is not read.",
|
||||
nl=f"{SN('ev_current_entity')} moet heten zoals de "
|
||||
"stroominstelling van de Alfen-integratie, dus de laadpaal "
|
||||
"wordt niet gelezen.")
|
||||
return
|
||||
if look["state"] == "empty":
|
||||
_advise(notes, "warn", key,
|
||||
f"{S('ev_current_entity')} is empty, so the charger is not "
|
||||
"read.",
|
||||
nl=f"{SN('ev_current_entity')} is leeg, dus de laadpaal wordt "
|
||||
"niet gelezen.")
|
||||
return
|
||||
if look["state"] not in ("ok", "missing"):
|
||||
return
|
||||
for role, entity in look["missing"]:
|
||||
required = role in chargers.REQUIRED
|
||||
_advise(notes, "warn" if required else "info", key,
|
||||
f"Home Assistant has no entity {entity}, which the charger "
|
||||
f"needs.",
|
||||
nl=f"Home Assistant kent {entity} niet, die de laadpaal "
|
||||
"nodig heeft.")
|
||||
lim = look["limit_a"]
|
||||
if look["ok"]:
|
||||
have = {r for r, _e in look["found"]}
|
||||
words = [(w_en, w_nl) for r, w_en, w_nl in _CHARGER_WORDS if r in have]
|
||||
tail_en = f", {lim:.0f} A" if lim is not None else ""
|
||||
_advise(notes, "ok", key,
|
||||
"Found: " + ", ".join(w[0] for w in words) + tail_en + ".",
|
||||
nl="Gevonden: " + ", ".join(w[1] for w in words) + tail_en
|
||||
+ ".")
|
||||
amps = chargers.max_a(opts)
|
||||
if lim is not None and amps is not None and amps > lim:
|
||||
_advise(notes, "warn", ("ev_max_a",),
|
||||
f"{S('ev_max_a')} is above the {lim:.0f} A the charger "
|
||||
"reports, so lower it.",
|
||||
nl=f"{SN('ev_max_a')} is hoger dan de {lim:.0f} A die de "
|
||||
"laadpaal meldt, dus verlaag ze.")
|
||||
|
||||
|
||||
def _entity_notes(notes, opts: dict, entities: dict, bad=()) -> None:
|
||||
"""A note for each entity these options read that Home Assistant does not
|
||||
have, or that reads in kW where watts are taken. A missing entity whose
|
||||
|
||||
Reference in New Issue
Block a user