cantabile_edge, cantabile_modbus, cantabile_eebus 0.1.1789: full source

This commit is contained in:
Setti Solutions S.R.L committed 2026-11-12 06:28:42 +02:00
1 parent 94f46db8f8
commit b24675fe9c
25 files changed
+766 -40

No files matched your search

+82 -1
View File
@@ -115,7 +115,48 @@ def test_model_constraint_and_limits(tmp_path):
def __init__(self, id, kind, params):
self.id, self.kind, self.params = id, kind, params
lim = g.limits([C("ev", "ev", {"phases": 3, "voltage": 230}), C("bat", "battery", {}), C("room", "heating", {})])
assert lim == {"ev": {"current_a": (None, 6.0)}, "bat": {"power_w": (None, 4200.0)}}
# the one limit shared: the charger 6 A x 690 V = 4140 W, the battery's charging the 60 W left
assert lim == {"ev": {"current_a": (None, 6.0)}, "bat": {"power_w": (None, 60.0)}}
class _C:
def __init__(self, id, kind, params, entities=None):
self.id, self.kind, self.params, self.entities = id, kind, params, entities or {}
def test_chargers_share_the_one_limit(tmp_path):
# a building's three garage chargers (Sim Durlach, feeder 6, 2026-11-08): before, each got the whole 4.2 kW
g = GridOrders(str(tmp_path), clock=lambda: NOW)
g.take(message())
evs = [_C(f"garage_{n}", "ev", {"phases": 3, "voltage": 230, "min_current_a": 6, "max_current_a": 16}) for n in (1, 2, 3)]
lim = g.limits(evs + [_C("bat", "battery", {})])
amps = [lim[f"garage_{n}"]["current_a"][1] for n in (1, 2, 3)]
assert amps == [6.0, 0.0, 0.0] # one at its minimum, the others paused: within the order
assert sum(a * 690 for a in amps) + lim["bat"]["power_w"][1] <= 4200.0
# the first has no car: the second takes the share
lim = g.limits(evs, connected={"garage_1": False, "garage_2": True, "garage_3": None})
assert [lim[f"garage_{n}"]["current_a"][1] for n in (1, 2, 3)] == [0.0, 6.0, 0.0]
# a larger order: each its share in turn, never above the order
g.take(message(limit_w=16000.0, id="cb01-2"))
lim = g.limits(evs)
amps = [lim[f"garage_{n}"]["current_a"][1] for n in (1, 2, 3)]
# the first's bound is above its 16 A (it draws at most 16 A = 11 040 W), the second gets the 4 960 W left
assert amps == [23.1, 7.1, 0.0] and sum(min(a, 16) * 690 for a in amps) <= 16000.0
def test_renewed_order_holds(tmp_path):
# the bridge renews the order (the same id, a later end): it holds past the first end
t = [NOW]
g = GridOrders(str(tmp_path), clock=lambda: t[0])
assert g.take(message()) == (True, "taken")
t[0] = NOW + 290
assert g.take(message(until=NOW + 1500)) == (False, "taken") # the same order: no new decision
t[0] = NOW + 880
g.take(message(until=NOW + 1500)) # the bridge repeats it (every 15 s)
t[0] = NOW + 1000
assert g.active() and g.active()["until"] == NOW + 1500
t[0] = NOW + 1501
assert g.active() is None
highspy = pytest.importorskip("highspy")
@@ -225,3 +266,43 @@ def test_controller_publishes_the_order_as_an_action(tmp_path):
assert asyncio.run(ctl.grid_order(message().encode()))
assert [k for k, _ in sink.sent].count("action") == 1
assert [d for k, d in sink.sent if k == "action"][0]["service"] == "grid.load_limit"
def test_executor_holds_every_charger_to_its_share(tmp_path):
"""Under an order the executor sets each charger to its share whatever the plan says: a charger the plan
leaves alone (no session to plan), one planned above its share, one whose share is 0 (paused)."""
import yaml
from cantabile_edge.config import Config
from cantabile_edge.consent import Consent
from cantabile_edge.controller import Controller
from cantabile_edge.executor import RELEASE
from cantabile_edge.mapping import Mapping
m = tmp_path / "mapping.yaml"
m.write_text(yaml.safe_dump({"version": 3, "assets": [], "controls": [
{"id": f"garage_{n}", "kind": "ev", "consent": True,
"entities": {"switch": f"switch.ev{n}", "current": f"number.ev{n}_amps", "connected": f"binary_sensor.ev{n}"},
"params": {"phases": 3, "voltage": 230, "min_current_a": 6, "max_current_a": 16}} for n in (1, 2, 3)]}))
t = [NOW]
ctl = Controller(Config(site_id="ab" * 16, mapping=str(m), state_dir=str(tmp_path)), Mapping.load(str(m)),
Consent(str(tmp_path)), sink=None, clock=lambda: t[0])
ctl.col.on_entities({"a": {f"binary_sensor.ev{n}": {"s": "on", "a": {}} for n in (1, 2, 3)}})
assert asyncio.run(ctl.grid_order(message().encode()))
ex = ctl.executor
cs = {c.id: c for c in ctl.controls}
ex.commands.clear()
ex.plan = None # no plan: each control's safe value (release)
want = {cid: ex.wanted(cs[cid], t[0]) for cid in cs}
assert want["garage_1"][0] == 6.0 and want["garage_1"][1] == "emergency" and "grid operator" in want["garage_1"][2]
assert want["garage_2"][0] == "off" and want["garage_3"][0] == "off"
# a plan above the share is capped; a plan below it stays the plan's
assert ex._grid_bound(cs["garage_1"], 16.0, "plan", "")[0] == 6.0
assert ex._grid_bound(cs["garage_1"], "off", "plan", "x") == ("off", "plan", "x")
assert ex._grid_bound(cs["garage_1"], RELEASE, "plan", "")[0] == 6.0
# the car on charger 1 leaves: the next round gives charger 2 the share
ctl.col.on_entities({"a": {"binary_sensor.ev1": {"s": "off", "a": {}}}})
ctl.grid_tick()
assert ctl.guard.extra_limits["garage_2"]["current_a"]["grid"] == (None, 6.0)
assert ctl.guard.extra_limits["garage_1"]["current_a"]["grid"] == (None, 0.0)
# the order ends: no bound, the plan's want again
assert asyncio.run(ctl.grid_order(message(active=False, state="normal", limit_w=0.0, id="cb01-9").encode()))
assert ex._grid_bound(cs["garage_1"], 16.0, "plan", "")[0] == 16.0