cantabile_edge, cantabile_modbus, cantabile_eebus 0.1.629: full source

This commit is contained in:
Setti Solutions S.R.L committed 2026-11-03 23:26:41 +02:00
1 parent d5fbcacaa4
commit 85edd56ec8
46 files changed
+3518 -267

No files matched your search

+46 -2
View File
@@ -115,7 +115,7 @@ 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.1)}, "bat": {"power_w": (None, 4200.0)}}
assert lim == {"ev": {"current_a": (None, 6.0)}, "bat": {"power_w": (None, 4200.0)}}
highspy = pytest.importorskip("highspy")
@@ -160,7 +160,7 @@ def test_controller_takes_an_order(tmp_path):
got = []
ctl.on_received.append(lambda ev: _append(got, ev))
assert asyncio.run(ctl.grid_order(message().encode()))
assert ctl.guard.extra_limits["car"]["current_a"] == {"cloud": (None, 10.0), "grid": (None, 6.1)}
assert ctl.guard.extra_limits["car"]["current_a"] == {"cloud": (None, 10.0), "grid": (None, 6.0)}
assert got and got[0]["by"] == "grid" and got[0]["args"]["level"] == "emergency" and "proof" not in got[0]["args"]
st = ctl.gridorders.for_model(NOW, 900, 192)
assert st["limit_w"] == 4200.0
@@ -181,3 +181,47 @@ def test_inventory_carries_the_eebus_ski():
# anything but a SKI is left out (the platform would refuse the inventory)
assert _eebus({"identifiers": [["mqtt", "cantabile_eebus_cb_c0b05c"]], "serial_number": "c0b05c0a"}) == {}
assert _eebus({"identifiers": [["mqtt", "cantabile_modbus_x"]], "serial_number": "1"}) == {}
def test_the_order_reaches_the_cloud_once_per_quarter_hour(tmp_path):
"""An action per ledger slot while the order holds (and on a change),
one when it ends: fleet's market ledger exempts those slots."""
t = [NOW]
g = GridOrders(str(tmp_path), clock=lambda: t[0])
assert g.action() is None # no order: nothing
g.take(message(until=NOW + 1800))
a = g.action()
assert (a["entity"], a["service"], a["value"], a["result"]) == ("grid:eebus-lpc", "grid.load_limit", "4200.0", "done")
assert "grid operator's order" in a["why"] and "cb01" in a["why"] and len(a["why"]) <= 500
assert g.action() is None # the same slot: told already
t[0] = (NOW // gridorder.SLOT_S + 1) * gridorder.SLOT_S + 5 # the next quarter-hour
assert g.action()["at"] == t[0]
g.take(message(until=NOW + 1800, limit_w=3000.0)) # a new limit in the same slot
assert g.action()["value"] == "3000.0"
t[0] = NOW + 1801 # over
end = g.action()
assert end["value"] == "" and "ended" in end["why"]
assert g.action() is None
def test_controller_publishes_the_order_as_an_action(tmp_path):
import yaml
from cantabile_edge.config import Config
from cantabile_edge.consent import Consent
from cantabile_edge.controller import Controller
from cantabile_edge.mapping import Mapping
m = tmp_path / "mapping.yaml"
m.write_text(yaml.safe_dump({"version": 3, "assets": [], "controls": []}))
class Sink:
def __init__(self):
self.sent = []
async def publish(self, kind, data):
self.sent.append((kind, data))
sink = Sink()
ctl = Controller(Config(site_id="ab" * 16, mapping=str(m), state_dir=str(tmp_path)), Mapping.load(str(m)),
Consent(str(tmp_path)), sink=sink, clock=lambda: NOW)
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"