cantabile_edge, cantabile_modbus, cantabile_eebus 0.1.676: full source

This commit is contained in:
Setti Solutions S.R.L committed 2026-11-04 10:33:14 +02:00
1 parent 3edd015039
commit d48b9aff87
32 files changed
+4633 -133

No files matched your search

+160 -11
View File
@@ -24,6 +24,15 @@ Commands (from the console through the link, which checked the signature):
"floor"?, "limit"? (empty: none), "until"?}
building {"v", "id", "building", "level", "control", "value", "from",
"until", "why"}: the building's requests (building.py)
preferences {"msg": JSON of preferences.apply_msg's {"set": {"item",
"value"}}, "level": "installer|operator", "by"}: an installer's or
Cantabile's setting within the owner's limits (preferences.py);
the owner's own come from Home Assistant and the panel
The owner's preferences (preferences.py): into every model state (a room's
schedule, a car's ready-by, a battery's reserve, the overrides), a plan at
once after a change (not held back by steady.py), their feedback and the
settings with the plan to the cloud (public_plan "preferences").
"""
import asyncio
import json
@@ -41,8 +50,10 @@ from .executor import Executor, building_msg
from .gridorder import GridOrders
from .guard import Guard
from .policy import Policy
from .preferences import Preferences
from .switchover import Switchover
from .inputs import Collector, History
from . import localml
log = logging.getLogger(__name__)
@@ -73,6 +84,7 @@ class Mode:
class Controller:
def __init__(self, cfg, mapping, consent, sink, clock=time.time):
self.cfg, self.mapping, self.sink, self.clock = cfg, mapping, sink, clock
self.consent = consent
self.controls = mapping.controls
self.inputs = mapping.inputs
self.mode = Mode(cfg.state_dir, cfg.mode)
@@ -108,6 +120,25 @@ class Controller:
self.on_plan = [] # callbacks (the announcer) when a plan is made
self.received: list[dict] = [] # the last commands and bundles (shown in HA, announce.py)
self.on_received = [] # callbacks (the announcer) for each
self.prefs = Preferences(cfg.state_dir, clock) # the owner's preferences (preferences.py)
self.on_prefs = [] # callbacks (the announcer) when they change
try:
self.prefs.write_view(self.controls, self.mode())
except Exception as e:
log.warning("preferences view: %s", e)
# the box trains its own models (boxtrain.py, methods.md B10): its
# history, the nightly job, and its models for the planner (bundles.own)
self.boxml = None
if getattr(cfg, "train", True):
try:
from .boxtrain import BoxTraining
from .trainhist import Store
self.boxml = BoxTraining(cfg, Store(os.path.join(cfg.state_dir, "training.db")), self.bundles,
kinds=self.planned_kinds, clock=clock)
self.bundles.own = self.boxml
except Exception as e: # never stops the controller
log.warning("the box's own training is off: %s", e)
self.boxml = None
# -- Home Assistant ----------------------------------------------------
def outside_entity(self) -> str:
@@ -181,6 +212,7 @@ class Controller:
try:
cfg = await client.send("get_config")
self.tz = (cfg or {}).get("time_zone") or "UTC"
self.prefs.tz = self.tz
lat, lon = (cfg or {}).get("latitude"), (cfg or {}).get("longitude")
if isinstance(lat, (int, float)) and isinstance(lon, (int, float)):
self.location = {"latitude": float(lat), "longitude": float(lon)}
@@ -215,6 +247,14 @@ class Controller:
value = st.get("s")
self.guard.observe(c.id, eid, value, now)
def planned_kinds(self) -> set[str]:
"""The kinds of the devices the box plans whose power is inside the
load (heating, cooling, EV): its base load is trained where any is
metered (inputs.controlled_power), as the planner uses it."""
if not (getattr(self.inputs, "controlled_power", None) or []):
return set()
return {c.kind for c in self.controls if c.kind in ("heating", "cooling", "ev")}
# -- plans ---------------------------------------------------------------
def model_state(self) -> dict:
now = self.clock()
@@ -223,13 +263,15 @@ class Controller:
# and of a thermostat that heats and cools only its active side
self.switch_sides()
controls = [c for c in self.controls if self.policy.plannable(c) and self.switchover.active(c)]
return inputs_mod.model_state(now, controls, self.inputs, self.col, self.bundles,
self.history, self.tz, self.forecast, self.guard.extra_limits,
building=self.building.for_model(start, inputs_mod.STEP_S, inputs_mod.STEPS),
location=self.location, shade_ctx=self.shade_ctx(),
grid_order=self.gridorders.for_model(start, inputs_mod.STEP_S, inputs_mod.STEPS),
grid_max_w=getattr(self.cfg, "grid_max_w", None),
ev_phases=self.ev_phases())
state = inputs_mod.model_state(now, controls, self.inputs, self.col, self.bundles,
self.history, self.tz, self.forecast, self.guard.extra_limits,
building=self.building.for_model(start, inputs_mod.STEP_S, inputs_mod.STEPS),
location=self.location, shade_ctx=self.shade_ctx(),
grid_order=self.gridorders.for_model(start, inputs_mod.STEP_S, inputs_mod.STEPS),
grid_max_w=getattr(self.cfg, "grid_max_w", None),
ev_phases=self.ev_phases())
# the owner's preferences per step (preferences.py)
return self.prefs.apply(state, {c.id: c for c in controls}, self.col, now)
async def make_plan(self) -> dict | None:
self.forecast = []
@@ -239,6 +281,7 @@ class Controller:
except Exception as e:
log.info("weather forecast: %s", e)
state = self.model_state()
cands, net_load = state.pop("_cands", None) or {}, state.pop("_net_load", None)
plan = await solve(state, self.cfg.solve_timeout_s)
if "error" in plan:
self.plan_error = plan["error"]
@@ -247,12 +290,25 @@ class Controller:
calm, self.calm = self.calm, False
if calm:
steady.carry_quarter(plan, self.plan, self.clock())
# changes that do not matter stay out of it (deadband, hold, gain: steady.py)
# what the plan does for each preference, and what each must costs
self.prefs.take_plan(plan, self.clock())
# changes that do not matter stay out of it (deadband, hold, gain: steady.py);
# the owner's preference changes are not held back
steady.stabilise(plan, state, [c for c in self.controls if c.id in plan.get("devices", {})], self.col,
self.guard.last_change, self.clock())
self.guard.last_change, self.clock(), free=self.prefs.free())
self.prefs.write_view(self.controls, self.mode())
plan["mode"] = self.mode()
plan["id"] = f"{self.cfg.site_id[:8]}-{plan['made']}"
plan["model"] = self.bundles.model_id()
if self.boxml is not None:
try: # SQLite and numpy: in a worker thread, the loop goes on
ml = await asyncio.to_thread(self.ml_after_plan, state, plan, cands, net_load)
# upward only with the owner's share consent: without it the box
# trains for itself and nothing of it leaves (no forecasts, no errors)
if getattr(self.consent, "share", False):
plan["ml"] = ml
except Exception as e:
log.warning("the box's model figures: %s", e)
self.plan, self.plan_error = plan, ""
self.executor.plan = plan
self.executor.inputs_ok = self.inputs_fresh()
@@ -276,7 +332,82 @@ class Controller:
# for the building (shed_w/grid_w per step, 24 h): absent from older agents
"flex": plan.get("flex", {}),
# the market's contracts in the plan (shadow): planned kWh and saving vs the grid
"market": plan.get("market", [])}
"market": plan.get("market", []),
# the owner's preferences: per level who set what and when, the
# overrides, and per preference met or not and each must's cost
"preferences": self.prefs.public(),
# the box's own models (boxtrain.py): held-out and live errors per source,
# hourly the forecasts it planned on; absent from older agents
**({"ml": plan["ml"]} if plan.get("ml") else {})}
# -- the owner's preferences (preferences.py) -------------------------------
async def prefs_change(self, msg: dict, level: str, by: str) -> tuple[bool, str]:
"""A change of the preferences (Home Assistant's entities and the
panel: the owner's; the console: an installer's or operator's): a
plan with it at once (not held back: steady.py), shown in HA."""
ok, why = self.prefs.apply_msg(msg, {c.id: c for c in self.controls}, self.tz, level, by)
log.info("preferences (%s, %s): %s %s", by, level, "applied" if ok else "refused", why)
await self._prefs_changed()
return ok, why
async def _prefs_changed(self):
self.prefs.write_view(self.controls, self.mode())
self.wake.set()
for cb in self.on_prefs:
try:
await cb()
except Exception as e:
log.warning("preferences: %s", e)
async def _prefs_watch(self, stop):
"""The panel's changes (prefs-inbox/), and overrides that expire."""
while not stop.is_set():
try:
n = self.prefs.take_inbox({c.id: c for c in self.controls}, self.tz)
if n or self.prefs.prune(self.clock()):
await self._prefs_changed()
except Exception as e:
log.warning("preferences: %s", e)
try:
await asyncio.wait_for(stop.wait(), PREFS_POLL_S)
except asyncio.TimeoutError:
pass
def ml_after_plan(self, state: dict, plan: dict, cands: dict, net_load) -> dict:
"""After a plan (in a worker thread): the weather the box forecast
with and each source's forecast kept for the training and the live
error (trainhist.py), and the figures for the plan's "ml"
(boxtrain.status), with, at most hourly, the forecasts it planned on:
the load (as planned: the base load where it plans devices), PV and
the member's net draw (the plan's import less export where it acts,
auto; else the metered load's forecast less PV) for the building."""
b, now, start = self.boxml, self.clock(), int(state["start"])
st = b.store
if self.forecast:
st.put_weather("forecast", "ha", localml.ha_hours(self.forecast), now)
m = self.bundles.model.get()
if isinstance(m, dict) and m.get("site") == self.cfg.site_id:
if m.get("weather"):
st.put_weather("forecast", "cloud", localml.cloud_hours(m["weather"]), m.get("issued"))
if m.get("weather_past"):
st.put_weather("observed", "cloud", localml.cloud_hours(m["weather_past"]), now)
for key, series in (("load", "plan"), ("pv", "pv")):
if cands.get(key):
b.record(series, start, cands[key], now)
auto = plan.get("mode") == "auto"
net = (plan.get("flex") or {}).get("net_w") if auto else \
([round(lo - pv) for lo, pv in zip(net_load, state["pv_w"])] if net_load else None)
if net:
b.record("net", start, {"own": net, "week_mean": b.week_mean("net", start, len(net))}, now)
fsrc = (state.get("sources") or {}).get("forecast") or {}
ups = {k: {"start": start, "step_s": state["step_s"], "values": [round(float(v), 1) for v in state[f"{k}_w"]],
"source": (fsrc.get(k) or {}).get("source") or (state.get("sources") or {}).get(k, "")[:80]}
for k in ("load", "pv")}
ups["load"]["series"] = "base_load" if b.kinds() else "load"
if net:
ups["net"] = {"start": start, "step_s": state["step_s"], "values": [float(v) for v in net],
"source": "plan" if auto else "forecast"}
return b.status(now, ups)
def inputs_fresh(self) -> bool:
"""The plan's room temperatures and the battery's state have a value
@@ -341,7 +472,12 @@ class Controller:
await stop.wait()
return
tasks = [asyncio.create_task(self._planner(stop)), asyncio.create_task(self._actor(stop)),
asyncio.create_task(self._bundle_watch(stop))]
asyncio.create_task(self._bundle_watch(stop)), asyncio.create_task(self._prefs_watch(stop))]
if self.boxml is not None:
# the nightly training (boxtrain.py): child processes, the loop only awaits them
tasks.append(asyncio.create_task(self.boxml.run(
stop, tz=lambda: self.tz, location=lambda: self.location, client=lambda: self.client,
weather_entity=lambda: self.inputs.weather or "")))
await stop.wait()
await asyncio.gather(*tasks, return_exceptions=True)
@@ -472,6 +608,18 @@ class Controller:
return ok, msg
if cmd == "building":
return await self.building_command(args, now)
if cmd == "preferences":
# the console's: an installer's or Cantabile's, within the owner's limits
level = args.get("level", "")
if level not in ("installer", "operator"):
return False, "level: installer or operator (the owner's own come from Home Assistant or the panel)"
try:
msg = json.loads(args.get("msg") or "{}")
except ValueError:
return False, "msg: JSON"
if not isinstance(msg, dict) or "set" not in msg:
return False, "msg: {\"set\": {\"item\", \"value\"}} (overrides are the owner's)"
return await self.prefs_change(msg, level, f"{level}:{str(args.get('by') or 'console')[:60]}")
return False, f"unknown command {cmd!r}"
async def building_command(self, args: dict, now: float) -> tuple[bool, str]:
@@ -668,6 +816,7 @@ async def solve(state: dict, timeout: float) -> dict:
PLAN_AT_S = 5.0 # a plan this long after a quarter hour: its first step is the quarter just begun
PREFS_POLL_S = 2.0 # how often the panel's preference changes are looked for
def plan_wait(now: float, interval: float, at: float = PLAN_AT_S) -> float: