cantabile_edge, cantabile_modbus, cantabile_eebus 0.1.1289: full source

This commit is contained in:
Setti Solutions S.R.L committed 2026-11-08 16:35:35 +02:00
1 parent 165dda4479
commit 05802d6f87
57 files changed
+5928 -232

No files matched your search

+144
View File
@@ -142,3 +142,147 @@ def test_services_and_cantabile_not_offered():
{"id": "t", "name": "MD Thermostat", "manufacturer": "Realtek", "model": "wr3", "domains": ["esphome"]}],
"entities": []}
assert [o["id"] for o in dp.offers(reg, [])] == ["t"]
# --- storage with losses (docs/design/storage-losses.md 10.1-10.3) ------------------------
def water_heater(eid="water_heater.tank", **attrs):
return {"entity_id": eid, "device_id": "d", "unique_id": eid,
"attributes": dict({"current_temperature": 52, "temperature": 55, "min_temp": 35, "max_temp": 65}, **attrs)}
def test_storage_quantities_write_roles_and_ratings():
for q, want in (("battery.standby_power", ("W", "gauge")), ("dhw.temp_top", ("°C", "gauge")),
("dhw.temp_bottom", ("°C", "gauge")), ("dhw.heater_power", ("W", "gauge")),
("dhw.tank_energy", ("Wh", "gauge")), ("dhw.energy", ("Wh", "counter"))):
assert dp.quantity_of(q) == want, q
assert {"water_heater", "sg_ready", "switch"} <= dp.WRITE_ROLES
# a tank's power is a diverter's watts (written); a battery's or a car's power is a reading
assert dp.is_write("dhw_tank", "power") and not dp.is_write("battery", "power") and not dp.is_write("ev", "power")
assert not dp.is_write("dhw_tank", "temp_top") and not dp.is_write("battery", "standby")
new = ("battery_standby_w", "battery_self_discharge_pct_month", "tank_volume_l", "tank_heater_kw", "tank_cop",
"tank_standby_w", "tank_erp_class", "tank_max_c", "tank_room_c", "tank_grossanlage")
assert all(k in dp.RATINGS for k in new) and len(dp.ERP_CLASSES) == 7 and dp.ERP_CLASSES[4] == "D"
assert dp.HEATERS == ("element", "heat_pump", "boiler", "district")
def test_battery_profiles_ask_for_the_idle_loss():
for pid in ("hm8042-hybrid", "solaredge-sunspec", "huawei-sun2000", "victron-gx", "sma-sunny-boy", "fronius-gen24"):
rated = {f["rating"]: f for f in PROFILES[pid]["fields"] if f.get("rating")}
assert {"battery_standby_w", "battery_self_discharge_pct_month"} <= set(rated), pid
assert rated["battery_standby_w"]["optional"] and rated["battery_standby_w"]["min"] > 0, pid
assert "learns" in rated["battery_standby_w"]["label"], pid
def test_water_heater_fragment_consent_off_heater_chosen_after():
"""Any HA water_heater (ha-water-heater): a dhw_tank control on it,
consent off; its current temperature as the tank's; optional roles only
where the device has them; the heater the installer's choice."""
p = PROFILES["ha-water-heater"]
ps = list(PROFILES.values())
assert [x["id"] for x in dp.fitting(ps, {"manufacturer": "Stiebel", "model": "WWK"}, ["mqtt"], ["water_heater"])] \
== ["ha-water-heater"]
s = dp.setup_for(p, ["mqtt"])
power = {"entity_id": "sensor.tank_heater_power", "device_id": "d", "unique_id": "p", "device_class": "power"}
frag, missing = dp.mapping_for("tank-1", p, s, [water_heater(), power], "Cellar", set())
tank = frag["controls"][0]
assert (tank["kind"], tank["consent"]) == ("dhw_tank", False)
assert tank["entities"] == {"water_heater": "water_heater.tank", "heater_power": "sensor.tank_heater_power"}
assert "heater" not in (tank.get("params") or {}) # never a guess
got = {a["asset"]: a for a in frag["assets"]}
assert got["dhw.temp_top"]["attribute"] == "current_temperature" and got["dhw.temp_top"]["unit"] == "°C"
assert got["dhw.heater_power"]["entity"] == "sensor.tank_heater_power"
assert "dhw.temp_bottom" in missing and not any("control" in m for m in missing)
assert not any(a["consent"] for a in frag["assets"])
assert dp.tank_of(frag) == {"heater": ""}
assert dp.set_tank_heater(frag, {"heater": "heat_pump"}) == {"heater": "heat_pump"}
assert frag["controls"][0]["params"]["heater"] == "heat_pump"
for bad in ({"heater": "gas"}, {}):
with pytest.raises(dp.Fail):
dp.set_tank_heater(frag, bad)
with pytest.raises(dp.Fail):
dp.set_tank_heater({"controls": [{"kind": "battery"}]}, {"heater": "element"})
# one writer per entity: the water heater's control writes it, not its power sensor
assert "water_heater.tank" in dp.control_writes(frag["controls"])
assert "sensor.tank_heater_power" not in dp.control_writes(frag["controls"])
def test_immersion_heater_on_a_relay_with_tank_sensors():
"""A tank without a water_heater entity: a relay and its sensors, found
by their names across HA (ha-immersion-heater; never offered by itself)."""
p = PROFILES["ha-immersion-heater"]
assert dp.fitting([p], {"manufacturer": "Shelly", "model": "Plus 1PM"}, ["shelly"], ["switch", "sensor.temperature"]) == []
s = dp.setup_for(p, [])
ents = [{"entity_id": "switch.keller_heizstab", "unique_id": "r"},
{"entity_id": "sensor.speicher_oben", "unique_id": "t1", "device_class": "temperature"},
{"entity_id": "sensor.speicher_unten", "unique_id": "t2", "device_class": "temperature"},
{"entity_id": "sensor.heizstab_power", "unique_id": "w", "device_class": "power"},
{"entity_id": "switch.fridge", "unique_id": "f"}]
frag, missing = dp.mapping_for("tank-2", p, s, ents, None, set())
c = frag["controls"][0]
assert c["kind"] == "dhw_tank" and c["params"] == {"heater": "element"}
assert c["entities"] == {"switch": "switch.keller_heizstab", "temp_top": "sensor.speicher_oben",
"temp_bottom": "sensor.speicher_unten", "heater_power": "sensor.heizstab_power"}
assert {a["asset"] for a in frag["assets"]} == {"dhw.temp_top", "dhw.temp_bottom", "dhw.heater_power"}
assert missing == []
# without the bottom sensor and the power sensor: the control still, those roles left out
frag2, _ = dp.mapping_for("tank-3", p, s, ents[:2], None, set())
assert frag2["controls"][0]["entities"] == {"switch": "switch.keller_heizstab", "temp_top": "sensor.speicher_oben"}
# without the top sensor: no control (a tank temperature is needed), reported
frag3, missing3 = dp.mapping_for("tank-4", p, s, ents[:1], None, set())
assert frag3["controls"] == [] and "dhw_tank control (temp_top)" in missing3
def test_rating_values_above_zero():
p = PROFILES["ha-water-heater"]
got = dp.rating_values(p, {"rated_tank_volume_l": "300", "rated_tank_erp_class": "3", "rated_tank_cop": " ",
"nope": "5"})
assert got == {"rated_tank_volume_l": 300.0, "rated_tank_erp_class": 3}
assert dp.ratings_of(p, got) == {"tank_volume_l": 300.0, "tank_erp_class": 3}
for bad in ({"rated_tank_volume_l": "0"}, {"rated_tank_volume_l": "-5"}, {"rated_tank_erp_class": "8"},
{"rated_tank_cop": "abc"}, {"rated_tank_grossanlage": "0"}):
with pytest.raises(dp.Fail):
dp.rating_values(p, bad)
f = {"name": "x", "label": "X", "type": "number", "rating": "battery_kwh", "min": 0}
with pytest.raises(dp.Fail, match="above 0"):
dp.rating_value(f, "0")
fields = dp.rating_fields(p, {"ratings": {"tank_volume_l": 300.0}})
assert next(x["value"] for x in fields if x["rating"] == "tank_volume_l") == 300.0
assert next(x["value"] for x in fields if x["rating"] == "tank_cop") is None
def test_storage_from_the_plan():
"""Both Devices pages: a battery's idle loss with its source, a tank's
facts, what it is not planned without (design 10.5)."""
frag = {"controls": [{"id": "battery", "kind": "battery", "entities": {"charge_from_grid": "switch.x"}},
{"id": "dhw_tank", "kind": "dhw_tank", "entities": {"water_heater": "water_heater.t"},
"params": {"heater": "heat_pump"}},
{"id": "heating", "kind": "heating", "entities": {"climate": "climate.y"}}]}
# no plan yet: the tank's gaps from what the installer and the owner gave
out = dp.storage_of(frag, {"tank_volume_l": 300}, {})
assert [x["kind"] for x in out] == ["battery", "dhw_tank"]
assert out[0] == {"id": "battery", "kind": "battery", "planned": False}
assert out[1]["missing"] == ["max_power_w", "cop"] and out[1]["volume_l"] == 300 and not out[1]["planned"]
out = dp.storage_of(frag, {"tank_volume_l": 300, "tank_heater_kw": 1.2, "tank_cop": 2.8}, None)
assert out[1]["missing"] == [] and out[1]["heater_kw"] == 1.2 and out[1]["cop"] == 2.8
# the plan's word
planning = {"storage": {"battery": {"kind": "battery", "standby_w": 49.0, "standby_src": "learnt",
"self_discharge_pct_month": 2.0, "self_discharge_src": "parameter",
"idle_loss_kwh_24h": 1.18, "standby_in_load": None},
"dhw_tank": {"kind": "dhw_tank", "heater": "heat_pump", "volume_l": 300, "standby_w": 122.0,
"loss_src": "assumed", "erp_class": "D", "temp_c": 51.5, "usable_kwh": 2.2}},
"refused": {}}
bat, tank = dp.storage_of(frag, {}, planning)
assert bat == {"id": "battery", "kind": "battery", "planned": True, "standby_w": 49.0, "standby_src": "learnt",
"self_discharge_pct_month": 2.0, "self_discharge_src": "parameter", "idle_loss_kwh_24h": 1.18}
assert tank["planned"] and tank["missing"] == [] and (tank["loss_src"], tank["erp_class"]) == ("assumed", "D")
# refused by the plan: its missing facts
planning = {"storage": {}, "refused": {"dhw_tank": {"why": "tank volume unknown", "missing": ["volume_l"]}}}
tank = dp.storage_of(frag, {}, planning)[1]
assert tank["refused"] and tank["missing"] == ["volume_l"] and not tank["planned"]
# a mapping's facts count as given; a device's volume entity too
frag["controls"][1]["params"] = {"heater": "element", "max_power_w": 3000}
frag["controls"][1]["entities"]["volume"] = "sensor.v"
assert dp.storage_of(frag, {}, {})[1]["missing"] == [] and dp.storage_of(frag, {}, {})[1]["heater_kw"] == 3.0
frag["controls"][1]["entities"] = {"switch": "switch.r"}
assert dp.storage_of(frag, {"tank_volume_l": 200}, {})[1]["missing"] == ["temp_sensor"]