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

+1 -1
View File
@@ -22,7 +22,7 @@ Settings → Add-ons → Add-on store → ⋮ → Repositories, add
`https://git.setti.solutions/Daniel/cantabile-ha-addons`, then install the
add-on. The Supervisor pulls the prebuilt images
(`git.setti.solutions/daniel/cantabile-ha-<edge|modbus|eebus>-<arch>:<version>`,
named by `image:` in each `config.yaml`); this version: `0.1.400`, arch amd64.
named by `image:` in each `config.yaml`); this version: `0.1.629`, arch amd64.
## Layout
+16
View File
@@ -6,6 +6,22 @@ names the commit; the agent inside is the Edge Agent image of that commit.
## 0.1.x
- The Devices page is clearer: the devices you use with Cantabile are a
short list, each with its state in words ("In use · reading on · control
on" or "Added · reading off"), its main values now ("21.5 °C, set to
22 °C, heating") and whether it is protected. Open a device to change it:
reading and control switch at once, what it reads is said in plain words
("room temperature (Living room), setpoint, heating/cooling state"), and
heating or cooling, room and window, indoor or outdoor and protection
each save on their own (Save shows once you change something). Removing
asks you to confirm. "Untested" profiles are marked with a small badge and
an "i" that explains it. Devices you can still add are listed apart,
below; a device in use is never offered again.
- Heat is shown as heat: a heat meter's or heat pump's heat is "Heat from
the building (thermal)" or "Heat delivered (thermal)", in kW and kWh, in
its own band below solar, home, grid and battery, never added to the
electricity. With your own heat pump its electric power and its COP are
shown beside the heat.
- Blinds, curtains, shutters and awnings: the Devices page offers any of
Home Assistant's covers (never a garage door, gate, door, damper or
window). You set the room it shades and which way the window faces.
+1 -1
View File
@@ -3,7 +3,7 @@
# the Hub's, wrapped by addon-run (Dockerfile). The image is prebuilt,
# per arch (README.md: building it from src/).
name: Cantabile Edge Agent
version: "0.1.400"
version: "0.1.629"
slug: cantabile_edge
description: Plans your home's energy with Cantabile and, with your consent, runs the devices you choose.
url: https://cantabile.cloud
+1 -1
View File
@@ -4,7 +4,7 @@
# Assistant that isn't a Cantabile Hub. Devices are paired on its page
# (ingress), by their SKI.
name: Cantabile EEBUS
version: "0.1.400"
version: "0.1.629"
slug: cantabile_eebus
description: Pairs with EEBUS devices (the grid operator's control box, heat pumps, wallboxes, inverters), shows them in Home Assistant under Cantabile's names and hands the grid operator's §14a limit to the Cantabile agent; every control with its protection.
url: https://cantabile.cloud
+1 -1
View File
@@ -4,7 +4,7 @@
# Home Assistant that isn't a Cantabile Hub. Devices come from the options;
# the page (ingress) shows them and each control's protection.
name: Cantabile Modbus bridge
version: "0.1.400"
version: "0.1.629"
slug: cantabile_modbus
description: Reads inverters, meters, relays and circuits by Cantabile's device profiles (Modbus, or Home Assistant's own entities) and shows them in Home Assistant, each control with its protection.
url: https://cantabile.cloud
+34 -2
View File
@@ -21,6 +21,7 @@ from .consent import Consent
from .controller import Controller
from .inventory import Inventory
from .dashboards import Dashboards
from .gridorder import GridOrders
from .devpage import App, Devices
from .joinpage import Fleet
from .mapping import Mapping, convert, scaled, value_of
@@ -61,9 +62,14 @@ class Agent:
self.control = Controller(cfg, mapping, self.consent, sink) if mapping.controls else None
if self.control is not None and hasattr(sink, "on_command"):
sink.on_command = self.control.command
# the grid operator's orders (EEBUS LPC, gridorder.py): the controller's,
# or, on a site without controls, checked and told to the cloud only
self.gridorders = self.control.gridorders if self.control is not None else GridOrders(cfg.state_dir)
# the devices' specs for the cloud (inventory.py)
self.inventory = Inventory(cfg, mapping, self.consent, sink,
self.control.policy if self.control is not None else None)
if self.control is not None:
self.inventory.ev_phases = self.control.ev_phases
self.client = None
self.exit_code = 0 # RESTART: start again (restart())
# A mapping for a site without one (propose.py): the HA add-on
@@ -234,10 +240,10 @@ class Agent:
async def inventory_loop(self):
"""The inventory at connecting, then every 30 minutes if it changed
(and once a day anyway)."""
(and once a day anyway); every minute while the link is not connected."""
while not self.stop.is_set():
try:
await asyncio.wait_for(self.inventory_now.wait(), 1800)
await asyncio.wait_for(self.inventory_now.wait(), 60 if self.inventory.waiting else 1800)
except asyncio.TimeoutError:
pass
self.inventory_now.clear()
@@ -263,6 +269,27 @@ class Agent:
time.strftime("%H:%M", time.gmtime(start)), seq, len(records),
self.queue.depth(), self.queue.dropped())
async def grid_order(self, payload) -> bool:
"""A message on cantabile-eebus/gridorder (announce.py): the
controller's when there is one (limits and a plan within them);
otherwise only checked and told to the cloud (nothing to limit)."""
if self.control is not None:
return await self.control.grid_order(payload)
changed, _ = self.gridorders.take(payload)
await self.grid_report()
return changed
async def grid_report(self):
"""The grid operator's order to the cloud as an action per
quarter-hour (gridorder.py action()): fleet's market ledger exempts
those slots."""
if self.control is not None:
return await self.control.grid_report()
a = self.gridorders.action()
pub = getattr(self.sink, "publish", None)
if a is not None and pub is not None:
await pub("action", a)
async def ticker(self):
i = self.cfg.interval_s
while not self.stop.is_set():
@@ -271,6 +298,11 @@ class Agent:
await asyncio.wait_for(self.stop.wait(), max(0.0, end - time.time()))
except asyncio.TimeoutError:
self.close_window(end)
if self.control is None: # the controller's own loop reports otherwise
try:
await self.grid_report()
except Exception as e:
log.warning("grid order report: %s", e)
async def uploader(self):
wait = 1.0
+174 -4
View File
@@ -27,6 +27,15 @@ building and the home, and what the agent does to each device):
sensor set_<control> per control: the value the agent wants it at now
and from whom (plan, cloud, building, owner, safe
default), whether it acts, the limits per level
sensor market_<contract>, market_position[_building], market_price, market_actuator_<device>
the shadow market (market.json, a signed `market`
bundle): per contract the exchange with its
counterpart (kW in the last settled quarter-hour,
a sentence in plain words, the period), the net
position, the shared energy's price, per border
actuator its contract target vs its state now;
sensors only, unavailable when the bundle is
stale (cantabile/<site>/market/availability)
On a building's own box (flats.json, a signed `flats` bundle from the
cloud: bundles.py) each flat is a device of its own ("Flat", via the
building's Cantabile device, area "Flats"): its grid power, its
@@ -229,6 +238,162 @@ class Announcer:
"json_attributes_template": "{{ {'commands': value_json.commands} | tojson }}", **common}
return out
# -- the shadow market (a `market` bundle: bundles.py) -------------------
def _market_avail(self) -> dict:
"""Unavailable when the agent is offline or the bundle is stale."""
return {"availability": [{"topic": f"{self.base}/availability"}, {"topic": f"{self.base}/market/availability"}],
"availability_mode": "all"}
@staticmethod
def _party(c: dict, lang: str) -> str:
p = c.get("counterpart") or {}
if p.get("type") == "homes":
return names.name("mk_flats", lang, p.get("count") or "")
return p.get("name") or p.get("type") or "?"
def _market_objects(self, m: dict | None) -> dict:
"""Per contract the exchange with its counterpart, the net position
of each of the box's nodes, the shared energy's price, per border
actuator its contract target: sensors only (never a control: the
actuators keep their own entities and protection)."""
ctl = getattr(self.agent, "control", None)
if m is None or (ctl is not None and ctl.mode() == "off"):
return {}
dev, out, lang = self._device(), {}, self.lang
common = {"state_topic": f"{self.base}/market", "json_attributes_topic": f"{self.base}/market",
**self._market_avail(), "device": dev}
for c in m.get("contracts") or []:
k = _slug(c["id"])
out[("sensor", f"market_{k}")] = {
"name": names.name("market_exchange", lang, self._party(c, lang)), "unique_id": f"{self.uid}_market_{k}",
"unit_of_measurement": "kW", "device_class": "power", "state_class": "measurement", "icon": "mdi:swap-horizontal",
"value_template": "{%% set v = value_json.contracts['%s'].kw %%}{{ v if v is not none else none }}" % k,
"json_attributes_template": "{{ value_json.contracts['%s'] | tojson }}" % k, **common}
nodes = {n.get("id"): n.get("name") for n in m.get("nodes") or []}
for p in m.get("positions") or []:
kind = str(p.get("node", "")).split(":")[0]
obj = "market_position" if kind == "site" else f"market_position_{_slug(kind)}"
out[("sensor", obj)] = {
"name": names.name("market_position", lang) if kind == "site" else
names.name("market_position_of", lang, nodes.get(p.get("node")) or kind),
"unique_id": f"{self.uid}_{obj}", "unit_of_measurement": "kWh", "state_class": "measurement",
"icon": "mdi:scale-balance",
"value_template": "{{ value_json.positions['%s'].net_kwh }}" % obj,
"json_attributes_template": "{{ value_json.positions['%s'] | tojson }}" % obj, **common}
if m.get("contracts"):
out[("sensor", "market_price")] = {
"name": names.name("market_price", lang), "unique_id": f"{self.uid}_market_price",
"unit_of_measurement": "EUR/kWh", "suggested_display_precision": 3, "state_class": "measurement",
"icon": "mdi:cash-multiple",
"value_template": "{% set v = value_json.price.now %}{{ (v / 100) | round(4) if v is not none else none }}",
"json_attributes_template": "{{ value_json.price | tojson }}", **common}
for a in m.get("actuators") or []:
k = _slug(a["device"])
out[("sensor", f"market_actuator_{k}")] = {
"name": names.name("market_actuator", lang, a.get("name") or a["device"]),
"unique_id": f"{self.uid}_market_actuator_{k}", "icon": "mdi:electric-switch",
"value_template": "{{ value_json.actuators['%s'].target }}" % k,
"json_attributes_template": "{{ value_json.actuators['%s'] | tojson }}" % k, **common}
return out
def _actual(self, device: str):
"""A border actuator's state in HA now, from the agent's own view of
the device's controls (None: not one the agent watches)."""
ctl = getattr(self.agent, "control", None)
col = getattr(ctl, "col", None)
if ctl is None or col is None:
return None
for c in ctl.controls:
if getattr(c, "device", "") == device:
for eid in (c.entities or {}).values():
st = col.state(eid)
if st is not None:
return st
return None
def market_state(self, m: dict) -> dict:
"""The market's values for HA: per contract (kW in the last settled
quarter-hour, a sentence in plain words, the details), positions,
the price now, the actuators' target and actual state."""
lang = self.lang
num = names.number
out = {"contracts": {}, "positions": {}, "actuators": {}, "price": {}, "shadow": True, "stale": bool(m.get("stale")),
"note": names.name("mk_shadow_note", lang), "issued": m.get("issued"), "slot": m.get("slot")}
links = {lk.get("id"): lk for lk in m.get("links") or []}
prices, applying = {}, []
for c in m.get("contracts") or []:
k = _slug(c["id"])
last, slot, per = c.get("last") or None, c.get("slot") or {}, c.get("period") or {}
party = self._party(c, lang)
head = names.name("mk_sells" if c["direction"] == "sells" else "mk_buys", lang, party)
kw = None
if last and last.get("measured"):
d = float(last.get("delivered_kwh") or 0)
kw = round(d * 4, 3)
amount = float(last.get("max_kwh") or 0) or float(last.get("guaranteed_kwh") or 0)
part = names.name("mk_slot", lang, num(d, lang, 2), num(amount, lang, 2)) if amount > 0 else \
names.name("mk_slot_open", lang, num(d, lang, 2))
else:
part = names.name("mk_no_value", lang)
price = slot.get("price_ct")
bits = [f"{head}: {part}"]
if price is not None:
bits.append(f"{num(float(price), lang)} ct/kWh")
prices[k] = price
if slot.get("applies"):
applying.append(float(price))
if not slot.get("applies"):
bits.append(names.name("mk_not_now", lang))
bits.append(names.name("mk_shadow", lang))
details = []
if last and last.get("kind") and last.get("deviation_kwh"):
dev = num(abs(float(last["deviation_kwh"])), lang, 2)
details.append(names.name("mk_deviation_self" if last.get("cause") == "self" else "mk_deviation_other", lang, dev))
if last.get("exempt"):
details.append(names.name("mk_exempt", lang, last.get("exempt_why") or ""))
pname = names.name("mk_" + (per.get("period") or "month"), lang) if ("mk_" + (per.get("period") or "month")) in names.NAMES \
else per.get("period", "")
period = names.name("mk_period", lang, pname, num(float(per.get("delivered_kwh") or 0), lang),
num(float(per.get("amount_eur") or 0), lang, 2))
if per.get("penalties_paid_eur") or per.get("penalties_received_eur"):
period += " · " + names.name("mk_penalties", lang, num(float(per.get("penalties_paid_eur") or 0), lang, 2),
num(float(per.get("penalties_received_eur") or 0), lang, 2))
meas = (links.get(c.get("link")) or {}).get("measurement") or {}
out["contracts"][k] = {
"kw": kw, "summary": " · ".join(bits), "details": "; ".join(details), "period_words": period,
"contract": c["id"], "name": c.get("name", ""), "direction": c["direction"], "counterpart": party,
"counterpart_type": (c.get("counterpart") or {}).get("type", ""), "link_kind": c.get("link_kind", ""),
"slot": slot, "last": last, "period": per, "kwh_period": per.get("delivered_kwh"), "price_ct": price,
"penalty_last_eur": (last or {}).get("penalty_eur"), "measurement": meas.get("kind", ""),
"trust": meas.get("trust", ""), "shadow": True}
out["price"] = {"now": round(sum(applying) / len(applying), 3) if applying else None, "unit": "ct/kWh",
"per_contract": prices}
for p in m.get("positions") or []:
kind = str(p.get("node", "")).split(":")[0]
obj = "market_position" if kind == "site" else f"market_position_{_slug(kind)}"
out["positions"][obj] = dict(p)
for a in m.get("actuators") or []:
k = _slug(a["device"])
tgt = a.get("target") or {}
actual = self._actual(a["device"])
if actual is None and a.get("last"):
actual = a["last"].get("value")
def say(x):
return names.name(f"mk_state_{x}", lang) if f"mk_state_{x}" in names.NAMES else \
(str(x) if x not in (None, "") else names.name("mk_unknown", lang))
kwb = f" ({num(float(tgt['kw']), lang)} kW)" if tgt.get("kw") is not None else ""
words = (f"{names.name('mk_target', lang)}: **{say(tgt.get('state'))}**{kwb} · {names.name('mk_actual', lang)}: "
f"**{say(actual)}** · {names.name('mk_protection', lang, a.get('protection') or 'none')} · "
+ names.name("mk_default", lang, say(a.get("default_state")), int((a.get("fallback_s") or 0) // 60)))
out["actuators"][k] = {"target": tgt.get("state"), "target_kw": tgt.get("kw"), "why": tgt.get("why", ""),
"actual": actual, "agrees": actual is not None and str(actual) == str(tgt.get("state")),
"words": words, "device": a["device"], "name": a.get("name", ""), "link": a.get("link"),
"protection": a.get("protection"), "default_state": a.get("default_state"),
"default_set": a.get("default_set"), "fallback_s": a.get("fallback_s"),
"last": a.get("last"), "shadow": True}
return out
def objects(self) -> dict:
"""Every discovery config HA should have now: (node, component,
object) -> config; node is the site's or a flat's."""
@@ -236,7 +401,8 @@ class Announcer:
tariff, model = self.bundles.tariff_now(now), self.bundles.model_info()
out = {}
for (comp, obj), conf in {**self._configs(), **self._plan_objects(),
**self._exchange_objects(tariff, model)}.items():
**self._exchange_objects(tariff, model),
**self._market_objects(self.bundles.market_now(now))}.items():
out[(self.uid, comp, obj)] = conf
out.update(self._flat_objects(self.bundles.flat_list()))
# The entity ids themselves (HA 2025.10+): without them HA makes them
@@ -332,6 +498,10 @@ class Announcer:
if ctl is not None and ctl.received:
# kept (retained) across restarts until new ones come
await c.publish(f"{self.base}/received", json.dumps({"commands": self.received_list(ctl)}), retain=True)
market = self.bundles.market_now(now)
if market is not None:
await c.publish(f"{self.base}/market/availability", "offline" if market["stale"] else "online", retain=True)
await c.publish(f"{self.base}/market", json.dumps(self.market_state(market), default=str), retain=True)
for f in self.bundles.flat_list()[:MAX_FLATS]:
await c.publish(f"{self.base}/flat/{_slug(f['id'])}", json.dumps(self.flat_state(f, now)), retain=True)
@@ -371,13 +541,13 @@ class Announcer:
async def _grid_orders(self, c):
"""The EEBUS bridge's grid order (gridorder.py), to the
controller: the grid operator's limit as a higher-rank decision."""
ctl = getattr(self.agent, "control", None)
if ctl is None:
take = getattr(self.agent, "grid_order", None)
if take is None:
return
def on(topic, payload):
if topic == gridorder.TOPIC and payload:
asyncio.ensure_future(ctl.grid_order(payload))
asyncio.ensure_future(take(payload))
c.on_message = on
try:
await c.subscribe(gridorder.TOPIC)
+247 -6
View File
@@ -1,5 +1,6 @@
# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
"""Data bundles the optimiser reads: tariffs and the forecast model.
"""Data bundles the optimiser reads: tariffs and the forecast model (and,
for Home Assistant only, a building's flats and the shadow market).
Both arrive as signed commands; the link checks the signature with the
bundle key, the kind, site and expiry, keeps only the newest, and writes the
@@ -24,10 +25,35 @@ Model v1 (the forecasts' data; the ML in ml/ is a placeholder, 2026-10-28):
hour_of_week = weekday (Monday 0) * 24 + hour, in the site's time zone.
Without a model (or a series): the same quarter hour yesterday from the
agent's own history (inputs.History), else 0.
Optional (2026-10-30; an older agent ignores them):
- per series "series": {"start": <unix, a quarter hour>, "step_s": 900,
"values": [at most 400 numbers]}: the cloud's trained forecast (W) for
the next ~48 h; where it covers a step it is taken, elsewhere the
hour_of_week profile.
- "rooms": {"<key>": {"r_k_per_kw": 0.5-200, "c_kwh_per_k": 0.1-100,
"power": "thermal"|"electric" (default thermal), ...}}: a room's R and C
identified in the cloud from its history (electric: fitted against the
heating's electric power; inputs.model_state converts with the cop).
Only from a model for this site (not "*"). A heating or cooling control
takes its room by its id, its id without _heating/_cooling/_heat/_cool,
its area, or, as the site's only such control, the model's only room
(room_rc). The mapping's own r_k_per_kw/c_kwh_per_k win.
- forecasts "base_load_w" (2026-11-03, Finding A2-a): the load without the
devices the box plans (load.power less heatpump.power, ac.power and
ev.power where the site has those controls); the plan's load. A model
without it: its load_w only where the box has no meter of a planned
device inside the load (inputs.model_state), else the box's history.
- per room "gain_w" (a constant internal gain, W of heat, or 24 values by
local hour) and "solar_w_per_kw" (W of heat per kW/m² of global
irradiance) (2026-11-03, Finding B6-a): the fit's other terms, taken
into the room's model with R and C (room_rc). Absent: 0, as before.
A malformed series or room refuses the whole bundle.
"""
import json
import logging
import math
import os
import re
import time
from dataclasses import dataclass
from datetime import datetime, timezone
@@ -85,14 +111,60 @@ def _check_tariff(d: dict):
raise ValueError("no import price")
SERIES_MAX = 400 # a model's series: steps of 900 s (~4 days)
ROOM_R = (0.5, 200.0) # K/kW
ROOM_C = (0.1, 100.0) # kWh/K
ROOM_SUFFIX = re.compile(r"_(heating|cooling|heat|cool)$")
# a room's fitted gains (Finding B6-a), as the planner takes them: outside
# these the fit's artefact (a negative sun, a gain above any home's) is cut
ROOM_GAIN_W = (-2000.0, 5000.0) # W of heat, internal (people, appliances)
ROOM_SOLAR_M2 = (0.0, 50.0) # W of heat per W/m² of global irradiance
def _clip(x: float, lo_hi) -> float:
return min(max(float(x), lo_hi[0]), lo_hi[1])
def _finite(x) -> bool:
return isinstance(x, (int, float)) and not isinstance(x, bool) and math.isfinite(x)
def _check_model(d: dict):
if d.get("v") != 1 or d.get("kind") != "model":
raise ValueError("not a v1 model")
for name, f in (d.get("forecasts") or {}).items():
if f.get("profile") != "hour_of_week" or len(f.get("values") or []) != 168:
if not isinstance(f, dict) or f.get("profile") != "hour_of_week" or len(f.get("values") or []) != 168:
raise ValueError(f"{name}: an hour_of_week profile of 168 values")
if not all(isinstance(x, (int, float)) for x in f["values"]):
raise ValueError(f"{name}: numbers")
s = f.get("series")
if s is None:
continue
if not isinstance(s, dict) or s.get("step_s") != 900 or not _finite(s.get("start")) or s["start"] % 900:
raise ValueError(f"{name}: a series in steps of 900 s from a quarter hour")
v = s.get("values")
if not isinstance(v, list) or len(v) > SERIES_MAX or not all(_finite(x) for x in v):
raise ValueError(f"{name}: a series of at most {SERIES_MAX} numbers")
rooms = d.get("rooms")
if rooms is None:
return
if not isinstance(rooms, dict):
raise ValueError("rooms: an object")
for key, r in rooms.items():
if not isinstance(r, dict):
raise ValueError(f"room {key}: an object")
rk, ck = r.get("r_k_per_kw"), r.get("c_kwh_per_k")
if not _finite(rk) or not ROOM_R[0] <= rk <= ROOM_R[1]:
raise ValueError(f"room {key}: r_k_per_kw {ROOM_R[0]}-{ROOM_R[1]}")
if not _finite(ck) or not ROOM_C[0] <= ck <= ROOM_C[1]:
raise ValueError(f"room {key}: c_kwh_per_k {ROOM_C[0]}-{ROOM_C[1]}")
if r.get("power", "thermal") not in ("thermal", "electric"):
raise ValueError(f"room {key}: power thermal or electric")
g = r.get("gain_w")
if g is not None and not (_finite(g) or (isinstance(g, list) and len(g) == 24 and all(_finite(x) for x in g))):
raise ValueError(f"room {key}: gain_w a number or 24 numbers (by local hour)")
if r.get("solar_m2") is not None and not _finite(r["solar_m2"]):
raise ValueError(f"room {key}: solar_m2 a number")
def _check_flats(d: dict):
@@ -108,6 +180,32 @@ def _check_flats(d: dict):
raise ValueError("flat label: a string")
MARKET_STALE_S = 4 * 900 # without stale_after_s in the bundle: four slots
def _check_market(d: dict):
"""The shadow market's values for this box's own nodes (fleet's
marketbundle.go, the market's step 1.5). Written by the link from a
signed `market` bundle; shown in HA (announce.py, dashboards.py), never
acted on: a bundle that is not marked shadow-only is refused."""
if d.get("v") != 1 or d.get("kind") != "market":
raise ValueError("not a v1 market bundle")
if d.get("shadow") is not True or d.get("commands") != "none":
raise ValueError("a market bundle is shadow only (commands: none)")
for key, most in (("contracts", 200), ("links", 200), ("actuators", 100), ("positions", 10), ("nodes", 10)):
v = d.get(key) or []
if not isinstance(v, list) or len(v) > most:
raise ValueError(f"{key}: a list of at most {most}")
for c in d.get("contracts") or []:
if not isinstance(c.get("id"), str) or not c["id"] or len(c["id"]) > 80:
raise ValueError("contract id: a string")
if c.get("direction") not in ("sells", "buys"):
raise ValueError("contract direction: sells or buys")
for a in d.get("actuators") or []:
if not isinstance(a.get("device"), str) or not a["device"] or len(a["device"]) > 80:
raise ValueError("actuator device: a string")
@dataclass
class Prices:
import_ct: list[float]
@@ -115,12 +213,49 @@ class Prices:
note: str # what the prices are ("tariff <id>", "flat fallback ...")
def _ahead_terms(ah, start: int, step_s: int, steps: int, terms) -> list | None:
"""A contract's coming slots (fleet marketbundle.go "ahead": windows of
slot indices with their amounts, the price as [count, ct] runs) as the
plan's steps: per step None (outside its windows or the slots told) or
terms(price, guaranteed_kwh, minimum_kwh, max_kwh). None when the
contract has no usable "ahead" (the caller then holds its slot's terms)."""
if not isinstance(ah, dict):
return None
try:
a0, astep, n = int(ah["start"]), int(ah.get("step_s") or 900), int(ah.get("slots") or 0)
prices = []
for cnt, ct in ah.get("price_ct") or []:
prices += [float(ct)] * int(cnt)
wins = [(int(w["from"]), int(w["to"]), w) for w in ah.get("windows") or []]
except (KeyError, TypeError, ValueError):
return None
if astep <= 0 or n <= 0 or n > 2000 or len(prices) < n:
return None
by_slot: dict[int, dict] = {}
for f, t, w in wins:
for i in range(max(0, f), min(n, t)):
by_slot[i] = w
out = []
for k in range(steps):
i = (start + k * step_s - a0) // astep
w = by_slot.get(i) if 0 <= i < n else None
if w is None:
out.append(None)
continue
try:
out.append(terms(prices[i], w.get("guaranteed_kwh"), w.get("minimum_kwh"), w.get("max_kwh")))
except (TypeError, ValueError):
out.append(None)
return out
class Bundles:
def __init__(self, state_dir: str, site: str, default_price_ct: float = 30.0):
self.site, self.default = site, default_price_ct
self.tariff = _File(os.path.join(state_dir, "tariff.json"), _check_tariff)
self.model = _File(os.path.join(state_dir, "model.json"), _check_model)
self.flats = _File(os.path.join(state_dir, "flats.json"), _check_flats)
self.market = _File(os.path.join(state_dir, "market.json"), _check_market)
self.flat: float | None = None # the last known tariff's mean import price
def stamps(self):
@@ -136,16 +271,19 @@ class Bundles:
imp = [x for x in t["import"] if x is not None]
self.flat = sum(imp) / len(imp)
flat = self.flat if self.flat is not None else self.default
# feed-in without a price is counted at 0 ct: the plan then values PV
# export at nothing (it may curtail or self-consume more), so its note says so
if t is None:
note = "no tariff yet: default price" if self.flat is None else "no tariff: last known flat price"
return Prices([flat] * steps, [0.0] * steps, note)
return Prices([flat] * steps, [0.0] * steps, note + "; feed-in at 0 ct/kWh")
expired = now >= t.get("expires", 0)
out_i, out_e, outside = [], [], 0
out_i, out_e, outside, no_export = [], [], 0, 0
for k in range(steps):
i = (start + k * step_s - t["start"]) // 900
pi = t["import"][i] if not expired and 0 <= i < len(t["import"]) else None
pe = (t.get("export") or [])[i] if not expired and 0 <= i < len(t.get("export") or []) else None
outside += pi is None
no_export += pe is None
out_i.append(pi if pi is not None else flat)
out_e.append(pe if pe is not None else 0.0)
if expired:
@@ -154,6 +292,10 @@ class Bundles:
note = f"tariff {t.get('id')}; {outside} of {steps} steps at the flat {flat:.1f} ct/kWh"
else:
note = f"tariff {t.get('id')}"
if no_export == steps:
note += "; feed-in at 0 ct/kWh"
elif no_export:
note += f"; feed-in at 0 ct/kWh in {no_export} of {steps} steps (no export price)"
return Prices(out_i, out_e, note)
def tariff_now(self, now: float) -> dict | None:
@@ -178,7 +320,8 @@ class Bundles:
if m is None or m.get("site") not in (self.site, "*"):
return None
return {"id": m.get("id"), "source": m.get("source") or m.get("id"), "issued": m.get("issued"),
"expires": m.get("expires"), "series": sorted((m.get("forecasts") or {}))}
"expires": m.get("expires"), "series": sorted((m.get("forecasts") or {})),
"rooms": sorted(m.get("rooms") or {}) if m.get("site") == self.site else []}
def flat_list(self) -> list[dict]:
d = self.flats.get()
@@ -188,6 +331,69 @@ class Bundles:
return []
return list(d.get("flats") or [])
def market_now(self, now: float) -> dict | None:
"""The market bundle for HA, or None (none, another site's, expired,
or nothing in it). "stale": older than its stale_after_s (the
entities go unavailable then)."""
d = self.market.get()
if d is None or d.get("site") not in (self.site,) or (d.get("expires") and d["expires"] <= now):
return None
if not (d.get("contracts") or d.get("actuators")):
return None
age = now - max(d.get("issued") or 0, d.get("slot") or 0)
return dict(d, stale=age > float(d.get("stale_after_s") or MARKET_STALE_S), age_s=int(age))
def market_for_model(self, start: int, step_s: int, steps: int, now: float) -> list[dict]:
"""The contracts in force as energy sources and sinks for the plan
(the market's step 2; shadow: the plan only, no commands). Per
contract: id, name, direction (buys: an import source, sells: an
export sink), price_ct, guaranteed_w (firm), minimum_wh (take-or-pay,
per step), max_w (0: none), from_step, to_step (the plan's steps it
applies in) and, from a bundle with the coming slots, terms: per plan
step None (it does not apply) or {price_ct, guaranteed_w, minimum_wh,
max_w}; the scalars are then those of its first step.
The coming slots (a contract's "ahead", fleet marketbundle.go since
market-polish): each step gets its own slot's terms (window, amounts,
price) as far as the bundle tells them, while the bundle is fresh.
A bundle without them (an older fleet) carries one slot's terms; they
are held for the steps while the bundle is fresh (its slot .. slot +
stale_after_s), nothing beyond: the plan never assumes a contract it
hasn't been told of."""
d = self.market_now(now)
if d is None or d.get("stale"):
return []
slot = int(d.get("slot") or 0)
h = int(d.get("step_s") or 900) / 3600.0 # the bundle's slot in hours: kWh per slot -> W
def terms(price, g_kwh, min_kwh, max_kwh):
return {"price_ct": float(price),
"guaranteed_w": max(0.0, float(g_kwh or 0)) * 1000.0 / h,
"minimum_wh": max(0.0, float(min_kwh or 0)) * 1000.0 * (step_s / 3600.0) / h,
"max_w": max(0.0, float(max_kwh or 0)) * 1000.0 / h}
until = slot + int(float(d.get("stale_after_s") or MARKET_STALE_S))
a = max(0, -(-(slot - start) // step_s))
b = min(steps, -(-(until - start) // step_s))
out = []
for c in d.get("contracts") or []:
head = {"id": c["id"], "name": str(c.get("name") or "")[:200], "direction": c["direction"]}
per = _ahead_terms(c.get("ahead"), start, step_s, steps, terms)
if per is not None:
on = [t for t, x in enumerate(per) if x is not None]
if on:
out.append({**head, **per[on[0]], "from_step": on[0], "to_step": on[-1] + 1, "terms": per})
continue
sl = c.get("slot") or {}
if b <= a or not sl.get("applies"):
continue
try:
x = terms(sl.get("price_ct"), sl.get("guaranteed_kwh"), sl.get("minimum_kwh"), sl.get("max_kwh"))
except (TypeError, ValueError):
continue
out.append({**head, **x, "from_step": a, "to_step": b})
return out
def forecast(self, name: str, start: int, step_s: int, steps: int, tz: str) -> list[float] | None:
"""A model series for the horizon, or None (no model or no such
series: the caller falls back to history)."""
@@ -198,12 +404,47 @@ class Bundles:
if f is None:
return None
zone = ZoneInfo(tz or "UTC")
ser = f.get("series") or {}
s0, sv = ser.get("start"), ser.get("values") or []
out = []
for k in range(steps):
d = datetime.fromtimestamp(start + k * step_s, timezone.utc).astimezone(zone)
t = start + k * step_s
i = int((t - s0) // 900) if sv else -1
if 0 <= i < len(sv):
out.append(float(sv[i])) # the trained series where it covers
continue
d = datetime.fromtimestamp(t, timezone.utc).astimezone(zone)
out.append(float(f["values"][d.weekday() * 24 + d.hour]))
return out
def room_rc(self, control_id: str, area: str = "", only_one: bool = False,
now: float | None = None) -> dict | None:
"""A heating or cooling control's room in the model (R and C fitted
in the cloud): by its id, its id without _heating/_cooling/_heat/
_cool, its area; else, when the caller says it is the site's only
such control (only_one), the model's only room. None without a
model for this site (a shared "*" model has no rooms), expired, or
no match. {"r_k_per_kw", "c_kwh_per_k", "power", "gain_w" (W, a
number or 24 by local hour; 0 when the model has none),
"solar_m2"}."""
m = self.model.get()
now = time.time() if now is None else now
if m is None or m.get("site") != self.site or (m.get("expires") and m["expires"] <= now):
return None
rooms = m.get("rooms") or {}
key = next((k for k in (control_id, ROOM_SUFFIX.sub("", control_id or ""), area or "")
if k and k in rooms), None)
if key is None and only_one and len(rooms) == 1:
key = next(iter(rooms))
if key is None:
return None
r = rooms[key]
g = r.get("gain_w") or 0.0
return {"r_k_per_kw": float(r["r_k_per_kw"]), "c_kwh_per_k": float(r["c_kwh_per_k"]),
"power": r.get("power") or "thermal",
"gain_w": [_clip(x, ROOM_GAIN_W) for x in g] if isinstance(g, list) else _clip(g, ROOM_GAIN_W),
"solar_m2": _clip(r.get("solar_m2") or 0.0, ROOM_SOLAR_M2)}
def model_id(self) -> str:
m = self.model.get()
return m.get("id", "") if m else ""
+4
View File
@@ -55,6 +55,10 @@ class Config:
solve_timeout_s: float = 60.0 # the optimiser's subprocess is killed after this
plan_stale_s: int = 1800 # older plans: the safe defaults
default_price_ct: float = 30.0 # ct/kWh when no tariff ever came
# The site's grid connection (W): the plan's import and export are at most
# this. 3 x 230 V x 63 A, a German house connection's usual main fuse;
# a site with a smaller fuse (or a building's larger one) sets its own.
grid_max_w: float = 43470.0
override_hold_s: int = 7200 # hands off a control the owner changed by hand
min_switch_s: int = 600 # anti-cycling: a switch stays on/off at least this long
max_actions_per_hour: int = 12 # per control
+77 -8
View File
@@ -35,6 +35,7 @@ import time
from . import inputs as inputs_mod
from .building import Building
from .bundles import Bundles
from .evphases import EvPhases
from .executor import Executor, building_msg
from .gridorder import GridOrders
from .guard import Guard
@@ -81,6 +82,7 @@ class Controller:
min_switch_s=cfg.min_switch_s, max_actions_per_hour=cfg.max_actions_per_hour, clock=clock)
self.saved_limits = self._load_limits() # the console's (limits.json)
self.gridorders = GridOrders(cfg.state_dir, clock) # the grid operator's, from the EEBUS bridge (3q)
self.evphases = EvPhases(cfg.state_dir) # each charger's phase count and its source
self.guard.extra_limits = self.saved_limits
self.policy = Policy(cfg.state_dir, cfg.site_id, clock)
self.guard.policy = self.policy
@@ -180,6 +182,7 @@ class Controller:
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)}
write_cell(self.cfg.state_dir, float(lat), float(lon))
except Exception as e:
log.warning("get_config: %s", e)
ids = self.entities()
@@ -222,7 +225,9 @@ class Controller:
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_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())
async def make_plan(self) -> dict | None:
self.forecast = []
@@ -261,15 +266,20 @@ class Controller:
"prices": plan.get("price_note", ""), "model": plan.get("model", ""),
"sources": plan.get("sources", {}),
# for the building (shed_w/grid_w per step, 24 h): absent from older agents
"flex": plan.get("flex", {})}
"flex": plan.get("flex", {}),
# the market's contracts in the plan (shadow): planned kWh and saving vs the grid
"market": plan.get("market", [])}
def inputs_fresh(self) -> bool:
"""The plan's room temperatures and the battery's state are not older
than an hour (a sensor that stopped must not steer)."""
now = self.clock()
"""The plan's room temperatures and the battery's state have a value
(a sensor that stopped must not steer). HA marks a device that went
away unavailable; a value that holds steady is not stale (the clone,
2026-10-30: a room at 23.5 °C for hours and a full battery held every
control at its safe default), and HA sends no update for an
unchanged value, so its age says nothing."""
for c in self.controls:
for role in ("temp", "soc"):
if role in c.entities and self.col.age(c.entities[role], now) > 3600:
if role in c.entities and self.col.value(c.entities[role]) is None:
return False
return True
@@ -378,8 +388,10 @@ class Controller:
async def _actor(self, stop):
while not stop.is_set():
now = self.clock()
self.history.add("load_w", self.col.total(self.inputs.load_power), now)
self.history.add("load_w", inputs_mod.base_load(self.col, self.inputs), now)
self.history.add("pv_w", self.col.total(self.inputs.pv_power), now)
if self.sample_ev_phases(now) and self.gridorders.active():
self._merge_limits() # a learned count: the order's current bound again
if int(now) % 900 < 60:
try:
self.history.save(now)
@@ -387,6 +399,10 @@ class Controller:
log.warning("history: %s", e)
self.executor.inputs_ok = self.inputs_fresh()
self.grid_tick()
try:
await self.grid_report()
except Exception as e:
log.warning("grid order report: %s", e)
if self.switch_sides():
self.wake.set() # heating <-> cooling: a plan for the new side
try:
@@ -506,12 +522,34 @@ class Controller:
os.replace(p + ".tmp", p)
# -- the chargers' phases (evphases.py) ------------------------------------
def ev_phases(self) -> dict:
"""{control id: {"phases", "source", "plan", "order"}} for the EV controls."""
return self.evphases.all(self.controls, self.col)
def sample_ev_phases(self, now: float) -> bool:
"""A reading per charger for learning its phases: its power (its own
`power` sensor, else the site's ev.power where it is the only
charger) and current (measured, else its setpoint). True when a
learned count changed."""
evs = [c for c in self.controls if c.kind == "ev"]
site_ev = self.mapping.by_asset.get("ev.power") if len(evs) == 1 and hasattr(self.mapping, "by_asset") else None
changed = False
for c in evs:
pw = c.entities.get("power") or (site_ev.entity if site_ev else "")
cur = c.entities.get("current_measured") or c.entities.get("current") or ""
p = inputs_mod.watts(self.col, pw) if pw else None
a = self.col.value(cur) if cur else None
changed |= self.evphases.sample(c.id, p, a, float(c.p("voltage")), now)
return changed
# -- the grid operator's limit (EEBUS LPC, gridorder.py) -----------------
def _merge_limits(self):
"""The guard's limits: the console's, and over them the grid order's
at level grid (the lower limit of the two holds)."""
merged = {c: {q: dict(lv) for q, lv in qs.items()} for c, qs in self.saved_limits.items()}
for cid, qs in self.gridorders.limits(self.controls).items():
phases = {cid: v["phases"] for cid, v in self.ev_phases().items()}
for cid, qs in self.gridorders.limits(self.controls, phases).items():
for q, (f, lim) in qs.items():
per = merged.setdefault(cid, {}).setdefault(q, {})
old = per.get("grid")
@@ -538,10 +576,18 @@ class Controller:
args.update(value="", state="normal")
log.info("grid operator's limit ended")
await self._received("grid", args, "grid", True, "limited" if o else "ended")
await self.grid_report()
self.wake.set()
self.act_now.set()
return True
async def grid_report(self):
"""The order to the cloud as an action (gridorder.py action()): each
quarter-hour it holds, and its end."""
a = self.gridorders.action()
if a is not None:
await self._publish("action", a)
def grid_tick(self):
"""An order that went stale or ended takes its limits away."""
if self.gridorders.order and not self.gridorders.active() and any(
@@ -562,6 +608,29 @@ def sender(cmd: str, args: dict) -> str:
return "cloud"
def write_cell(state_dir: str, lat: float, lon: float) -> bool:
"""The site's weather cell (0.1°, ~10 km) for the link: site_cell.json
in the state directory; the heartbeat carries it to the cloud only with
the owner's share consent (edge/link). Written when it changed."""
cell = {"lat": round(lat, 1), "lon": round(lon, 1)}
path = os.path.join(state_dir, "site_cell.json")
try:
with open(path) as f:
if json.load(f) == cell:
return False
except (OSError, ValueError):
pass
try:
tmp = path + ".tmp"
with open(tmp, "w") as f:
json.dump(cell, f)
os.replace(tmp, path)
except OSError as e:
log.warning("site cell: %s", e)
return False
return True
SECRET_ARGS = ("proof", "sig", "bundle", "token", "password")
+31 -6
View File
@@ -67,6 +67,7 @@ from dataclasses import dataclass, field
from . import limits as limits_mod
from . import switchover as switchover_mod
from .evphases import ASSUMED
log = logging.getLogger(__name__)
@@ -78,7 +79,9 @@ ENTITIES = {
"cooling": {"climate": ("climate",), "temp": ("sensor",)},
"ev": {"switch": ("switch", "input_boolean"), "current": ("number", "input_number"),
"connected": ("binary_sensor", "input_boolean"), "soc": ("sensor", "input_number"),
"energy_needed": ("sensor",), "departure": ("sensor", "input_datetime")},
"energy_needed": ("sensor",), "departure": ("sensor", "input_datetime"),
# read only, for its phase count (evphases.py): reported, or learned from power and current
"phases": ("sensor",), "power": ("sensor",), "current_measured": ("sensor",)},
"cover": {"cover": ("cover",)},
}
WRITES = {"charge_from_grid", "setpoint", "mode", "climate", "switch", "current", "cover"}
@@ -91,15 +94,31 @@ PARAMS = {
"manual_option": "manual", "auto_option": "self-consumption", "min_switch_s": 0.0},
"heating": {"r_k_per_kw": 10.0, "c_kwh_per_k": 3.0, "max_power_w": 2000.0, "comfort_c": 21.0,
"setback_c": 17.0, "cop": 1.0, # cop 1: electric; a heat pump ~3
"min_switch_s": 600.0},
"min_switch_s": 600.0,
# the room's other heat (W; the model's fit, inputs._room_rc): internal
# (a number, or 24 by local hour) and the sun's per W/m² of irradiance
"internal_gain_w": 0.0, "solar_m2": 0.0},
"cooling": {"r_k_per_kw": 10.0, "c_kwh_per_k": 3.0, "max_power_w": 1500.0, "comfort_c": 25.0,
"setup_c": 28.0, "cop": 3.0, "min_switch_s": 600.0},
"setup_c": 28.0, "cop": 3.0, "min_switch_s": 600.0, "internal_gain_w": 0.0, "solar_m2": 0.0},
"ev": {"need_wh": 10000.0, "departure": "07:00", "min_current_a": 6.0, "max_current_a": 16.0,
"phases": 1.0, "voltage": 230.0, "min_switch_s": 0.0},
"phases": None, "voltage": 230.0, "min_switch_s": 0.0}, # phases: None, not said (evphases.py)
"cover": {"room": "", "gain_w": 800.0, "min_switch_s": 0.0, "area": "", "facing": -1.0,
"open_pct": 100.0, "shade_pct": 30.0, "closed_pct": 0.0, "quiet_from": "22:00", "quiet_to": "07:00",
"night_moves": 0.0, "min_move_s": 1800.0, "override_hold_s": 14400.0},
}
def ev_volts(params: dict, purpose: str) -> float:
"""A charger's volts x phases (W per A) for a purpose: "plan" (the
plan's power per A) or "order" (the grid order's current bound,
gridorder.limits). params["phases"]: the count from the best evidence
(evphases.py: mapping, device, learned); without it each purpose takes
its own safe side (evphases.ASSUMED: the order 3, the plan 1)."""
v = float(params.get("voltage") or PARAMS["ev"]["voltage"])
return v * float(params.get("phases") or ASSUMED[purpose])
HHMM_RE = re.compile(r"^([01][0-9]|2[0-3]):[0-5][0-9]$")
# each kind's device range per quantity (firmware's; limits.effective)
RANGE = {
@@ -254,16 +273,22 @@ class Inputs:
load_power: str | list = "" # sensor W: the house's load (history fallback)
pv_power: str | list = "" # sensor W: PV (history fallback); a list: their sum
humidity: list[str] = field(default_factory=list)
# sensors W of the planned devices' own draw inside load_power (heat pump,
# A/C, charger): taken off it, so the plan's base load is the house
# without the devices it plans itself (inputs.base_load). None: from the
# mapping's assets (mapping.controlled_power); an empty list: load_power
# has none of them in it.
controlled_power: list[str] | None = None
def entities(self) -> list[str]:
one = [self.outside_temp, self.weather, self.sun]
summed = [e for x in (self.load_power, self.pv_power) for e in ([x] if isinstance(x, str) else x)]
return [e for e in self.presence + self.humidity + one + summed if e]
return [e for e in self.presence + self.humidity + one + summed + list(self.controlled_power or []) if e]
def parse_inputs(raw: dict | None) -> Inputs:
raw = dict(raw or {})
for k in ("presence", "humidity"):
for k in ("presence", "humidity", "controlled_power"):
if isinstance(raw.get(k), str):
raw[k] = [raw[k]]
unknown = set(raw) - set(Inputs.__dataclass_fields__)
+169 -20
View File
@@ -32,7 +32,10 @@
and dearest hour of the next 24; the coming actions by
the controls' names or "nothing planned", the prices of 48 h
as a table, what Cantabile sets in words: only in auto, the
commands received: only when there are any); the building
commands received: only when there are any); the market
(shadow; only when the site has contracts: positions, the
price, a card per contract in plain words, the border
actuators' contract target against their state); the building
the home is in or asked to join
energy power and price over time, energy per day and month (if mapped)
climate rooms, thermostats, heat pump (if mapped)
@@ -81,8 +84,9 @@ class Home:
def __init__(self, mapping, uid: str, ours: dict[str, str], flats: list[dict], lang: str,
mode: str = "shadow", reg: dict | None = None, states: list[dict] | None = None,
links: dict[str, str] | None = None, joining: dict | None = None,
profiles: list[dict] | None = None):
profiles: list[dict] | None = None, market: dict | None = None):
self.m, self.uid, self.ours, self.flats, self.lang = mapping, uid, ours, flats, lang
self.market = market # the shadow market's bundle (bundles.market_now), or None
self.mode, self.links, self.joining, self.profiles = mode, links or {}, joining, profiles or []
self._views: dict[str, devview.DeviceView] = {}
self.assets = {a.asset: a for a in mapping.assets}
@@ -146,6 +150,17 @@ class Home:
return [a.entity for a in self.m.assets
if a.kind == "counter" and a.asset.startswith(prefix) and self.alive(a.entity)]
def thermal(self) -> set[str]:
"""The entities of heat (heat.power, heat.energy and their parts):
thermal, never shown or summed as electricity."""
return {a.entity for a in self.m.assets
if a.asset.split(".")[0] == "heat" and a.asset.rsplit(".", 1)[-1] in ("power", "energy")}
def heat_label(self) -> str:
"""Heat delivered by a heat pump of this site, else heat from the
building (a heat meter: district heat, the building's heat pump)."""
return self.n("heat_delivered" if self.each("heatpump.power") else "heat_building")
def our(self, suffix: str) -> str | None:
return self.ours.get(f"{self.uid}_{suffix}")
@@ -300,6 +315,48 @@ def energy_now(h: "Home") -> list[dict]:
return now
def _kilo(e: str, unit: str, dec: str) -> str:
"""A jinja expression of a heat sensor in kW or kWh (from W/Wh too)."""
return ("{% set u = state_attr('" + e + "', 'unit_of_measurement') %}{% set v = states('" + e + "') | float(none) %}"
"{% if v is none %}–{% else %}{{ ('%.1f' | format(v / 1000 if u in ['W', 'Wh'] else v))"
" | replace('.', '" + dec + "') }} " + unit + "{% endif %}")
def _thermal(h: "Home", e: str, label: str, cols=6, value: bool = False) -> dict:
"""A heat value (thermal) as its label and kW or kWh: never a tile that
could pass for electricity. value: one of a device's values."""
a = next((a for a in h.m.assets if a.entity == e), None)
unit = "kWh" if a is not None and a.kind == "counter" else "kW"
dec = "," if h.lang.split("-")[0] in DECIMAL_COMMA else "."
c = {"type": "markdown", "text_only": True, "grid_options": {"columns": cols}, "entity_id": e,
"content": f"<ha-icon icon=\"mdi:radiator\"></ha-icon> {label} \n**{_kilo(e, unit, dec)}**"}
return dict(c, _value=True) if value else c
def heat_band(h: "Home") -> list[dict]:
"""Heat now and today, thermal, in a band of its own below the
electricity: from the building (district heat, a heat meter) or
delivered by the site's heat pump, then with its electric power and the
COP (heat ÷ electricity). Never summed with electric power."""
heat, hp = h.each("heat.power"), h.each("heatpump.power")
if not heat and not h.each("heat.energy"):
return []
label = h.heat_label()
out = [_thermal(h, e, f"{label} {n}".strip()) for n, e in heat]
out += [_today(e, f"{h.n('heat_today')} {n}".strip(), "mdi:radiator") for n, e in h.each("heat.energy")]
if hp:
out += [_tile(e, f"{h.n('heat_pump_electric')} {n}".strip(), "mdi:heat-pump", "deep-purple")
for n, e in hp]
if len(heat) == 1 and len(hp) == 1:
q, w = heat[0][1], hp[0][1]
dec = "," if h.lang.split("-")[0] in DECIMAL_COMMA else "."
out.append(_md("{% set q = states('" + q + "') | float(none) %}{% set w = states('" + w + "') | float(none) %}"
+ h.n("cop") + " \n**{% if q is not none and w %}{{ ('%.1f' | format(q / w)) | replace('.', '"
+ dec + "') }}{% else %}–{% endif %}**", 6, text_only=True))
out.append(_md("<small>ⓘ " + h.n("heat_not_electric") + "</small>", text_only=True))
return out
def _label(h: "Home", c) -> str:
"""A control as people call it: the mapping's name, else the name HA
shows for its device's main entity, else its id in words."""
@@ -408,6 +465,46 @@ def plan_cards(h: "Home") -> list[dict]:
return out[:1 if price else 0] + _fill(out[1 if price else 0:])
def market_band(h: "Home") -> dict | None:
"""The shadow market, only when the site has contracts (a `market`
bundle): the net position(s) and the shared energy's price (quarters),
per contract a card in plain words (halves: "Sells to Home B: 1.2 kWh
last quarter-hour (contract 1.5) · 16.4 ct/kWh · shadow", the period
below), the border actuators' contract target against their state now,
and what shadow means. HA's core cards only."""
m = h.market
if not m or not m.get("contracts"):
return None
top = []
for p in m.get("positions") or []:
kind = str(p.get("node", "")).split(":")[0]
top.append(_tile(h.our("market_position" if kind == "site" else f"market_position_{_slug(kind)}"),
icon="mdi:scale-balance", cols=6))
top.append(_tile(h.our("market_price"), h.n("market_price"), "mdi:cash-multiple", cols=6))
stale = "<small>" + h.n("mk_stale") + "</small>"
deals = []
for c in m.get("contracts") or []:
e = h.our(f"market_{_slug(c['id'])}")
if not e:
continue
deals.append(_md(
"{% set e = '" + e + "' %}{% if states(e) == 'unavailable' or not state_attr(e, 'summary') %}" + stale
+ "{% else %}**{{ state_attr(e, 'summary') }}**{% set d = state_attr(e, 'details') %}"
"{% if d %} \n<small>{{ d }}</small>{% endif %} \n<small>{{ state_attr(e, 'period_words') }}</small>{% endif %}",
12))
rows = []
for a in m.get("actuators") or []:
e = h.our(f"market_actuator_{_slug(a['device'])}")
if e:
rows.append("**" + (a.get("name") or a["device"]).replace("*", "") + "**: {% set e = '" + e + "' %}"
"{% if states(e) == 'unavailable' %}" + stale + "{% else %}{{ state_attr(e, 'words') }}{% endif %}")
acts = _md(" \n".join(rows)) if rows else None
if not deals and not acts:
return None
note = _md("<small>ⓘ " + h.n("mk_shadow_note") + "</small>")
return _band(*_fill([c for c in top if c]), *_fill(deals), acts, note, heading=h.n("card_market"), icon="mdi:handshake-outline")
# -- the views ----------------------------------------------------------------------
def view_overview(h: Home) -> dict | None:
@@ -444,10 +541,11 @@ def view_overview(h: Home) -> dict | None:
return _view(h.n("dash_main"), "overview", "mdi:leaf",
_band(*status), _band(*now),
_band(*_fill(heat_band(h)), heading=h.n("heat"), icon="mdi:radiator"),
devices_heading(h, bool(devices)), *devices,
_band(*today, heading=h.n("today"), icon="mdi:calendar-today"),
_band(*plan_cards(h), heading=h.n("card_plan"), icon="mdi:chart-timeline-variant"),
building_section(h), columns=WIDE)
market_band(h), building_section(h), columns=WIDE)
def view_energy(h: Home) -> dict | None:
@@ -475,9 +573,13 @@ def view_climate(h: Home) -> dict | None:
for e in dict.fromkeys(c.entities["climate"] for c in rooms if "climate" in c.entities)],
_history(h.n("card_rooms"), list(dict.fromkeys(e for e in temps if e))
+ [h.our(f"set_{c.id}") for c in rooms] + [h.asset("outside.temp")])),
# electric and thermal apart, never in one graph
_section(_heading(h.n("heat_pump"), "mdi:heat-pump"),
_history(h.n("card_power"), h.all("heatpump.power", "heat.power")),
_stats(h.n("card_daily"), h.all("heatpump.energy", "heat.energy"))))
_history(h.n("electric_power"), h.all("heatpump.power")),
_stats(h.n("card_daily"), h.all("heatpump.energy"))),
_section(_heading(h.n("heat"), "mdi:radiator"),
_history(h.heat_label(), h.all("heat.power")),
_stats(h.n("heat_today"), h.all("heat.energy"))))
def _short(name: str, device: str) -> str:
@@ -560,9 +662,15 @@ def device_cards(h: Home, d: dict, detail: bool = False) -> list[dict]:
cards, used = [], set()
thermal = h.thermal()
def tile(eid, trend=None, cols=6):
if eid and eid not in used:
used.add(eid)
if eid in thermal: # heat: thermal, never like electricity
label = h.heat_label() if eid in h.all("heat.power") else h.n("heat_today")
cards.append(_thermal(h, eid, label, cols))
return
cards.append(_tile(eid, nm(eid), trend=power(eid) if trend is None else trend, cols=cols))
soc = dv.flow.get("soc") or next((e for e in dv.headline if st(e).get("device_class") == "battery"), None)
if dv.card in ("flow", "gauge") and soc:
@@ -592,7 +700,7 @@ def device_cards(h: Home, d: dict, detail: bool = False) -> list[dict]:
for e in dv.headline:
tile(e, None if dv.card in ("flow", "meter", "charger") or detail else False)
if dv.channels:
power_ch = [c for c in dv.channels if c.get("power")]
power_ch = [c for c in dv.channels if c.get("power") and c["power"] not in thermal] # never heat
if power_ch:
cards.append(_sum(h.n("total"), [c["power"] for c in power_ch]))
cards.append({"type": "distribution", "grid_options": {"columns": "full"},
@@ -735,10 +843,13 @@ def device_card(h: Home, d: dict, skip: set[str] = frozenset()) -> dict | None:
picked.append(e)
picked = picked[:HEADLINE]
used |= set(picked)
cards += [{"type": "tile", "entity": e, "name": nm(e), "_value": True,
thermal = h.thermal()
cards += [_thermal(h, e, h.heat_label() if e in h.all("heat.power") else h.n("heat_today"), value=True)
if e in thermal else
{"type": "tile", "entity": e, "name": nm(e), "_value": True,
**({"icon_tap_action": {"action": "more-info"}}
if e.split(".")[0] in ("switch", "light", "fan", "input_boolean") else {})} for e in picked]
power_ch = [c for c in dv.channels if c.get("power") and c["power"] not in skip]
power_ch = [c for c in dv.channels if c.get("power") and c["power"] not in skip and c["power"] not in thermal]
if power_ch:
cards.append({"type": "markdown", "text_only": True, "content":
"{% set e = " + json.dumps([c["power"] for c in power_ch]) + " %}" + h.n("total")
@@ -798,18 +909,22 @@ def device_subview(h: Home, d: dict) -> dict | None:
value it has (not diagnostic), and its power over the last day."""
dv = h.view(d)
cards = device_cards(h, d, detail=True)
shown = {x if isinstance(x, str) else x.get("entity") for c in cards for x in [c, *(c.get("entities") or [])]}
shown = {x if isinstance(x, str) else x.get("entity") or x.get("entity_id")
for c in cards for x in [c, *(c.get("entities") or [])]}
shown |= set(dv.switches)
thermal = h.thermal()
rest = [e["entity_id"] for e in h.ents_of(d) if not e.get("entity_category") and e["entity_id"] not in shown
and e["entity_id"].split(".")[0] in VALUES and h.alive(e["entity_id"])]
powers = [e for e in dv.entities() | set(rest) if (((h.states or {}).get(e) or {}).get("attributes") or {})
.get("device_class") == "power"]
.get("device_class") == "power" and e not in thermal]
heat = sorted(e for e in dv.entities() | set(rest) | shown if e in thermal and e in h.all("heat.power"))
return {"type": "sections", "title": _name(d), "path": _subpath(d), "subview": True,
"back_path": f"/{URL}/overview", "max_columns": 3, "sections": [x for x in (
_section(_heading(_name(d), "mdi:information-outline"), *cards),
_section(_heading(h.n("values"), "mdi:format-list-bulleted"),
{"type": "entities", "entities": rest, "grid_options": {"columns": "full"}} if rest else None),
_section(_heading(h.n("card_power"), "mdi:flash"), _history(h.n("card_power"), sorted(powers))))
_section(_heading(h.n("card_power"), "mdi:flash"), _history(h.n("card_power"), sorted(powers))),
_section(_heading(h.n("heat"), "mdi:radiator"), _history(h.heat_label(), heat)))
if x]}
@@ -858,15 +973,19 @@ def dashboard(h: Home) -> dict | None:
def energy_prefs(h: Home) -> dict | None:
"""HA's Energy preferences from the mapping, or None (nothing to show).
Parts (two inverters, two batteries) are each a source of their own."""
Parts (two inverters, two batteries) are each a source of their own.
The grid in HA's current form (energy storage 1.3, in HA 2026.10): a
source per grid connection with its import and export meter side by
side (parts paired in order); legacy_grid() gives the older form."""
src = []
imp, exp = h.all("grid.import_energy"), h.all("grid.export_energy")
if imp or exp:
for i in range(max(len(imp), len(exp))):
e_in, e_out = (imp[i] if i < len(imp) else None), (exp[i] if i < len(exp) else None)
src.append({"type": "grid", "cost_adjustment_day": 0,
"flow_from": [{"stat_energy_from": e, "stat_cost": None, "number_energy_price": None,
"entity_energy_price": h.our("price_import_now")} for e in imp],
"flow_to": [{"stat_energy_to": e, "stat_compensation": None, "number_energy_price": None,
"entity_energy_price": h.our("price_export_now")} for e in exp]})
"stat_energy_from": e_in, "stat_cost": None, "number_energy_price": None,
"entity_energy_price": h.our("price_import_now") if e_in else None,
"stat_energy_to": e_out, "stat_compensation": None, "number_energy_price_export": None,
"entity_energy_price_export": h.our("price_export_now") if e_out else None})
for e in h.all("pv.energy"):
src.append({"type": "solar", "stat_energy_from": e, "config_entry_solar_forecast": None})
ins, outs = dict(h.each("battery.charge_energy")), dict(h.each("battery.discharge_energy"))
@@ -883,6 +1002,23 @@ def energy_prefs(h: Home) -> dict | None:
return {"energy_sources": src, "device_consumption": devices}
def legacy_grid(prefs: dict) -> dict:
"""The preferences in HA's older grid form (energy storage before 1.3):
one grid source, its import and export meters in flow_from / flow_to.
The same dict when there is no grid."""
grids = [s for s in prefs["energy_sources"] if s["type"] == "grid"]
if not grids:
return prefs
one = {"type": "grid", "cost_adjustment_day": 0,
"flow_from": [{"stat_energy_from": g["stat_energy_from"], "stat_cost": None, "number_energy_price": None,
"entity_energy_price": g["entity_energy_price"]}
for g in grids if g["stat_energy_from"]],
"flow_to": [{"stat_energy_to": g["stat_energy_to"], "stat_compensation": None, "number_energy_price": None,
"entity_energy_price": g["entity_energy_price_export"]}
for g in grids if g["stat_energy_to"]]}
return dict(prefs, energy_sources=[one] + [s for s in prefs["energy_sources"] if s["type"] != "grid"])
class Dashboards:
def __init__(self, agent):
self.agent = agent
@@ -918,9 +1054,10 @@ class Dashboards:
links: dict[str, str] | None = None) -> Home:
ctl = getattr(self.agent, "control", None)
flats = ctl.bundles.flat_list() if ctl is not None else []
market = ctl.bundles.market_now(time.time()) if ctl is not None else None
return Home(self.agent.mapping, f"cantabile_{self.agent.cfg.site_id}", ours, flats,
getattr(self.agent, "ha_language", "en"), self.mode(), reg, states, links,
self.joining() if self.agent.cfg.join_page_port else None, self.profiles)
self.joining() if self.agent.cfg.join_page_port else None, self.profiles, market)
@staticmethod
async def links(client, devices: list[dict]) -> dict[str, str]:
@@ -985,9 +1122,21 @@ class Dashboards:
if prefs and (prefs.get("energy_sources") or prefs.get("device_consumption")):
return # the owner's: never overwritten
want = energy_prefs(h)
if want is not None:
if want is None:
return
try:
await client.dashboard("energy/save_prefs", **want)
log.info("HA's Energy dashboard set up from the mapping")
except HAError as e:
# an HA from before the unified grid (energy storage 1.3) takes
# only flow_from / flow_to: the same preferences in that form
old = legacy_grid(want)
if old is want or "invalid_format" not in str(e):
raise
await client.dashboard("energy/save_prefs", **old)
log.info("HA's Energy dashboard set up from the mapping (HA %s: the older grid form)",
getattr(client, "version", None))
return
log.info("HA's Energy dashboard set up from the mapping")
async def run(self, stop: asyncio.Event):
while not stop.is_set():
+329 -124
View File
@@ -66,16 +66,67 @@ SWITCHOVER_WORDS = {"heating": M("Heating"), "cooling": M("Cooling"),
"outdoor": M("By outdoor temperature")}
CUSTODIAN_WORDS = {"building_operator": M("the building's operator"), "grid_operator": M("the grid operator"),
"metering_operator": M("the metering operator"), "community_operator": M("the energy community's operator")}
PROFILE_STATUS = {"verified": M("Checked on a device"),
"documented": M("From the maker's documentation, not yet checked on a device"),
"draft": M("Draft: check the values before you turn reading on")}
PROFILE_STATUS = {"verified": (M("checked"), "ok", M("Checked on a device: its values are known to be right.")),
"documented": (M("untested"), "quiet", M("Made from the maker's documentation, not yet checked on a "
"device. Look at its values before you turn reading on.")),
"draft": (M("draft"), "warn", M("A draft profile: check the values before you turn reading on."))}
CONTROL_WORDS = {"battery": M("battery"), "heating": M("heating"), "cooling": M("cooling"), "ev": M("charging"),
"cover": M("blinds and curtains")}
# What a device reads, by meaning (the devices tool gives the keys:
# devprofiles.reads); never the asset ids.
QUANTITY_WORDS = {
"room.temp": M("room temperature"), "room.humidity": M("humidity"), "room.setpoint": M("setpoint"),
"room.setpoint_range": M("heat/cool range"), "heating_cooling.state": M("heating/cooling state"),
"heating.state": M("heating state"), "cooling.state": M("cooling state"),
"room.setpoint_low": M("heat/cool range"), "room.setpoint_high": M("heat/cool range"),
"room.shade_position": M("position"), "room.shade_tilt": M("tilt"),
"pv.power": M("solar power"), "pv.energy": M("solar energy"), "grid.power": M("grid power"),
"grid.import_power": M("power from the grid"), "grid.export_power": M("power to the grid"),
"grid.import_energy": M("energy from the grid"), "grid.export_energy": M("energy to the grid"),
"battery.power": M("battery power"), "battery.soc": M("battery charge"),
"battery.charge_energy": M("energy charged"), "battery.discharge_energy": M("energy discharged"),
"load.power": M("home consumption"), "load.energy": M("home energy"),
"heatpump.power": M("heat pump power"), "heatpump.energy": M("heat pump energy"),
"heat.power": M("heat delivered (thermal)"), "heat.energy": M("heat energy (thermal)"), "heatpump.flow_temp": M("flow temperature"),
"heatpump.dhw_temp": M("hot water temperature"), "outside.temp": M("outdoor temperature"),
"outside.humidity": M("outdoor humidity"), "gas.energy": M("gas"), "ev.power": M("charging power"),
"ev.energy": M("energy charged"), "circuit.power": M("power"), "circuit.energy": M("energy")}
# A device's main values now (devprofiles.live) on the list.
LIVE_WORDS = {"room.setpoint": M("set to {v}"), "room.setpoint_range": M("set to {low}–{high}"),
"pv.power": M("solar {v}"), "battery.soc": M("battery {v}"), "battery.power": M("battery {v}"),
"grid.power": M("grid {v}"), "load.power": M("home {v}"), "ev.power": M("charging {v}"),
"heatpump.power": M("heat pump {v}"), "heat.power": M("heat {v} (thermal)"), "heatpump.flow_temp": M("flow {v}"),
"outside.temp": M("outside {v}"), "room.humidity": M("humidity {v}"), "room.shade_position": M("{v} open")}
HVAC_WORDS = {"heating": M("heating"), "cooling": M("cooling"), "idle": M("idle")}
LIVE_UNITS = {"room.temp": "°C", "room.setpoint": "°C", "room.setpoint_range": "°C", "heatpump.flow_temp": "°C",
"outside.temp": "°C", "battery.soc": "%", "room.humidity": "%", "room.shade_position": "%"}
MORE_CSS = """nav{display:flex;gap:1rem;margin:0 0 1rem;border-bottom:1px solid #d5ded9;padding-bottom:.5rem}
nav a{color:inherit;text-decoration:none}nav a.on{font-weight:600;border-bottom:2px solid #2f6f55}
.badge{display:inline-block;font-size:.8rem;padding:.05rem .45rem;border-radius:4px;background:#e2e9e5;margin-left:.3rem}
ul.plain{margin:.3rem 0;padding-left:1.2rem}form.inline{display:inline}form.inline button{margin:0 0 0 .4rem;padding:.2rem .6rem}
@media (prefers-color-scheme:dark){nav{border-color:#2b3a32}.badge{background:#2b3a32}}"""
a{color:#2f6f55}.muted,.model{color:#5a6b62;font-size:.85rem}
.badge{display:inline-block;font-size:.75rem;padding:0 .45rem;border-radius:1rem;background:#e2e9e5;margin-left:.3rem;vertical-align:.1em}
.badge.quiet{background:none;box-shadow:inset 0 0 0 1px #d5ded9;color:#5a6b62}.badge.warn{background:#fbe9c9;color:#7a4b00}
ul.plain{list-style:none;margin:.3rem 0;padding:0}input[type=radio]{width:auto}select{font:inherit;padding:.45rem .6rem;border-radius:6px;width:100%}
a.box.dev{display:flex;gap:.8rem;align-items:center;color:inherit;text-decoration:none}a.box.dev:hover{border-color:#2f6f55}
.grow{flex:1;min-width:0}.grow>span{display:block}.grow>span.name{display:inline;font-weight:600}.grow>span.badge{display:inline-block}
.chev{color:#5a6b62;font-size:1.4rem}.state{font-size:.9rem}.values{font-size:1.05rem;font-weight:600}.box.now>span{display:block}
.dot{display:inline-block;width:.55rem;height:.55rem;border-radius:50%;margin-right:.45rem;background:#9fb0a6}.dot.on{background:#2f9e6e}
.offer .top{display:flex;gap:.8rem;align-items:center}.offer .top form{margin:0}.offer .top button{margin:0;white-space:nowrap}.offer p.note{margin:.4rem 0 0;font-size:.9rem}
.add{margin-top:2rem;border-top:2px solid #d5ded9}a.back{text-decoration:none;font-size:.9rem}.model.head{margin:-.6rem 0 .6rem}
section.set{border-top:1px solid #d5ded9;margin-top:1rem}section.set h3{margin:.9rem 0 .4rem}section.set p{margin:.35rem 0}
.switch{display:flex;align-items:center;justify-content:space-between;gap:.8rem;padding:.3rem 0}.switch form{margin:0}.switch button{margin:0;white-space:nowrap}
.pill{display:inline-block;font-size:.75rem;padding:0 .5rem;border-radius:1rem;background:#e2e9e5;color:#5a6b62;font-weight:600}.pill.on{background:#e3f4ea;color:#1f5d3a}
.row{display:flex;gap:.6rem}.row>div{flex:1}.right{text-align:right}
details.remove summary{color:#b3261e;cursor:pointer;padding:.8rem 0 .2rem}button.danger{background:none;color:#b3261e;box-shadow:inset 0 0 0 2px #b3261e}
details.more summary{cursor:pointer;margin:.6rem 0}
details.info{display:inline;position:relative}details.info>summary{display:inline-flex;align-items:center;justify-content:center;width:1.2rem;height:1.2rem;margin:0 .35rem;vertical-align:.1em;list-style:none;border:1px solid #5a6b62;border-radius:50%;color:#5a6b62;font:italic 600 .72rem/1 Georgia,serif;cursor:pointer}
details.info>summary::-webkit-details-marker{display:none}details.info>.info-body{position:absolute;left:0;z-index:3;width:min(22rem,80vw);margin-top:.4rem;padding:.7rem .9rem;background:#fff;color:#1d2a24;border:1px solid #d5ded9;border-radius:8px;box-shadow:0 6px 24px rgba(0,0,0,.18);font-size:.9rem;font-weight:normal}
@supports selector(:has(a)){form.changes .save{display:none}form.changes:has(input:default:not(:checked),input:checked:not(:default),option:checked:not(:default),input[value=outdoor]:checked) .save{display:inline-block}
form.changes:not(:has(input[value=outdoor]:checked)) .outdoor{display:none}}
@media (prefers-color-scheme:dark){nav{border-color:#2b3a32}a{color:#5cc493}.badge,.pill{background:#2b3a32}.muted,.model,.chev{color:#9fb0a6}
.badge.quiet{background:none;box-shadow:inset 0 0 0 1px #2b3a32;color:#9fb0a6}.badge.warn{background:#3a2c12;color:#f3c77e}.pill.on{background:#19302a;color:#a6e0bf}
.dot.on{background:#5cc493}.add,section.set{border-color:#2b3a32}a.box.dev:hover{border-color:#5cc493}select{background:#18221d;color:#e3ebe6;border:1px solid #3a4b42}
details.remove summary{color:#f08a80}button.danger{background:none;color:#f08a80;box-shadow:inset 0 0 0 2px #f08a80}
details.info>.info-body{background:#18221d;color:#e3ebe6;border-color:#2b3a32}details.info>summary{border-color:#9fb0a6;color:#9fb0a6}}"""
# HA's hash (#devices, #building: Cantabile's dashboard) to this page's ?p=
HASH_SCRIPT = ("(function(){try{var P=window.parent;if(P===window)return;var m=/^#(devices|building)$/.exec(P.location.hash||'');"
"if(m&&location.search.indexOf('p=')<0&&location.search.indexOf('code=')<0)location.replace('?p='+m[1])}"
@@ -101,10 +152,6 @@ def _write_json(path: str, data, mode: int = 0o644):
os.replace(tmp, path)
def quantity_words(q: str) -> str:
return q.replace("_", " ").replace(".", ": ", 1)
def with_attrs(ents: list[dict], states: dict) -> list[dict]:
"""Registry entries with HA's attributes now (where the state is known:
the profiles' conditions on attributes, as the Hub's registry has them)."""
@@ -235,19 +282,25 @@ class Devices:
"""The page's data: the devices used here, HA's devices with a
profile (and what each would give), the rest, or HA's error."""
devs, policy = self.load(), self.policy()
try:
reg, states = await self.snapshot()
ha_error = ""
except Exception as e:
reg, states, ha_error = {}, {}, str(e)
used = []
for d in sorted(devs.values(), key=lambda d: d.get("added", 0)):
frag, p = self.frag(d["id"]), self.profile(d["profile"]) or {}
targets = dp.device_targets(d, frag, self.local())
assets = frag.get("assets", [])
used.append(dict(d, manufacturer=p.get("manufacturer", ""), model=p.get("name", d["profile"]),
status=p.get("status", ""), assets=frag.get("assets", []), controls=frag.get("controls", []),
status=p.get("status", ""), generic=bool(p) and dp.fallback(p), assets=assets,
controls=frag.get("controls", []), reads=dp.reads(assets), live=dp.live(assets, states),
assumed=dp.reads([a for a in assets if a.get("assumed")]), lacks=dp.lacks(d.get("missing")),
switchover=dp.switchover_of(frag), shade=dp.shade_of(frag), place=dp.place_of(frag),
weather=dp.weather_of(frag), policy={t: policy[t] for t in targets if t in policy},
protection=dp.level_of(policy, [f"device:{d['id']}"])[0]))
try:
reg, states = await self.snapshot()
except Exception as e:
return {"devices": used, "offers": [], "rest": [], "ha_error": str(e), "site": self.site(), "rooms": []}
if ha_error:
return {"devices": used, "offers": [], "rest": [], "ha_error": ha_error, "site": self.site(), "rooms": []}
main = self.main()
src = main.get("source")
mapped = {}
@@ -445,8 +498,9 @@ def custodian(e: dict | None) -> str:
# ---- the page -----------------------------------------------------------------------------
def form(do: str, inner: str, button: str, cls: str = "", inline: bool = False) -> str:
return (f'<form method="post" action=""{" class=" + chr(34) + "inline" + chr(34) if inline else ""}>'
def form(do: str, inner: str, button: str, cls: str = "", inline: bool = False, form_cls: str = "") -> str:
fc = " ".join(x for x in ("inline" if inline else "", form_cls) if x)
return (f'<form method="post" action=""{" class=" + chr(34) + fc + chr(34) if fc else ""}>'
f'<input type="hidden" name="do" value="{E(do)}">{inner}'
f'<button{" class=" + chr(34) + cls + chr(34) if cls else ""}>{E(button)}</button></form>')
@@ -455,55 +509,163 @@ def hidden(**kw) -> str:
return "".join(f'<input type="hidden" name="{E(k)}" value="{E(str(v))}">' for k, v in kw.items())
def what_lines(assets: list, controls: list, would: bool) -> str:
reads = ", ".join(quantity_words(a["asset"]) for a in assets) or T("nothing")
kinds = ", ".join(dict.fromkeys(T(CONTROL_WORDS.get(c["kind"], c["kind"])) for c in controls))
if would:
out = T("Cantabile would read: {values}.", values=reads)
if kinds:
out += " " + T("It could control: {controls}.", controls=kinds)
else:
out = T("Cantabile reads: {values}.", values=reads)
if kinds:
out += " " + T("It may control: {controls}.", controls=kinds)
return f'<p class="note">{E(out)}</p>'
def info(body: str) -> str:
"""An "i": its explanation (escaped HTML) opens below on a click or tap."""
name = E(T("More information"))
return (f'<details class="info"><summary title="{name}" aria-label="{name}">i</summary>'
f'<div class="info-body">{body}</div></details>')
def notes(d: dict) -> str:
def quantity_words(q: str) -> str:
"""A quantity without words of its own: room.living_room.x -> room: living room.x."""
return q.replace("_", " ").replace(".", ": ", 1)
def room_name(place: str, rooms) -> str:
"""An asset's place (an area's slug) as HA names it: living_room -> Living room."""
return next((r for r in rooms or [] if dp.slug(r) == place), place.replace("_", " ").capitalize())
def reads_words(keys, rooms=()) -> str:
"""[(key, place)] (devprofiles.reads) in words: room temperature
(Living room), setpoint, heating/cooling state; a room named once."""
named, out = set(), []
for k, place in keys or []:
w = T(QUANTITY_WORDS[k]) if k in QUANTITY_WORDS else quantity_words(k)
if place and place not in named:
named.add(place)
w = T("{what} ({room})", what=w, room=room_name(place, rooms))
out.append(w)
return ", ".join(dict.fromkeys(out))
def ids_words(ids, rooms=()) -> str:
return reads_words(dp.reads(list(ids)), rooms)
def control_words(controls) -> str:
return ", ".join(dict.fromkeys(T(CONTROL_WORDS.get(c["kind"], c["kind"])) for c in controls or []))
def _num(v, digits: int = 1) -> str:
v = round(float(v), digits)
return i18n.number(int(v) if v == int(v) else v)
def live_words(k: str, v) -> str:
"""One of a device's values now (devprofiles.live): 21.5 °C, set to 22 °C, heating."""
if k == dp.HVAC:
return T(HVAC_WORDS.get(v, HVAC_WORDS["idle"]))
if k in ("heating.state", "cooling.state"):
return T(HVAC_WORDS[k.split(".")[0] if v else "idle"])
if k == dp.RANGE:
return T(LIVE_WORDS[k], low=_num(v[0]), high=_num(v[1])) + " °C"
unit = LIVE_UNITS.get(k)
s = f"{_num(v)} {unit}" if unit else f"{_num(v / 1000)} kW" if abs(v) >= 1000 or k == "heat.power" else f"{_num(v, 0)} W"
return T(LIVE_WORDS[k], v=s) if k in LIVE_WORDS else s
def device_values(d: dict) -> str:
return ", ".join(live_words(k, v) for k, v in d.get("live") or [])
def device_state(d: dict) -> str:
"""In use · reading on · control off / Added · reading off, with a dot."""
on = bool(d.get("read"))
parts = [T("In use"), T("reading on")] if on else [T("Added"), T("reading off")]
if d.get("controls"):
parts.append(T("control on") if d.get("control") else T("control off"))
return f'<span class="dot{" on" if on else ""}" aria-hidden="true"></span>' + E(" · ".join(parts))
def model_line(d: dict) -> str:
if d.get("generic"):
return E(d.get("model", "").split(" (")[0]) + f' · <span class="muted">{E(T("generic profile"))}</span>'
return E(f"{d.get('manufacturer', '')} {d.get('model', '')}".strip())
def status_badge(status, explain: bool = True) -> str:
"""A profile's status as a small badge, its explanation behind an "i"."""
if status not in PROFILE_STATUS:
return ""
word, cls, why = PROFILE_STATUS[status]
return f'<span class="badge {cls}">{E(T(word))}</span>' + (info(E(T(why))) if explain else "")
def protection_badge(d: dict) -> str:
if d.get("protection") == "sealed":
return f'<span class="badge warn">{E(T("sealed"))}</span>'
if any((e or {}).get("level") == "protected" for e in (d.get("policy") or {}).values()):
return f'<span class="badge">{E(T("protected"))}</span>'
return ""
def section(title: str, body: str, extra: str = "") -> str:
return f'<section class="set"><h3>{E(title)}{extra}</h3>{body}</section>'
def save_button() -> str:
"""Save, shown only once something changed (by CSS)."""
return f'<div class="right"><button class="save">{E(T("Save"))}</button></div>'
def notes(d: dict, rooms=(), values: bool = True) -> str:
"""What a device lacks or leaves to others, in words (quietly); the
values it does not report only on its own page (values)."""
out = []
if d.get("missing"):
out.append(T("Not found in Home Assistant: {entities}.", entities=", ".join(d["missing"])))
gone = d.get("left_out") or {}
if gone.get("values"):
lacks = dp.lacks(d.get("missing"))
if lacks["values"] and values:
out.append(T("This device does not report: {values}.", values=reads_words([(k, "") for k in lacks["values"]])))
gone = [T("{control} control", control=T(CONTROL_WORDS.get(k, k))) for k in lacks["controls"]] + lacks["other"]
if gone:
out.append(T("Not found in Home Assistant: {entities}.", entities=", ".join(gone)))
left = d.get("left_out") or {}
if left.get("values"):
out.append(T("Read by your own mapping already, so left out here: {values}.",
values=", ".join(quantity_words(q) for q in gone["values"])))
if gone.get("controls"):
values=ids_words(left["values"], rooms)))
if left.get("controls"):
out.append(T("Another control writes to this device already, so Cantabile leaves out: {controls}.",
controls=", ".join(T(CONTROL_WORDS.get(k, k)) for k in gone["controls"])))
assumed = [a["asset"] for a in d.get("assets") or [] if a.get("assumed")]
controls=", ".join(T(CONTROL_WORDS.get(k, k)) for k in left["controls"])))
assumed = d.get("assumed") if "assumed" in d else dp.reads([a for a in d.get("assets") or [] if a.get("assumed")])
if assumed:
out.append(T("Assumed, not measured: {values}. This device does not report back (infrared): Cantabile shows "
"what was last sent to it and never takes it as feedback.",
values=", ".join(quantity_words(q) for q in assumed)))
"what was last sent to it and never takes it as feedback.", values=reads_words(assumed)))
return "".join(f'<p class="note">{E(x)}</p>' for x in out)
def consent_html(d: dict, site: dict) -> str:
"""Reading and control, the owner's per device, as on the Hub's Devices
page; only with the site's consent (the add-on's configuration)."""
kinds = ", ".join(dict.fromkeys(T(CONTROL_WORDS.get(c["kind"], c["kind"])) for c in d.get("controls") or []))
"""Reading and control, each a switch that takes effect at once, as on
the Hub's Devices page; only with the site's consent (the add-on's
configuration)."""
kinds = control_words(d.get("controls"))
if not site.get("share"):
return ('<p class="note">' + E(T("Reading and control are off. To switch them on, first give your consent in the "
"add-on's configuration (Settings, Add-ons, Cantabile Edge Agent, Configuration)."))
+ "</p>")
ctl = ('<label class="check"><input type="checkbox" name="control" value="1"' + (" checked" if d.get("control") else "")
+ "><span>" + E(T("Cantabile may control it ({controls}), within your limits", controls=kinds))
+ "</span></label>") if kinds else ""
note = ('<p class="note">' + E(T("Control also needs your consent to control devices in the add-on's configuration."))
+ "</p>") if kinds and not site.get("control") else ""
return form("consent", hidden(id=d["id"]) + '<label class="check"><input type="checkbox" name="read" value="1"'
+ (" checked" if d.get("read") else "") + "><span>" + E(T("Cantabile may read these values"))
+ f"</span></label>{ctl}", T("Save")) + note
return section(T("Reading and control"), '<p class="note">' + E(T(
"Reading and control are off. To switch them on, first give your consent in the add-on's configuration "
"(Settings, Add-ons, Cantabile Edge Agent, Configuration).")) + "</p>")
read, control = bool(d.get("read")), bool(d.get("control"))
def row(title, note, on, values):
pill = f'<span class="pill{" on" if on else ""}">{E(T("On") if on else T("Off"))}</span>'
inner = hidden(id=d["id"]) + "".join(f'<input type="hidden" name="{k}" value="{"1" if v else ""}">'
for k, v in values.items())
return (f'<div class="switch"><div><b>{E(title)}</b> {pill}<br><span class="note">{E(note)}</span></div>'
+ form("consent", inner, T("Turn off") if on else T("Turn on"), "secondary") + "</div>")
rows = row(T("Reading"), T("Cantabile may read these values."), read, {"read": not read, "control": control and not read})
if kinds:
rows += row(T("Control"), T("Cantabile may control it ({controls}), within your limits.", controls=kinds), control,
{"read": read or not control, "control": not control})
if not site.get("control"):
rows += '<p class="note">' + E(T("Control also needs your consent to control devices in the add-on's "
"configuration.")) + "</p>"
return section(T("Reading and control"), rows)
def reads_html(d: dict, rooms=()) -> str:
lines = [f'<p>{E(T("Reads: {values}", values=reads_words(d.get("reads"), rooms) or T("nothing")))}</p>']
kinds = control_words(d.get("controls"))
if kinds:
lines.append(f'<p>{E(T("Controls: {controls}", controls=kinds))}</p>')
return section(T("What Cantabile reads"), "".join(lines) + notes(d, rooms))
def switchover_html(d: dict) -> str:
@@ -514,14 +676,14 @@ def switchover_html(d: dict) -> str:
lo, hi = sw.get("heat_below_c", 15), sw.get("cool_above_c", 22)
radios = "".join(f'<label class="check"><input type="radio" name="mode" value="{k}"{" checked" if k == mode else ""}>'
f"<span>{E(T(w))}</span></label>" for k, w in SWITCHOVER_WORDS.items())
inner = (hidden(id=d["id"]) + f'<p class="note">{E(T("This thermostat can heat and cool. Cantabile uses one at a time; the other is never touched."))}</p>'
+ radios
+ f'<label for="lo-{E(d["id"])}">{E(T("By outdoor temperature: heat below (°C)"))}</label>'
f'<input id="lo-{E(d["id"])}" type="number" step="0.5" name="heat_below_c" value="{E(f"{lo:g}")}">'
f'<label for="hi-{E(d["id"])}">{E(T("cool above (°C)"))}</label>'
f'<input id="hi-{E(d["id"])}" type="number" step="0.5" name="cool_above_c" value="{E(f"{hi:g}")}">'
f'<p class="note">{E(T("Between the two it stays as it is, so it does not switch back and forth on a mild day."))}</p>')
return f"<details><summary>{E(T('Heating or cooling'))}</summary>{form('switchover', inner, T('Save'))}</details>"
inner = (hidden(id=d["id"]) + radios + '<div class="outdoor row">'
f'<div><label for="lo-{E(d["id"])}">{E(T("Heat below (°C)"))}</label>'
f'<input id="lo-{E(d["id"])}" type="number" step="0.5" name="heat_below_c" value="{E(f"{lo:g}")}"></div>'
f'<div><label for="hi-{E(d["id"])}">{E(T("Cool above (°C)"))}</label>'
f'<input id="hi-{E(d["id"])}" type="number" step="0.5" name="cool_above_c" value="{E(f"{hi:g}")}"></div></div>'
f'<p class="note outdoor">{E(T("Between the two it stays as it is, so it does not switch back and forth on a mild day."))}</p>')
about = info(E(T("This thermostat can heat and cool. Cantabile uses one at a time; the other is never touched.")))
return section(T("Heating or cooling"), form("switchover", inner, T("Save"), "save", form_cls="changes"), about)
FACING_WORDS = {"": M("Not known"), "N": M("North"), "NE": M("North-east"), "E": M("East"), "SE": M("South-east"),
@@ -532,7 +694,7 @@ def _room_options(rooms, current: str) -> str:
names = list(rooms)
if current and current not in {dp.slug(r) for r in names}:
names.append(current)
return f'<option value="">{E(T("No room"))}</option>' + "".join(
return f'<option value=""{"" if current else " selected"}>{E(T("No room"))}</option>' + "".join(
f'<option value="{E(r)}"{" selected" if dp.slug(r) == current else ""}>{E(r)}</option>' for r in names)
@@ -546,15 +708,16 @@ def shade_html(d: dict, rooms) -> str:
opts = "".join(f'<option value="{k}"{" selected" if k == cur else ""}>{E(T(w))}</option>' for k, w in FACING_WORDS.items())
if cur is None:
opts += f'<option value="{face:g}" selected>{face:g}°</option>'
inner = (hidden(id=d["id"]) + '<p class="note">' + E(T(
inner = (hidden(id=d["id"]) + '<div class="row">'
f'<div><label for="area-{E(d["id"])}">{E(T("Room it shades"))}</label><select id="area-{E(d["id"])}" name="area">'
f'{_room_options(rooms, sh.get("area") or "")}</select></div>'
f'<div><label for="face-{E(d["id"])}">{E(T("The window faces"))}</label><select id="face-{E(d["id"])}" name="facing">{opts}</select></div></div>'
'<p class="note">' + E(T("Without the window's direction Cantabile only closes it at night in winter.")) + "</p>")
about = info(E(T(
"Cantabile moves it by the sun: in summer it closes it while the sun is on the window and the room is warm; in "
"winter it opens it for the sun's warmth and closes it at night. Never between 22:00 and 07:00, and after you "
"move it by hand it waits 4 hours.")) + "</p>"
f'<label for="area-{E(d["id"])}">{E(T("Room it shades"))}</label><select id="area-{E(d["id"])}" name="area">'
f'{_room_options(rooms, sh.get("area") or "")}</select>'
f'<label for="face-{E(d["id"])}">{E(T("The window faces"))}</label><select id="face-{E(d["id"])}" name="facing">{opts}</select>'
'<p class="note">' + E(T("Without the window's direction Cantabile only closes it at night in winter.")) + "</p>")
return f"<details><summary>{E(T('Room and window'))}</summary>{form('shade', inner, T('Save'))}</details>"
"move it by hand it waits 4 hours.")))
return section(T("Room and window"), form("shade", inner, T("Save"), "save", form_cls="changes"), about)
def place_html(d: dict, rooms) -> str:
@@ -566,9 +729,9 @@ def place_html(d: dict, rooms) -> str:
inner = (hidden(id=d["id"])
+ f'<label class="check"><input type="radio" name="place" value="indoor"{chk("indoor")}><span>{E(T("Indoor, in the room:"))}</span></label>'
+ f'<select name="area" aria-label="{E(T("Room"))}">{_room_options(rooms, pl.get("area") or "")}</select>'
+ f'<label class="check"><input type="radio" name="place" value="outdoor"{chk("outdoor")}><span>{E(T("Outdoor: Cantabile's outside temperature"))}</span></label>'
+ '<p class="note">' + E(T("An outdoor sensor counts before the weather service's temperature.")) + "</p>")
return f"<details><summary>{E(T('Indoor or outdoor'))}</summary>{form('place', inner, T('Save'))}</details>"
+ f'<label class="check"><input type="radio" name="place" value="outdoor"{chk("outdoor")}><span>{E(T("Outdoor: Cantabile's outside temperature"))}</span></label>')
about = info(E(T("An outdoor sensor counts before the weather service's temperature.")))
return section(T("Indoor or outdoor"), form("place", inner, T("Save"), "save", form_cls="changes"), about)
def weather_html(d: dict) -> str:
@@ -577,9 +740,11 @@ def weather_html(d: dict) -> str:
if not w:
return ""
if w.get("active"):
return '<p class="note">' + E(T("Cantabile plans with this forecast: solar, heating, cooling and the shades.")) + "</p>"
return ('<p class="note">' + E(T("Not used for planning: Cantabile plans with one weather forecast, another one's."))
+ "</p>" + form("weather", hidden(id=d["id"]), T("Plan with this forecast"), "secondary"))
return section(T("Forecast"), '<p class="note">' + E(T(
"Cantabile plans with this forecast: solar, heating, cooling and the shades.")) + "</p>")
return section(T("Forecast"), '<p class="note">' + E(T(
"Not used for planning: Cantabile plans with one weather forecast, another one's.")) + "</p>"
+ form("weather", hidden(id=d["id"]), T("Plan with this forecast"), "secondary"))
def protection_html(d: dict) -> str:
@@ -589,11 +754,15 @@ def protection_html(d: dict) -> str:
targets = [f"device:{d['id']}"] + [f"control:{c['id']}" for c in d.get("controls") or [] if c.get("id")]
targets += [t for t in pol if t not in targets]
seal = (pol.get(f"device:{d['id']}") or {}) if d.get("protection") == "sealed" else None
profile_name = f"{d.get('manufacturer', '')} {d.get('model', '')}".strip()
rows = []
covers = {f"control:{c['id']}" for c in d.get("controls") or [] if c.get("kind") == "cover" and c.get("id")}
kinds = {f"control:{c['id']}": c.get("kind") for c in d.get("controls") or [] if c.get("id")}
covers = {t for t, k in kinds.items() if k == "cover"}
for t in targets:
e = pol.get(t) or {}
kind, _, name = t.partition(":")
if kind == "control" and kinds.get(t) in CONTROL_WORDS:
name = T(CONTROL_WORDS[kinds[t]])
what = {"device": T("The whole device"), "control": T("Control {name}", name=name)}.get(kind, name)
lift = T("Allow Cantabile to move it") if t in covers else T("Allow switching off")
lvl = "sealed" if seal else e.get("level", "none")
@@ -602,54 +771,61 @@ def protection_html(d: dict) -> str:
rows.append(f'<li>{E(what)}: <b>{E(T("Sealed"))}</b> · {E(T("only {who} can change it", who=custodian(seal or e)))}'
f'{": " + E(why) if why else ""}</li>')
continue
why = f" ({E(e['why'])})" if e.get("why") and lvl == "protected" else ""
# the profile's default says only the profile's name: not repeated
why = f" ({E(e['why'])})" if e.get("why") and e["why"] != profile_name and lvl == "protected" else ""
state = f"<b>{E(T('protected'))}</b>" if lvl == "protected" else E(T("not protected"))
rows.append(f"<li>{E(what)}: {state}{why} "
rows.append(f'<li class="switch"><span>{E(what)}: {state}{why}</span>'
+ form("protect", hidden(id=d["id"], target=t, level="none" if lvl == "protected" else "protected"),
lift if lvl == "protected" else T("Protect"), "secondary", inline=True)
lift if lvl == "protected" else T("Protect"), "secondary")
+ "</li>")
return (f"<details><summary>{E(T('Protection'))}</summary><p class=\"note\">"
+ E(T("Protected: Cantabile never switches it off or cuts it; you still can, by hand. Sealed: only its "
"custodian can change it.")) + ("" if not covers else " " + E(T(
"A protected blind or curtain is not moved by Cantabile at all.")))
+ f'</p><ul class="plain">{"".join(rows)}</ul></details>')
about = E(T("Protected: Cantabile never switches it off or cuts it; you still can, by hand. Sealed: only its "
"custodian can change it.") + ("" if not covers else " " + T(
"A protected blind or curtain is not moved by Cantabile at all.")))
return section(T("Protection"), f'<ul class="plain">{"".join(rows)}</ul>', info(about))
def badges(d: dict) -> str:
st = PROFILE_STATUS.get(d.get("status"))
out = f'<span class="badge">{E(d.get("manufacturer", ""))} {E(d.get("model", ""))}</span>'
if d.get("generic"):
out += f'<span class="badge">{E(T("generic profile"))}</span>'
if st:
out += f'<span class="badge">{E(T(st))}</span>'
return out
def remove_html(d: dict) -> str:
return ('<section class="set"><details class="remove"><summary>' + E(T("Remove from Cantabile")) + "</summary>"
+ '<p class="note">' + E(T("Cantabile stops using it. Home Assistant keeps the device as it is.")) + "</p>"
+ form("remove", hidden(id=d["id"]), T("Yes, remove it"), "danger") + "</details></section>")
def device_card(d: dict, site: dict, rooms=()) -> str:
return (f'<div class="box"><b>{E(d["name"])}</b>{badges(d)}{what_lines(d.get("assets") or [], d.get("controls") or [], False)}'
f'{notes(d)}{consent_html(d, site)}{switchover_html(d)}{shade_html(d, rooms)}{place_html(d, rooms)}'
f'{weather_html(d)}{protection_html(d)}'
f"<details><summary>{E(T('Remove'))}</summary><p class=\"note\">"
+ E(T("Cantabile stops using it. Home Assistant keeps the device as it is."))
+ "</p>" + form("remove", hidden(id=d["id"]), T("Stop using it with Cantabile"), "secondary") + "</details></div>")
def device_row(d: dict) -> str:
"""A device in use on the list: name, state in words, main values now,
protection; its settings on its own page."""
vals = device_values(d)
return (f'<a class="box dev" href="?p=devices&amp;d={urllib.parse.quote(d["id"])}"><span class="grow">'
f'<span class="name">{E(d["name"])}</span> {protection_badge(d)}'
f'<span class="model">{model_line(d)}</span><span class="state">{device_state(d)}</span>'
+ (f'<span class="values">{E(vals)}</span>' if vals else "")
+ '</span><span class="chev" aria-hidden="true">›</span></a>')
def offer_card(h: dict) -> str:
def offer_card(h: dict, rooms=()) -> str:
"""A device HA has that a profile knows, not used yet: what it would
give, "Use with Cantabile"."""
p = h["profile"]
name = h["name"] or h["model"]
generic = f' · <span class="muted">{E(T("generic profile"))}</span>' if h.get("generic") else ""
would = T("Cantabile would read: {values}.", values=reads_words(dp.reads(h["assets"]), rooms) or T("nothing"))
kinds = control_words(h["controls"])
if kinds:
would += " " + T("It could control: {controls}.", controls=kinds)
proposed = ""
if h.get("in_mapping"):
ids = ", ".join(quantity_words(i) for i in h["in_mapping"])
ids = ids_words(h["in_mapping"], rooms)
proposed = '<p class="note">' + E(
T("Proposed by Cantabile (not checked yet): {values}. Use it with Cantabile to confirm it as this profile "
"sees it.", values=ids) if h.get("mapping_source") in ("proposed", "imported")
else T("In your own mapping: {values}. Those stay as they are.", values=ids)) + "</p>"
body = what_lines(h["assets"], h["controls"], True) + notes(h) + proposed
view = dict(h, status=p.get("status"))
head = (f'<span class="grow"><span class="name">{E(name)}</span> {status_badge(p.get("status"), False)}'
f'<span class="model">{E((h.get("manufacturer", "") + " " + h.get("model", "")).strip())}{generic}</span></span>')
body = f'<p class="note">{E(would)}</p>' + notes(h, rooms, False) + proposed
if not h["assets"] and not h["controls"]:
return f'<div class="box"><b>{E(name)}</b>{badges(view)}{body}</div>'
return (f'<div class="box"><b>{E(name)}</b>{badges(view)}{body}'
+ form("use", hidden(ha_device=h["id"], profile=p["id"], name=name), T("Use with Cantabile")) + "</div>")
return f'<div class="box offer"><div class="top">{head}</div>{body}</div>'
return (f'<div class="box offer"><div class="top">{head}'
+ form("use", hidden(ha_device=h["id"], profile=p["id"], name=name), T("Use with Cantabile"), "secondary")
+ f"</div>{body}</div>")
def nav(page: str) -> str:
@@ -664,32 +840,55 @@ def html(title: str, body: str, page: str) -> str:
f"<script>{HASH_SCRIPT}</script></head><body>{nav(page)}{body}</body></html>")
def messages(msg: str, err: str) -> str:
return (f'<div class="box ok">{E(msg)}</div>' if msg else "") + (f'<div class="box err">{E(err)}</div>' if err else "")
def page_devices(v: dict, msg: str = "", err: str = "") -> str:
body = [f"<h2>{E(T('Devices'))}</h2><p class=\"note\">"
+ E(T("Home Assistant's devices that a Cantabile device profile knows. Use one with Cantabile to see it on "
"Cantabile's dashboard and to let Cantabile read it once you have given your consent.")) + "</p>"]
if msg:
body.append(f'<div class="box ok">{E(msg)}</div>')
if err:
body.append(f'<div class="box err">{E(err)}</div>')
about = E(T("Home Assistant's devices that a Cantabile device profile knows. Use one with Cantabile to see it on "
"Cantabile's dashboard and to let Cantabile read it once you have given your consent."))
body = [f"<h2>{E(T('Devices'))}{info(about)}</h2>" + messages(msg, err)]
rooms = v.get("rooms") or []
if v.get("devices"):
body.append(f"<h3>{E(T('Used with Cantabile'))}</h3>")
body += [device_card(d, v.get("site") or {}, v.get("rooms") or []) for d in v["devices"]]
n = len(v["devices"])
body.append(f"<h3>{E(TN('{n} device in use with Cantabile', '{n} devices in use with Cantabile', n))}</h3>")
body += [device_row(dict(d, rooms=rooms)) for d in v["devices"]]
elif not v.get("ha_error"):
body.append('<p class="note">' + E(T("No device is used with Cantabile yet. Add one below.")) + "</p>")
body.append(f'<div class="add"><h2>{E(T("Add a device"))}</h2>')
if v.get("offers"):
body.append(f"<h3>{E(T('In Home Assistant, with a profile'))}</h3>")
body += [offer_card(h) for h in v["offers"]]
elif not v.get("ha_error") and not v.get("devices"):
body.append('<p class="note">' + E(T("No device in Home Assistant fits a Cantabile profile yet.")) + "</p>")
body.append(f"<h3>{E(T('Devices in Home Assistant you can add'))}</h3>")
body += [offer_card(h, rooms) for h in v["offers"]]
elif not v.get("ha_error"):
body.append('<p class="note">' + E(T("No other device in Home Assistant fits a Cantabile profile.")) + "</p>")
if v.get("rest"):
n = len(v["rest"])
body.append(f"<details><summary>{E(TN('{n} more in Home Assistant without a profile (not used by Cantabile)', '{n} more in Home Assistant without a profile (not used by Cantabile)', n))}</summary><ul class=\"plain\">"
+ "".join(f"<li>{E(h['name'] or h['model'])} <small>{E(h['manufacturer'])} {E(h['model'])}</small></li>"
body.append(f"<details class=\"more\"><summary class=\"note\">{E(TN('{n} more in Home Assistant without a profile (not used by Cantabile)', '{n} more in Home Assistant without a profile (not used by Cantabile)', n))}</summary><ul class=\"plain\">"
+ "".join(f"<li>{E(h['name'] or h['model'])} <small class=\"muted\">{E(h['manufacturer'])} {E(h['model'])}</small></li>"
for h in v["rest"]) + "</ul></details>")
if v.get("ha_error"):
body.append('<p class="note">' + E(T("Home Assistant's devices could not be read: {error}", error=v["ha_error"])) + "</p>")
body.append("</div>")
return html(T("Cantabile: devices"), "".join(body), "devices")
def page_device(v: dict, dev_id: str, msg: str = "", err: str = "") -> str:
"""One device in use: its state and values, then its settings, each
section saving on its own. The list when there is no such device."""
d = next((x for x in v.get("devices") or [] if x["id"] == dev_id), None)
if not d:
return page_devices(v, msg, err)
rooms = v.get("rooms") or []
vals = device_values(d)
body = (f'<a class="back" href="?p=devices">‹ {E(T("Devices"))}</a><h2>{E(d["name"])}</h2>'
f'<div class="model head">{model_line(d)} {status_badge(d.get("status"))}</div>{messages(msg, err)}'
f'<div class="box now"><span class="state">{device_state(d)}</span>'
+ (f'<span class="values">{E(vals)}</span>' if vals else "") + "</div>"
+ consent_html(d, v.get("site") or {}) + reads_html(d, rooms) + switchover_html(d) + shade_html(d, rooms)
+ place_html(d, rooms) + weather_html(d) + protection_html(d) + remove_html(d))
return html(f'Cantabile: {d["name"]}', body, "devices")
def page_refused(why: str, page: str = "devices") -> str:
head = T("Devices") if page == "devices" else T("Building or community")
return html("Cantabile", f"<h2>{E(head)}</h2><div class=\"box err\">{E(why)}</div>", page)
@@ -733,8 +932,10 @@ class App(JoinPage):
return status, out
if url.path not in ("/", "") or method not in ("GET", "POST"):
return 404, T("Not found")
dev_id = (q.get("d") or [""])[0]
if method == "GET":
return 200, page_devices(await self.devices.view())
v = await self.devices.view()
return 200, page_device(v, dev_id) if dev_id else page_devices(v)
f = {k: v[0] for k, v in urllib.parse.parse_qs(data.decode("utf-8", "replace")).items()}
by = hdr.get("x-remote-user-display-name") or hdr.get("x-remote-user-name") or "owner"
return 200, await self.post_devices(f, by)
@@ -747,6 +948,7 @@ class App(JoinPage):
msg = T("{name} is used with Cantabile now. It is on Cantabile's dashboard; reading and control are off "
"until you switch them on below.",
name=d["name"])
return page_device(await dv.view(), d["id"], msg)
elif do == "remove":
await dv.remove(f.get("id", ""))
msg = T("Cantabile no longer uses it.")
@@ -773,4 +975,7 @@ class App(JoinPage):
msg = T("Saved. Cantabile starts again to apply it (a few seconds).")
except dp.Fail as e:
err = str(e)
return page_devices(await dv.view(), msg, err)
# a device's setting: back to its page (removed: the list)
if do in ("use", "remove") or not f.get("id"):
return page_devices(await dv.view(), msg, err)
return page_device(await dv.view(), f["id"], msg, err)
+99
View File
@@ -695,3 +695,102 @@ def one_weather(frag: dict, others) -> None:
frag["weather_entity"] = ent
if any((o.get("inputs") or {}).get("weather") for o in others):
frag.pop("inputs", None)
# --- what a device reads, in the pages' words (both Devices pages) -------------------------
# The Hub panel and the add-on's page show a device's values by their
# meaning, never by asset id: room.living_room.setpoint_low and _high are
# one "heat/cool range", heating.state and cooling.state one "heating/
# cooling state". Here only the keys and values; each page has its words
# (its own catalogue) for each key.
RANGE, HVAC = "room.setpoint_range", "heating_cooling.state"
# A device's main values on the list, in this order (at most a few).
HEADLINE = ("room.temp", "room.setpoint", RANGE, HVAC, "heating.state", "cooling.state", "pv.power", "battery.soc",
"battery.power", "grid.power", "load.power", "ev.power", "heatpump.power", "heat.power",
"heatpump.flow_temp", "outside.temp", "room.humidity", "room.shade_position", "circuit.power")
def base_quantity(q: str) -> tuple[str, str]:
"""(quantity, place) of an asset id or a profile's value: room.living_room.temp
-> ("room.temp", "living_room"), room.{area}.humidity -> ("room.humidity", ""),
pv.2.power -> ("pv.power", ""), room.kitchen.temp_2 -> ("room.temp", "kitchen")."""
head, _, rest = q.partition(".")
if head in PER_PLACE and "." in rest:
place, _, last = rest.rpartition(".")
base = f"{head}.{last}"
if base not in QUANTITIES:
base = f"{head}.{re.sub(r'_[0-9]+$', '', last)}"
return base, ("" if place.startswith("{") else place)
part = part_of(q)
if part:
return part[0], ""
return (q if q in QUANTITIES else re.sub(r"_[0-9]+$", "", q)), ""
def reads(assets) -> list:
"""What a device reads: [(key, place)] in its order, each once; a
setpoint's low and high as RANGE, heating and cooling state as HVAC."""
keys = [base_quantity(a["asset"] if isinstance(a, dict) else a) for a in assets or []]
have = {k for k, _ in keys}
out = []
for k, place in keys:
if k in ("room.setpoint_low", "room.setpoint_high"):
k = RANGE
elif k in ("heating.state", "cooling.state") and {"heating.state", "cooling.state"} <= have:
k = HVAC
if [k, place] not in out:
out.append([k, place])
return out
def lacks(missing) -> dict:
"""mapping_for's missing in parts: the values a device does not give
(keys, as reads()), the controls not found (kinds), anything else."""
out = {"values": [], "controls": [], "other": []}
for m in missing or []:
m = str(m)
if re.fullmatch(r"[a-z_]+ control \(.*\)", m):
out["controls"].append(m.split(" ", 1)[0])
elif quantity_of(m) or quantity_of(base_quantity(m)[0]):
k = [x for x, _ in reads([m])][0]
if k not in out["values"]:
out["values"].append(k)
else:
out["other"].append(m)
return out
def asset_value(a: dict, states: dict):
"""One value now from HA's states ({entity_id: {"state", "attributes"}}):
a number (scaled), True/False (an "equals"), or None (not known)."""
s = (states or {}).get(a.get("entity"))
if not s:
return None
raw = (s.get("attributes") or {}).get(a["attribute"]) if a.get("attribute") else s.get("state")
if raw is None or raw in ("unavailable", "unknown", ""):
return None
if a.get("equals") is not None:
return str(raw) == str(a["equals"])
try:
return float(raw) * float(a.get("scale", 1))
except (TypeError, ValueError):
return None
def live(assets, states, limit: int = 3) -> list:
"""A device's main values now: [(key, value)] in HEADLINE's order, at
most limit; RANGE's value [low, high], HVAC's "heating", "cooling" or
"idle"; values not known left out."""
now = {}
for a in assets or []:
k = base_quantity(a["asset"])[0]
if k not in now:
v = asset_value(a, states)
if v is not None:
now[k] = v
if now.get("room.setpoint") is None and now.get("room.setpoint_low") is not None \
and now.get("room.setpoint_high") is not None:
now[RANGE] = [now["room.setpoint_low"], now["room.setpoint_high"]]
if "heating.state" in now and "cooling.state" in now:
now[HVAC] = "heating" if now.pop("heating.state") else "cooling" if now.pop("cooling.state") else "idle"
return [[k, now[k]] for k in HEADLINE if k in now][:limit]
+165
View File
@@ -0,0 +1,165 @@
# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
"""How many phases a charger charges on (docs/methods.md, finding A8-a): one
or three, never one fixed default. Per EV control, from the best evidence,
with where it came from:
mapping the control's `phases` (the installer's, or the device profile's)
device the charger reports it: the control's `phases` entity (the
EEBUS bridge's "phases" value; a sensor 1..3)
learned seen by the box while the car charged steadily: power / (volts
x current) is about 1, 2 or 3 (within 0.3) over ~10 minutes of
samples with more than 1 kW and more than 5 A, the power within
10 % and the current within 0.5 A. Kept in <state>/ev_phases.json
(phases, n: the times seen, at); learned again when the next
readings disagree CONFIRM times in a row
assumed nothing known: each use takes its own safe side
(ASSUMED): the §14a order's current bound 3 phases (a 3-phase
charger then never draws above the ordered power), the plan 1
phase (it reckons with a slower charger: starts earlier and
still meets the departure)
The current in the ratio is the charger's measured current (the control's
`current_measured` sensor) where mapped, else its setpoint (`current`); a
car that draws less than the setpoint gives a ratio between the steps,
which decides nothing, or a wrong one, which the next sessions put right.
"""
import json
import logging
import os
log = logging.getLogger(__name__)
ASSUMED = {"order": 3.0, "plan": 1.0}
WINDOW_S = 600.0 # ~10 minutes of samples decide
MIN_SPAN_S = 540.0
MIN_SAMPLES = 5
MIN_POWER_W = 1000.0
MIN_CURRENT_A = 5.0
POWER_STEADY = 0.10 # (max - min) / mean
CURRENT_STEADY_A = 0.5
TOLERANCE = 0.3
CONFIRM = 3 # disagreeing decisions in a row before the learned count changes
def ratio_phases(power_w: float, volts: float, current_a: float) -> int | None:
"""1, 2 or 3 when power / (volts x current) is within TOLERANCE of it."""
if volts <= 0 or current_a <= 0:
return None
r = power_w / (volts * current_a)
k = round(r)
return int(k) if 1 <= k <= 3 and abs(r - k) <= TOLERANCE else None
def steady(samples: list[tuple[float, float, float]]) -> tuple[float, float] | None:
"""(mean power, mean current) of samples (t, W, A) that are steady
charging over the window, else None."""
if len(samples) < MIN_SAMPLES or samples[-1][0] - samples[0][0] < MIN_SPAN_S:
return None
ps, cs = [s[1] for s in samples], [s[2] for s in samples]
if min(ps) <= MIN_POWER_W or min(cs) <= MIN_CURRENT_A:
return None
mp = sum(ps) / len(ps)
if (max(ps) - min(ps)) / mp > POWER_STEADY or max(cs) - min(cs) > CURRENT_STEADY_A:
return None
return mp, sum(cs) / len(cs)
class EvPhases:
def __init__(self, state_dir: str):
self.path = os.path.join(state_dir, "ev_phases.json")
self.learned: dict[str, dict] = {}
self.window: dict[str, list] = {}
try:
with open(self.path) as f:
raw = json.load(f)
self.learned = {k: v for k, v in raw.items() if isinstance(v, dict) and v.get("phases") in (1, 2, 3)}
except (OSError, ValueError, AttributeError):
pass
def _save(self):
tmp = self.path + ".tmp"
try:
with open(tmp, "w") as f:
json.dump(self.learned, f)
os.replace(tmp, self.path)
except OSError as e:
log.warning("ev phases: %s", e)
def sample(self, cid: str, power_w: float | None, current_a: float | None, volts: float, now: float) -> bool:
"""One reading while it charges (or not); True when the learned
phase count changed."""
w = self.window.setdefault(cid, [])
if power_w is None or current_a is None or power_w <= MIN_POWER_W or current_a <= MIN_CURRENT_A:
w.clear() # not charging steadily: start again
return False
w.append((now, float(power_w), float(current_a)))
while w and now - w[0][0] > WINDOW_S:
w.pop(0)
st = steady(w)
if st is None:
return False
k = ratio_phases(st[0], volts, st[1])
if k is None:
if w[-1][0] - w[0][0] >= WINDOW_S - 1:
w.pop(0) # a steady but odd ratio: slide on
return False
w.clear()
return self.decide(cid, k, now)
def decide(self, cid: str, k: int, now: float) -> bool:
cur = self.learned.get(cid)
if cur is None:
self.learned[cid] = {"phases": k, "n": 1, "at": int(now)}
log.info("ev %s: %d phase(s), learned", cid, k)
self._save()
return True
if cur["phases"] == k:
cur.update(n=int(cur.get("n", 0)) + 1, at=int(now))
cur.pop("against", None)
self._save()
return False
against = cur.get("against") or {}
n = int(against.get("n", 0)) + 1 if against.get("phases") == k else 1
if n >= CONFIRM:
self.learned[cid] = {"phases": k, "n": n, "at": int(now)}
log.info("ev %s: %d phase(s) now, learned again (was %d)", cid, k, cur["phases"])
self._save()
return True
cur["against"] = {"phases": k, "n": n}
self._save()
return False
def resolve(self, c, col=None) -> tuple[float | None, str]:
"""A control's phase count and its source (module docstring); None
and "assumed" when nothing says."""
p = c.params.get("phases")
if p:
return float(p), "mapping"
ent = c.entities.get("phases")
if ent and col is not None:
v = col.value(ent)
if v is not None and 1 <= round(v) <= 3:
return float(round(v)), "device"
got = self.learned.get(c.id)
if got:
return float(got["phases"]), "learned"
return None, "assumed"
def all(self, controls, col=None) -> dict[str, dict]:
"""{control id: {"phases": n or None, "source", "plan", "order"}}
for the EV controls (plan and order: the count each use takes)."""
out = {}
for c in controls:
if c.kind != "ev":
continue
n, src = self.resolve(c, col)
out[c.id] = {"phases": n, "source": src, "plan": n or ASSUMED["plan"], "order": n or ASSUMED["order"]}
return out
def words(info: dict) -> str:
"""'1 phase, learned', '3 phases, assumed for the grid order (1 for the plan)'."""
n, src = info.get("phases"), info.get("source", "")
if n:
return f"{int(n)} phase{'s' if n != 1 else ''}, {src}"
return f"phases unknown: {int(ASSUMED['order'])} assumed for the grid order, {int(ASSUMED['plan'])} for the plan"
+38 -5
View File
@@ -44,13 +44,18 @@ import base64
import hashlib
import json
import logging
import math
import os
import time
from .controls import ev_volts
log = logging.getLogger(__name__)
TOPIC = "cantabile-eebus/gridorder"
STALE_S = 300
SLOT_S = 900 # the cloud's ledger slots (fleet marketrun.go)
ACTION_ENTITY, ACTION_SERVICE = "grid:eebus-lpc", "grid.load_limit"
PENALTY_EUR_KWH = 500.0 # like a relayed emergency (building.py): nobody overrides it
# -- Ed25519 verification (RFC 8032, 5.1.7), pure Python ----------------------
@@ -136,6 +141,7 @@ class GridOrders:
self.order: dict | None = None # the last good order
self.received = 0.0
self.error = ""
self.reported = None # (order id, limit, slot) last told to the cloud
def pinned(self) -> str:
try:
@@ -198,6 +204,30 @@ class GridOrders:
o = self.active()
return f"verified:eebus-lpc:{o['control_box']['ski']}:{o['id']}" if o else ""
def action(self) -> dict | None:
"""What the cloud records of the order (an `action`, kind grid
limit): once per quarter-hour while it holds (and when it changes),
once when it ends. Fleet's market ledger takes a slot with one as
under a grid operator's order (higher rank: the contracts' deviations
in it are exempt). None: nothing new to tell."""
o, now = self.active(), self.clock()
if o:
key = (o["id"], o.get("limit_w"), int(now) // SLOT_S)
if key == self.reported:
return None
self.reported = key
cb = o.get("control_box") or {}
until = f", until {time.strftime('%H:%M', time.gmtime(o['until']))} UTC" if o.get("until") else ""
return {"v": 1, "at": int(now), "asset": "", "entity": ACTION_ENTITY, "service": ACTION_SERVICE,
"value": str(float(o["limit_w"])), "result": "done",
"why": f"a grid operator's order: EEBUS LPC {o.get('state', 'limited')}, {float(o['limit_w']):.0f} W, "
f"control box {cb.get('ski', '')}, order {o['id']}{until}"[:500]}
if self.reported is None:
return None
self.reported = None
return {"v": 1, "at": int(now), "asset": "", "entity": ACTION_ENTITY, "service": ACTION_SERVICE, "value": "",
"result": "done", "why": "the grid operator's order ended"}
def for_model(self, start: int, step_s: int, steps: int) -> dict:
"""The optimiser's constraint: the controllable consumers' draw from
the grid at most limit_w (state["grid_order"])."""
@@ -212,11 +242,12 @@ class GridOrders:
return {"limit_w": float(o["limit_w"]), "from_step": int(a), "to_step": int(b), "penalty": PENALTY_EUR_KWH,
"id": o["id"], "proof": self.proof(), "state": o.get("state", "")}
def limits(self, controls) -> dict:
def limits(self, controls, phases: dict | None = None) -> dict:
"""Grid-level bounds per controllable device (§14a: chargers,
batteries' charging; heat pumps are held by the bridge itself): no
single one above the whole limit. {control id: {quantity: (floor,
limit)}}."""
single one above the whole limit. phases: {control id: count} from
evphases (else the mapping's; unknown: 3, never above the ordered
power). {control id: {quantity: (floor, limit)}}."""
o = self.active()
if not o:
return {}
@@ -224,8 +255,10 @@ class GridOrders:
out = {}
for c in controls:
if c.kind == "ev":
volts = float(c.params.get("voltage", 230)) * float(c.params.get("phases", 3))
out[c.id] = {"current_a": (None, round(w / volts, 1))}
n = (phases or {}).get(c.id) or c.params.get("phases")
volts = ev_volts({**c.params, "phases": n}, "order")
# down to 0.1 A: never above the ordered power (6.1 A x 690 V = 4209 W)
out[c.id] = {"current_a": (None, math.floor(w / volts * 10 + 1e-9) / 10)}
elif c.kind == "battery":
out[c.id] = {"power_w": (None, w)}
return out
+94 -17
View File
@@ -201,10 +201,6 @@ MESSAGES = {
"dashboard and to let Cantabile read it once you have given your consent.":
"Geräte aus Home Assistant, die ein Geräteprofil von Cantabile kennt. Nutzen Sie eines mit Cantabile, um es im "
"Dashboard von Cantabile zu sehen und, sobald Sie eingewilligt haben, von Cantabile auslesen zu lassen.",
"Used with Cantabile": "Mit Cantabile genutzt",
"In Home Assistant, with a profile": "In Home Assistant, mit Profil",
"No device in Home Assistant fits a Cantabile profile yet.":
"Noch passt kein Gerät in Home Assistant zu einem Profil von Cantabile.",
("{n} more in Home Assistant without a profile (not used by Cantabile)",
"{n} more in Home Assistant without a profile (not used by Cantabile)"):
("{n} weiteres Gerät in Home Assistant ohne Profil (von Cantabile nicht genutzt)",
@@ -212,10 +208,6 @@ MESSAGES = {
"Home Assistant's devices could not be read: {error}": "Die Geräte von Home Assistant konnten nicht gelesen werden: {error}",
"Use with Cantabile": "Mit Cantabile nutzen",
"generic profile": "allgemeines Profil",
"Checked on a device": "An einem Gerät geprüft",
"From the maker's documentation, not yet checked on a device":
"Aus der Dokumentation des Herstellers, noch nicht an einem Gerät geprüft",
"Draft: check the values before you turn reading on": "Entwurf: Prüfen Sie die Werte, bevor Sie das Auslesen einschalten",
"nothing": "nichts",
"battery": "Batterie",
"heating": "Heizen",
@@ -259,8 +251,6 @@ MESSAGES = {
"Eine geschützte Jalousie oder ein geschützter Vorhang wird von Cantabile gar nicht bewegt.",
"Cantabile would read: {values}.": "Cantabile würde auslesen: {values}.",
"It could control: {controls}.": "Steuern könnte es: {controls}.",
"Cantabile reads: {values}.": "Cantabile liest aus: {values}.",
"It may control: {controls}.": "Steuern darf es: {controls}.",
"Not found in Home Assistant: {entities}.": "In Home Assistant nicht gefunden: {entities}.",
"Read by your own mapping already, so left out here: {values}.":
"Schon von Ihrer eigenen Zuordnung ausgelesen, daher hier weggelassen: {values}.",
@@ -274,7 +264,6 @@ MESSAGES = {
"Von Cantabile vorgeschlagen (noch nicht geprüft): {values}. Nutzen Sie es mit Cantabile, um es so zu bestätigen, "
"wie dieses Profil es sieht.",
"In your own mapping: {values}. Those stay as they are.": "In Ihrer eigenen Zuordnung: {values}. Diese bleiben, wie sie sind.",
"Cantabile may read these values": "Cantabile darf diese Werte auslesen",
"Save": "Speichern",
"Saved.": "Gespeichert.",
"Heating or cooling": "Heizen oder Kühlen",
@@ -285,8 +274,6 @@ MESSAGES = {
"By outdoor temperature": "Nach Außentemperatur",
"This thermostat can heat and cool. Cantabile uses one at a time; the other is never touched.":
"Dieses Thermostat kann heizen und kühlen. Cantabile nutzt immer nur eines; das andere bleibt unberührt.",
"By outdoor temperature: heat below (°C)": "Nach Außentemperatur: heizen unter (°C)",
"cool above (°C)": "kühlen über (°C)",
"Between the two it stays as it is, so it does not switch back and forth on a mild day.":
"Dazwischen bleibt es, wie es ist, damit es an einem milden Tag nicht hin und her schaltet.",
"Protection": "Schutz",
@@ -307,10 +294,8 @@ MESSAGES = {
"the grid operator": "der Netzbetreiber",
"the metering operator": "der Messstellenbetreiber",
"the energy community's operator": "der Betreiber der Energiegemeinschaft",
"Remove": "Entfernen",
"Cantabile stops using it. Home Assistant keeps the device as it is.":
"Cantabile nutzt es nicht mehr. Home Assistant behält das Gerät, wie es ist.",
"Stop using it with Cantabile": "Nicht mehr mit Cantabile nutzen",
"Cantabile no longer uses it.": "Cantabile nutzt es nicht mehr.",
"Cantabile could not ask Home Assistant who you are ({error}).":
"Cantabile konnte Home Assistant nicht fragen, wer Sie sind ({error}).",
@@ -335,8 +320,6 @@ MESSAGES = {
"First give your consent in the add-on's configuration (Settings, Add-ons, Cantabile Edge Agent, Configuration).":
"Geben Sie zuerst Ihre Einwilligung in der Konfiguration des Add-ons (Einstellungen, Add-ons, Cantabile Edge "
"Agent, Konfiguration).",
"Cantabile may control it ({controls}), within your limits":
"Cantabile darf es steuern ({controls}), innerhalb Ihrer Grenzen",
"Control also needs your consent to control devices in the add-on's configuration.":
"Zum Steuern braucht es außerdem Ihre Einwilligung zum Steuern von Geräten in der Konfiguration des Add-ons.",
"Saved. Cantabile starts again to apply it (a few seconds).":
@@ -347,4 +330,98 @@ MESSAGES = {
"bis Sie sie unten einschalten.",
"Only an administrator of Home Assistant can use this page.":
"Nur ein Administrator von Home Assistant kann diese Seite nutzen.",
'room temperature': 'Raumtemperatur',
'humidity': 'Luftfeuchte',
'setpoint': 'Solltemperatur',
'heat/cool range': 'Heiz-/Kühlbereich',
'heating/cooling state': 'Heiz-/Kühlzustand',
'heating state': 'Heizzustand',
'cooling state': 'Kühlzustand',
'position': 'Stellung',
'tilt': 'Neigung',
'solar power': 'Solarleistung',
'solar energy': 'Solarertrag',
'grid power': 'Netzleistung',
'power from the grid': 'Bezug aus dem Netz',
'power to the grid': 'Einspeisung ins Netz',
'energy from the grid': 'Energie aus dem Netz',
'energy to the grid': 'Energie ins Netz',
'battery power': 'Batterieleistung',
'battery charge': 'Batterieladestand',
'energy charged': 'geladene Energie',
'energy discharged': 'entladene Energie',
'home consumption': 'Hausverbrauch',
'home energy': 'Hausenergie',
'heat pump power': 'Leistung der Wärmepumpe',
'heat pump energy': 'Energie der Wärmepumpe',
'flow temperature': 'Vorlauftemperatur',
'hot water temperature': 'Warmwassertemperatur',
'outdoor temperature': 'Außentemperatur',
'outdoor humidity': 'Außenluftfeuchte',
'gas': 'Gas',
'charging power': 'Ladeleistung',
'power': 'Leistung',
'energy': 'Energie',
'set to {v}': 'eingestellt auf {v}',
'set to {low}–{high}': 'eingestellt auf {low}–{high}',
'solar {v}': 'Solar {v}',
'battery {v}': 'Batterie {v}',
'grid {v}': 'Netz {v}',
'home {v}': 'Haus {v}',
'charging {v}': 'lädt mit {v}',
'heat pump {v}': 'Wärmepumpe {v}',
'flow {v}': 'Vorlauf {v}',
'outside {v}': 'außen {v}',
'humidity {v}': 'Luftfeuchte {v}',
'{v} open': '{v} offen',
'idle': 'in Ruhe',
'checked': 'geprüft',
'Checked on a device: its values are known to be right.':
'An einem Gerät geprüft: Seine Werte stimmen nachweislich.',
'untested': 'ungeprüft',
"Made from the maker's documentation, not yet checked on a device. Look at its values before you turn reading on.":
'Nach der Dokumentation des Herstellers erstellt, noch nicht an einem Gerät geprüft. Sehen Sie sich seine Werte an, bevor Sie das Auslesen einschalten.',
'draft': 'Entwurf',
'A draft profile: check the values before you turn reading on.':
'Ein Profil im Entwurf: Prüfen Sie die Werte, bevor Sie das Auslesen einschalten.',
'Reading': 'Auslesen',
'Cantabile may read these values.': 'Cantabile darf diese Werte auslesen.',
'Reading and control': 'Auslesen und Steuern',
'What Cantabile reads': 'Was Cantabile ausliest',
'Forecast': 'Wettervorhersage',
'{what} ({room})': '{what} ({room})',
'In use': 'In Betrieb',
'reading on': 'Auslesen an',
'Added': 'Hinzugefügt',
'reading off': 'Auslesen aus',
'Control': 'Steuern',
'Cantabile may control it ({controls}), within your limits.':
'Cantabile darf es steuern ({controls}), innerhalb Ihrer Grenzen.',
'{control} control': 'Steuerung {control}',
'control on': 'Steuern an',
'control off': 'Steuern aus',
'sealed': 'versiegelt',
'Add a device': 'Ein Gerät hinzufügen',
'On': 'An',
'Off': 'Aus',
'Heat below (°C)': 'Heizen unter (°C)',
'Cool above (°C)': 'Kühlen über (°C)',
'Devices in Home Assistant you can add': 'Geräte in Home Assistant, die Sie hinzufügen können',
'This device does not report: {values}.': 'Dieses Gerät meldet nicht: {values}.',
'Remove from Cantabile': 'Aus Cantabile entfernen',
'Yes, remove it': 'Ja, entfernen',
'No device is used with Cantabile yet. Add one below.':
'Noch kein Gerät wird mit Cantabile genutzt. Fügen Sie unten eines hinzu.',
'Turn off': 'Ausschalten',
'Turn on': 'Einschalten',
('{n} device in use with Cantabile', '{n} devices in use with Cantabile'):
('{n} Gerät mit Cantabile in Betrieb', '{n} Geräte mit Cantabile in Betrieb'),
'More information': 'Mehr Informationen',
'No other device in Home Assistant fits a Cantabile profile.':
'Kein weiteres Gerät in Home Assistant passt zu einem Cantabile-Profil.',
'Reads: {values}': 'Liest: {values}',
'Controls: {controls}': 'Steuert: {controls}',
'heat delivered (thermal)': 'gelieferte Wärme (thermisch)',
'heat energy (thermal)': 'Wärmemenge (thermisch)',
'heat {v} (thermal)': 'Wärme {v} (thermisch)',
}
+123 -13
View File
@@ -2,7 +2,9 @@
"""Inputs for the optimiser: the latest states of the controls' and the
inputs' entities (presence, temperatures, humidity, outside temperature,
sun, weather), a short local history of load and PV (the fallback when no
model bundle says), and the 15-minute model state for the horizon.
model bundle says), and the 15-minute model state for the horizon. A
room's R and C: the mapping's, else the model bundle's (its rooms), else
the defaults; sources.rooms says which per control.
None of this leaves the site; only the plan does (controller.py).
@@ -138,10 +140,36 @@ def _hourly(forecast: list[dict], key: str, start: int, steps: int) -> list[floa
return out
def _series(name, start, bundles, history, tz, now_value, default):
s = bundles.forecast(name, start, STEP_S, STEPS, tz) if bundles else None
if s is not None:
return s, "model"
def watts(col: Collector, eid: str) -> float | None:
"""A power sensor's value in W (HA's kW converted)."""
v = col.value(eid)
if v is not None and col.attr(eid, "unit_of_measurement") == "kW":
v *= 1000.0
return v
def base_load(col: Collector, inputs) -> float | None:
"""The house's load without the devices the plan plans itself (Finding
A2-a): load_power less the planned devices' own meters
(inputs.controlled_power: heat pump, A/C, charger), never below 0. A
device meter without a value counts 0 (a charger's sensor goes
unavailable while idle). None while the load has none."""
eids = [inputs.load_power] if isinstance(inputs.load_power, str) else list(inputs.load_power or [])
vals = [watts(col, e) for e in eids if e]
if not vals or None in vals:
return None
ctl = sum(watts(col, e) or 0.0 for e in getattr(inputs, "controlled_power", None) or [])
return max(0.0, sum(vals) - ctl)
def _series(name, start, bundles, history, tz, now_value, default, model_names=None):
"""A series for the horizon: the model bundle's (the first of
model_names it has, default name), else the same quarter hour 1 or 2
days back, else the value now, else the default."""
for m in (model_names if model_names is not None else (name,)):
s = bundles.forecast(m, start, STEP_S, STEPS, tz) if bundles else None
if s is not None:
return s, "model" if m == name else f"model ({m})"
out, have = [], 0
for k in range(STEPS):
t = start + k * STEP_S
@@ -228,15 +256,28 @@ def cover_position(col: Collector, ent: str) -> float | None:
def model_state(now: float, controls, inputs, col: Collector, bundles, history, tz: str,
weather_forecast: list[dict] | None, extra_limits: dict | None = None,
occupied_hours=(6, 22), building: dict | None = None, location: dict | None = None,
shade_ctx: dict | None = None, grid_order: dict | None = None) -> dict:
shade_ctx: dict | None = None, grid_order: dict | None = None,
grid_max_w: float | None = None, ev_phases: dict | None = None) -> dict:
"""The 48 h, 15-minute input for the optimiser (JSON: it runs in a
subprocess). location: the site's latitude and longitude (HA's
configuration); shade_ctx: per cover control its room (temp_c,
setpoint_c, season: shades.py)."""
setpoint_c, season: shades.py); grid_max_w: the site's connection (W,
config; the optimiser's default without it); ev_phases: per EV control
its phase count and source (evphases.EvPhases.all), else the mapping's."""
start = int(now // STEP_S * STEP_S)
zone = ZoneInfo(tz or "UTC")
prices = bundles.prices(start, STEP_S, STEPS, now)
load, load_src = _series("load_w", start, bundles, history, tz, col.total(inputs.load_power), 500.0)
# the base load: the house without the planned devices (Finding A2-a), so
# the plan, the building's shift and the grid order's base (optimizer)
# do not count them twice. The model's base_load_w is that; its load_w
# (load.power as metered) is too only where no planned device has a meter
# inside it, else the history of the base load stands in.
ctl = getattr(inputs, "controlled_power", None) or []
load, load_src = _series("load_w", start, bundles, history, tz, base_load(col, inputs), 500.0,
("base_load_w",) if ctl else ("base_load_w", "load_w"))
if ctl and not load_src.startswith("model") and bundles and \
bundles.forecast("load_w", start, STEP_S, 1, tz) is not None:
load_src += " (the model's load_w includes the planned devices: not used)"
pv, pv_src = _series("pv_w", start, bundles, history, tz, None, 0.0)
t_out = _hourly(weather_forecast or [], "temperature", start, STEPS)
t_src = "weather forecast"
@@ -270,7 +311,8 @@ def model_state(now: float, controls, inputs, col: Collector, bundles, history,
continue
h = datetime.fromtimestamp(start + k * STEP_S, timezone.utc).astimezone(zone).hour
occupied.append(1 if occupied_hours[0] <= h < occupied_hours[1] else 0)
devices = []
devices, rooms_src, phases_src = [], {}, {}
one_room = sum(c.kind in ("heating", "cooling") for c in controls) == 1
for c in controls:
if hasattr(c, "learn_range"):
c.learn_range(col) # a climate's own min_temp..max_temp
@@ -278,18 +320,34 @@ def model_state(now: float, controls, inputs, col: Collector, bundles, history,
for q in _quantities(c):
b = c.bounds(q, (extra_limits or {}).get(c.id))
bounds[q] = [b.lo, b.hi, b.lo_by, b.hi_by]
devices.append({"id": c.id, "kind": c.kind, "params": {k: c.p(k) for k in _param_names(c)},
params = {k: c.p(k) for k in _param_names(c)}
if c.kind == "ev":
# the count from the best evidence (evphases.py); None: the plan assumes 1
info = (ev_phases or {}).get(c.id) or \
({"phases": float(c.params["phases"]), "source": "mapping"} if c.params.get("phases") else
{"phases": None, "source": "assumed"})
params["phases"] = info["phases"]
phases_src[c.id] = {"phases": info["phases"], "source": info["source"]}
if c.kind in ("heating", "cooling"):
rooms_src[c.id] = _room_rc(c, params, bundles, one_room, now)
devices.append({"id": c.id, "kind": c.kind, "params": params,
"state": device_state(c, col, now), "bounds": bounds,
"channels": sorted(c.writable())})
clouds = _hourly(weather_forecast or [], "cloud_coverage", start, STEPS)
sky = [max(0.0, min(1.0, 1 - c / 100.0)) for c in clouds] if clouds else [1.0] * STEPS
ghi, ghi_src = irradiance(start, location, sky, sun if peak > 100 else None)
out = {"v": 1, "start": start, "step_s": STEP_S, "steps": STEPS, "tz": tz,
"price_import": prices.import_ct, "price_export": prices.export_ct, "price_note": prices.note,
"load_w": load, "pv_w": pv, "t_out": t_out, "sun": sun, "occupied": occupied, "sky": sky,
"location": location or {},
"ghi_w_m2": ghi, "location": location or {},
"sources": {"load": load_src, "pv": pv_src, "t_out": t_src, "model": bundles.model_id(),
"sky": "weather forecast" if clouds else "clear"},
"devices": devices, "building": building or {}, "grid_order": grid_order or {}}
"sky": "weather forecast" if clouds else "clear", "rooms": rooms_src, "ghi": ghi_src,
"ev_phases": phases_src},
"devices": devices, "building": building or {}, "grid_order": grid_order or {},
# the market's contracts in force as sources and sinks (shadow, the market's step 2)
"market": bundles.market_for_model(start, STEP_S, STEPS, now) if hasattr(bundles, "market_for_model") else []}
if grid_max_w:
out["grid_max_w"] = float(grid_max_w)
# a shade's moves by rules on the sun and its room (shades.py); the
# optimiser takes them as they are
from . import shades
@@ -299,6 +357,58 @@ def model_state(now: float, controls, inputs, col: Collector, bundles, history,
return out
def irradiance(start: int, location: dict | None, sky: list[float],
pv_sun: list[float] | None) -> tuple[list[float], str]:
"""Global irradiance per step (W/m²) for a room's fitted sun
(solar_m2): Haurwitz's clear sky, 1098 cos z exp(-0.057 / cos z), at
the step's middle where the site's location is known, times the sky
(1 - the forecast's cloud cover); else the PV forecast scaled to its
peak (pv_sun, A1.2) times 1000 W/m², a rough stand-in; else none (the
sun's elevation now, held, would shine all night)."""
from .shades import sun_position
lat, lon = (location or {}).get("latitude"), (location or {}).get("longitude")
if lat is None or lon is None:
if pv_sun is None:
return [0.0] * STEPS, "none (no location, no PV forecast)"
return [1000.0 * max(0.0, x) for x in pv_sun], "PV forecast"
out = []
for k in range(STEPS):
elev, _ = sun_position(start + k * STEP_S + STEP_S / 2, float(lat), float(lon))
cz = math.sin(math.radians(elev))
clear = 1098.0 * cz * math.exp(-0.057 / cz) if cz > 0.01 else 0.0
out.append(round(clear * max(0.0, min(1.0, sky[k] if k < len(sky) else 1.0)), 1))
return out, "clear sky x the sky"
def _room_rc(c, params: dict, bundles, only_one: bool, now: float) -> str:
"""A heating or cooling control's R and C: the mapping's own, else the
model's room (bundles.room_rc), else the defaults. A room fitted against
the electric power: C * cop and R / cop (the same RC; the optimiser's
heat is cop * P). Into params; where they came from."""
if "r_k_per_kw" in c.params or "c_kwh_per_k" in c.params:
return "mapping"
get = getattr(bundles, "room_rc", None)
rc = get(c.id, str(c.params.get("area") or ""), only_one, now) if get else None
if rc is None:
return "defaults"
r, cap = rc["r_k_per_kw"], rc["c_kwh_per_k"]
gain, solar = rc.get("gain_w") or 0.0, rc.get("solar_m2") or 0.0
cop = number(params.get("cop")) or 1.0
if rc.get("power") == "electric" and cop > 0:
# fitted in electric equivalents: the heat is cop times (like C)
r, cap = r / cop, cap * cop
gain = [g * cop for g in gain] if isinstance(gain, list) else gain * cop
solar *= cop
params["r_k_per_kw"], params["c_kwh_per_k"] = r, cap
# the fit's internal gain and sun (Finding B6-a): R and C were fitted
# with them; the mapping's own, where it sets them, win
if "internal_gain_w" not in c.params:
params["internal_gain_w"] = gain
if "solar_m2" not in c.params:
params["solar_m2"] = solar
return "model"
def _quantities(c) -> list[str]:
return {"battery": ["soc_pct", "power_w"], "heating": ["temp_c"], "cooling": ["temp_c"],
"ev": ["current_a"], "cover": ["position_pct"]}[c.kind]
+47 -7
View File
@@ -53,7 +53,27 @@ def _hash(eid: str) -> str:
return "h" + hashlib.sha256(eid.encode()).hexdigest()[:12]
def build(mapping, registries: dict, specs: dict, policy: dict, now: float) -> dict:
def battery_ratings(controls) -> dict:
"""A mapping's battery's ratings (an installer's mapping, an HA add-on
site: no devices tool's specs): the capacity and power its control's
params state (battery_kwh, battery_kw), never the defaults (a guess is
no rating). The cloud's planners (tenant power, Energy Sharing) read
them."""
kwh = kw = 0.0
for c in {c.id: c for c in controls if c.kind == "battery"}.values():
if "capacity_wh" in c.params:
kwh += float(c.params["capacity_wh"]) / 1000
if "max_charge_w" in c.params or "max_discharge_w" in c.params:
kw += max(float(c.p("max_charge_w")), float(c.p("max_discharge_w"))) / 1000
out = {"battery_kwh": round(kwh, 3)} if kwh > 0 else {}
if out and kw > 0:
out["battery_kw"] = round(kw, 3)
return out
def build(mapping, registries: dict, specs: dict, policy: dict, now: float, ev_phases: dict | None = None) -> dict:
"""ev_phases: per EV control its phase count and source (evphases.py),
told on the device of the control ("1 phase, learned" in the console)."""
ents = {e["entity_id"]: e for e in registries.get("entities") or []}
devs = {d["id"]: d for d in registries.get("devices") or []}
asset_of = {a.entity: a.asset for a in mapping.assets}
@@ -65,6 +85,7 @@ def build(mapping, registries: dict, specs: dict, policy: dict, now: float) -> d
"""The readings that are assumed, not measured (mapping.py)."""
return sorted({a.asset for e in eids for a in of_entity.get(e, []) if getattr(a, "assumed", False)})
control_of = {}
controls = {c.id: c for c in mapping.controls}
for c in mapping.controls:
for e in c.entities.values():
control_of.setdefault(e, c.id)
@@ -102,13 +123,18 @@ def build(mapping, registries: dict, specs: dict, policy: dict, now: float) -> d
continue
d = (ents.get(eid) or {}).get("device_id")
groups.setdefault(d or "", []).append(eid)
for d, eids in sorted(groups.items()):
# a control's ratings once (on its first device); not where the devices tool's spec has it (its own ratings)
rated: set[str] = {c for s in (specs.get("devices") or {}).values() for c in s.get("controls") or []}
for d, eids in sorted(groups.items(), key=lambda kv: (not kv[0], kv[0])):
hd = devs.get(d) or {}
if not d:
continue # entities without a device: nothing to tell
out.append({"id": "ha:" + hashlib.sha256(d.encode()).hexdigest()[:12], "source": "ha",
mine = {control_of[e] for e in eids if e in control_of}
ratings = battery_ratings([controls[i] for i in sorted(mine - rated)])
rated |= mine
if not d and not ratings:
continue # entities without a device: nothing to tell but a battery's ratings
out.append({"id": "ha:" + (hashlib.sha256(d.encode()).hexdigest()[:12] if d else "mapping"), "source": "ha",
**{k: hd.get(k) or "" for k in VERSION_KEYS}, **_eebus(hd),
"category": "", "profile": "", "profile_version": "", "ratings": {},
"category": "", "profile": "", "profile_version": "", "ratings": ratings,
"assets": sorted({asset_of[e] for e in eids if e in asset_of}),
"assumed": assumed_of(eids),
"controls": sorted({control_of[e] for e in eids if e in control_of}),
@@ -121,6 +147,11 @@ def build(mapping, registries: dict, specs: dict, policy: dict, now: float) -> d
if kind == "entity":
t = f"asset:{asset_of[name]}" if name in asset_of else f"entity:{_hash(name)}"
prot.append({"target": t, **{k: e.get(k) for k in ("level", "custodian", "source", "set_by", "role", "at", "why")}})
for d in out:
ph = {c: {"phases": ev_phases[c]["phases"], "source": ev_phases[c]["source"]}
for c in d.get("controls") or [] if c in (ev_phases or {})}
if ph:
d["ev_phases"] = ph
return {"v": 1, "made": int(now), "devices": out, "protection": prot}
@@ -132,6 +163,8 @@ class Inventory:
self.policy = policy or Policy(cfg.state_dir, cfg.site_id, clock)
self.specs_path = os.path.join(cfg.state_dir, "devices-specs.json")
self.sent_digest, self.sent_at = "", 0.0
self.waiting = False # the link was not connected: try again soon (agent.inventory_loop)
self.ev_phases = None # () -> per EV control its phases and source (the controller's; agent.py)
def specs(self) -> dict:
try:
@@ -147,6 +180,12 @@ class Inventory:
pub = getattr(self.sink, "publish", None)
if pub is None or client is None:
return False
# The link drops what comes before its NATS connection is up (at
# start the agent reaches HA first): not sent then, tried again.
cloud = getattr(self.sink, "cloud", None)
self.waiting = cloud is not None and not await cloud()
if self.waiting:
return False
try:
reg = await client.registries()
except Exception as e:
@@ -155,7 +194,8 @@ class Inventory:
now = self.clock()
if self.sent_at and now - self.sent_at < MIN_GAP_S:
return False
inv = build(self.mapping, reg, self.specs(), self.policy.entries(), now)
inv = build(self.mapping, reg, self.specs(), self.policy.entries(), now,
self.ev_phases() if self.ev_phases else None)
body = {k: v for k, v in inv.items() if k != "made"}
digest = hashlib.sha256(json.dumps(body, sort_keys=True).encode()).hexdigest()
if digest == self.sent_digest and now - self.sent_at < RESEND_S:
+31
View File
@@ -127,6 +127,8 @@ class Mapping:
for key, total in (("pv_power", "pv.power"), ("load_power", "load.power")):
if not getattr(self.inputs, key) and total in self.totals:
setattr(self.inputs, key, list(self.totals[total].entities))
if self.inputs.controlled_power is None:
self.inputs.controlled_power = controlled_power(self)
@classmethod
def load(cls, path: str) -> "Mapping":
@@ -334,3 +336,32 @@ def suggest(states: list[dict], registries: dict) -> dict:
out.append({"entity": eid, "asset": asset, "kind": "counter" if counter else "gauge",
"unit": target, "consent": False})
return {"version": 1, "assets": out}
# The metered draw of the devices the plan plans itself, per control kind
# (Finding A2-a): a site's load.power (an inverter's "house load", the sim's)
# is the whole house with them; the plan adds them as decisions, so their
# own meters come off the load (inputs.base_load). Not the battery: a house
# load never includes its charging.
CONTROLLED_POWER = {"heating": ("heatpump.power", "heating.power"), "cooling": ("ac.power", "cooling.power"),
"ev": ("ev.power",)}
def controlled_power(m: "Mapping") -> list[str]:
"""The entities of the planned devices' own power assets: for each kind
of control the mapping has, its assets (or their parts: ev.1.power, ...),
once each (a heat pump that heats and cools is one meter)."""
kinds = {c.kind for c in m.controls}
out = []
for kind, names in CONTROLLED_POWER.items():
if kind not in kinds:
continue
for name in names:
if name in m.by_asset:
ents = [m.by_asset[name].entity]
elif name in m.totals:
ents = list(m.totals[name].entities)
else:
ents = []
out += [e for e in ents if e not in out]
return out
+57
View File
@@ -36,6 +36,44 @@ NAMES = {
"flat_flex": {"en": "Flexibility", "de": "Flexibilität"},
"flat_request": {"en": "Building's request", "de": "Anfrage des Gebäudes"},
"flat_state": {"en": "State", "de": "Zustand"},
# the shadow market (a `market` bundle; announce.py, dashboards.py)
"market_exchange": {"en": "Exchange with {0}", "de": "Austausch mit {0}"},
"market_position": {"en": "Market net position", "de": "Markt: Nettoposition"},
"market_position_of": {"en": "{0}: market net position", "de": "{0}: Markt-Nettoposition"},
"market_price": {"en": "Shared energy price", "de": "Preis geteilter Energie"},
"market_actuator": {"en": "{0}: contract target", "de": "{0}: Vertragsvorgabe"},
"card_market": {"en": "Market (shadow)", "de": "Markt (Schattenbetrieb)"},
"mk_sells": {"en": "Sells to {0}", "de": "Verkauft an {0}"},
"mk_buys": {"en": "Buys from {0}", "de": "Kauft von {0}"},
"mk_flats": {"en": "{0} flats, summed", "de": "{0} Wohnungen, summiert"},
"mk_slot": {"en": "{0} kWh last quarter-hour (contract {1})", "de": "{0} kWh letzte Viertelstunde (Vertrag {1})"},
"mk_slot_open": {"en": "{0} kWh last quarter-hour", "de": "{0} kWh letzte Viertelstunde"},
"mk_no_value": {"en": "no metered value yet", "de": "noch kein Messwert"},
"mk_not_now": {"en": "not in force this quarter-hour", "de": "in dieser Viertelstunde nicht gültig"},
"mk_shadow": {"en": "shadow", "de": "Schattenbetrieb"},
"mk_period": {"en": "So far this {0}: {1} kWh · {2} €", "de": "Bisher in diesem {0}: {1} kWh · {2} €"},
"mk_month": {"en": "month", "de": "Monat"},
"mk_quarter": {"en": "quarter", "de": "Quartal"},
"mk_year": {"en": "year", "de": "Jahr"},
"mk_penalties": {"en": "penalties {0} € paid, {1} € received", "de": "Vertragsstrafen {0} € gezahlt, {1} € erhalten"},
"mk_deviation_self": {"en": "deviation {0} kWh, caused here", "de": "Abweichung {0} kWh, hier verursacht"},
"mk_deviation_other": {"en": "deviation {0} kWh, caused by the other side", "de": "Abweichung {0} kWh, von der Gegenseite verursacht"},
"mk_exempt": {"en": "exempt: {0}", "de": "ausgenommen: {0}"},
"mk_state_on": {"en": "on", "de": "ein"},
"mk_state_off": {"en": "off", "de": "aus"},
"mk_state_hold": {"en": "hold", "de": "halten"},
"mk_target": {"en": "contract", "de": "Vertrag"},
"mk_actual": {"en": "now", "de": "jetzt"},
"mk_unknown": {"en": "unknown", "de": "unbekannt"},
"mk_default": {"en": "falls back to {0} after {1} min", "de": "fällt nach {1} min auf {0} zurück"},
"mk_protection": {"en": "protection: {0}", "de": "Schutz: {0}"},
"mk_net_bought": {"en": "Net bought this month", "de": "Netto gekauft diesen Monat"},
"mk_shadow_note": {"en": "Shadow market: computed and shown only. Nothing is switched by it, no money moves; "
"your devices keep their own settings and protection.",
"de": "Schattenmarkt: nur berechnet und angezeigt. Nichts wird dadurch geschaltet, kein Geld "
"fließt; Ihre Geräte behalten ihre eigenen Einstellungen und ihren Schutz."},
"mk_stale": {"en": "The market's values are out of date (no new values from Cantabile Cloud).",
"de": "Die Marktwerte sind veraltet (keine neuen Werte von der Cantabile Cloud)."},
# dashboards (dashboards.py)
"dash_main": {"en": "Cantabile"},
"dash_climate": {"en": "Heating & cooling", "de": "Heizung & Kühlung"},
@@ -82,6 +120,16 @@ NAMES = {
"from_grid": {"en": "From the grid", "de": "Aus dem Netz"},
"to_grid": {"en": "To the grid", "de": "Ins Netz"},
"heat_pump": {"en": "Heat pump", "de": "Wärmepumpe"},
# heat (heat.power, heat.energy) is thermal: never shown as electricity
"heat": {"en": "Heat (thermal)", "de": "Wärme (thermisch)"},
"heat_building": {"en": "Heat from the building (thermal)", "de": "Wärme aus dem Gebäude (thermisch)"},
"heat_delivered": {"en": "Heat delivered (thermal)", "de": "Gelieferte Wärme (thermisch)"},
"heat_today": {"en": "Heat today (thermal)", "de": "Wärme heute (thermisch)"},
"heat_pump_electric": {"en": "Heat pump, electric", "de": "Wärmepumpe, elektrisch"},
"cop": {"en": "COP (heat ÷ electricity)", "de": "COP (Wärme ÷ Strom)"},
"electric_power": {"en": "Electric power", "de": "Elektrische Leistung"},
"heat_not_electric": {"en": "Heat, not electricity: not part of solar, home or grid above.",
"de": "Wärme, kein Strom: nicht Teil von Solar, Haus oder Netz oben."},
"ev": {"en": "Car", "de": "Auto"},
"circuits": {"en": "Circuits", "de": "Stromkreise"},
"of": {"en": "of", "de": "von"},
@@ -126,6 +174,15 @@ NAMES = {
}
COMMA = ("de", "fr", "it", "es", "nl", "ro", "ru", "pt", "pl", "cs", "da", "sv", "nb", "fi", "tr", "uk")
def number(v: float, language: str | None, digits: int = 1) -> str:
"""A number with the language's decimal mark (1.5 / 1,5)."""
s = f"{v:.{digits}f}"
return s.replace(".", ",") if lang(language) in COMMA else s
def lang(code: str | None) -> str:
"""HA's language ("de", "de-CH", "pt-BR") as our two-letter key."""
return (code or "en").split("-")[0].split("_")[0].lower() or "en"
+188 -20
View File
@@ -14,9 +14,12 @@ Device models (one function each, small and replaceable):
battery state of charge (estimated from params.soc_pct when no sensor),
charge/discharge power, efficiency, never both at once (binary)
heating a room's first-order thermal model (R in K/kW, C in kWh/K, the
cooling outside temperature, heat from the device and from the sun
through the room's covers); comfort target when occupied,
setback/setup otherwise; a switched boiler is on/off (binary)
cooling outside temperature, heat from the device, from the sun
through the room's covers, and the fit's internal gain and sun
(internal_gain_w, solar_m2 x the irradiance; the sun's only
where the room has no cover in the plan, which carries it));
comfort target when occupied, setback/setup otherwise; a
switched boiler is on/off (binary)
ev the energy needed by the departure time, between min and max
current (binary), only while connected
cover the share of the sun's heat into its room (0 closed .. 1 open);
@@ -31,6 +34,16 @@ limit_w from the grid beyond the household's own net load, i.e. the import
at most max(0, load - pv) + limit_w per step, with a penalty far above any
building request (nobody overrides it).
The market's contracts in force (state["market"], from the market bundle;
the market's step 2, minimal, shadow: the plan only, no commands): a contract
the home buys on is an import source at its price, one it sells on an
export sink, through the same meter as the grid (which keeps the tariff's
import and feed-in prices), so the plan exchanges where the contract beats
the grid. Each step uses its own slot's terms (window, amounts, price) when
the bundle carries the coming slots (a contract's "terms", term_at). The
plan's "market" lists per contract the planned kWh and the saving against
the grid (vs_grid_eur).
The building's requests (building.py, state["building"]): a load limit on
the grid import and a thermostat cap are constraints with a penalty (binding
far above any price, flexible with a price), a shift request a soft target
@@ -46,6 +59,9 @@ from zoneinfo import ZoneInfo
import highspy
from highspy.highs import highs_linear_expression as Lin
from .controls import ev_volts
from .evphases import words as phase_words
PENALTY_LIMIT = 5.0 # EUR per unit and step across a floor or limit (K, % of a battery, kWh)
COMFORT_EUR_KH = 2.0 # EUR per K and hour below (heating) or above (cooling) the comfort target
# while someone is home: above any price (0.30 let a room cool by 1.3 K
@@ -56,6 +72,10 @@ CHANGE_EUR_KW = 0.002 # EUR per kW a device's power changes from one step to
# plans (equal prices would otherwise give any order, and a charger or
# boiler must not be switched back and forth)
CLOSED_EUR = 0.00002 # EUR per step for a closed cover: open when nothing is at stake
MARKET_MAX_W = 100000.0 # W: a contract without a maximum, and the meter's import/export switch
GRID_MAX_W = 43470.0 # W: the site's connection when the state does not say (state["grid_max_w"],
# config grid_max_w): a German house connection's usual 63 A main fuse,
# 3 x 230 V x 63 A
class Model:
@@ -89,31 +109,112 @@ class Model:
# -- the house -------------------------------------------------------
def grid(self):
"""Import and export through the meter, each at most the site's
connection (grid_max_w; never below the house's own net load or
surplus, so there is always a plan). The meter either imports or
exports in a step: a binary where the feed-in pays at least the
import price (else importing to export at once would earn money and
the plan would run to the bound). Elsewhere none is needed: doing
both costs more than doing less of each, which keeps the balance and
only loosens every other constraint on the import (all are upper
bounds: the building's, the grid order's); steps with a market
contract have their own switch (market)."""
s = self.s
self.gin, self.gout = [], []
cap = float(s.get("grid_max_w") or GRID_MAX_W)
self.gin, self.gout, self.gcap = [], [], []
for t in range(self.T):
gi = self.var(cost=self.eur_wh(t, s["price_import"][t]))
go = self.var(cost=-self.eur_wh(t, s["price_export"][t]))
net = s["load_w"][t] - s["pv_w"][t]
hi_in, hi_out = max(cap, net), max(cap, -net)
gi = self.var(0.0, hi_in, cost=self.eur_wh(t, s["price_import"][t]))
go = self.var(0.0, hi_out, cost=-self.eur_wh(t, s["price_export"][t]))
spill = self.var(0.0, max(0.0, s["pv_w"][t]))
self.gin.append(gi)
self.gout.append(go)
self.gcap.append((hi_in, hi_out))
self.flows[t] += [(1, gi), (-1, go), (-1, spill)]
def grid_switch(self, skip):
"""The meter's import/export switch where the feed-in pays at least
the import price (grid), outside the steps in skip (market's own)."""
s = self.s
for t in range(self.T):
if t in skip or s["price_export"][t] < s["price_import"][t]:
continue
hi_in, hi_out = self.gcap[t]
imp = self.var(0.0, 1.0, integer=True)
self.h.addConstr(self.gin[t] - hi_in * imp <= 0)
self.h.addConstr(self.gout[t] + hi_out * imp <= hi_out)
def market(self):
"""The market's contracts in force (state["market"], bundles.py
market_for_model; shadow, the market's step 2): a contract the home
buys on is an import source at its price, one it sells on an export
sink, both through the same meter as the grid. Bounds per step: the
best-effort maximum; a firm amount (bought: at least that much; sold:
a shortfall costs the contract's price again, a stand-in for its
penalty); take-or-pay (the minimum is paid whether used or not). The
meter either imports or exports in a step (a binary where a contract
applies), so nothing bought is resold."""
self.buy = [[] for _ in range(self.T)] # contract import vars per step
self.sell = [[] for _ in range(self.T)]
self.contracts = [] # (contract, {step: var}, {step: paid var})
steps = set()
for c in self.s.get("market") or []:
vars_, paid_ = {}, {}
for t in range(max(0, c["from_step"]), min(self.T, c["to_step"])):
k = term_at(c, t) # this step's terms (the coming slots), else the contract's
if k is None:
continue
hi = k["max_w"] if k.get("max_w") else MARKET_MAX_W
eur = self.eur_wh(t, k["price_ct"])
if c["direction"] == "buys":
lo = min(k.get("guaranteed_w") or 0.0, hi)
mwh = k.get("minimum_wh") or 0.0
if mwh > 0: # take-or-pay: what is paid covers at least the minimum
v = self.var(lo, hi)
paid = self.var(mwh / self.dt, cost=eur)
self.h.addConstr(paid - v >= 0)
paid_[t] = paid
else:
v = self.var(lo, hi, cost=eur)
self.buy[t].append(v)
self.flows[t].append((1, v))
else:
v = self.var(0.0, hi, cost=-eur)
g = min(k.get("guaranteed_w") or 0.0, hi)
if g > 0:
short = self.var(cost=eur)
self.h.addConstr(v + short >= g)
self.sell[t].append(v)
self.flows[t].append((-1, v))
vars_[t] = v
steps.add(t)
self.contracts.append((c, vars_, paid_))
for t in steps:
imp = self.var(0.0, 1.0, integer=True)
self.h.addConstr(sum(self.buy[t], start=Lin()) + self.gin[t] - MARKET_MAX_W * imp <= 0)
self.h.addConstr(sum(self.sell[t], start=Lin()) + self.gout[t] + MARKET_MAX_W * imp <= MARKET_MAX_W)
self.grid_switch(steps)
def meter_in(self, t):
"""The meter's import in step t: the grid's and the contracts' bought."""
return self.gin[t] + sum(self.buy[t], start=Lin()) if self.buy[t] else self.gin[t]
def building(self):
"""The building's load limit and shift request (on the grid import)."""
"""The building's load limit and shift request (on the meter's import)."""
req = self.s.get("building") or {}
lim = req.get("load_limit")
if lim:
for t in range(lim["from_step"], lim["to_step"]):
over = self.var(cost=lim["penalty"] * self.dt / 1000.0)
self.h.addConstr(self.gin[t] - over <= lim["value"])
self.h.addConstr(self.meter_in(t) - over <= lim["value"])
sh = req.get("shift_request")
if sh:
a, b = sh["from_step"], sh["to_step"]
base = sum(max(0.0, self.s["load_w"][t] - self.s["pv_w"][t]) * self.dt for t in range(a, b))
target = max(0.0, base - sh["value"] * 1000.0)
miss = self.var(cost=sh["penalty"] / 1000.0)
imp = sum((self.dt * self.gin[t] for t in range(a, b)), start=Lin())
imp = sum((self.dt * self.meter_in(t) for t in range(a, b)), start=Lin())
self.h.addConstr(imp - miss <= target)
def grid_order(self):
@@ -124,7 +225,7 @@ class Model:
for t in range(o["from_step"], min(o["to_step"], self.T)):
base = max(0.0, self.s["load_w"][t] - self.s["pv_w"][t])
over = self.var(cost=o["penalty"] * self.dt / 1000.0)
self.h.addConstr(self.gin[t] - over <= base + o["limit_w"])
self.h.addConstr(self.meter_in(t) - over <= base + o["limit_w"])
def balance(self):
s = self.s
@@ -180,6 +281,9 @@ class Model:
switched = not cooling and "switch" in d["channels"] and "climate" not in d["channels"]
temps, powers, prev = [], [], t0
a = self.dt / (r * c)
internal = self.hourly(p.get("internal_gain_w") or 0.0)
solar = float(p.get("solar_m2") or 0.0)
ghi = self.s.get("ghi_w_m2") or [0.0] * self.T
for t in range(self.T):
if switched:
u = self.var(0.0, 1.0, integer=True)
@@ -190,10 +294,13 @@ class Model:
self.flows[t].append((-1, pw))
tt = self.var(-50.0, 80.0)
sign = -1.0 if cooling else 1.0
gain = sum((g * gx for g, gx in gains(t)), start=Lin())
# T[t+1] = T[t] (1 - a) + a T_out + dt/C (± cop P/1000 + sun gain/1000)
covers = gains(t)
gain = sum((g * gx for g, gx in covers), start=Lin())
# the fit's (B6): its internal gain, and its sun where no cover carries it
fixed = internal[t] + (solar * ghi[t] if not covers and t < len(ghi) else 0.0)
# T[t+1] = T[t] (1 - a) + a T_out + dt/C (± cop P/1000 + sun gain/1000 + fixed gains/1000)
self.h.addConstr(tt - (1 - a) * prev - sign * self.dt / c * cop / 1000.0 * pw
- self.dt / c / 1000.0 * gain == a * self.s["t_out"][t])
- self.dt / c / 1000.0 * gain == a * self.s["t_out"][t] + self.dt / c / 1000.0 * fixed)
occ = self.s["occupied"][t + 1] if t + 1 < self.T else self.s["occupied"][t]
target = min(max(comfort if occ else other, lo), hi)
miss = self.var(cost=COMFORT_EUR_KH * self.dt)
@@ -219,12 +326,21 @@ class Model:
self.devices[d["id"]] = ("cooling" if cooling else "heating",
{"temp": temps, "power": powers, "pmax": pmax, "switched": switched})
def hourly(self, v) -> list[float]:
"""A number, or 24 by local hour, per step."""
if not isinstance(v, list):
return [float(v)] * self.T
zone = ZoneInfo(self.s.get("tz") or "UTC")
start, step = self.s["start"], self.s["step_s"]
return [float(v[datetime.fromtimestamp(start + t * step, timezone.utc).astimezone(zone).hour % len(v)])
for t in range(self.T)]
def ev(self, d):
p, st, b = d["params"], d["state"], d["bounds"]
if not st.get("connected"):
self.devices[d["id"]] = ("ev", None)
return
volts = float(p["voltage"]) * float(p["phases"])
volts = ev_volts(p, "plan")
imax = min(float(p["max_current_a"]), b["current_a"][1])
imin = max(float(p["min_current_a"]), b["current_a"][0])
dep = departure_step(self.s, str(st.get("departure") or p["departure"]))
@@ -279,6 +395,7 @@ class Model:
def solve(self, time_limit: float):
s = self.s
self.grid()
self.market()
covers = [d for d in s["devices"] if d["kind"] == "cover"]
rooms = {d["id"] for d in s["devices"] if d["kind"] in ("heating", "cooling")}
for d in covers:
@@ -349,10 +466,59 @@ def plan(s: dict, time_limit: float = 30.0) -> dict:
devices[d["id"]] = entries
energy = sum(m.eur_wh(t, pi[t]) * x(m.gin[t]) - m.eur_wh(t, s["price_export"][t]) * x(m.gout[t])
for t in range(T))
return {"v": 1, "made": int(time.time()), "start": start, "step_s": step, "status": status,
"solve_s": round(time.time() - t0, 2), "devices": devices, "flex": flexibility(m, x),
"cost_eur": round(energy, 2), "baseline_eur": round(baseline(s, m, x), 2),
"price_note": s.get("price_note", ""), "sources": s.get("sources", {})}
market = market_plan(s, m, x)
energy += sum(c["cost_eur"] for c in market)
out = {"v": 1, "made": int(time.time()), "start": start, "step_s": step, "status": status,
"solve_s": round(time.time() - t0, 2), "devices": devices, "flex": flexibility(m, x),
"cost_eur": round(energy, 2), "baseline_eur": round(baseline(s, m, x), 2),
"price_note": s.get("price_note", ""), "sources": s.get("sources", {})}
if market:
out["market"] = market
return out
def term_at(c: dict, t: int) -> dict | None:
"""A market contract's terms in plan step t: its per-step terms when the
bundle told the coming slots (None: it does not apply then), else the
contract's own (held over from_step .. to_step)."""
per = c.get("terms")
if per is None:
return c
return per[t] if 0 <= t < len(per) else None
def market_plan(s, m, x) -> list[dict]:
"""Per contract in the plan (shadow): the planned energy per step and in
all (kWh), what is paid, and what it saves against the grid: bought,
import price x kWh used - contract price x kWh paid; sold, (contract
price - feed-in) x kWh (EUR; < 0: dearer than the grid). A take-or-pay
contract's paid energy is at least its minimum, used or not (what the
optimisation pays). Fields: id, name, direction, price_ct, steps [{t,
kwh, paid_kwh}] (steps with energy used or paid only), planned_kwh,
paid_kwh, cost_eur (> 0 paid, < 0 received), vs_grid_eur."""
out = []
pi, pe = s["price_import"], s["price_export"]
for c, vars_, paid_ in getattr(m, "contracts", []):
steps, kwh, paid_kwh, cost, vs = [], 0.0, 0.0, 0.0, 0.0
for t, v in sorted(vars_.items()):
e = max(x(v), 0.0) * m.dt / 1000.0
pay = max(x(paid_[t]) * m.dt / 1000.0, e) if t in paid_ else e
if pay < 1e-6:
continue
price = (term_at(c, t) or c)["price_ct"]
steps.append({"t": s["start"] + t * s["step_s"], "kwh": round(e, 4), "paid_kwh": round(pay, 4)})
kwh += e
paid_kwh += pay
if c["direction"] == "buys":
cost += pay * price / 100.0
vs += (e * pi[t] - pay * price) / 100.0
else:
cost -= e * price / 100.0
vs += e * (price - pe[t]) / 100.0
out.append({"id": c["id"], "name": c.get("name", ""), "direction": c["direction"], "price_ct": c["price_ct"],
"steps": steps, "planned_kwh": round(kwh, 4), "paid_kwh": round(paid_kwh, 4),
"cost_eur": round(cost, 4), "vs_grid_eur": round(vs, 4)})
return out
FLEX_STEPS = 96 # 24 h at 15 min: what a building plans with
@@ -432,9 +598,11 @@ def _entry(d, kind, v, t, x, s, pi):
if p is None:
return {"set": "off", "why": "after departure"}
amps = round(x(p) / v["volts"])
ph = ((s.get("sources") or {}).get("ev_phases") or {}).get(d["id"])
ph = f"; {phase_words(ph)}" if ph else ""
if amps < 1:
return {"set": "off", "why": "wait for cheaper hours"}
return {"set": amps, "why": f"cheapest hours before departure ({pi[t]:.1f} ct)"}
return {"set": "off", "why": "wait for cheaper hours" + ph}
return {"set": amps, "why": f"cheapest hours before departure ({pi[t]:.1f} ct){ph}"}
if kind == "cover" and v.get("schedule"):
sch = v["schedule"]
return {"set": sch["set"][t], "why": sch["why"][t] if sch["set"][t] is not None else ""}
+5 -3
View File
@@ -4,8 +4,10 @@ package main
// Data bundles for the agent (data only, never code that runs):
// tariffs (prices for the next 48 h), models (the forecasts' data), a
// building box's flats and the devices' protection (acl: it
// never expires; a newer one replaces it). Fleet
// building box's flats, the devices' protection (acl: it
// never expires; a newer one replaces it) and the shadow market's values
// for the box's own nodes (market, the market's step 1.5: shown in HA, never
// a command). Fleet
// sends one as a signed command, `tariff` or `model`, with args
// {"bundle": base64 of the bundle's JSON bytes, "sig": base64 of their
// signature with the BUNDLE key}. The command itself is checked with the
@@ -84,7 +86,7 @@ func checkBundle(key crypto.PublicKey, kind, site string, args map[string]string
return data, h, nil
}
// bundleCmd: a `tariff`, `model` or `flats` command (already checked as a
// bundleCmd: a `tariff`, `model`, `flats`, `acl` or `market` command (already checked as a
// command); flats: a building's own box gets its flats for Home Assistant.
func (l *link) bundleCmd(c command, now time.Time) (bool, string) {
key, err := l.bundleCert()
+24
View File
@@ -118,6 +118,30 @@ func TestBundles(t *testing.T) {
if ok, _ := l.bundleCmd(cmd("acl", bundleArgs(t, k, acl)), now); ok {
t.Error("another site's acl stored")
}
// the shadow market (step 1.5): for this site only, signed with the bundle key, newest kept
market := func(id string, issued int64, site string) map[string]any {
return map[string]any{"v": 1, "kind": "market", "id": id, "site": site, "issued": issued, "expires": now.Add(6 * time.Hour).Unix(),
"shadow": true, "commands": "none", "contracts": []map[string]any{{"id": "k1", "direction": "sells"}}}
}
if ok, msg := l.bundleCmd(cmd("market", bundleArgs(t, k, market("mk2", now.Unix(), site))), now); !ok {
t.Errorf("market: %s", msg)
}
if b, _ := os.ReadFile(filepath.Join(l.cfg.AgentState, "market.json")); !strings.Contains(string(b), `"id":"mk2"`) {
t.Errorf("market.json: %s", b)
}
for name, c := range map[string]struct {
args map[string]string
want string
}{
"other site": {bundleArgs(t, k, market("mk3", now.Unix()+1, strings.Repeat("cd", 16))), "not for this site"},
"other key": {bundleArgs(t, other, market("mk4", now.Unix()+1, site)), "bad bundle signature"},
"older": {bundleArgs(t, k, market("mk1", now.Add(-time.Hour).Unix(), site)), "older than the one the agent has (mk2)"},
"every site": {bundleArgs(t, k, market("mk5", now.Unix()+1, "*")), "not for this site"},
} {
if ok, msg := l.bundleCmd(cmd("market", c.args), now); ok || !strings.HasSuffix(msg, c.want) {
t.Errorf("market %s: %v %q", name, ok, msg)
}
}
// no certificate on the box: refused, not crashed
l.cfg.BundleCert = filepath.Join(t.TempDir(), "missing.pem")
if ok, msg := l.bundleCmd(cmd("tariff", bundleArgs(t, k, tariff("t7", now.Unix()))), now); ok || !strings.HasPrefix(msg, "no bundle certificate") {
+23
View File
@@ -123,6 +123,27 @@ func setupDone(path string) bool {
return done
}
// boxLocation: the site's country and postcode as the owner gave them in
// the panel's Location (config.json "location"), and when they last
// changed ("at", Unix seconds: fleet keeps the newer of the box's and the
// console's). Never the latitude and longitude. nil without a country.
func boxLocation(path string) map[string]any {
loc, _ := jsonFile(path)["location"].(map[string]any)
country, _ := loc["country"].(string)
if len(country) != 2 {
return nil
}
code, _ := loc["postal_code"].(string)
if len(code) > 16 {
code = code[:16]
}
out := map[string]any{"country": country, "postal_code": code}
if at, ok := loc["at"].(float64); ok && at > 0 {
out["at"] = int64(at)
}
return out
}
// updateState: cantabile-update's state and the release the channel offers
// (update.json in /run). Until its first check after a boot (the timer:
// 2 minutes) there is no file: "not-checked", not null (dev.20's first
@@ -238,6 +259,8 @@ func (l *link) status() map[string]any {
return map[string]any{"v": 1, "version": osVersion(), "link": version, "slot": slot(), "uptime_s": uptime(),
"load": load, "cpu_pct": cpuPct, "ram_pct": ram, "temp_c": temperature(), "cpu_mhz": mhz, "disk": disk,
"wifi": wifiState(), "addons": addons, "channel": ch,
// its interfaces and addresses, an IPv4 default route (netstate.go)
"net": netState(), "route4": defaultRoute4(read("/proc/net/route")),
"update": map[string]any{"state": ust, "available": uav},
// SMART on the box's own disk, as cantabile-status read it (null
// until its first run); fleet's hardware view counts the bad ones.
+21
View File
@@ -23,3 +23,24 @@ func TestSetupDone(t *testing.T) {
t.Fatal("not a bool")
}
}
// The heartbeat's location: country and postcode (never lat/lon), "at".
func TestBoxLocation(t *testing.T) {
p := filepath.Join(t.TempDir(), "config.json")
if boxLocation(p) != nil {
t.Fatal("no file: none")
}
os.WriteFile(p, []byte(`{"location": {"country": "DE", "postal_code": "10115", "latitude": 52.53, "longitude": 13.38, "at": 1761800000}}`), 0o600)
l := boxLocation(p)
if l["country"] != "DE" || l["postal_code"] != "10115" || l["at"] != int64(1761800000) || l["latitude"] != nil || len(l) != 3 {
t.Fatalf("%v", l)
}
os.WriteFile(p, []byte(`{"location": {"country": "AT"}}`), 0o600)
if l := boxLocation(p); l["country"] != "AT" || l["postal_code"] != "" || l["at"] != nil {
t.Fatalf("%v", l)
}
os.WriteFile(p, []byte(`{"location": {"latitude": 1}}`), 0o600)
if boxLocation(p) != nil {
t.Fatal("no country: none")
}
}
+33 -2
View File
@@ -30,6 +30,7 @@ import (
"fmt"
"io"
"log/slog"
"math"
"math/bits"
"math/rand"
"net/http"
@@ -755,11 +756,41 @@ func (l *link) heartbeat() map[string]any {
if p := l.pathInfo(); p != nil {
hb["path"] = p
}
hb["consents"] = consents(env("CANTABILE_CONSENT_FILE",
c := consents(env("CANTABILE_CONSENT_FILE",
map[bool]string{true: "/data/cantabile/edge/consent.json", false: "/data/state/consent.json"}[isHub()]))
hb["consents"] = c
// the site's weather cell (0.1°), for the cloud's weather: only with
// the owner's share consent
if c["share"] == true {
if cell := siteCell(filepath.Join(l.cfg.AgentState, "site_cell.json")); cell != nil {
hb["cell"] = cell
}
// the Hub's country and postcode (the panel's Location; tariffs,
// Mieterstrom and energy sharing go by them): with the same consent
if isHub() {
if loc := boxLocation(boxConfig); loc != nil {
hb["location"] = loc
}
}
}
return hb
}
// siteCell: [lat, lon] rounded to 0.1° from the agent's site_cell.json
// (written from HA's configuration), or nil (none, malformed, out of range).
func siteCell(path string) []float64 {
var c struct{ Lat, Lon *float64 }
b, err := os.ReadFile(path)
if err != nil || json.Unmarshal(b, &c) != nil || c.Lat == nil || c.Lon == nil {
return nil
}
lat, lon := math.Round(*c.Lat*10)/10, math.Round(*c.Lon*10)/10
if math.Abs(lat) > 90 || math.Abs(lon) > 180 {
return nil
}
return []float64{lat, lon}
}
// consents: the owner's consents as fleet keeps them (per
// recipient; nothing goes to a recipient without its yes): share (with
// Cantabile), control, and the recipients granted (building, grid,
@@ -900,7 +931,7 @@ func (l *link) do(c command) (bool, string, func()) {
case "refresh":
l.publish("hb", l.heartbeat())
return true, "heartbeat sent", nil
case "tariff", "model", "flats", "acl": // data bundles: checked here, read by the agent (bundles.go; flats: a building's box; acl: the devices' protection)
case "tariff", "model", "flats", "acl", "market": // data bundles: checked here, read by the agent (bundles.go; flats: a building's box; acl: the devices' protection; market: the shadow market's values)
ok, msg := l.bundleCmd(c, time.Now())
if ok && c.Cmd == "acl" && isHub() {
devicesPolicy() // the policy in force for add-ons and the Modbus bridge (/usr/lib/cantabile/devices)
+34
View File
@@ -253,3 +253,37 @@ func TestConsentsCarrySourceAndVersion(t *testing.T) {
t.Fatalf("no file: %v", c)
}
}
// The site's weather cell (the agent's site_cell.json) goes into the
// heartbeat only with the share consent, rounded to 0.1°, on a Hub too;
// a missing or bad file: no cell.
func TestHeartbeatCell(t *testing.T) {
for _, hub := range []string{"0", "1"} {
t.Setenv("CANTABILE_HUB", hub)
dir := t.TempDir()
cons := filepath.Join(dir, "consent.json")
t.Setenv("CANTABILE_CONSENT_FILE", cons)
l := &link{cfg: config{AgentState: dir}, loc: &local{}}
cell := filepath.Join(dir, "site_cell.json")
os.WriteFile(cell, []byte(`{"lat": 47.0234, "lon": 28.86}`), 0o644)
os.WriteFile(cons, []byte(`{"share": true}`), 0o600)
if c, ok := l.heartbeat()["cell"].([]float64); !ok || len(c) != 2 || c[0] != 47.0 || c[1] != 28.9 {
t.Fatalf("hub %s, shared: %v", hub, l.heartbeat()["cell"])
}
os.WriteFile(cons, []byte(`{"share": false, "control": true}`), 0o600)
if c, ok := l.heartbeat()["cell"]; ok {
t.Fatalf("hub %s, not shared: %v", hub, c)
}
os.WriteFile(cons, []byte(`{"share": true}`), 0o600)
for _, bad := range []string{`not json`, `{"lat": 47.0}`, `{"lat": 95, "lon": 28}`, `{"lat": 47, "lon": -181}`, `{"lat": "47", "lon": 28}`} {
os.WriteFile(cell, []byte(bad), 0o644)
if c, ok := l.heartbeat()["cell"]; ok {
t.Fatalf("hub %s, %s: %v", hub, bad, c)
}
}
os.Remove(cell)
if c, ok := l.heartbeat()["cell"]; ok {
t.Fatalf("hub %s, no file: %v", hub, c)
}
}
}
+86
View File
@@ -0,0 +1,86 @@
// Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
package main
// The box's own network in its status report: a new
// Hub whose Wi-Fi chip was not recognised enrolled over Ethernet, and its
// owner could not find its panel on the LAN. Support sees in the console
// which interfaces the box has, their MAC, link and addresses, and whether
// it has an IPv4 default route ("no IPv4"), next to the hardware report's
// Wi-Fi chip and driver. Box-level operations data (no network names, no
// other devices on the LAN); sent also before the owner claims the box
// (fleet unclaimed.go).
import (
"net"
"net/netip"
"os"
"path/filepath"
"strings"
)
// ifaceInfo: one interface as the kernel reports it.
type ifaceInfo struct {
Name, MAC string
Addrs []string // CIDR
}
// netState: the physical interfaces (the box's own adapters: those with
// /sys/class/net/<if>/device; not containers' or loopback).
func netState() []map[string]any {
ifs, _ := net.Interfaces()
var in []ifaceInfo
for _, i := range ifs {
x := ifaceInfo{Name: i.Name, MAC: i.HardwareAddr.String()}
addrs, _ := i.Addrs()
for _, a := range addrs {
x.Addrs = append(x.Addrs, a.String())
}
in = append(in, x)
}
return netEntries("/sys/class/net", in)
}
// netEntries: the report's entries for these interfaces (sys: the sysfs
// net directory).
func netEntries(sys string, ifs []ifaceInfo) []map[string]any {
out := []map[string]any{}
for _, i := range ifs {
p := filepath.Join(sys, i.Name)
if _, err := os.Stat(p + "/device"); err != nil {
continue
}
kind := "ethernet"
if _, err := os.Stat(p + "/wireless"); err == nil {
kind = "wifi"
}
v4, v6 := []string{}, []string{}
for _, a := range i.Addrs {
pfx, err := netip.ParsePrefix(a)
if err != nil {
continue
}
switch ad := pfx.Addr(); {
case ad.Is4():
v4 = append(v4, pfx.String())
case ad.IsGlobalUnicast(): // global and unique local; not link-local
v6 = append(v6, pfx.String())
}
}
out = append(out, map[string]any{"interface": i.Name, "kind": kind, "mac": i.MAC,
"link": read(p + "/operstate"), "ipv4": v4, "ipv6": v6})
}
return out
}
// defaultRoute4: /proc/net/route has an IPv4 default route (destination
// 00000000) that is up.
func defaultRoute4(table string) bool {
for _, ln := range strings.Split(table, "\n")[1:] {
f := strings.Fields(ln)
if len(f) >= 4 && f[1] == "00000000" {
return true
}
}
return false
}
+42
View File
@@ -0,0 +1,42 @@
// Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
package main
import (
"os"
"path/filepath"
"reflect"
"testing"
)
// TestNetEntries: the box's own adapters only, Wi-Fi told apart, IPv4 and
// routable IPv6 (not link-local).
func TestNetEntries(t *testing.T) {
sys := t.TempDir()
for _, d := range []string{"enp1s0/device", "wlp2s0/device", "wlp2s0/wireless", "veth1234", "lo"} {
os.MkdirAll(filepath.Join(sys, d), 0o755)
}
os.WriteFile(filepath.Join(sys, "enp1s0", "operstate"), []byte("up\n"), 0o644)
got := netEntries(sys, []ifaceInfo{
{Name: "lo", Addrs: []string{"127.0.0.1/8"}},
{Name: "enp1s0", MAC: "00:11:22:33:44:55", Addrs: []string{"192.0.2.23/24", "fe80::1/64", "2001:db8::23/64"}},
{Name: "wlp2s0", MAC: "66:77:88:99:aa:bb"},
{Name: "veth1234", Addrs: []string{"198.51.100.1/16"}},
})
want := []map[string]any{
{"interface": "enp1s0", "kind": "ethernet", "mac": "00:11:22:33:44:55", "link": "up",
"ipv4": []string{"192.0.2.23/24"}, "ipv6": []string{"2001:db8::23/64"}},
{"interface": "wlp2s0", "kind": "wifi", "mac": "66:77:88:99:aa:bb", "link": "", "ipv4": []string{}, "ipv6": []string{}},
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %v", got)
}
table := "Iface\tDestination\tGateway \tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU\tWindow\tIRTT\n" +
"enp1s0\t0001A8C0\t00000000\t0001\t0\t0\t100\t00FFFFFF\t0\t0\t0\n"
if defaultRoute4(table) {
t.Error("a LAN route is no default route")
}
if !defaultRoute4(table+"enp1s0\t00000000\t0101A8C0\t0003\t0\t0\t100\t00000000\t0\t0\t0\n") || defaultRoute4("") {
t.Error("default route")
}
}
+25
View File
@@ -197,3 +197,28 @@ def test_restart_for_a_devices_consent(tmp_path, monkeypatch):
assert a.exit_code == agent_mod.RESTART and not a.stop.is_set() # the page answers first
await asyncio.wait_for(a.stop.wait(), 1)
asyncio.run(go())
def test_a_site_without_controls_still_tells_the_cloud_of_a_grid_order(tmp_path):
"""No controls, nothing to limit: the grid operator's order (EEBUS LPC)
is still checked and told to the cloud (fleet's ledger exempts the slot)."""
import time
from test_gridorder import message
class Sink(FlakySink):
def __init__(self):
super().__init__()
self.published = []
async def publish(self, kind, data):
self.published.append((kind, data))
sink = Sink()
a = make_agent(tmp_path, sink)
assert a.control is None
now = int(time.time())
assert asyncio.run(a.grid_order(message(**{"from": now, "until": now + 900}).encode()))
assert [(k, d["service"], d["value"]) for k, d in sink.published] == [("action", "grid.load_limit", "4200.0")]
asyncio.run(a.grid_report()) # the same quarter-hour: told already
assert len(sink.published) == 1
# a forged one changes nothing
assert not asyncio.run(a.grid_order(b'{"order": "{}", "sig": "", "key": ""}'))
+185
View File
@@ -0,0 +1,185 @@
# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
"""The plan's base load (Finding A2-a) and a room's fitted gains (Finding
B6-a), from the mapping and the model bundle through the model state into
the plan."""
import json
import pytest
import yaml
from cantabile_edge.bundles import Bundles
from cantabile_edge.controls import Control
from cantabile_edge.inputs import Collector, base_load, model_state
from cantabile_edge.mapping import Mapping
from test_control_parts import SITE
START = 1_800_000_000 // 3600 * 3600 # a Friday, on the hour (UTC)
def gauge(entity, asset):
return {"entity": entity, "asset": asset, "kind": "gauge", "unit": "W", "consent": True}
def heater(cid="living"):
return {"id": cid, "kind": "heating", "consent": True,
"entities": {"climate": f"climate.{cid}", "temp": f"sensor.{cid}_temp"}}
def site_mapping(tmp_path, assets, controls, inputs=None):
m = {"version": 3, "assets": [gauge("sensor.load", "load.power")] + assets,
"inputs": dict({"load_power": "sensor.load"}, **(inputs or {})), "controls": controls}
p = tmp_path / "mapping.yaml"
p.write_text(yaml.safe_dump(m))
return Mapping.load(str(p))
def watts(col, **vals):
col.on_entities({"a": {e.replace("__", "."): {"s": str(v), "a": {"unit_of_measurement": "W"}}
for e, v in vals.items()}})
# -- which meters come off the load -------------------------------------------------
def test_mapping_names_the_planned_devices_meters(tmp_path):
ev = {"id": "car", "kind": "ev", "consent": True, "entities": {"switch": "switch.ev"}}
m = site_mapping(tmp_path, [gauge("sensor.hp", "heatpump.power"), gauge("sensor.ac", "ac.power"),
gauge("sensor.wb1", "ev.1.power"), gauge("sensor.wb2", "ev.2.power")],
[heater(), ev])
# the heat pump and both chargers; not the A/C: no cooling control plans it, it stays in the base
assert m.inputs.controlled_power == ["sensor.hp", "sensor.wb1", "sensor.wb2"]
assert {"sensor.hp", "sensor.wb1", "sensor.wb2"} <= set(m.inputs.entities()) # watched
# the mapping's own list wins (an empty one: the load has none of them in it)
m = site_mapping(tmp_path, [gauge("sensor.hp", "heatpump.power")], [heater()], {"controlled_power": []})
assert m.inputs.controlled_power == []
m = site_mapping(tmp_path, [], [heater()], {"controlled_power": "sensor.heater_plug"})
assert m.inputs.controlled_power == ["sensor.heater_plug"]
def test_base_load_is_the_house_without_them(tmp_path):
m = site_mapping(tmp_path, [gauge("sensor.hp", "heatpump.power")], [heater()])
col = Collector()
assert base_load(col, m.inputs) is None # no load yet
watts(col, sensor__load=6400)
assert base_load(col, m.inputs) == 6400.0 # the heat pump's meter not there yet: 0
watts(col, sensor__hp=4000)
assert base_load(col, m.inputs) == 2400.0
col.on_entities({"a": {"sensor.hp": {"s": "4.5", "a": {"unit_of_measurement": "kW"}}}})
assert base_load(col, m.inputs) == 1900.0 # kW read as W
watts(col, sensor__hp=7000)
assert base_load(col, m.inputs) == 0.0 # never below 0 (meters out of step)
def test_model_state_plans_on_the_base_load(tmp_path):
"""The measured case of A2-a: the house meters 6400 W with its 4 kW
heater running; the plan's load is 2400 W, so the heater at 4 kW makes
6400 W of import, not 10 400 W; the grid order's base is 2400 W too."""
pytest.importorskip("highspy")
from cantabile_edge import optimizer
m = site_mapping(tmp_path, [gauge("sensor.hp", "heatpump.power")], [heater()])
col = Collector()
watts(col, sensor__load=6400, sensor__hp=4000, sensor__living_temp=15)
ctl = [Control("living", "heating", {"climate": "climate.living", "temp": "sensor.living_temp"}, True,
{"max_power_w": 4000.0, "comfort_c": 22.0, "setback_c": 22.0})]
b = Bundles(str(tmp_path / "none"), SITE)
s = model_state(START, ctl, m.inputs, col, b, None, "UTC", None)
assert s["load_w"] == [2400.0] * len(s["load_w"]) and s["sources"]["load"] == "now"
s["steps"] = 8
for k in ("load_w", "pv_w", "t_out", "sun", "occupied", "sky", "price_import", "price_export", "ghi_w_m2"):
s[k] = s[k][:8]
s["t_out"] = [-5.0] * 8
s["grid_order"] = {"limit_w": 4200.0, "from_step": 0, "to_step": 8, "penalty": 500.0}
mod = optimizer.Model(s)
mod.solve(10)
val = mod.h.getSolution().col_value
gin = val[mod.gin[0].index]
assert gin == pytest.approx(2400.0 + 4000.0, abs=1.0) # cold room: the heater full on
assert gin <= 2400.0 + 4200.0 + 1e-6 # the order's base is the base load
def model(**forecasts):
return {"v": 1, "kind": "model", "id": "m1", "site": SITE, "issued": 1,
"forecasts": {k: {"profile": "hour_of_week", "values": [v] * 168} for k, v in forecasts.items()}}
def test_which_model_series_is_the_base(tmp_path):
(tmp_path / "model.json").write_text(json.dumps(model(load_w=6000.0)))
b = Bundles(str(tmp_path), SITE)
col = Collector()
watts(col, sensor__load=6400, sensor__hp=4000)
with_hp = site_mapping(tmp_path, [gauge("sensor.hp", "heatpump.power")], [heater()])
s = model_state(START, [], with_hp.inputs, col, b, None, "UTC", None)
# the model's load_w is load.power with the heat pump in it: not the plan's load here
assert s["load_w"][0] == 2400.0 and "not used" in s["sources"]["load"]
without = site_mapping(tmp_path, [], [heater()]) # no meter of a planned device in the load
s = model_state(START, [], without.inputs, col, b, None, "UTC", None)
assert s["load_w"][0] == 6000.0 and s["sources"]["load"] == "model"
(tmp_path / "model.json").write_text(json.dumps(model(load_w=6000.0, base_load_w=1800.0)))
s = model_state(START, [], with_hp.inputs, col, Bundles(str(tmp_path), SITE), None, "UTC", None)
assert s["load_w"][0] == 1800.0 and s["sources"]["load"] == "model (base_load_w)"
# -- a room's fitted gains (B6-a) --------------------------------------------------
def room_model(tmp_path, **room):
m = model(load_w=500.0)
m["rooms"] = {"living": dict({"r_k_per_kw": 6.0, "c_kwh_per_k": 5.0}, **room)}
(tmp_path / "model.json").write_text(json.dumps(m))
return Bundles(str(tmp_path), SITE)
def test_bundle_carries_the_gains(tmp_path):
b = room_model(tmp_path, gain_w=300.0, solar_m2=4.0)
rc = b.room_rc("living")
assert (rc["gain_w"], rc["solar_m2"]) == (300.0, 4.0)
assert room_model(tmp_path, gain_w=[100.0] * 24).room_rc("living")["gain_w"] == [100.0] * 24
assert room_model(tmp_path, solar_m2=-3.0).room_rc("living")["solar_m2"] == 0.0 # a fit's artefact: cut
assert room_model(tmp_path).room_rc("living")["gain_w"] == 0.0 # an older bundle: none
bad = room_model(tmp_path, gain_w=[1.0] * 5)
assert bad.room_rc("living") is None and "gain_w" in bad.model.error
# into the model state: an electric fit's heat is cop times, like C
hp = Control("living", "heating", {"climate": "climate.living"}, True, {"cop": 3.0})
b = room_model(tmp_path, gain_w=100.0, solar_m2=2.0, power="electric")
s = model_state(START, [hp], site_mapping(tmp_path, [], []).inputs, Collector(), b, None, "UTC", None,
location={"latitude": 52.5, "longitude": 13.4})
p = s["devices"][0]["params"]
assert (p["internal_gain_w"], p["solar_m2"]) == (300.0, 6.0) and s["sources"]["rooms"]["living"] == "model"
# the irradiance for the sun: by day only (Berlin: 11:00 UTC near noon, 00:00 UTC night)
ghi = s["ghi_w_m2"]
noon = [ghi[k] for k in range(len(ghi)) if (START + k * 900) % 86400 == 11 * 3600]
night = [ghi[k] for k in range(len(ghi)) if (START + k * 900) % 86400 == 0]
assert min(noon) > 100 and max(night) == 0.0 and s["sources"]["ghi"] == "clear sky x the sky"
def test_planner_uses_the_gains():
"""With the fit's gains the room needs less heating, and with the
heating off it follows the fit's equation."""
pytest.importorskip("highspy")
from cantabile_edge import optimizer
from test_optimizer import heating, house
def heat_kwh(**gains):
d = heating(temp=21.0)
d["params"].update(gains)
s = house([d], steps=96)
s["ghi_w_m2"] = [500.0] * 96
s["occupied"], s["t_out"] = [1] * 96, [-10.0] * 96 # the heater always on, part load
m = optimizer.Model(s)
m.solve(20)
val = m.h.getSolution().col_value
return sum(val[v.index] for v in m.devices["heating"][1]["power"]) * 0.25 / 1000, m, val
none, _, _ = heat_kwh()
inner, _, _ = heat_kwh(internal_gain_w=500.0)
sun, _, _ = heat_kwh(solar_m2=1.0)
assert inner == pytest.approx(none - 0.5 * 24, abs=1.0) # 500 W for 24 h less to buy
assert sun == pytest.approx(none - 0.5 * 24, abs=1.0) # 1 m² x 500 W/m²
# the room's step with the heater off: (1 - a) T + a T_out + dt/C x gains
d = heating(temp=21.0, power=0)
d["params"].update(internal_gain_w=[0.0] * 12 + [1000.0] * 12) # by local hour
s = house([d], steps=8)
m = optimizer.Model(s)
m.solve(10)
val = m.h.getSolution().col_value
temps = [val[v.index] for v in m.devices["heating"][1]["temp"]]
a = 0.25 / 30
# START 00:00 UTC = 01:00 Berlin: hours 0..11 local, no gain
assert temps[0] == pytest.approx(21.0 * (1 - a) + a * 5.0, abs=1e-6)
assert m.hourly(list(range(24)))[:5] == [1.0, 1.0, 1.0, 1.0, 2.0] # 24 values: by the local hour
+21
View File
@@ -132,6 +132,27 @@ def test_tariff_and_fallbacks(tmp_path):
assert b.prices(start, 900, 1, start).note.startswith("no tariff")
def test_feed_in_at_zero_is_said(tmp_path):
"""Finding A1-a: where no export price is known the feed-in is 0 ct, and
the plan's note says so (no tariff, expired, no or a short export series)."""
b = Bundles(str(tmp_path), SITE, default_price_ct=33)
start = 1_800_000_000 // 900 * 900
p = b.prices(start, 900, 4, start)
assert p.export_ct == [0.0] * 4 and "feed-in at 0 ct/kWh" in p.note
t = {"v": 1, "kind": "tariff", "id": "t1", "site": SITE, "issued": start, "start": start, "step_s": 900,
"steps": 4, "expires": start + 7200, "currency": "EUR", "unit": "ct/kWh",
"import": [10, 10, 10, 10], "export": [5, 5, 5, 5]}
(tmp_path / "tariff.json").write_text(json.dumps(t))
assert b.prices(start, 900, 4, start).note == "tariff t1" # all priced: nothing to say
assert "feed-in at 0 ct/kWh" in b.prices(start, 900, 2, start + 7200).note # expired
t["export"] = [5, None, None, None]
(tmp_path / "tariff.json").write_text(json.dumps(t))
assert "feed-in at 0 ct/kWh in 3 of 4 steps" in b.prices(start, 900, 4, start).note
del t["export"]
(tmp_path / "tariff.json").write_text(json.dumps(t))
assert b.prices(start, 900, 4, start).note == "tariff t1; feed-in at 0 ct/kWh"
def test_model_bundle(tmp_path):
b = Bundles(str(tmp_path), SITE)
m = {"v": 1, "kind": "model", "id": "m1", "site": "*", "issued": 1,
+74
View File
@@ -0,0 +1,74 @@
# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
"""Heat is thermal (dashboards.py): a flat on district heat whose heat meter
shows 2200 W while its electricity is PV 37 W + grid 68 W = home 105 W. The
heat shows as heat from the building (thermal) in kW, in a band of its own
below the electricity, never in the electricity tiles or summed with them;
with a heat pump of the site, its electric power and the COP beside it."""
import json
import yaml
from cantabile_edge import dashboards as db
from cantabile_edge.mapping import Mapping
def st(eid, state, **attrs):
return {"entity_id": eid, "state": state, "attributes": attrs}
def home(tmp_path, heat_pump=False, lang="en"):
assets = [{"entity": "sensor.pv", "asset": "pv.power", "kind": "gauge", "unit": "W", "consent": True},
{"entity": "sensor.grid", "asset": "grid.power", "kind": "gauge", "unit": "W", "consent": True},
{"entity": "sensor.load", "asset": "load.power", "kind": "gauge", "unit": "W", "consent": True},
{"entity": "sensor.heat_power", "asset": "heat.power", "kind": "gauge", "unit": "W", "consent": True},
{"entity": "sensor.heat_energy", "asset": "heat.energy", "kind": "counter", "unit": "Wh", "consent": True}]
states = [st("sensor.pv", "37", unit_of_measurement="W", device_class="power"),
st("sensor.grid", "68", unit_of_measurement="W", device_class="power"),
st("sensor.load", "105", unit_of_measurement="W", device_class="power"),
st("sensor.heat_power", "2200", unit_of_measurement="W", device_class="power", friendly_name="Heat meter Heat power"),
st("sensor.heat_energy", "5120", unit_of_measurement="kWh", device_class="energy")]
if heat_pump:
assets.append({"entity": "sensor.hp", "asset": "heatpump.power", "kind": "gauge", "unit": "W", "consent": True})
states.append(st("sensor.hp", "550", unit_of_measurement="W", device_class="power"))
p = tmp_path / "m.yaml"
p.write_text(yaml.safe_dump({"version": 3, "assets": assets}))
reg = {"devices": [{"id": "hm", "name": "Heat meter", "manufacturer": "Sim", "model": "Heat meter"}],
"entities": [{"entity_id": e, "device_id": "hm", "platform": "mqtt", "unique_id": e}
for e in ("sensor.heat_power", "sensor.heat_energy")], "areas": []}
return db.Home(Mapping.load(str(p)), "cantabile_x", {}, [], lang, reg=reg, states=states)
def test_heat_is_thermal_and_apart(tmp_path):
h = home(tmp_path)
now = db.energy_now(h)
assert "sensor.heat_power" not in json.dumps(now) # not with solar, home, grid
band = db.heat_band(h)
text = json.dumps(band, ensure_ascii=False)
assert "Heat from the building (thermal)" in text and " kW" in text and "Heat today (thermal)" in text
assert "not part of solar, home or grid" in text and "COP" not in text
assert not any(c.get("type") == "tile" and c.get("entity") == "sensor.heat_power" for c in band)
ov = json.dumps(db.view_overview(h), ensure_ascii=False)
assert ov.index("Heat (thermal)") > ov.index("sensor.load") # its own band, below the electricity
# the heat meter's card: heat in kW, never a W tile
card = db.device_card(h, h.devices[0])
assert card and not any(c.get("entity") == "sensor.heat_power" for c in card["cards"])
assert "Heat from the building (thermal)" in json.dumps(card, ensure_ascii=False)
# the climate view: never electric and thermal in one graph
for sec in db.view_climate(h)["sections"]:
for c in sec["cards"]:
if c.get("type") == "history-graph":
assert not ({"sensor.heat_power"} & set(c["entities"]) and {"sensor.hp"} & set(c["entities"]))
def test_heat_pump_heat_and_cop(tmp_path):
h = home(tmp_path, heat_pump=True)
text = json.dumps(db.heat_band(h), ensure_ascii=False)
assert "Heat delivered (thermal)" in text and "Heat pump, electric" in text and "COP (heat ÷ electricity)" in text
graphs = [c for s in db.view_climate(h)["sections"] for c in s["cards"] if c.get("type") == "history-graph"]
assert ["sensor.hp"] in [g["entities"] for g in graphs] and ["sensor.heat_power"] in [g["entities"] for g in graphs]
def test_heat_in_german(tmp_path):
text = json.dumps(db.heat_band(home(tmp_path, lang="de")), ensure_ascii=False)
assert "Wärme aus dem Gebäude (thermisch)" in text and "Wärme heute (thermisch)" in text
assert "replace('.', ',')" in text
+1 -1
View File
@@ -203,7 +203,7 @@ def test_flat_with_only_a_meter():
assert "sensor.meter_rssi" not in str(v[sub]) # not diagnostics
assert h.view(r.devices[0]).profile == "shelly-pro-3em"
assert "Grid" in str(b["now"])
assert energy_prefs(h)["energy_sources"][0]["flow_from"][0]["stat_energy_from"] == "sensor.meter_energy"
assert energy_prefs(h)["energy_sources"][0]["stat_energy_from"] == "sensor.meter_energy"
def villa() -> Reg:
+70 -6
View File
@@ -10,7 +10,7 @@ import yaml
from cantabile_edge import agent as agent_mod
from cantabile_edge.config import Config
from cantabile_edge.dashboards import dashboard, energy_prefs
from cantabile_edge.dashboards import dashboard, energy_prefs, legacy_grid
from cantabile_edge.ha import HAClient, HAError
from cantabile_edge.mapping import Mapping
from cantabile_edge.store import Queue
@@ -30,8 +30,10 @@ ASSETS = [
class FakeHA(HAClient):
"""HAClient with HA's answers in memory (its own checks stay)."""
def __init__(self, prefs=None, boards_=None, devices=(), entities=(), areas=(), states=None, panels=("app",)):
def __init__(self, prefs=None, boards_=None, devices=(), entities=(), areas=(), states=None, panels=("app",),
grid_form="unified"):
super().__init__("ws://fake", "t")
self.grid_form = grid_form
self.prefs, self.boards, self.sent, self.configs = prefs, boards_ or [], [], {}
self.devices, self.entities, self.areas, self.states = list(devices), list(entities), list(areas), states
self.panels = {p: {} for p in panels}
@@ -55,6 +57,13 @@ class FakeHA(HAClient):
raise HAError("not_found")
return self.prefs
if type_ == "energy/save_prefs":
grids = [s for s in f.get("energy_sources", []) if s["type"] == "grid"]
# HA's check of the grid's form: 2026.10 (energy storage 1.3) a
# source per connection, earlier one with flow_from / flow_to
wrong = "stat_energy_from" if self.grid_form == "legacy" else "flow_from"
if any(wrong in g for g in grids) or (self.grid_form == "legacy" and len(grids) > 1):
raise HAError(str({"code": "invalid_format",
"message": f"not a valid option at 'energy_sources[0].{wrong}'"}))
self.prefs = f
return f
if type_ == "lovelace/dashboards/list":
@@ -121,10 +130,13 @@ def test_one_dashboard_with_views_from_what_the_home_has(tmp_path):
assert all(len(s["cards"]) > 1 or s["cards"][0]["type"] != "heading" for s in view["sections"])
assert {s["column_span"] for s in v["overview"]["sections"]} == {2} # bands: the whole width
# HA's Energy: grid with our price sensors
grid = ha.prefs["energy_sources"][0]
assert grid["flow_from"][0] == {"stat_energy_from": "sensor.grid_in", "stat_cost": None,
"number_energy_price": None, "entity_energy_price": "sensor.c_price_import_now"}
assert grid["flow_to"][0]["entity_energy_price"] == "sensor.c_price_export_now"
assert ha.prefs["energy_sources"] == [{
"type": "grid", "cost_adjustment_day": 0,
"stat_energy_from": "sensor.grid_in", "stat_cost": None, "number_energy_price": None,
"entity_energy_price": "sensor.c_price_import_now",
"stat_energy_to": "sensor.grid_out", "stat_compensation": None, "number_energy_price_export": None,
"entity_energy_price_export": "sensor.c_price_export_now"}]
assert len([s for s in ha.sent if s[0] == "energy/save_prefs"]) == 1
# nothing changed: nothing saved again
n = len([s for s in ha.sent if s[0] == "lovelace/config/save"])
asyncio.run(a.dashboards.ensure(ha))
@@ -263,6 +275,58 @@ def test_owner_energy_prefs_kept_and_other_boards_untouched(tmp_path):
assert {"x", "inv"} <= ids and "old" not in ids # our retired one removed, others kept
def test_energy_prefs_in_the_older_grid_form_for_an_older_ha(tmp_path):
"""HA before energy storage 1.3 refuses the unified grid: the agent
saves the same preferences with flow_from / flow_to instead."""
a = make(tmp_path)
ha = FakeHA(grid_form="legacy")
asyncio.run(a.dashboards.ensure(ha))
assert [s[0] for s in ha.sent].count("energy/save_prefs") == 2
assert ha.prefs["energy_sources"] == [{
"type": "grid", "cost_adjustment_day": 0,
"flow_from": [{"stat_energy_from": "sensor.grid_in", "stat_cost": None, "number_energy_price": None,
"entity_energy_price": "sensor.c_price_import_now"}],
"flow_to": [{"stat_energy_to": "sensor.grid_out", "stat_compensation": None, "number_energy_price": None,
"entity_energy_price": "sensor.c_price_export_now"}]}]
asyncio.run(a.dashboards.ensure(ha)) # saved: not again
assert [s[0] for s in ha.sent].count("energy/save_prefs") == 2
def test_energy_prefs_two_grid_meters_a_source_each_and_one_legacy_source():
want = {"energy_sources": [
{"type": "grid", "cost_adjustment_day": 0, "stat_energy_from": "sensor.a_in", "stat_cost": None,
"number_energy_price": None, "entity_energy_price": "sensor.p_in", "stat_energy_to": "sensor.a_out",
"stat_compensation": None, "number_energy_price_export": None, "entity_energy_price_export": "sensor.p_out"},
{"type": "grid", "cost_adjustment_day": 0, "stat_energy_from": "sensor.b_in", "stat_cost": None,
"number_energy_price": None, "entity_energy_price": "sensor.p_in", "stat_energy_to": None,
"stat_compensation": None, "number_energy_price_export": None, "entity_energy_price_export": None},
{"type": "solar", "stat_energy_from": "sensor.pv", "config_entry_solar_forecast": None}],
"device_consumption": []}
old = legacy_grid(want)
assert [s["type"] for s in old["energy_sources"]] == ["grid", "solar"]
g = old["energy_sources"][0]
assert [x["stat_energy_from"] for x in g["flow_from"]] == ["sensor.a_in", "sensor.b_in"]
assert g["flow_to"] == [{"stat_energy_to": "sensor.a_out", "stat_compensation": None,
"number_energy_price": None, "entity_energy_price": "sensor.p_out"}]
no_grid = {"energy_sources": want["energy_sources"][2:], "device_consumption": []}
assert legacy_grid(no_grid) is no_grid
def test_energy_prefs_other_errors_not_retried(tmp_path):
a = make(tmp_path)
ha = FakeHA()
async def refuse(type_, timeout=60, _events=None, **f):
if type_ == "energy/save_prefs":
ha.sent.append((type_, f))
raise HAError("unauthorized")
return await FakeHA._send(ha, type_, timeout, _events, **f)
ha._send = refuse
with pytest.raises(HAError):
asyncio.run(a.dashboards._energy(ha, a.dashboards.home({})))
assert [s[0] for s in ha.sent].count("energy/save_prefs") == 1
def test_client_refuses_other_dashboards():
ha = FakeHA()
with pytest.raises(HAError):
+20 -14
View File
@@ -111,20 +111,22 @@ class Site:
def test_page_shows_ha_devices_with_their_profile_and_what_they_would_give(tmp_path):
s = Site(tmp_path)
out = s.req()
assert "<nav>" in out and "?p=building" in out and "Devices</h2>" in out
assert "<nav>" in out and "?p=building" in out and "Devices<details" in out
assert out.count("Use with Cantabile</button>") == 2 # the inverter and the A/C
assert "Cantabile would read: pv: power" in out and "battery: soc" in out
assert "Cantabile would read: solar power" in out and "battery charge" in out
assert "It could control: heating, cooling." in out and "generic profile" in out
assert "room: living room.temp" in out
assert "room temperature (Living room), setpoint, heating/cooling state" in out # words, never asset ids
assert "room: " not in out and "Devices in Home Assistant you can add" in out
assert "Proposed by Cantabile (not checked yet)" in out # the install's proposal
assert "1 more in Home Assistant without a profile" in out and "Lamp" in out
assert "Used with Cantabile" not in out
assert "in use with Cantabile" not in out
def test_use_with_cantabile_writes_the_fragment_consent_off(tmp_path):
s = Site(tmp_path)
out = s.post(do="use", ha_device=AC, profile="ha-thermostat", name="Living room A/C")
assert "is used with Cantabile now" in out and s.changes == 1
assert "Reading and control" in out and "‹ Devices" in out # its own page
dev = next(iter(s.devices().values()))
assert dev["ha_device"] == AC and dev["profile"] == "ha-thermostat" and not dev["read"] and not dev["control"]
frag = s.frag(dev["id"])
@@ -140,11 +142,14 @@ def test_use_with_cantabile_writes_the_fragment_consent_off(tmp_path):
assert {t for t, e in prot.items() if e["level"] == "protected"} == {f"control:{c['id']}" for c in frag["controls"]}
specs = json.loads((s.state / "devices-specs.json").read_text())["devices"]
assert specs[dev["id"]]["profile"] == "ha-thermostat" and specs[dev["id"]]["ha_device"] == AC
# the page: used, reading off, no second offer for it
# the list: in use, reading off, in words, no second offer for it
out = s.req()
assert "Used with Cantabile" in out and "first give your consent in the add-on&#x27;s configuration" in out
assert 'name="read"' not in out
assert "1 device in use with Cantabile" in out and "Added · reading off · control off" in out
assert f'href="?p=devices&amp;d={dev["id"]}"' in out and "21.5 °C, set to 22 °C, idle" in out
assert out.count("Use with Cantabile</button>") == 1
out = s.req(query=f"p=devices&d={dev['id']}")
assert "first give your consent in the add-on&#x27;s configuration" in out and 'name="read"' not in out
assert '<span class="badge quiet">untested</span>' in out
assert "This device is used with Cantabile already." in s.post(do="use", ha_device=AC, profile="ha-thermostat")
assert len(s.devices()) == 1
# the agent loads it: values and both controls, nothing consented
@@ -182,10 +187,10 @@ def test_a_hand_written_mapping_wins_and_one_writer_per_entity(tmp_path):
"controls": [{"id": "heating", "kind": "heating", "consent": False, "entities": {"climate": "climate.living_ac"}}]}
s = Site(tmp_path, mapping=hand)
out = s.req()
assert "In your own mapping: room: living.temp. Those stay as they are." in out
assert "In your own mapping: room temperature (Living). Those stay as they are." in out
# the climate entity is the hand mapping's (read and written there):
# nothing is left for the thermostat's fragment, and the page says why
assert "Read by your own mapping already, so left out here: room: living room.temp" in out
assert "Read by your own mapping already, so left out here: room temperature (Living room)" in out
assert "Another control writes to this device already, so Cantabile leaves out: heating, cooling." in out
out = s.post(do="use", ha_device=AC, profile="ha-thermostat")
assert "or your mapping has them all already" in out and not (tmp_path / "state" / "devices.json").exists()
@@ -207,12 +212,12 @@ def test_reading_and_control_need_the_site_consent(tmp_path):
assert not any(a["consent"] for a in s.frag(dev_id)["assets"]) and s.restarts == 0
# with it: the switches, as on the Hub's Devices page
s.give_consent(share=True, control=False)
out = s.req()
out = s.req(query=f"p=devices&d={dev_id}")
assert 'name="read"' in out and 'name="control"' in out
assert "Cantabile may control it (heating, cooling), within your limits" in out
assert "Control also needs your consent to control devices" in out
out = s.post(do="consent", id=dev_id, read="1")
assert "Cantabile starts again" in out and s.restarts == 1
assert "Cantabile starts again" in out and s.restarts == 1 and "In use · reading on · control off" in out
assert s.devices()[dev_id]["read"] and not s.devices()[dev_id]["control"]
frag = s.frag(dev_id)
assert all(a["consent"] for a in frag["assets"]) and not any(c["consent"] for c in frag["controls"])
@@ -232,7 +237,7 @@ def test_protection_and_heating_or_cooling(tmp_path):
s.post(do="use", ha_device=AC, profile="ha-thermostat")
dev_id = next(iter(s.devices()))
heat = next(c["id"] for c in s.frag(dev_id)["controls"] if c["kind"] == "heating")
out = s.req()
out = s.req(query=f"p=devices&d={dev_id}")
assert "Allow switching off" in out
out = s.req("POST", "p=devices", {"do": "protect", "id": dev_id, "target": f"control:{heat}", "level": "none"},
hdr={"x-remote-user-display-name": "Ana"})
@@ -242,7 +247,7 @@ def test_protection_and_heating_or_cooling(tmp_path):
# a seal from the cloud: shown locked, nothing to lift, not removable
(s.state / "acl.json").write_text(json.dumps({"v": 1, "kind": "acl", "site": "ab" * 16, "entries": [
{"target": f"device:{dev_id}", "level": "sealed", "custodian": "grid_operator", "at": 1}]}))
out = s.req()
out = s.req(query=f"p=devices&d={dev_id}")
assert "Sealed" in out and "only the grid operator can change it" in out
assert "only they can change it" in s.post(do="protect", id=dev_id, target=f"device:{dev_id}", level="none")
assert "Sealed: only the grid operator can remove it." in s.post(do="remove", id=dev_id)
@@ -291,7 +296,8 @@ def test_german(tmp_path):
return s.req()
import contextvars
out = contextvars.copy_context().run(run)
assert '<html lang="de">' in out and "Geräte</h2>" in out and "Mit Cantabile nutzen" in out
assert '<html lang="de">' in out and "Geräte<details" in out and "Mit Cantabile nutzen" in out
assert "Raumtemperatur (Living room)" in out and "Geräte in Home Assistant, die Sie hinzufügen können" in out
assert "Cantabile würde auslesen" in out and "Steuern könnte es: Heizen, Kühlen." in out
assert "Use with Cantabile" not in out
+184
View File
@@ -0,0 +1,184 @@
# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
"""A charger's phases from the best evidence (evphases.py, Finding A8-a):
the mapping, the device, learned from power and current, else each use's
safe side (the grid order 3, the plan 1)."""
import asyncio
import json
import random
import pytest
import yaml
from cantabile_edge import evphases
from cantabile_edge.controls import Control, ev_volts
from cantabile_edge.evphases import EvPhases
from cantabile_edge.gridorder import GridOrders
from cantabile_edge.inputs import Collector
from test_gridorder import NOW, message
V = 230.0
def ev(cid="car", entities=None, **params):
return Control(cid, "ev", dict({"switch": "switch.wb"}, **(entities or {})), True, params)
def charge(store, cid, watts, amps, minutes=10, every=60, start=NOW, jitter=0.0, seed=1):
"""Samples of a charging car, every `every` s for `minutes`; True when a
learned count changed."""
rnd = random.Random(seed)
changed = False
for k in range(int(minutes * 60 / every) + 1):
w = watts * (1 + rnd.uniform(-jitter, jitter))
changed |= store.sample(cid, w, amps, V, start + k * every)
return changed
# -- the sources ----------------------------------------------------------------
def test_each_source(tmp_path):
col = Collector()
store = EvPhases(str(tmp_path))
assert store.resolve(ev(phases=1), col) == (1.0, "mapping")
dev = ev(entities={"phases": "sensor.wb_phases"})
assert store.resolve(dev, col) == (None, "assumed") # the sensor has no value yet
col.on_entities({"a": {"sensor.wb_phases": {"s": "1", "a": {}}}})
assert store.resolve(dev, col) == (1.0, "device")
assert store.resolve(ev(entities={"phases": "sensor.wb_phases"}, phases=3), col) == (3.0, "mapping") # mapping first
store.learned["car"] = {"phases": 1, "n": 1, "at": NOW}
assert store.resolve(ev(), col) == (1.0, "learned")
assert store.resolve(ev("other"), col) == (None, "assumed")
a = store.all([ev("other"), Control("bat", "battery", {"setpoint": "number.b"}, True, {})], col)
assert a == {"other": {"phases": None, "source": "assumed", "plan": 1.0, "order": 3.0}}
assert evphases.words(a["other"]).startswith("phases unknown: 3 assumed for the grid order, 1 for the plan")
assert evphases.words({"phases": 1.0, "source": "learned"}) == "1 phase, learned"
# each use's safe side
assert ev_volts({}, "order") == 690.0 and ev_volts({}, "plan") == 230.0
assert ev_volts({"phases": 3}, "plan") == 690.0 and ev_volts({"phases": 1}, "order") == 230.0
# -- learning ---------------------------------------------------------------------
def test_learns_one_and_three_phases(tmp_path):
store = EvPhases(str(tmp_path))
assert charge(store, "one", 16 * V * 0.98, 16.0, jitter=0.02) # 1 phase, 16 A: about 3.6 kW
assert charge(store, "three", 16 * V * 3 * 0.97, 16.0, jitter=0.02) # 3 phases, 16 A: about 10.7 kW
assert store.learned["one"]["phases"] == 1 and store.learned["three"]["phases"] == 3
# kept across a restart
again = EvPhases(str(tmp_path))
assert again.resolve(ev("one")) == (1.0, "learned") and again.resolve(ev("three")) == (3.0, "learned")
assert json.loads((tmp_path / "ev_phases.json").read_text())["one"]["n"] == 1
def test_unsteady_or_short_samples_decide_nothing(tmp_path):
store = EvPhases(str(tmp_path))
assert not charge(store, "car", 3 * 16 * V, 16.0, minutes=5) # too short
assert not charge(store, "car", 3 * 16 * V, 16.0, jitter=0.25, start=NOW + 3600) # power swings
assert not charge(store, "car", 800.0, 16.0, start=NOW + 7200) # below 1 kW
assert not charge(store, "car", 2.5 * 16 * V, 16.0, start=NOW + 9000) # ratio 2.5: between the steps
assert not charge(store, "car", 3 * 4 * V, 4.0, start=NOW + 12000) # 4 A: too little current
# a pause in the middle starts the window again
for k in range(6):
store.sample("car", 3 * 16 * V, 16.0, V, NOW + 20000 + k * 60)
store.sample("car", None, None, V, NOW + 20360)
for k in range(6):
assert not store.sample("car", 3 * 16 * V, 16.0, V, NOW + 20420 + k * 60)
assert "car" not in store.learned
def test_learned_again_when_readings_keep_disagreeing(tmp_path):
store = EvPhases(str(tmp_path))
charge(store, "car", 3 * 16 * V, 16.0)
assert store.learned["car"]["phases"] == 3
# a 1-phase car on the same charger: once is not enough
assert not charge(store, "car", 16 * V, 16.0, start=NOW + 3600)
assert store.learned["car"]["phases"] == 3 and store.learned["car"]["against"] == {"phases": 1, "n": 1}
charge(store, "car", 3 * 16 * V, 16.0, start=NOW + 7200) # agrees again: reset
assert "against" not in store.learned["car"] and store.learned["car"]["n"] == 2
for i in range(evphases.CONFIRM):
changed = charge(store, "car", 16 * V, 16.0, start=NOW + 10000 + i * 3600)
assert changed and store.learned["car"]["phases"] == 1
assert EvPhases(str(tmp_path)).learned["car"]["phases"] == 1
# -- the grid order ------------------------------------------------------------------
class C:
def __init__(self, id, params):
self.id, self.kind, self.params = id, "ev", params
def test_order_bound_never_above_the_order(tmp_path):
g = GridOrders(str(tmp_path), clock=lambda: NOW)
g.take(message()) # 4200 W
lim = g.limits([C("unknown", {}), C("three", {"phases": 3})], {"unknown": None})
for cid in ("unknown", "three"):
amps = lim[cid]["current_a"][1]
assert amps == 6.0 and amps * 3 * V <= 4200.0 # a 3-phase charger: never above 4.2 kW
# a 1-phase charger learned as one: 4200 / 230 = 18.26, down to 18.2 A (4186 W)
lim = g.limits([C("one", {})], {"one": 1.0})
assert lim["one"]["current_a"][1] == 18.2 and 18.2 * V <= 4200.0
def test_controller_learns_and_the_order_follows(tmp_path):
"""Through the controller: an order while the phases are unknown gives
6.0 A; the car charges on one phase for ten minutes (the charger's own
power sensor, its setpoint); the order's bound becomes 18.2 A, the plan
and the inventory say "1 phase, learned"."""
from cantabile_edge.config import Config
from cantabile_edge.consent import Consent
from cantabile_edge.controller import Controller
from cantabile_edge.inventory import build
from cantabile_edge.mapping import Mapping
m = tmp_path / "mapping.yaml"
m.write_text(yaml.safe_dump({"version": 3, "assets": [
{"entity": "sensor.wb_power", "asset": "ev.power", "kind": "gauge", "unit": "W", "consent": True}],
"controls": [{"id": "car", "kind": "ev", "consent": True,
"entities": {"switch": "switch.wb", "current": "number.wb_amps"}}]}))
t = [NOW]
mp = Mapping.load(str(m))
ctl = Controller(Config(site_id="ab" * 16, mapping=str(m), state_dir=str(tmp_path)), mp,
Consent(str(tmp_path)), sink=None, clock=lambda: t[0])
assert asyncio.run(ctl.grid_order(message(until=NOW + 7200).encode()))
assert ctl.guard.extra_limits["car"]["current_a"]["grid"] == (None, 6.0)
assert ctl.ev_phases()["car"]["source"] == "assumed"
ctl.col.on_entities({"a": {"number.wb_amps": {"s": "16", "a": {}}}})
changed = False
for k in range(11):
t[0] = NOW + k * 60
ctl.gridorders.received = t[0] # the bridge repeats its order every 15 s
ctl.col.on_entities({"a": {"sensor.wb_power": {"s": str(3650 + 10 * (k % 2)), "a": {"unit_of_measurement": "W"}}}})
if ctl.sample_ev_phases(t[0]):
changed = True
ctl._merge_limits()
assert changed and ctl.ev_phases()["car"] == {"phases": 1.0, "source": "learned", "plan": 1.0, "order": 1.0}
assert ctl.guard.extra_limits["car"]["current_a"]["grid"] == (None, 18.2)
st = ctl.model_state()
assert st["sources"]["ev_phases"]["car"] == {"phases": 1.0, "source": "learned"}
assert next(d for d in st["devices"] if d["id"] == "car")["params"]["phases"] == 1.0
reg = {"entities": [{"entity_id": e, "device_id": "wb"} for e in ("switch.wb", "number.wb_amps", "sensor.wb_power")],
"devices": [{"id": "wb", "manufacturer": "go-e", "model": "HOME"}]}
inv = build(mp, reg, {}, {}, t[0], ctl.ev_phases())
told = [d["ev_phases"] for d in inv["devices"] if d.get("ev_phases")]
assert told == [{"car": {"phases": 1.0, "source": "learned"}}]
def test_plan_takes_the_count_and_says_it():
"""The plan: unknown phases are planned at 1 (230 V per A), a learned
3 at 690 V per A; the EV's entries say which and why."""
pytest.importorskip("highspy")
from cantabile_edge import optimizer
from cantabile_edge.controls import PARAMS
from test_optimizer import house
def plan(phases, source):
d = {"id": "ev", "kind": "ev", "channels": ["switch", "current"],
"params": dict(PARAMS["ev"], need_wh=5000, phases=phases),
"state": {"connected": True}, "bounds": {"current_a": [0, 16, "device", "device"]}}
s = house([d])
s["sources"] = {"ev_phases": {"ev": {"phases": phases, "source": source}}}
m = optimizer.Model(s)
m.solve(20)
return m.devices["ev"][1]["volts"], optimizer.plan(s, 20)["devices"]["ev"]
volts, entries = plan(None, "assumed")
assert volts == 230.0 and any("1 for the plan" in e["why"] for e in entries)
volts, entries = plan(3.0, "learned")
assert volts == 690.0 and any("3 phases, learned" in e["why"] for e in entries)
+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"
+25
View File
@@ -0,0 +1,25 @@
# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
"""Inputs the plan steers by count as fresh while they have a value: a room
that holds 23.5 °C for hours or a full battery is not a stopped sensor (the
clone, 2026-10-30); unavailable is."""
from types import SimpleNamespace
from cantabile_edge.controller import Controller
from cantabile_edge.inputs import Collector
def fresh(states: dict) -> bool:
col = Collector()
col.on_entities({"a": {e: {"s": s, "lc": 1_000_000} for e, s in states.items()}}, now=1_000_000)
ctl = SimpleNamespace(col=col, clock=lambda: 1_000_000 + 6 * 3600,
controls=[SimpleNamespace(entities={"temp": "sensor.room", "soc": "sensor.soc"})])
return Controller.inputs_fresh(ctl)
def test_steady_values_are_fresh():
assert fresh({"sensor.room": "23.5", "sensor.soc": "100"})
def test_unavailable_is_not():
assert not fresh({"sensor.room": "unavailable", "sensor.soc": "100"})
assert not fresh({"sensor.room": "23.5"})
+184
View File
@@ -0,0 +1,184 @@
# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
"""The shadow market's bundle as fleet makes it (platform/services/fleet/
marketbundle.go; the market's step 1.5): read by the agent (bundles.py),
shown in HA as sensors only (announce.py: the exchange per contract, the
net position, the shared energy's price, the border actuators' contract
target against their state), unavailable when stale, and as the
dashboard's Market band (dashboards.py), in English and German."""
import asyncio
import json
import time
from cantabile_edge import names
from cantabile_edge.bundles import Bundles
from cantabile_edge.dashboards import Home, dashboard, market_band
from test_announce import make, put
from test_control_parts import SITE
from test_dashboards import FakeHA, make as make_agent, views, cards
UID = f"cantabile_{SITE}"
K1 = "0243f0a0-8c1f-4fae-8e7f-9d2e75cd86f4"
def bundle(now, **kw):
slot = int(now // 900 * 900)
return {"v": 1, "kind": "market", "id": "mb1", "site": SITE, "issued": int(now), "expires": int(now) + 6 * 3600,
"shadow": True, "commands": "none", "note": "Shadow market", "slot": slot, "step_s": 900, "stale_after_s": 3600,
"nodes": [{"id": f"site:{SITE}", "kind": "site", "name": "Haus Rose"}],
"contracts": [{"id": K1, "name": "Rose to Linde", "template": "neighbour_pv_share", "node": f"site:{SITE}",
"direction": "sells", "counterpart": {"name": "Home B", "type": "home"}, "link": "L1",
"link_kind": "direct_line", "version": 1,
"slot": {"applies": True, "guaranteed_kwh": 0, "minimum_kwh": 0, "max_kwh": 1.5, "price_ct": 16.4},
"last": {"slot": slot - 900, "measured": True, "metered_kwh": 1.2, "guaranteed_kwh": 0, "max_kwh": 1.5,
"delivered_kwh": 1.2, "deviation_kwh": 0, "kind": "", "cause": "", "exempt": False,
"exempt_why": "", "price_ct": 16.4, "penalty_eur": 0},
"period": {"period": "month", "start": slot - 86400, "slots": 96, "unmeasured": 0, "exempt_slots": 0,
"guaranteed_kwh": 0, "delivered_kwh": 31.5, "amount_eur": 5.17,
"penalties_paid_eur": 0, "penalties_received_eur": 0}},
{"id": "flats:b1:sells", "name": "Flats, summed", "node": "building:b1", "direction": "sells",
"counterpart": {"name": "3 flat(s)", "type": "homes", "count": 3}, "link": "", "link_kind": "building_internal",
"slot": {"applies": False, "price_ct": 24}, "last": None,
"period": {"period": "quarter", "delivered_kwh": 2, "amount_eur": 0.48}}],
"links": [{"id": "L1", "kind": "direct_line", "node": f"site:{SITE}", "counterpart": {"name": "Home B", "type": "home"},
"measurement": {"kind": "meter", "trust": "high", "at": "self", "kwh": 1.2}, "contracts": [K1]}],
"actuators": [{"link": "L1", "device": "relay-1", "name": "Shelly Pro 1PM", "controls": ["boiler"], "protection": "protected",
"default_state": "off", "default_set": True, "fallback_s": 600,
"target": {"state": "on", "kw": 6, "why": "contract in this slot: Rose to Linde"},
"last": {"value": "on", "at": slot - 600, "result": "done"}}],
"positions": [{"node": f"site:{SITE}", "period": "month", "start": slot - 86400, "sold_kwh": 31.5, "bought_kwh": 0,
"net_kwh": -31.5, "receives_eur": 5.17, "pays_eur": 0, "penalties_paid_eur": 0,
"penalties_received_eur": 0, "net_eur": 5.17}], **kw}
def test_read_refused_and_stale(tmp_path):
now = time.time()
b = Bundles(str(tmp_path), SITE)
put(tmp_path / "market.json", bundle(now))
m = b.market_now(now)
assert m is not None and not m["stale"] and m["contracts"][0]["id"] == K1
assert b.market_now(now + 3 * 900)["stale"] is False
assert b.market_now(now + 5 * 900)["stale"] is True # older than stale_after_s: four slots
assert b.market_now(now + 7 * 3600) is None # expired
put(tmp_path / "market.json", bundle(now, site="00" * 16)) # another site's
assert b.market_now(now) is None
put(tmp_path / "market.json", bundle(now, commands="load_limit")) # not shadow only: refused
assert b.market_now(now) is None and "shadow only" in b.market.error
put(tmp_path / "market.json", bundle(now, shadow=False))
assert b.market_now(now) is None
put(tmp_path / "market.json", bundle(now, contracts=[], actuators=[])) # nothing in it: nothing shown
assert b.market_now(now) is None
def test_entities_and_state(tmp_path):
a, ann = make(tmp_path)
for c in a.control.controls:
if c.id == "boiler":
c.device = "relay-1" # the box's device it came with (a mapping fragment's)
now = time.time()
put(tmp_path / "market.json", bundle(now))
objs = ann.objects()
k = K1.replace("-", "_")
ex = objs[(UID, "sensor", f"market_{k}")]
assert ex["name"] == "Exchange with Home B" and ex["unit_of_measurement"] == "kW" and ex["device_class"] == "power"
assert ex["availability_mode"] == "all" and {x["topic"] for x in ex["availability"]} == {
f"cantabile/{SITE}/availability", f"cantabile/{SITE}/market/availability"}
assert "availability_topic" not in ex
assert objs[(UID, "sensor", "market_flats_b1_sells")]["name"] == "Exchange with 3 flats, summed"
assert objs[(UID, "sensor", "market_position")]["unit_of_measurement"] == "kWh"
assert objs[(UID, "sensor", "market_price")]["unit_of_measurement"] == "EUR/kWh"
act = objs[(UID, "sensor", "market_actuator_relay_1")]
assert act["name"] == "Shelly Pro 1PM: contract target"
# sensors only: no switch, number, select or button of the market
assert not [o for (_, comp, o) in objs if o.startswith("market") and comp != "sensor"]
assert ex["default_entity_id"] == f"sensor.cantabile_market_{k}"
# the state: kW of the last quarter-hour, the sentence, the actuator against HA's own state
a.control.col.on_entities({"a": {"switch.boiler": {"s": "off"}}}, now)
st = ann.market_state(a.control.bundles.market_now(now))
c = st["contracts"][k]
assert c["kw"] == 4.8 and c["kwh_period"] == 31.5 and c["trust"] == "high" and c["shadow"] is True
assert c["summary"] == "Sells to Home B: 1.20 kWh last quarter-hour (contract 1.50) · 16.4 ct/kWh · shadow"
assert "So far this month: 31.5 kWh · 5.17 €" == c["period_words"]
flats = st["contracts"]["flats_b1_sells"]
assert flats["kw"] is None and "not in force" in flats["summary"] and "quarter" in flats["period_words"]
assert st["price"]["now"] == 16.4 # the contracts in force this slot
r = st["actuators"]["relay_1"]
assert r["target"] == "on" and r["actual"] == "off" and r["agrees"] is False
assert "contract: **on** (6.0 kW) · now: **off**" in r["words"] and "falls back to off after 10 min" in r["words"]
assert st["positions"]["market_position"]["net_kwh"] == -31.5
# German
a.ha_language = "de"
objs = ann.objects()
assert objs[(UID, "sensor", f"market_{k}")]["name"] == "Austausch mit Home B"
assert objs[(UID, "sensor", "market_actuator_relay_1")]["name"] == "Shelly Pro 1PM: Vertragsvorgabe"
c = ann.market_state(a.control.bundles.market_now(now))["contracts"][k]
assert c["summary"] == "Verkauft an Home B: 1,20 kWh letzte Viertelstunde (Vertrag 1,50) · 16,4 ct/kWh · Schattenbetrieb"
# off: nothing of the market in HA; no bundle: nothing either
put(tmp_path / "market.json", bundle(now, site="00" * 16))
assert not [o for (_, _, o) in ann.objects() if o.startswith("market")]
def test_stale_goes_unavailable(tmp_path):
a, ann = make(tmp_path)
now = time.time()
put(tmp_path / "market.json", bundle(now - 2 * 3600, expires=int(now) + 3600))
class Pub:
def __init__(self):
self.out = {}
async def publish(self, topic, payload, retain=False):
self.out[topic] = payload
p = Pub()
ann.client = p
async def cloud():
return True
a.cloud = cloud
asyncio.run(ann._states())
assert p.out[f"cantabile/{SITE}/market/availability"] == "offline"
assert json.loads(p.out[f"cantabile/{SITE}/market"])["stale"] is True
put(tmp_path / "market.json", bundle(now))
asyncio.run(ann._states())
assert p.out[f"cantabile/{SITE}/market/availability"] == "online"
def test_dashboard_band(tmp_path):
now = time.time()
k = K1.replace("-", "_")
ents = [{"platform": "mqtt", "unique_id": f"{UID}_{s}", "entity_id": f"sensor.c_{s}"}
for s in (f"market_{k}", "market_flats_b1_sells", "market_position", "market_price", "market_actuator_relay_1")]
ha = FakeHA(entities=ents)
a = make_agent(tmp_path)
put(tmp_path / "market.json", bundle(now))
from cantabile_edge.dashboards import Dashboards
asyncio.run(Dashboards(a).ensure(ha))
over = views(ha)["overview"]
band = [s for s in over["sections"] if s["cards"] and s["cards"][0].get("heading") == "Market (shadow)"]
assert len(band) == 1 and band[0]["column_span"] == 2
cs = band[0]["cards"][1:]
widths = [c["grid_options"]["columns"] for c in cs]
assert widths[:2] == [12, 12] # the position and the price: halves of the row (no hole)
assert widths[2:4] == [12, 12] # a card per contract: halves
assert all(c["type"] in ("tile", "markdown") for c in cs)
text = json.dumps(cs, ensure_ascii=False)
assert "state_attr(e, 'summary')" in text and "sensor.c_market_actuator_relay_1" in text and "ⓘ" in text
# no contracts: no band
put(tmp_path / "market.json", bundle(now, contracts=[]))
h = Home(a.mapping, UID, {e["unique_id"]: e["entity_id"] for e in ents}, [], "en",
market=a.control.bundles.market_now(now))
assert market_band(h) is None
# German heading
h = Home(a.mapping, UID, {e["unique_id"]: e["entity_id"] for e in ents}, [], "de", market=bundle(now))
assert market_band(h)["cards"][0]["heading"] == "Markt (Schattenbetrieb)"
assert dashboard(h) is not None
def test_names_have_german():
for key in [k for k in names.NAMES if k.startswith(("market_", "mk_")) or k == "card_market"]:
assert names.NAMES[key].get("de") and names.NAMES[key].get("en"), key
en, de = names.NAMES[key]["en"], names.NAMES[key]["de"]
assert sorted(x for x in en if x in "0123456789") == sorted(x for x in de if x in "0123456789"), key # the same {n}s
+221
View File
@@ -0,0 +1,221 @@
# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
"""The market's contracts in the plan (step 2, minimal; shadow):
a contract is an import source or an export sink at its price, next to the
grid; the plan uses it where it beats the grid, within its bounds."""
import json
import time
import pytest
highspy = pytest.importorskip("highspy")
from cantabile_edge import optimizer # noqa: E402
from cantabile_edge.bundles import Bundles # noqa: E402
START = 1793232000
STEPS = 16
def house(market=None, load=1000.0, pv=0.0, imp=30.0, exp=8.0):
s = {"v": 1, "start": START, "step_s": 900, "steps": STEPS, "tz": "Europe/Berlin",
"price_import": [imp] * STEPS, "price_export": [exp] * STEPS, "price_note": "test",
"load_w": [load] * STEPS, "pv_w": [pv] * STEPS, "t_out": [5.0] * STEPS,
"sun": [0.0] * STEPS, "occupied": [1] * STEPS, "devices": []}
if market is not None:
s["market"] = market
return s
def contract(direction, price, max_w=0.0, guaranteed_w=0.0, minimum_wh=0.0, a=0, b=8, cid="c1"):
return {"id": cid, "name": cid, "direction": direction, "price_ct": price, "max_w": max_w,
"guaranteed_w": guaranteed_w, "minimum_wh": minimum_wh, "from_step": a, "to_step": b}
def kwh(p, cid="c1"):
return next(c for c in p["market"] if c["id"] == cid)["planned_kwh"]
def test_cheaper_contract_replaces_grid_import():
p = optimizer.plan(house([contract("buys", 20.0, max_w=600)]), 10)
# 8 steps x 600 W x 0.25 h = 1.2 kWh, the rest from the grid
assert kwh(p) == pytest.approx(1.2, abs=1e-3)
assert p["market"][0]["vs_grid_eur"] == pytest.approx(1.2 * (30 - 20) / 100, abs=1e-3)
assert p["cost_eur"] < optimizer.plan(house(), 10)["cost_eur"]
def test_dearer_contract_not_used():
p = optimizer.plan(house([contract("buys", 35.0, max_w=600)]), 10)
assert kwh(p) == pytest.approx(0.0, abs=1e-6)
def test_export_to_a_contract_above_feed_in():
# PV surplus 2 kW: the contract (15 ct) beats the feed-in (8 ct), up to its 1.5 kW
p = optimizer.plan(house([contract("sells", 15.0, max_w=1500)], load=500.0, pv=2500.0), 10)
assert kwh(p) == pytest.approx(8 * 1.5 * 0.25, abs=1e-3)
assert p["market"][0]["vs_grid_eur"] == pytest.approx(3.0 * (15 - 8) / 100, abs=1e-3)
# below the feed-in: not used
p = optimizer.plan(house([contract("sells", 5.0, max_w=1500)], load=500.0, pv=2500.0), 10)
assert kwh(p) == pytest.approx(0.0, abs=1e-6)
def test_bounds_firm_and_only_in_its_steps():
# firm 800 W bought even when dearer than the grid; only in steps 2..5
p = optimizer.plan(house([contract("buys", 40.0, max_w=1000, guaranteed_w=800, a=2, b=6)]), 10)
c = p["market"][0]
assert {s["t"] for s in c["steps"]} == {START + t * 900 for t in range(2, 6)}
assert c["planned_kwh"] == pytest.approx(4 * 0.8 * 0.25, abs=1e-3)
# nothing bought on a contract is resold to the grid, even far below the feed-in
p = optimizer.plan(house([contract("buys", 1.0, max_w=5000)], load=300.0, exp=8.0), 10)
assert kwh(p) == pytest.approx(8 * 0.3 * 0.25, abs=1e-3)
def test_take_or_pay_minimum_is_paid():
# the minimum (100 Wh per step) is paid even when dearer: so it is used
p = optimizer.plan(house([contract("buys", 40.0, max_w=1000, minimum_wh=100.0)]), 10)
assert kwh(p) == pytest.approx(8 * 0.1, abs=1e-3)
def test_no_market_unchanged_plan():
a, b = optimizer.plan(house(), 10), optimizer.plan(house([]), 10)
assert "market" not in a and "market" not in b
assert a["cost_eur"] == b["cost_eur"] and a["flex"] == b["flex"]
def test_bundle_to_model(tmp_path):
now = time.time()
slot = int(now // 900 * 900)
b = Bundles(str(tmp_path), "site1")
assert b.market_for_model(slot, 900, 192, now) == [] # no market.json
d = {"v": 1, "kind": "market", "site": "site1", "shadow": True, "commands": "none", "slot": slot, "step_s": 900,
"stale_after_s": 3600, "issued": slot, "expires": slot + 86400,
"contracts": [{"id": "k1", "name": "PV share", "direction": "buys",
"slot": {"applies": True, "guaranteed_kwh": 0.1, "minimum_kwh": 0, "max_kwh": 0.5, "price_ct": 22.0}},
{"id": "k2", "name": "off now", "direction": "sells", "slot": {"applies": False, "price_ct": 10.0}}]}
(tmp_path / "market.json").write_text(json.dumps(d))
m = b.market_for_model(slot, 900, 192, now)
assert len(m) == 1 and m[0]["id"] == "k1" and m[0]["direction"] == "buys"
assert m[0]["max_w"] == pytest.approx(2000.0) and m[0]["guaranteed_w"] == pytest.approx(400.0)
assert (m[0]["from_step"], m[0]["to_step"]) == (0, 4) # held while fresh: 4 slots
def ahead_bundle(slot, contracts, stale_after_s=3600):
return {"v": 1, "kind": "market", "site": "site1", "shadow": True, "commands": "none", "slot": slot, "step_s": 900,
"stale_after_s": stale_after_s, "issued": slot, "expires": slot + 6 * 3600, "contracts": contracts}
def test_bundle_coming_slots_to_model(tmp_path):
"""A bundle with the coming slots (fleet "ahead"): each plan step gets
its own slot's window, amounts and price, beyond the bundle's freshness
window and its current slot; a contract without them as before."""
now = time.time()
slot = int(now // 900 * 900)
b = Bundles(str(tmp_path), "site1")
ahead = {"start": slot, "step_s": 900, "slots": 192,
"windows": [{"from": 8, "to": 12, "guaranteed_kwh": 0.1, "minimum_kwh": 0, "max_kwh": 0.5},
{"from": 100, "to": 104, "guaranteed_kwh": 0, "minimum_kwh": 0.05, "max_kwh": 1.0}],
"price_ct": [[10, 20.0], [182, 25.0]]}
d = ahead_bundle(slot, [
# not applying in the current slot, but later: still planned
{"id": "k1", "name": "later", "direction": "buys", "slot": {"applies": False, "price_ct": 20.0}, "ahead": ahead},
# an ahead with no window: nothing to plan, though its current slot applies
{"id": "k2", "name": "none", "direction": "sells", "slot": {"applies": True, "price_ct": 9.0, "max_kwh": 1},
"ahead": {"start": slot, "step_s": 900, "slots": 192, "windows": [], "price_ct": [[192, 9.0]]}},
# an older fleet's contract without ahead: held while fresh, as before
{"id": "k3", "name": "old", "direction": "buys", "slot": {"applies": True, "max_kwh": 0.25, "price_ct": 21.0}}])
(tmp_path / "market.json").write_text(json.dumps(d))
m = {c["id"]: c for c in b.market_for_model(slot, 900, 192, now)}
assert set(m) == {"k1", "k3"}
k1 = m["k1"]
assert (k1["from_step"], k1["to_step"]) == (8, 104) and len(k1["terms"]) == 192
on = [t for t, x in enumerate(k1["terms"]) if x is not None]
assert on == [8, 9, 10, 11, 100, 101, 102, 103]
assert [k1["terms"][t]["price_ct"] for t in (8, 9, 10, 11)] == [20.0, 20.0, 25.0, 25.0]
assert k1["terms"][8]["max_w"] == pytest.approx(2000.0) and k1["terms"][8]["guaranteed_w"] == pytest.approx(400.0)
assert k1["terms"][100]["max_w"] == pytest.approx(4000.0) and k1["terms"][100]["minimum_wh"] == pytest.approx(50.0)
assert k1["price_ct"] == 20.0 and k1["max_w"] == pytest.approx(2000.0) # the first step's, for the shown plan
assert (m["k3"]["from_step"], m["k3"]["to_step"]) == (0, 4) and "terms" not in m["k3"]
# a plan starting two slots later: the slots shift with it
m = {c["id"]: c for c in b.market_for_model(slot + 1800, 900, 192, now)}
assert m["k1"]["from_step"] == 6 and m["k1"]["terms"][5] is None and m["k1"]["terms"][6] is not None
# stale: nothing at all
d["issued"] = d["slot"] = slot - 7200
d["contracts"][0]["ahead"]["start"] = slot - 7200
(tmp_path / "market.json").write_text(json.dumps(d))
assert Bundles(str(tmp_path), "site1").market_for_model(slot, 900, 192, now) == [] # a new reader: same size, same second
def test_bundle_bad_ahead_falls_back_to_the_slot(tmp_path):
now = time.time()
slot = int(now // 900 * 900)
b = Bundles(str(tmp_path), "site1")
d = ahead_bundle(slot, [{"id": "k1", "name": "x", "direction": "buys", "slot": {"applies": True, "max_kwh": 0.5, "price_ct": 22.0},
"ahead": {"start": slot, "slots": 192, "windows": [{"from": 0, "to": 4}], "price_ct": [[3, 1.0]]}}])
(tmp_path / "market.json").write_text(json.dumps(d))
m = b.market_for_model(slot, 900, 192, now)
assert len(m) == 1 and "terms" not in m[0] and (m[0]["from_step"], m[0]["to_step"]) == (0, 4)
def per_step(on, price, max_w=0.0):
return [{"price_ct": price(t), "max_w": max_w, "guaranteed_w": 0.0, "minimum_wh": 0.0} if t in on else None
for t in range(STEPS)]
def test_plan_uses_each_steps_terms():
# cheaper than the grid (30 ct) only in steps 2..3 (20 ct); dearer in 4..5 (35 ct); outside its windows not at all
terms = per_step({2, 3, 4, 5, 10}, lambda t: 20.0 if t < 4 else 35.0, max_w=600)
c = {**contract("buys", 20.0, max_w=600, a=2, b=11), "terms": terms}
got = optimizer.plan(house([c]), 10)["market"][0]
assert {s["t"] for s in got["steps"]} == {START + t * 900 for t in (2, 3)}
assert got["planned_kwh"] == pytest.approx(2 * 0.6 * 0.25, abs=1e-3)
assert got["cost_eur"] == pytest.approx(0.3 * 20 / 100, abs=1e-4)
assert got["vs_grid_eur"] == pytest.approx(0.3 * (30 - 20) / 100, abs=1e-4)
def test_plan_firm_amount_per_step():
# a firm 800 W in step 6 only (dearer than the grid): bought there and nowhere else
terms = per_step({6, 7}, lambda t: 40.0, max_w=1000)
terms[6]["guaranteed_w"] = 800.0
c = {**contract("buys", 40.0, max_w=1000, a=6, b=8), "terms": terms}
got = optimizer.plan(house([c]), 10)["market"][0]
assert [s["t"] for s in got["steps"]] == [START + 6 * 900]
assert got["planned_kwh"] == pytest.approx(0.8 * 0.25, abs=1e-3)
def test_second_order_contract_effective_price(tmp_path):
"""A contract through a middle node (fleet contracts-v2): fleet already
puts the buyer's effective price (energy + the middle node's fee, per kWh
that arrives) in price_ct and the ahead runs; the agent plans with it and
ignores the extra "via" and "parts" (it computes no price itself)."""
now = time.time()
slot = int(now // 900 * 900)
b = Bundles(str(tmp_path), "site1")
d = {"v": 1, "kind": "market", "site": "site1", "shadow": True, "commands": "none", "slot": slot, "step_s": 900,
"stale_after_s": 3600, "issued": slot, "expires": slot + 86400,
"contracts": [{"id": "k1", "name": "A to B via pool", "direction": "buys", "via": {"name": "Pool", "type": "pool"},
"slot": {"applies": True, "guaranteed_kwh": 0, "minimum_kwh": 0, "max_kwh": 0.45, "price_ct": 24.444,
"parts": {"energy_ct": 20, "transfer_ct": 2, "loss_pct": 10}},
"ahead": {"start": slot, "step_s": 900, "slots": 8,
"windows": [{"from": 0, "to": 8, "guaranteed_kwh": 0, "minimum_kwh": 0, "max_kwh": 0.45}],
"price_ct": [[4, 24.444], [4, 13.333]]}}]}
(tmp_path / "market.json").write_text(json.dumps(d))
m = b.market_for_model(slot, 900, 8, now)
assert len(m) == 1 and m[0]["price_ct"] == pytest.approx(24.444)
assert [x["price_ct"] for x in m[0]["terms"]] == pytest.approx([24.444] * 4 + [13.333] * 4)
assert m[0]["max_w"] == pytest.approx(1800.0)
def test_take_or_pay_cost_is_the_paid_minimum():
"""Finding A2-c: a take-or-pay contract used below its minimum costs the
minimum in cost_eur and its line, as in the optimisation."""
# load 300 W = 75 Wh per step, all of it from the contract (20 ct < 30 ct);
# the minimum is 100 Wh per step: 8 x 0.1 kWh paid at 20 ct
p = optimizer.plan(house([contract("buys", 20.0, max_w=1000, minimum_wh=100.0)], load=300.0), 10)
c = p["market"][0]
assert c["planned_kwh"] == pytest.approx(8 * 0.075, abs=1e-3)
assert c["paid_kwh"] == pytest.approx(8 * 0.1, abs=1e-3)
assert c["cost_eur"] == pytest.approx(0.8 * 0.20, abs=1e-3)
assert c["vs_grid_eur"] == pytest.approx(0.6 * 0.30 - 0.8 * 0.20, abs=1e-3)
grid = 8 * 0.075 * 0.30 # steps 8..15 from the grid
assert p["cost_eur"] == pytest.approx(grid + 0.16, abs=0.006)
assert all(st["paid_kwh"] == pytest.approx(0.1, abs=1e-4) for st in c["steps"])
+199
View File
@@ -0,0 +1,199 @@
# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
"""The model bundle's trained series and rooms (bundles.py, Model v1's
optional keys), a room's R and C in the model state (inputs.py), and the
site's weather cell for the link (controller.py)."""
import asyncio
import json
import math
from types import SimpleNamespace
import pytest
from cantabile_edge.bundles import Bundles
from cantabile_edge.config import Config
from cantabile_edge.controller import Controller, write_cell
from cantabile_edge.controls import PARAMS, Control
from cantabile_edge.inputs import Collector, model_state
from cantabile_edge.mapping import Mapping
from test_control_parts import SITE, Clock, consent # noqa: F401
START = 1_800_000_000 // 3600 * 3600 # a Friday, on the hour (UTC)
HOW = list(range(168)) # hour-of-week profile: its index
def model(**kw):
m = {"v": 1, "kind": "model", "id": "m1", "site": SITE, "issued": 1,
"forecasts": {"load_w": {"profile": "hour_of_week", "values": HOW}}}
m.update(kw)
return m
def put(tmp_path, m):
(tmp_path / "model.json").write_text(json.dumps(m))
return Bundles(str(tmp_path), SITE)
def hour(t):
from datetime import datetime, timezone
d = datetime.fromtimestamp(t, timezone.utc)
return float(d.weekday() * 24 + d.hour)
# -- the series ---------------------------------------------------------------
def test_series_where_it_covers_profile_elsewhere(tmp_path):
m = model()
m["forecasts"]["load_w"]["series"] = {"start": START + 2 * 900, "step_s": 900, "values": [1000, 2000, 3000]}
s = put(tmp_path, m).forecast("load_w", START, 900, 8, "UTC")
assert s[2:5] == [1000.0, 2000.0, 3000.0]
assert s[:2] == [hour(START)] * 2 and s[5:] == [hour(START + k * 900) for k in range(5, 8)]
# a series that began before the horizon: its later values
m["forecasts"]["load_w"]["series"] = {"start": START - 900, "step_s": 900, "values": [5, 6]}
assert put(tmp_path, m).forecast("load_w", START, 900, 3, "UTC") == [6.0, hour(START), hour(START)]
# without a series: the profile as before
assert put(tmp_path, model()).forecast("load_w", START, 900, 2, "UTC") == [hour(START)] * 2
def test_expired_model_is_not_used(tmp_path):
m = model(expires=START, rooms={"living_room": {"r_k_per_kw": 8, "c_kwh_per_k": 4}})
m["forecasts"]["load_w"]["series"] = {"start": START, "step_s": 900, "values": [1000] * 8}
b = put(tmp_path, m)
assert b.forecast("load_w", START, 900, 8, "UTC") is None # the agent's history then
assert b.room_rc("living_room", now=START) is None
assert b.room_rc("living_room", now=START - 1) is not None
@pytest.mark.parametrize("series,rooms", [
({"start": START, "step_s": 600, "values": [1]}, None),
({"start": START + 60, "step_s": 900, "values": [1]}, None),
({"start": "x", "step_s": 900, "values": [1]}, None),
({"start": START, "step_s": 900, "values": [1] * 401}, None),
({"start": START, "step_s": 900, "values": [1, "2"]}, None),
({"start": START, "step_s": 900, "values": [1, True]}, None),
({"start": START, "step_s": 900, "values": [1, float("nan")]}, None),
({"start": START, "step_s": 900, "values": [float("inf")]}, None),
({"start": START, "step_s": 900, "values": None}, None),
([1, 2, 3], None),
(None, {"r": {"r_k_per_kw": 0.4, "c_kwh_per_k": 3}}),
(None, {"r": {"r_k_per_kw": 201, "c_kwh_per_k": 3}}),
(None, {"r": {"r_k_per_kw": 10, "c_kwh_per_k": 0.05}}),
(None, {"r": {"r_k_per_kw": 10, "c_kwh_per_k": 101}}),
(None, {"r": {"r_k_per_kw": 10}}),
(None, {"r": {"r_k_per_kw": "10", "c_kwh_per_k": 3}}),
(None, {"r": {"r_k_per_kw": 10, "c_kwh_per_k": 3, "power": "gas"}}),
(None, {"r": [10, 3]}),
(None, [{"r_k_per_kw": 10, "c_kwh_per_k": 3}]),
])
def test_malformed_series_or_room_refuses_the_bundle(tmp_path, series, rooms):
m = model()
if series is not None:
m["forecasts"]["load_w"]["series"] = series
if rooms is not None:
m["rooms"] = rooms
b = put(tmp_path, m)
assert b.forecast("load_w", START, 900, 2, "UTC") is None and b.model_id() == ""
assert b.model.error
def test_good_series_and_rooms_pass(tmp_path):
m = model(rooms={"a": {"r_k_per_kw": 0.5, "c_kwh_per_k": 100, "power": "electric", "rmse_k": 0.3, "days": 21},
"b": {"r_k_per_kw": 200, "c_kwh_per_k": 0.1}})
m["forecasts"]["load_w"]["series"] = {"start": START, "step_s": 900, "values": [1.5] * 400}
b = put(tmp_path, m)
assert b.model_id() == "m1" and b.model_info()["rooms"] == ["a", "b"]
# -- rooms --------------------------------------------------------------------
def test_room_matching(tmp_path):
rooms = {"living_room": {"r_k_per_kw": 8, "c_kwh_per_k": 4, "power": "thermal"},
"bedroom": {"r_k_per_kw": 12, "c_kwh_per_k": 2},
"studio_cooling": {"r_k_per_kw": 20, "c_kwh_per_k": 1, "power": "electric"}}
b = put(tmp_path, model(rooms=rooms))
rc = b.room_rc("bedroom")
assert rc == {"r_k_per_kw": 12.0, "c_kwh_per_k": 2.0, "power": "thermal",
"gain_w": 0.0, "solar_m2": 0.0} # a model without the gains: none
for cid in ("living_room_heating", "living_room_cooling", "living_room_heat", "living_room_cool"):
assert b.room_rc(cid)["r_k_per_kw"] == 8.0, cid
assert b.room_rc("studio_cooling")["power"] == "electric" # its own id first
assert b.room_rc("heat_pump", area="bedroom")["r_k_per_kw"] == 12.0 # by its area
assert b.room_rc("heat_pump") is None
assert b.room_rc("heat_pump", only_one=True) is None # more than one room
one = put(tmp_path, model(rooms={"flat": {"r_k_per_kw": 9, "c_kwh_per_k": 5}}))
assert one.room_rc("heat_pump", only_one=True)["r_k_per_kw"] == 9.0
assert one.room_rc("heat_pump") is None
# a shared model or another site's: no rooms
assert put(tmp_path, model(site="*", rooms=rooms)).room_rc("bedroom") is None
assert put(tmp_path, model(site="cd" * 16, rooms=rooms)).room_rc("bedroom") is None
assert put(tmp_path, model()).room_rc("bedroom") is None
INPUTS = SimpleNamespace(load_power="", outside_temp="", weather="", sun="", presence=[])
def heating(cid, **params):
return Control(cid, "heating", {"climate": f"climate.{cid}"}, True, params)
def state(b, controls):
s = model_state(START, controls, INPUTS, Collector(), b, None, "UTC", None)
return {d["id"]: d["params"] for d in s["devices"]}, s["sources"]
def test_model_state_takes_the_rooms(tmp_path):
rooms = {"living_room": {"r_k_per_kw": 8, "c_kwh_per_k": 4},
"bedroom": {"r_k_per_kw": 6, "c_kwh_per_k": 2, "power": "electric"}}
b = put(tmp_path, model(rooms=rooms))
controls = [heating("living_room_heating"),
heating("bedroom", cop=3.0), # a heat pump, fitted on electric power
heating("office", r_k_per_kw=15.0), # the mapping's own: wins
heating("bath")] # no room: defaults
p, src = state(b, controls)
assert (p["living_room_heating"]["r_k_per_kw"], p["living_room_heating"]["c_kwh_per_k"]) == (8.0, 4.0)
r, c = p["bedroom"]["r_k_per_kw"], p["bedroom"]["c_kwh_per_k"]
assert math.isclose(r, 2.0) and math.isclose(c, 6.0) and math.isclose(r * c, 6 * 2) # tau kept
assert p["office"]["r_k_per_kw"] == 15.0 and p["office"]["c_kwh_per_k"] == PARAMS["heating"]["c_kwh_per_k"]
assert p["bath"]["r_k_per_kw"] == PARAMS["heating"]["r_k_per_kw"]
assert src["rooms"] == {"living_room_heating": "model", "bedroom": "model", "office": "mapping", "bath": "defaults"}
assert src["model"] == "m1"
# the mapping's C alone also wins over the model
p, src = state(b, [heating("living_room", c_kwh_per_k=7.0)])
assert p["living_room"]["r_k_per_kw"] == PARAMS["heating"]["r_k_per_kw"] and src["rooms"]["living_room"] == "mapping"
def test_model_state_only_room_and_no_model(tmp_path):
b = put(tmp_path, model(rooms={"flat": {"r_k_per_kw": 9, "c_kwh_per_k": 5, "power": "electric"}}))
p, src = state(b, [heating("boiler")]) # cop 1: as fitted
assert (p["boiler"]["r_k_per_kw"], p["boiler"]["c_kwh_per_k"]) == (9.0, 5.0) and src["rooms"] == {"boiler": "model"}
cool = Control("ac", "cooling", {"climate": "climate.ac"}, True, {})
p, src = state(b, [heating("boiler"), cool]) # two: no guess
assert src["rooms"] == {"boiler": "defaults", "ac": "defaults"}
p, src = state(Bundles(str(tmp_path / "none"), SITE), [heating("boiler")])
assert src["rooms"] == {"boiler": "defaults"} and src["model"] == ""
# -- the weather cell -----------------------------------------------------------
def test_write_cell(tmp_path):
p = tmp_path / "site_cell.json"
assert write_cell(str(tmp_path), 47.0234, 28.8361) is True
assert json.loads(p.read_text()) == {"lat": 47.0, "lon": 28.8}
st = p.stat().st_mtime_ns
assert write_cell(str(tmp_path), 46.98, 28.79) is False and p.stat().st_mtime_ns == st # the same cell
assert write_cell(str(tmp_path), 52.52, 13.405) is True and json.loads(p.read_text()) == {"lat": 52.5, "lon": 13.4}
assert not (tmp_path / "site_cell.json.tmp").exists()
assert write_cell(str(tmp_path / "missing"), 1.0, 2.0) is False # only logged
def test_on_connect_writes_the_cell(tmp_path, consent): # noqa: F811
(tmp_path / "mapping.yaml").write_text(json.dumps({"version": 3, "assets": [], "controls": []}))
m = Mapping.load(str(tmp_path / "mapping.yaml"))
ctl = Controller(Config(site_id=SITE, mapping="", state_dir=str(tmp_path)), m, consent, None, clock=Clock())
class Client:
async def send(self, *a, **kw):
return {"time_zone": "Europe/Chisinau", "latitude": 47.0105, "longitude": 28.8638}
async def subscribe_entities(self, *a, **kw):
pass
asyncio.run(ctl.on_connect(Client()))
assert json.loads((tmp_path / "site_cell.json").read_text()) == {"lat": 47.0, "lon": 28.9}
assert ctl.location == {"latitude": 47.0105, "longitude": 28.8638}
+35
View File
@@ -112,3 +112,38 @@ def test_cover_shades_a_cooled_room_in_the_sun():
blind = p["devices"]["blind"]
assert at(blind, START + 12 * 3600)["set"] < 100, blind # the sun on it: shaded
assert at(blind, START + 3 * 3600)["set"] == 100, blind # night, nothing at stake: open
def test_feed_in_above_import_still_plans():
"""Finding A2-b: a step where the feed-in pays more than the import
(a negative day-ahead price, a hand-typed feed-in) made the MILP
unbounded; import and export are now bounded by the connection and
exclusive in such a step."""
s = house([battery()], steps=16)
s["price_import"][5], s["price_export"][5] = 5.0, 8.0
s["price_import"][6], s["price_export"][6] = -10.0, 0.0 # paid to import, nothing for feed-in
s["grid_max_w"] = 20000.0
m = optimizer.Model(s)
m.solve(20)
val = m.h.getSolution().col_value
gin, gout = [val[v.index] for v in m.gin], [val[v.index] for v in m.gout]
for t in range(16):
assert min(gin[t], gout[t]) < 1e-6, (t, gin[t], gout[t]) # never both at once
assert gin[t] <= 20000.0 + 1e-6 and gout[t] <= 20000.0 + 1e-6
p = optimizer.plan(s, 20)
assert p["status"] and p["devices"]["battery"]
# without the connection in the state: the default bound, still a plan
del s["grid_max_w"]
assert optimizer.plan(s, 20)["status"]
def test_grid_bound_never_below_the_house():
"""The connection bound never makes a plan infeasible: a forecast load
above it is still imported."""
s = house([], steps=8)
s["load_w"] = [30000.0] * 8
s["grid_max_w"] = 10000.0
m = optimizer.Model(s)
m.solve(10)
val = m.h.getSolution().col_value
assert val[m.gin[0].index] == pytest.approx(30000.0)
+30
View File
@@ -135,6 +135,19 @@ def test_inventory_build(mapping, tmp_path):
assert "asset:load.power" in targets and "control:x" not in targets
def test_inventory_mapping_battery_ratings(mapping):
"""An installer's mapping (no devices tool): the battery's rated kWh and
kW from its control's params, once (on the first device of its
entities); without a device in HA on an entry of the mapping's own."""
inv = build(mapping, REG, {}, {}, 100)
rated = [(d["id"], d["ratings"]) for d in inv["devices"] if d["ratings"]]
assert len(rated) == 1 and rated[0][1] == {"battery_kwh": 10.0, "battery_kw": 3.0}
assert rated[0][0] != "ha:mapping" # on the Victron (d1)
inv = build(mapping, {"devices": [], "entities": []}, {}, {}, 100)
assert [(d["id"], d["ratings"]) for d in inv["devices"]] == [("ha:mapping", {"battery_kwh": 10.0, "battery_kw": 3.0})]
assert "sensor." not in json.dumps(inv)
def test_inventory_sent_with_consent_when_changed(mapping, consent, tmp_path):
cfg = Config(site_id=SITE, mapping="", state_dir=str(tmp_path))
clock = Clock()
@@ -153,3 +166,20 @@ def test_inventory_sent_with_consent_when_changed(mapping, consent, tmp_path):
consent.set(False, "test")
clock.t += 1000
assert not run(inv.maybe_send(client))
def test_inventory_waits_for_the_link(mapping, consent, tmp_path):
"""Before the link's NATS connection is up the link drops it: not sent
(nor counted as sent), tried again once the link is connected."""
cfg = Config(site_id=SITE, mapping="", state_dir=str(tmp_path))
sink = Sink()
up = [False]
async def cloud():
return up[0]
sink.cloud = cloud
inv = Inventory(cfg, mapping, consent, sink, clock=Clock())
client = FakeClient(REG)
assert not asyncio.run(inv.maybe_send(client)) and inv.waiting and not sink.sent
up[0] = True
assert asyncio.run(inv.maybe_send(client)) and not inv.waiting and sink.sent[0][0] == "inventory"
+3 -3
View File
@@ -421,9 +421,9 @@ def test_addon_devices_page_shade(tmp_path):
assert 'name="ha_device" value="rbs"' in out and 'name="ha_device" value="garage"' not in out
s.post(do="use", ha_device="rbs", profile="ha-cover", name="Left curtain")
dev_id = next(i for i, d in s.devices().items() if d["profile"] == "ha-cover")
out = s.req()
out = s.req(query=f"p=devices&d={dev_id}")
assert "Room and window" in out and '<option value="Bedroom" selected>Bedroom</option>' in out
assert "It may control: blinds and curtains." in out and "Allow Cantabile to move it" in out
assert "Controls: blinds and curtains" in out and "Allow Cantabile to move it" in out
s.post(do="use", ha_device="dev-ac", profile="ha-thermostat") # the living room's A/C
ac = next(i for i, d in s.devices().items() if d["profile"] == "ha-thermostat")
s.post(do="shade", id=dev_id, area="Living room", facing="SE")
@@ -436,6 +436,6 @@ def test_addon_devices_page_shade(tmp_path):
def run():
i18n.use("de")
return s.req()
return s.req(query=f"p=devices&d={dev_id}")
de = contextvars.copy_context().run(run)
assert "Raum und Fenster" in de and "Südosten" in de and "Bewegen durch Cantabile erlauben" in de