cantabile_edge, cantabile_modbus, cantabile_eebus 0.1.901: full source
No files matched your search
@@ -22,7 +22,7 @@ Settings → Add-ons → Add-on store → ⋮ → Repositories, add
|
|||||||
`https://git.setti.solutions/Daniel/cantabile-ha-addons`, then install the
|
`https://git.setti.solutions/Daniel/cantabile-ha-addons`, then install the
|
||||||
add-on. The Supervisor pulls the prebuilt images
|
add-on. The Supervisor pulls the prebuilt images
|
||||||
(`git.setti.solutions/daniel/cantabile-ha-<edge|modbus|eebus>-<arch>:<version>`,
|
(`git.setti.solutions/daniel/cantabile-ha-<edge|modbus|eebus>-<arch>:<version>`,
|
||||||
named by `image:` in each `config.yaml`); this version: `0.1.676`, arch amd64.
|
named by `image:` in each `config.yaml`); this version: `0.1.901`, arch amd64.
|
||||||
|
|
||||||
## Layout
|
## Layout
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,24 @@ licences permit use and redistribution with these notices.
|
|||||||
|
|
||||||
## Go modules of the link (src/edge/link)
|
## Go modules of the link (src/edge/link)
|
||||||
|
|
||||||
|
### github.com/coder/websocket v1.8.15
|
||||||
|
|
||||||
|
```
|
||||||
|
Copyright (c) 2025 Coder
|
||||||
|
|
||||||
|
Permission to use, copy, modify, and distribute this software for any
|
||||||
|
purpose with or without fee is hereby granted, provided that the above
|
||||||
|
copyright notice and this permission notice appear in all copies.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
```
|
||||||
|
|
||||||
### github.com/creack/pty v1.1.24
|
### github.com/creack/pty v1.1.24
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -49,6 +49,24 @@ nothing more is collected or sent.
|
|||||||
their settings) and **Building** (joining a building). It also sets up
|
their settings) and **Building** (joining a building). It also sets up
|
||||||
Home Assistant's Energy dashboard while you have none of your own. Your
|
Home Assistant's Energy dashboard while you have none of your own. Your
|
||||||
own dashboards are never changed.
|
own dashboards are never changed.
|
||||||
|
- **Cantabile support may open this Home Assistant** (off by default): when
|
||||||
|
you turn it on, Cantabile's support team can open this Home Assistant
|
||||||
|
through Cantabile's connection to help you, signed in as a user of its
|
||||||
|
own, "Cantabile support" (an administrator, so that it can look at your
|
||||||
|
integrations, logs and repairs; never the owner, and never with your
|
||||||
|
password, which it cannot change either). The add-on makes that user and
|
||||||
|
gives it a new password for each session that nobody sees; afterwards the
|
||||||
|
user is disabled again and has no password. A session lasts at most an
|
||||||
|
hour, ends after 15 minutes without use, and every request is recorded.
|
||||||
|
You see the user in Settings → People while it exists, and while a
|
||||||
|
session is open the add-on's own pages (below) say so at the top, with
|
||||||
|
**End now**: it ends the session at once and disables the user again.
|
||||||
|
Turning the option off and restarting the add-on ends it too. On these
|
||||||
|
pages the user "Cantabile support" cannot switch reading or control on,
|
||||||
|
change a protection or make the other choices that are yours. Home
|
||||||
|
Assistant must answer plain HTTP on its own port for this (with an
|
||||||
|
`ssl_certificate` in its `http:` configuration support is told it cannot
|
||||||
|
open it).
|
||||||
|
|
||||||
## Pages
|
## Pages
|
||||||
|
|
||||||
|
|||||||
@@ -275,6 +275,15 @@ def main():
|
|||||||
put(f"{DATA}/claim-code", code + "\n", own=own)
|
put(f"{DATA}/claim-code", code + "\n", own=own)
|
||||||
env["CANTABILE_MODE"] = opts.get("mode", "shadow")
|
env["CANTABILE_MODE"] = opts.get("mode", "shadow")
|
||||||
env["CANTABILE_DASHBOARDS"] = "1" if opts.get("dashboards", True) else "0"
|
env["CANTABILE_DASHBOARDS"] = "1" if opts.get("dashboards", True) else "0"
|
||||||
|
# Remote Home Assistant for Cantabile support (edge/link/hatunnel.go):
|
||||||
|
# only when the owner turns it on here (off by default); the link then
|
||||||
|
# offers it and keeps HA's "Cantabile support" user, through the
|
||||||
|
# Supervisor. Turning it off (and the add-on's restart) ends a session.
|
||||||
|
if opts.get("support_ha") is True:
|
||||||
|
env["CANTABILE_HA_REMOTE"] = "1"
|
||||||
|
log("remote Home Assistant for Cantabile support: allowed in the configuration")
|
||||||
|
else:
|
||||||
|
env.pop("CANTABILE_HA_REMOTE", None)
|
||||||
mqtt(env, own)
|
mqtt(env, own)
|
||||||
|
|
||||||
put(f"{DATA}/config.yaml", CONFIG, 0o644, own, keep=True)
|
put(f"{DATA}/config.yaml", CONFIG, 0o644, own, keep=True)
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
# the Hub's, wrapped by addon-run (Dockerfile). The image is prebuilt,
|
# the Hub's, wrapped by addon-run (Dockerfile). The image is prebuilt,
|
||||||
# per arch (README.md: building it from src/).
|
# per arch (README.md: building it from src/).
|
||||||
name: Cantabile Edge Agent
|
name: Cantabile Edge Agent
|
||||||
version: "0.1.676"
|
version: "0.1.901"
|
||||||
slug: cantabile_edge
|
slug: cantabile_edge
|
||||||
description: Plans your home's energy with Cantabile and, with your consent, runs the devices you choose.
|
description: Plans your home's energy with Cantabile and, with your consent, runs the devices you choose.
|
||||||
url: https://cantabile.cloud
|
url: https://cantabile.cloud
|
||||||
@@ -41,15 +41,20 @@ map:
|
|||||||
# consent_data: the owner's consent, given here when installing (needed: the
|
# consent_data: the owner's consent, given here when installing (needed: the
|
||||||
# add-on connects to Cantabile only with it); consent_control: control,
|
# add-on connects to Cantabile only with it); consent_control: control,
|
||||||
# separately (the Hub's wizard has the same two; DOCS.md "Your consent").
|
# separately (the Hub's wizard has the same two; DOCS.md "Your consent").
|
||||||
|
# support_ha: Cantabile's team may open this Home Assistant as its user
|
||||||
|
# "Cantabile support" (an administrator, never the owner) through Cantabile's
|
||||||
|
# connection; off unless the owner turns it on (edge/link/hatunnel.go).
|
||||||
options:
|
options:
|
||||||
consent_data: false
|
consent_data: false
|
||||||
consent_control: false
|
consent_control: false
|
||||||
mode: shadow
|
mode: shadow
|
||||||
claim_code: ""
|
claim_code: ""
|
||||||
dashboards: true
|
dashboards: true
|
||||||
|
support_ha: false
|
||||||
schema:
|
schema:
|
||||||
consent_data: bool
|
consent_data: bool
|
||||||
consent_control: bool
|
consent_control: bool
|
||||||
mode: list(off|shadow)
|
mode: list(off|shadow)
|
||||||
claim_code: password?
|
claim_code: password?
|
||||||
dashboards: bool
|
dashboards: bool
|
||||||
|
support_ha: bool
|
||||||
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 8.2 KiB |
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 14 KiB |
@@ -28,3 +28,13 @@ configuration:
|
|||||||
Das Dashboard von Cantabile in Home Assistant (ein Eintrag in der
|
Das Dashboard von Cantabile in Home Assistant (ein Eintrag in der
|
||||||
Seitenleiste: Übersicht, Energie, Geräte, Gebäude) und das
|
Seitenleiste: Übersicht, Energie, Geräte, Gebäude) und das
|
||||||
Energie-Dashboard, solange Sie kein eigenes haben.
|
Energie-Dashboard, solange Sie kein eigenes haben.
|
||||||
|
support_ha:
|
||||||
|
name: Der Cantabile-Support darf dieses Home Assistant öffnen
|
||||||
|
description: >-
|
||||||
|
Wenn Sie dies einschalten, darf das Support-Team von Cantabile dieses
|
||||||
|
Home Assistant über die Verbindung zu Cantabile öffnen, angemeldet als
|
||||||
|
Benutzer „Cantabile support“ (ein Administrator; nie Sie und nie mit
|
||||||
|
Ihrem Passwort). Jede Sitzung wird aufgezeichnet, dauert höchstens eine
|
||||||
|
Stunde und endet nach 15 Minuten ohne Nutzung; danach wird der
|
||||||
|
Benutzer wieder deaktiviert. Schalten Sie es aus und starten Sie das
|
||||||
|
Add-on neu, um eine Sitzung sofort zu beenden.
|
||||||
@@ -27,3 +27,12 @@ configuration:
|
|||||||
Cantabile's dashboard in Home Assistant (one sidebar entry: overview,
|
Cantabile's dashboard in Home Assistant (one sidebar entry: overview,
|
||||||
energy, devices, building), and the Energy dashboard while you have
|
energy, devices, building), and the Energy dashboard while you have
|
||||||
none of your own.
|
none of your own.
|
||||||
|
support_ha:
|
||||||
|
name: Cantabile support may open this Home Assistant
|
||||||
|
description: >-
|
||||||
|
When you turn this on, Cantabile's support team may open this Home
|
||||||
|
Assistant through Cantabile's connection, signed in as the user
|
||||||
|
"Cantabile support" (an administrator; never you, and never with your
|
||||||
|
password). Each session is recorded, lasts at most an hour and ends
|
||||||
|
after 15 minutes without use; the user is disabled again afterwards.
|
||||||
|
Turn it off and restart the add-on to end a session at once.
|
||||||
@@ -1,5 +1,15 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## Unreleased
|
||||||
|
|
||||||
|
- Declares to the grid operator's control box only what is known: the
|
||||||
|
heat pumps' and wallboxes' reported power, else the rating you enter on
|
||||||
|
a device's page. Unknown: nothing is declared and the grid operator's
|
||||||
|
use case (LPC) is not offered; the page says why. No more 32 kW, 6 kW per
|
||||||
|
heat pump or 16 A x 3 per wallbox stood in for an unknown power.
|
||||||
|
- The failsafe limit and time are the control box's own; until it sent
|
||||||
|
them §14a's 4.2 kW for the use case's shortest 2 hours.
|
||||||
|
|
||||||
## 0.1.x
|
## 0.1.x
|
||||||
|
|
||||||
- First version (experimental, not yet tried with real devices): pairs
|
- First version (experimental, not yet tried with real devices): pairs
|
||||||
|
|||||||
@@ -49,8 +49,23 @@ lifts its limit, and it can't be unpaired on the page (only the grid
|
|||||||
operator, through Cantabile). While it limits the home, the add-on passes
|
operator, through Cantabile). While it limits the home, the add-on passes
|
||||||
the limit on to the heat pumps and wallboxes at once and the Cantabile
|
the limit on to the heat pumps and wallboxes at once and the Cantabile
|
||||||
agent plans within it. Without the control box's heartbeat for two
|
agent plans within it. Without the control box's heartbeat for two
|
||||||
minutes the failsafe limit holds (4.2 kW unless the control box set
|
minutes the failsafe limit holds for its failsafe time: the control box's
|
||||||
another) for its failsafe time.
|
own values; until it sent them §14a's minimum of 4.2 kW (BNetzA
|
||||||
|
BK6-22-300) for the shortest time the EEBUS use case allows (2 hours).
|
||||||
|
|
||||||
|
Toward the control box the add-on declares the controllable devices'
|
||||||
|
nominal power: what the heat pumps and wallboxes report (a heat pump's
|
||||||
|
nominal power, a wallbox's maximum current and phases), else the rating
|
||||||
|
you enter on a device's page (from its nameplate). It never declares a
|
||||||
|
guess: while any heat pump or wallbox has no known power, or none is
|
||||||
|
paired, nothing is declared and the add-on does not offer the grid
|
||||||
|
operator's use case (LPC); the page's grid card says why ("nominal power
|
||||||
|
unknown: enter the controllable devices' ratings") and which device needs
|
||||||
|
a rating. When a limit ends, a wallbox goes back to its own maximum
|
||||||
|
current, else to the current it had before; if neither is known it stays
|
||||||
|
and the page says so. While its power is not known, a device's share of a
|
||||||
|
limit is an equal one (the sum never exceeds the limit), and a W budget is
|
||||||
|
turned into amps as if on three phases (never more than the budget).
|
||||||
|
|
||||||
## Protection
|
## Protection
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
# Assistant that isn't a Cantabile Hub. Devices are paired on its page
|
# Assistant that isn't a Cantabile Hub. Devices are paired on its page
|
||||||
# (ingress), by their SKI.
|
# (ingress), by their SKI.
|
||||||
name: Cantabile EEBUS
|
name: Cantabile EEBUS
|
||||||
version: "0.1.676"
|
version: "0.1.901"
|
||||||
slug: cantabile_eebus
|
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.
|
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
|
url: https://cantabile.cloud
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 8.2 KiB |
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 14 KiB |
@@ -4,7 +4,7 @@
|
|||||||
# Home Assistant that isn't a Cantabile Hub. Devices come from the options;
|
# Home Assistant that isn't a Cantabile Hub. Devices come from the options;
|
||||||
# the page (ingress) shows them and each control's protection.
|
# the page (ingress) shows them and each control's protection.
|
||||||
name: Cantabile Modbus bridge
|
name: Cantabile Modbus bridge
|
||||||
version: "0.1.676"
|
version: "0.1.901"
|
||||||
slug: cantabile_modbus
|
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.
|
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
|
url: https://cantabile.cloud
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 8.2 KiB |
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 14 KiB |
@@ -13,9 +13,18 @@ Tariff v1 (agreed with the cloud side, 2026-10-28):
|
|||||||
quarter hour>, "step_s": 900, "steps": 192, "expires", "currency": "EUR",
|
quarter hour>, "step_s": 900, "steps": 192, "expires", "currency": "EUR",
|
||||||
"unit": "ct/kWh", "import": [192 numbers|null], "export": [...],
|
"unit": "ct/kWh", "import": [192 numbers|null], "export": [...],
|
||||||
"estimated": [indexes], "other": {...}, "sources": [...]}
|
"estimated": [indexes], "other": {...}, "sources": [...]}
|
||||||
After `expires` (or outside its steps): the last known flat price (the
|
Its prices are taken as they are, the steps fleet estimated (listed in
|
||||||
mean of its import prices), and the plan says so. Without any tariff:
|
"estimated": the day-ahead before it is published) too; a step it has no
|
||||||
the configured default price.
|
price for (null, or beyond its steps) has none here either. After
|
||||||
|
`expires`, or without any tariff, there is no price at all: no default,
|
||||||
|
no last price held (docs/parameters.md U1). prices() says so (status
|
||||||
|
"none" and the reason); the planner then plans without prices (comfort,
|
||||||
|
limits and the owner's musts only) and the box's panel says why and what
|
||||||
|
to do. A missing export price is no price, not 0 ct (optimizer.py).
|
||||||
|
Gas and heat (fleet heatprice.go, 2026-11-05): "gas" and "heat" per step
|
||||||
|
(ct/kWh gross: gas billed on Hs, heat delivered), null where none;
|
||||||
|
carrier_prices() reads them the same way (no default, nothing held past
|
||||||
|
the bundle's expiry). A bundle may price gas or heat alone.
|
||||||
|
|
||||||
Model v1 (the forecasts' data; the ML in ml/ is a placeholder, 2026-10-28):
|
Model v1 (the forecasts' data; the ML in ml/ is a placeholder, 2026-10-28):
|
||||||
{"v":1, "kind":"model", "id", "site" (or "*"), "issued", "expires"?,
|
{"v":1, "kind":"model", "id", "site" (or "*"), "issued", "expires"?,
|
||||||
@@ -115,12 +124,12 @@ def _check_tariff(d: dict):
|
|||||||
raise ValueError("not a v1 tariff")
|
raise ValueError("not a v1 tariff")
|
||||||
if int(d["step_s"]) != 900 or int(d["start"]) % 900:
|
if int(d["step_s"]) != 900 or int(d["start"]) % 900:
|
||||||
raise ValueError("steps of 900 s from a quarter hour")
|
raise ValueError("steps of 900 s from a quarter hour")
|
||||||
for k in ("import", "export"):
|
for k in ("import", "export", "gas", "heat"):
|
||||||
v = d.get(k) or []
|
v = d.get(k) or []
|
||||||
if not isinstance(v, list) or not all(x is None or isinstance(x, (int, float)) for x in v):
|
if not isinstance(v, list) or not all(x is None or isinstance(x, (int, float)) for x in v):
|
||||||
raise ValueError(f"{k}: numbers or null")
|
raise ValueError(f"{k}: numbers or null")
|
||||||
if not [x for x in d.get("import") or [] if x is not None]:
|
if not [x for k in ("import", "gas", "heat") for x in d.get(k) or [] if x is not None]:
|
||||||
raise ValueError("no import price")
|
raise ValueError("no import, gas or heat price")
|
||||||
|
|
||||||
|
|
||||||
SERIES_MAX = 400 # a model's series: steps of 900 s (~4 days)
|
SERIES_MAX = 400 # a model's series: steps of 900 s (~4 days)
|
||||||
@@ -220,9 +229,28 @@ def _check_market(d: dict):
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Prices:
|
class Prices:
|
||||||
import_ct: list[float]
|
import_ct: list # per step ct/kWh, or None where the tariff has no price
|
||||||
export_ct: list[float]
|
export_ct: list # per step ct/kWh, or None (export unpriced: no value, not 0 ct)
|
||||||
note: str # what the prices are ("tariff <id>", "flat fallback ...")
|
note: str # what the prices are ("tariff <id>", "no tariff: ...")
|
||||||
|
status: str = "tariff" # tariff | none (no price to plan with)
|
||||||
|
reason: str = "" # none: never | expired | other_site | no_price_now
|
||||||
|
tariff_id: str = ""
|
||||||
|
expired_at: int = 0 # the tariff's expiry (reason expired)
|
||||||
|
priced_steps: int = 0 # the steps from the first with an import price, without a gap
|
||||||
|
|
||||||
|
|
||||||
|
NO_TARIFF = "no tariff: price optimisation off"
|
||||||
|
NO_TARIFF_WHY = {
|
||||||
|
"never": "no tariff received yet: the platform has none for this site, or has not delivered it",
|
||||||
|
"expired": "the tariff {id} expired at {at}",
|
||||||
|
"other_site": "the tariff file is another site's",
|
||||||
|
"no_price_now": "the tariff {id} has no import price for now",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def no_tariff_note(reason: str, tariff_id: str = "", expired_at: int = 0) -> str:
|
||||||
|
at = datetime.fromtimestamp(expired_at, timezone.utc).strftime("%Y-%m-%d %H:%M UTC") if expired_at else ""
|
||||||
|
return f"{NO_TARIFF} ({NO_TARIFF_WHY.get(reason, reason).format(id=tariff_id, at=at)})"
|
||||||
|
|
||||||
|
|
||||||
def _ahead_terms(ah, start: int, step_s: int, steps: int, terms) -> list | None:
|
def _ahead_terms(ah, start: int, step_s: int, steps: int, terms) -> list | None:
|
||||||
@@ -262,13 +290,12 @@ def _ahead_terms(ah, start: int, step_s: int, steps: int, terms) -> list | None:
|
|||||||
|
|
||||||
|
|
||||||
class Bundles:
|
class Bundles:
|
||||||
def __init__(self, state_dir: str, site: str, default_price_ct: float = 30.0):
|
def __init__(self, state_dir: str, site: str):
|
||||||
self.site, self.default = site, default_price_ct
|
self.site = site
|
||||||
self.tariff = _File(os.path.join(state_dir, "tariff.json"), _check_tariff)
|
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.model = _File(os.path.join(state_dir, "model.json"), _check_model)
|
||||||
self.flats = _File(os.path.join(state_dir, "flats.json"), _check_flats)
|
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.market = _File(os.path.join(state_dir, "market.json"), _check_market)
|
||||||
self.flat: float | None = None # the last known tariff's mean import price
|
|
||||||
self._box_key, self._box, self.box_errors = None, {}, {}
|
self._box_key, self._box, self.box_errors = None, {}, {}
|
||||||
self._box_cache: dict = {} # (series, start, model id) -> the box's forecast
|
self._box_cache: dict = {} # (series, start, model id) -> the box's forecast
|
||||||
|
|
||||||
@@ -278,39 +305,66 @@ class Bundles:
|
|||||||
return (self.tariff.stamp(), self.model.stamp())
|
return (self.tariff.stamp(), self.model.stamp())
|
||||||
|
|
||||||
def prices(self, start: int, step_s: int, steps: int, now: float) -> Prices:
|
def prices(self, start: int, step_s: int, steps: int, now: float) -> Prices:
|
||||||
|
"""The tariff's prices for the horizon, as they are: None where it
|
||||||
|
has none. No tariff, another site's, an expired one or none for the
|
||||||
|
first step: status "none", no prices at all, and why."""
|
||||||
t = self.tariff.get()
|
t = self.tariff.get()
|
||||||
if t is not None and t.get("site") not in (self.site, None, ""):
|
none = [None] * steps
|
||||||
t = None # the link refuses these; a copied file must not count either
|
|
||||||
if t is not None:
|
|
||||||
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:
|
if t is None:
|
||||||
note = "no tariff yet: default price" if self.flat is None else "no tariff: last known flat price"
|
return Prices(none, list(none), no_tariff_note("never"), "none", "never")
|
||||||
return Prices([flat] * steps, [0.0] * steps, note + "; feed-in at 0 ct/kWh")
|
if t.get("site") not in (self.site, None, ""):
|
||||||
expired = now >= t.get("expires", 0)
|
return Prices(none, list(none), no_tariff_note("other_site"), "none", "other_site")
|
||||||
out_i, out_e, outside, no_export = [], [], 0, 0
|
tid, exp = str(t.get("id") or ""), int(t.get("expires", 0) or 0)
|
||||||
|
if now >= exp:
|
||||||
|
return Prices(none, list(none), no_tariff_note("expired", tid, exp), "none", "expired", tid, exp)
|
||||||
|
imp, exs = t["import"], t.get("export") or []
|
||||||
|
out_i, out_e = [], []
|
||||||
for k in range(steps):
|
for k in range(steps):
|
||||||
i = (start + k * step_s - t["start"]) // 900
|
i = (start + k * step_s - t["start"]) // 900
|
||||||
pi = t["import"][i] if not expired and 0 <= i < len(t["import"]) else None
|
# a step after the tariff's expiry has no price either
|
||||||
pe = (t.get("export") or [])[i] if not expired and 0 <= i < len(t.get("export") or []) else None
|
live = start + k * step_s < exp
|
||||||
outside += pi is None
|
out_i.append(imp[i] if live and 0 <= i < len(imp) else None)
|
||||||
no_export += pe is None
|
out_e.append(exs[i] if live and 0 <= i < len(exs) else None)
|
||||||
out_i.append(pi if pi is not None else flat)
|
n = next((k for k, v in enumerate(out_i) if v is None), steps)
|
||||||
out_e.append(pe if pe is not None else 0.0)
|
if n == 0:
|
||||||
if expired:
|
return Prices(none, list(none), no_tariff_note("no_price_now", tid), "none", "no_price_now", tid, exp)
|
||||||
note = f"tariff {t.get('id')} expired: flat {flat:.1f} ct/kWh"
|
note = f"tariff {tid}"
|
||||||
elif outside:
|
if n < steps:
|
||||||
note = f"tariff {t.get('id')}; {outside} of {steps} steps at the flat {flat:.1f} ct/kWh"
|
note += f"; prices for {n * step_s / 3600:g} h of {steps * step_s / 3600:g} h: the plan covers those"
|
||||||
else:
|
no_export = sum(v is None for v in out_e[:n])
|
||||||
note = f"tariff {t.get('id')}"
|
if no_export == n:
|
||||||
if no_export == steps:
|
note += "; no export price: export unpriced (no value)"
|
||||||
note += "; feed-in at 0 ct/kWh"
|
|
||||||
elif no_export:
|
elif no_export:
|
||||||
note += f"; feed-in at 0 ct/kWh in {no_export} of {steps} steps (no export price)"
|
note += f"; no export price in {no_export} of {n} steps: export unpriced there"
|
||||||
return Prices(out_i, out_e, note)
|
est = len([x for x in t.get("estimated") or [] if isinstance(x, int)])
|
||||||
|
if est:
|
||||||
|
note += f"; {est} steps estimated by the platform"
|
||||||
|
return Prices(out_i, out_e, note, "tariff", "", tid, exp, n)
|
||||||
|
|
||||||
|
def carrier_prices(self, carrier: str, start: int, step_s: int, steps: int, now: float) -> list:
|
||||||
|
"""The tariff's gas or heat prices for the horizon (ct/kWh gross),
|
||||||
|
None where it has none: no tariff, another site's, expired, a step
|
||||||
|
beyond it or a bundle without the series."""
|
||||||
|
t = self.tariff.get()
|
||||||
|
none = [None] * steps
|
||||||
|
if t is None or t.get("site") not in (self.site, None, "") or now >= int(t.get("expires", 0) or 0):
|
||||||
|
return none
|
||||||
|
xs, exp = t.get(carrier) or [], int(t.get("expires", 0) or 0)
|
||||||
|
out = []
|
||||||
|
for k in range(steps):
|
||||||
|
i = (start + k * step_s - t["start"]) // 900
|
||||||
|
out.append(xs[i] if start + k * step_s < exp and 0 <= i < len(xs) else None)
|
||||||
|
return out
|
||||||
|
|
||||||
|
def box_params(self, now: float) -> dict:
|
||||||
|
"""The Parameters page's values the box plans with (fleet heatprice.go
|
||||||
|
boxParams: the tariff bundle's "params", e.g. heat.hybrid_switch_eur),
|
||||||
|
{} without a valid tariff bundle: what needs one is refused."""
|
||||||
|
t = self.tariff.get()
|
||||||
|
if t is None or t.get("site") not in (self.site, None, "") or now >= int(t.get("expires", 0) or 0):
|
||||||
|
return {}
|
||||||
|
p = t.get("params")
|
||||||
|
return {k: v for k, v in p.items() if isinstance(k, str)} if isinstance(p, dict) else {}
|
||||||
|
|
||||||
def tariff_now(self, now: float) -> dict | None:
|
def tariff_now(self, now: float) -> dict | None:
|
||||||
"""The tariff in force for HA: import now and next step, export now,
|
"""The tariff in force for HA: import now and next step, export now,
|
||||||
|
|||||||
@@ -21,8 +21,12 @@ from .mapping import Mapping, suggest
|
|||||||
|
|
||||||
class _Json(logging.Formatter):
|
class _Json(logging.Formatter):
|
||||||
def format(self, r):
|
def format(self, r):
|
||||||
return json.dumps({"time": self.formatTime(r, "%Y-%m-%dT%H:%M:%S"), "level": r.levelname,
|
out = {"time": self.formatTime(r, "%Y-%m-%dT%H:%M:%S"), "level": r.levelname,
|
||||||
"logger": r.name, "msg": r.getMessage()})
|
"logger": r.name, "msg": r.getMessage()}
|
||||||
|
if r.exc_info:
|
||||||
|
# a logged exception's traceback (exc_info=True) in the same line
|
||||||
|
out["exc"] = self.formatException(r.exc_info)
|
||||||
|
return json.dumps(out)
|
||||||
|
|
||||||
|
|
||||||
def _logging():
|
def _logging():
|
||||||
|
|||||||
@@ -54,7 +54,6 @@ class Config:
|
|||||||
replan_debounce_s: float = 3.0 # a pause after a change before planning again
|
replan_debounce_s: float = 3.0 # a pause after a change before planning again
|
||||||
solve_timeout_s: float = 60.0 # the optimiser's subprocess is killed after this
|
solve_timeout_s: float = 60.0 # the optimiser's subprocess is killed after this
|
||||||
plan_stale_s: int = 1800 # older plans: the safe defaults
|
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
|
# 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;
|
# 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.
|
# a site with a smaller fuse (or a building's larger one) sets its own.
|
||||||
@@ -90,6 +89,11 @@ def load(path: str | None, require_site: bool = True) -> Config:
|
|||||||
sink = SinkConfig(**(raw.pop("sink", None) or {}))
|
sink = SinkConfig(**(raw.pop("sink", None) or {}))
|
||||||
ha = raw.pop("ha", None) or {}
|
ha = raw.pop("ha", None) or {}
|
||||||
token_file = os.environ.get("HA_TOKEN_FILE", ha.get("token_file", ""))
|
token_file = os.environ.get("HA_TOKEN_FILE", ha.get("token_file", ""))
|
||||||
|
# a price without a tariff is no longer configurable (docs/parameters.md U1):
|
||||||
|
# an older file's default_price_ct is ignored, said once
|
||||||
|
if raw.pop("default_price_ct", None) is not None:
|
||||||
|
import logging
|
||||||
|
logging.getLogger(__name__).warning("config: default_price_ct is ignored: without a tariff the plan has no prices")
|
||||||
cfg = Config(sink=sink, ha_url=ha.get("url", Config.ha_url), **raw)
|
cfg = Config(sink=sink, ha_url=ha.get("url", Config.ha_url), **raw)
|
||||||
env = os.environ
|
env = os.environ
|
||||||
cfg.site_id = env.get("CANTABILE_SITE_ID", cfg.site_id)
|
cfg.site_id = env.get("CANTABILE_SITE_ID", cfg.site_id)
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ class Controller:
|
|||||||
self.mode = Mode(cfg.state_dir, cfg.mode)
|
self.mode = Mode(cfg.state_dir, cfg.mode)
|
||||||
self.col = Collector()
|
self.col = Collector()
|
||||||
self.history = History(os.path.join(cfg.state_dir, "history.json"))
|
self.history = History(os.path.join(cfg.state_dir, "history.json"))
|
||||||
self.bundles = Bundles(cfg.state_dir, cfg.site_id, cfg.default_price_ct)
|
self.bundles = Bundles(cfg.state_dir, cfg.site_id)
|
||||||
self.guard = Guard(self.controls, consent, self.mode, override_hold_s=cfg.override_hold_s,
|
self.guard = Guard(self.controls, consent, self.mode, override_hold_s=cfg.override_hold_s,
|
||||||
min_switch_s=cfg.min_switch_s, max_actions_per_hour=cfg.max_actions_per_hour, clock=clock)
|
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.saved_limits = self._load_limits() # the console's (limits.json)
|
||||||
@@ -256,8 +256,28 @@ class Controller:
|
|||||||
return {c.kind for c in self.controls if c.kind in ("heating", "cooling", "ev")}
|
return {c.kind for c in self.controls if c.kind in ("heating", "cooling", "ev")}
|
||||||
|
|
||||||
# -- plans ---------------------------------------------------------------
|
# -- plans ---------------------------------------------------------------
|
||||||
|
def owner_ratings(self):
|
||||||
|
"""The owner's ratings of each control's device (the panel's Devices
|
||||||
|
page: devices-specs.json, written by the Hub's devices tool) into
|
||||||
|
the controls (Control.rated, rated_device): a battery's capacity,
|
||||||
|
power and efficiency where the mapping and the device say none."""
|
||||||
|
try:
|
||||||
|
with open(os.path.join(self.cfg.state_dir, "devices-specs.json")) as f:
|
||||||
|
specs = json.load(f).get("devices") or {}
|
||||||
|
except (OSError, ValueError, AttributeError):
|
||||||
|
specs = {}
|
||||||
|
by = {}
|
||||||
|
for d in specs.values() if isinstance(specs, dict) else []:
|
||||||
|
if not isinstance(d, dict):
|
||||||
|
continue
|
||||||
|
for cid in d.get("controls") or []:
|
||||||
|
by.setdefault(cid, (d.get("ratings") or {}, str(d.get("name") or "")))
|
||||||
|
for c in self.controls:
|
||||||
|
c.rated, c.rated_device = by.get(c.id, ({}, ""))
|
||||||
|
|
||||||
def model_state(self) -> dict:
|
def model_state(self) -> dict:
|
||||||
now = self.clock()
|
now = self.clock()
|
||||||
|
self.owner_ratings()
|
||||||
start = int(now // inputs_mod.STEP_S * inputs_mod.STEP_S)
|
start = int(now // inputs_mod.STEP_S * inputs_mod.STEP_S)
|
||||||
# sealed devices are not planned at all
|
# sealed devices are not planned at all
|
||||||
# and of a thermostat that heats and cools only its active side
|
# and of a thermostat that heats and cools only its active side
|
||||||
@@ -314,9 +334,11 @@ class Controller:
|
|||||||
self.executor.inputs_ok = self.inputs_fresh()
|
self.executor.inputs_ok = self.inputs_fresh()
|
||||||
self.act_now.set() # carry out a new plan now
|
self.act_now.set() # carry out a new plan now
|
||||||
n = sum(len(v) for v in plan["devices"].values())
|
n = sum(len(v) for v in plan["devices"].values())
|
||||||
log.info("plan %s (%s, %.1f s): %d changes in 48 h, %.2f EUR vs %.2f EUR baseline; %s",
|
cost = (f"{plan['cost_eur']:.2f} EUR vs {plan['baseline_eur']:.2f} EUR baseline"
|
||||||
plan["id"], plan["status"], plan["solve_s"], n, plan["cost_eur"], plan["baseline_eur"],
|
if plan.get("cost_eur") is not None and plan.get("baseline_eur") is not None else "no cost (no tariff)")
|
||||||
plan.get("price_note", ""))
|
log.info("plan %s (%s, %.1f s): %d changes, %s; %s%s", plan["id"], plan["status"], plan["solve_s"], n, cost,
|
||||||
|
plan.get("price_note", ""),
|
||||||
|
"".join(f"; {k} not planned: {v['why']}" for k, v in (plan.get("refused") or {}).items()))
|
||||||
await self._publish("plan", self.public_plan(plan))
|
await self._publish("plan", self.public_plan(plan))
|
||||||
for cb in self.on_plan:
|
for cb in self.on_plan:
|
||||||
await cb(plan)
|
await cb(plan)
|
||||||
@@ -328,6 +350,9 @@ class Controller:
|
|||||||
"mode": plan["mode"], "devices": plan["devices"], "cost_eur": plan["cost_eur"],
|
"mode": plan["mode"], "devices": plan["devices"], "cost_eur": plan["cost_eur"],
|
||||||
"baseline_eur": plan["baseline_eur"], "status": plan["status"],
|
"baseline_eur": plan["baseline_eur"], "status": plan["status"],
|
||||||
"prices": plan.get("price_note", ""), "model": plan.get("model", ""),
|
"prices": plan.get("price_note", ""), "model": plan.get("model", ""),
|
||||||
|
# without a tariff: priced false, cost and baseline null (U1); controls not
|
||||||
|
# planned and why (U2); absent from older agents
|
||||||
|
"priced": plan.get("priced", True), "refused": plan.get("refused", {}),
|
||||||
"sources": plan.get("sources", {}),
|
"sources": plan.get("sources", {}),
|
||||||
# for the building (shed_w/grid_w per step, 24 h): absent from older agents
|
# for the building (shed_w/grid_w per step, 24 h): absent from older agents
|
||||||
"flex": plan.get("flex", {}),
|
"flex": plan.get("flex", {}),
|
||||||
@@ -364,6 +389,8 @@ class Controller:
|
|||||||
while not stop.is_set():
|
while not stop.is_set():
|
||||||
try:
|
try:
|
||||||
n = self.prefs.take_inbox({c.id: c for c in self.controls}, self.tz)
|
n = self.prefs.take_inbox({c.id: c for c in self.controls}, self.tz)
|
||||||
|
while self.prefs.outbox: # the owner's own tariff to the cloud (link op own_tariff)
|
||||||
|
await self._publish("own_tariff", self.prefs.outbox.pop(0))
|
||||||
if n or self.prefs.prune(self.clock()):
|
if n or self.prefs.prune(self.clock()):
|
||||||
await self._prefs_changed()
|
await self._prefs_changed()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -417,7 +444,8 @@ class Controller:
|
|||||||
control at its safe default), and HA sends no update for an
|
control at its safe default), and HA sends no update for an
|
||||||
unchanged value, so its age says nothing."""
|
unchanged value, so its age says nothing."""
|
||||||
for c in self.controls:
|
for c in self.controls:
|
||||||
for role in ("temp", "soc"):
|
# a battery without its state of charge is not planned (inputs.battery_refusal), the rest is
|
||||||
|
for role in ("temp",):
|
||||||
if role in c.entities and self.col.value(c.entities[role]) is None:
|
if role in c.entities and self.col.value(c.entities[role]) is None:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
@@ -518,7 +546,8 @@ class Controller:
|
|||||||
try:
|
try:
|
||||||
await self.make_plan()
|
await self.make_plan()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.warning("plan: %s", e)
|
# with its traceback: where it failed (the message alone did not say)
|
||||||
|
log.warning("plan: %s", e, exc_info=True)
|
||||||
self.wake.clear()
|
self.wake.clear()
|
||||||
woken = False
|
woken = False
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ that the owner's consent for the whole site must cover control
|
|||||||
consent: false
|
consent: false
|
||||||
entities: {charge_from_grid: switch.inverter_charge_from_grid,
|
entities: {charge_from_grid: switch.inverter_charge_from_grid,
|
||||||
setpoint: number.battery_power, soc: sensor.battery_soc}
|
setpoint: number.battery_power, soc: sensor.battery_soc}
|
||||||
params: {capacity_wh: 10000, max_charge_w: 3000, max_discharge_w: 3000}
|
params: {capacity_wh: 10000, max_charge_w: 3000, max_discharge_w: 3000, efficiency: 0.95}
|
||||||
limits: {soc_pct: {owner: [20, 100]}}
|
limits: {soc_pct: {owner: [20, 100]}}
|
||||||
safe: {charge_from_grid: false}
|
safe: {charge_from_grid: false}
|
||||||
|
|
||||||
@@ -30,7 +30,18 @@ Kinds, their entities (* needed) and what the executor sets (channels):
|
|||||||
battery charge_from_grid (switch) and/or setpoint (number, W: + charge,
|
battery charge_from_grid (switch) and/or setpoint (number, W: + charge,
|
||||||
- discharge); mode (select: the inverter's own mode, set to
|
- discharge); mode (select: the inverter's own mode, set to
|
||||||
params.manual_option while the plan sets power, else to
|
params.manual_option while the plan sets power, else to
|
||||||
params.auto_option); soc (sensor %), power (sensor W).
|
params.auto_option); soc (sensor %), power (sensor W);
|
||||||
|
the device's own ratings where it reports them: capacity
|
||||||
|
(sensor kWh or Wh), rated_power (sensor W or kW, charge and
|
||||||
|
discharge), efficiency (sensor %, round trip).
|
||||||
|
The battery's facts (battery_facts): capacity, charge and
|
||||||
|
discharge power and efficiency from the mapping's params, else
|
||||||
|
the device's reported ratings (those entities), else the owner's
|
||||||
|
ratings on the panel (devices-specs.json: battery_kwh,
|
||||||
|
battery_kw, battery_efficiency_pct). No default stands in for
|
||||||
|
any of them (docs/parameters.md U2): without one the battery is
|
||||||
|
not planned and the plan says which is missing and where to
|
||||||
|
enter it; without a state of charge from its sensor neither.
|
||||||
With a setpoint the agent sets the power there (and the mode)
|
With a setpoint the agent sets the power there (and the mode)
|
||||||
and keeps charge_from_grid OFF: on many inverters (and the
|
and keeps charge_from_grid OFF: on many inverters (and the
|
||||||
simulated ones) the switch means "charge at full power" and would
|
simulated ones) the switch means "charge at full power" and would
|
||||||
@@ -38,7 +49,28 @@ Kinds, their entities (* needed) and what the executor sets (channels):
|
|||||||
Only a battery without a setpoint (an HM8042 as the
|
Only a battery without a setpoint (an HM8042 as the
|
||||||
profile has it: the switch alone) is driven through charge_from_grid.
|
profile has it: the switch alone) is driven through charge_from_grid.
|
||||||
heating climate* (target temperature) or switch* (on/off, a boiler);
|
heating climate* (target temperature) or switch* (on/off, a boiler);
|
||||||
temp (sensor °C, else the climate's current temperature)
|
temp (sensor °C, else the climate's current temperature);
|
||||||
|
source (select: a hybrid's heat source, its options named in
|
||||||
|
params.source_options {heat_pump: ..., boiler: ...}); efficiency
|
||||||
|
(sensor %, read only: a boiler's rated efficiency).
|
||||||
|
What makes the heat (params.heat_source; docs/methods.md E6):
|
||||||
|
electric (default) electricity at params.cop (a heat pump
|
||||||
|
~3, direct electric 1): its heat costs the import
|
||||||
|
price ÷ COP
|
||||||
|
heat_pump the same, with params.cop_curve [[°C outside, COP],
|
||||||
|
...] from its datasheet where given
|
||||||
|
boiler gas: heat costs the gas price ÷ the boiler's
|
||||||
|
efficiency (heat out per kWh of gas billed, Hs)
|
||||||
|
district the heat price per kWh (the heat meter's: the
|
||||||
|
site's heat tariff or its building's)
|
||||||
|
hybrid a heat pump and a boiler; the plan chooses one per
|
||||||
|
quarter-hour (the cheaper heat, in the comfort's
|
||||||
|
bounds) and sets the source select
|
||||||
|
The boiler's efficiency and max_heat_w (W of heat) are facts of
|
||||||
|
the device (FACTS: the mapping's params, else its efficiency
|
||||||
|
sensor, else the owner's ratings boiler_efficiency_pct and
|
||||||
|
heat_kw); without them, or without the gas or heat price, the
|
||||||
|
control is not planned and the panel says why (U2's rule).
|
||||||
cooling climate* (hvac_mode cool/off, target temperature); temp
|
cooling climate* (hvac_mode cool/off, target temperature); temp
|
||||||
A climate's own range (its min_temp..max_temp) narrows the
|
A climate's own range (its min_temp..max_temp) narrows the
|
||||||
kind's range, and a target temperature is rounded to its
|
kind's range, and a target temperature is rounded to its
|
||||||
@@ -74,8 +106,14 @@ log = logging.getLogger(__name__)
|
|||||||
# kind -> {entity role: allowed domains}; the roles the executor writes
|
# kind -> {entity role: allowed domains}; the roles the executor writes
|
||||||
ENTITIES = {
|
ENTITIES = {
|
||||||
"battery": {"charge_from_grid": ("switch", "input_boolean"), "setpoint": ("number", "input_number"),
|
"battery": {"charge_from_grid": ("switch", "input_boolean"), "setpoint": ("number", "input_number"),
|
||||||
"mode": ("select", "input_select"), "soc": ("sensor", "input_number"), "power": ("sensor",)},
|
"mode": ("select", "input_select"), "soc": ("sensor", "input_number"), "power": ("sensor",),
|
||||||
"heating": {"climate": ("climate",), "switch": ("switch", "input_boolean"), "temp": ("sensor",)},
|
# read only: the device's own ratings (battery_facts)
|
||||||
|
"capacity": ("sensor", "input_number"), "rated_power": ("sensor", "input_number"),
|
||||||
|
"efficiency": ("sensor", "input_number")},
|
||||||
|
"heating": {"climate": ("climate",), "switch": ("switch", "input_boolean"), "temp": ("sensor",),
|
||||||
|
"source": ("select", "input_select"),
|
||||||
|
# read only: the boiler's own rated efficiency (%; heat_facts)
|
||||||
|
"efficiency": ("sensor", "input_number")},
|
||||||
"cooling": {"climate": ("climate",), "temp": ("sensor",)},
|
"cooling": {"climate": ("climate",), "temp": ("sensor",)},
|
||||||
"ev": {"switch": ("switch", "input_boolean"), "current": ("number", "input_number"),
|
"ev": {"switch": ("switch", "input_boolean"), "current": ("number", "input_number"),
|
||||||
"connected": ("binary_sensor", "input_boolean"), "soc": ("sensor", "input_number"),
|
"connected": ("binary_sensor", "input_boolean"), "soc": ("sensor", "input_number"),
|
||||||
@@ -84,25 +122,34 @@ ENTITIES = {
|
|||||||
"phases": ("sensor",), "power": ("sensor",), "current_measured": ("sensor",)},
|
"phases": ("sensor",), "power": ("sensor",), "current_measured": ("sensor",)},
|
||||||
"cover": {"cover": ("cover",)},
|
"cover": {"cover": ("cover",)},
|
||||||
}
|
}
|
||||||
WRITES = {"charge_from_grid", "setpoint", "mode", "climate", "switch", "current", "cover"}
|
WRITES = {"charge_from_grid", "setpoint", "mode", "climate", "switch", "current", "cover", "source"}
|
||||||
NEEDED = {"battery": (("charge_from_grid", "setpoint"),), "heating": (("climate", "switch"),),
|
NEEDED = {"battery": (("charge_from_grid", "setpoint"),), "heating": (("climate", "switch"),),
|
||||||
"cooling": (("climate",),), "ev": (("switch",),), "cover": (("cover",),)}
|
"cooling": (("climate",),), "ev": (("switch",),), "cover": (("cover",),)}
|
||||||
# defaults per kind (params may override each)
|
# defaults per kind (params may override each)
|
||||||
PARAMS = {
|
PARAMS = {
|
||||||
"battery": {"capacity_wh": 5000.0, "max_charge_w": 2500.0, "max_discharge_w": 2500.0, "efficiency": 0.95,
|
# capacity_wh, max_charge_w, max_discharge_w, efficiency (one way, 0-1):
|
||||||
"soc_pct": 50.0, # soc_pct: the estimate when no sensor says
|
# facts of the device, no default (FACTS; battery_facts)
|
||||||
"manual_option": "manual", "auto_option": "self-consumption", "min_switch_s": 0.0,
|
"battery": {"manual_option": "manual", "auto_option": "self-consumption", "min_switch_s": 0.0,
|
||||||
# steady.py: the least time between two changes of a mode / a setpoint, and
|
# steady.py: the least time between two changes of a mode / a setpoint, and
|
||||||
# the least gain (ct) a battery's change must bring over its hold
|
# the least gain (ct) a battery's change must bring over its hold
|
||||||
"hold_mode_s": 1800.0, "hold_set_s": 900.0, "min_gain_ct": 1.0},
|
"hold_mode_s": 1800.0, "hold_set_s": 900.0, "min_gain_ct": 1.0},
|
||||||
"heating": {"r_k_per_kw": 10.0, "c_kwh_per_k": 3.0, "max_power_w": 2000.0, "comfort_c": 21.0,
|
# r_k_per_kw, c_kwh_per_k (the room), max_power_w, cop (the device): facts,
|
||||||
"setback_c": 17.0, "cop": 1.0, # cop 1: electric; a heat pump ~3
|
# no default (FACTS: without them the room is not optimised,
|
||||||
|
# inputs.room_refusal). comfort_c / setback_c / setup_c: the box's copy of
|
||||||
|
# the Parameters rows comfort.heating_setpoints / comfort.cooling_setpoints
|
||||||
|
# (docs/parameters.md C7) until the bundle carries them; the owner's
|
||||||
|
# schedule (preferences.py) and the mapping's win
|
||||||
|
"heating": {"comfort_c": 21.0, "setback_c": 17.0,
|
||||||
"min_switch_s": 600.0,
|
"min_switch_s": 600.0,
|
||||||
# the room's other heat (W; the model's fit, inputs._room_rc): internal
|
# 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
|
# (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, "hold_mode_s": 1800.0, "hold_set_s": 900.0, "min_gain_ct": 1.0},
|
# none modelled unless the fit or the mapping gives it
|
||||||
"cooling": {"r_k_per_kw": 10.0, "c_kwh_per_k": 3.0, "max_power_w": 1500.0, "comfort_c": 25.0,
|
"internal_gain_w": 0.0, "solar_m2": 0.0, "hold_mode_s": 1800.0, "hold_set_s": 900.0, "min_gain_ct": 1.0,
|
||||||
"setup_c": 28.0, "cop": 3.0, "min_switch_s": 600.0, "internal_gain_w": 0.0, "solar_m2": 0.0,
|
# what makes the heat (docs/methods.md E6): electric | heat_pump | boiler | district | hybrid;
|
||||||
|
# a hybrid's source select options; a heat pump's datasheet COP by outside temperature
|
||||||
|
"heat_source": "electric", "source_options": {}, "cop_curve": []},
|
||||||
|
"cooling": {"comfort_c": 25.0,
|
||||||
|
"setup_c": 28.0, "min_switch_s": 600.0, "internal_gain_w": 0.0, "solar_m2": 0.0,
|
||||||
"hold_mode_s": 1800.0, "hold_set_s": 900.0, "min_gain_ct": 1.0},
|
"hold_mode_s": 1800.0, "hold_set_s": 900.0, "min_gain_ct": 1.0},
|
||||||
"ev": {"need_wh": 10000.0, "departure": "07:00", "min_current_a": 6.0, "max_current_a": 16.0,
|
"ev": {"need_wh": 10000.0, "departure": "07:00", "min_current_a": 6.0, "max_current_a": 16.0,
|
||||||
"phases": None, "voltage": 230.0, "min_switch_s": 0.0, # phases: None, not said (evphases.py)
|
"phases": None, "voltage": 230.0, "min_switch_s": 0.0, # phases: None, not said (evphases.py)
|
||||||
@@ -113,6 +160,25 @@ PARAMS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# params a kind accepts without a default: facts of the device that come
|
||||||
|
# from the mapping, the device or its owner, or the feature refuses
|
||||||
|
FACTS = {"battery": ("capacity_wh", "max_charge_w", "max_discharge_w", "efficiency"),
|
||||||
|
# the room's R and C, the device's electric power and COP (room_refusal);
|
||||||
|
# a boiler's or a heat station's: its efficiency (heat per kWh of gas billed, Hs, 0-1)
|
||||||
|
# and its most heat (W); heat_facts
|
||||||
|
"heating": ("r_k_per_kw", "c_kwh_per_k", "max_power_w", "cop", "boiler_efficiency", "max_heat_w"),
|
||||||
|
"cooling": ("r_k_per_kw", "c_kwh_per_k", "max_power_w", "cop")}
|
||||||
|
HEAT_SOURCES = ("electric", "heat_pump", "boiler", "district", "hybrid")
|
||||||
|
# the owner's ratings of a heating device (devices-specs.json) and their labels
|
||||||
|
HEAT_RATINGS = {"boiler_efficiency": ("boiler_efficiency_pct", "Boiler efficiency, % (heat per kWh of gas billed)"),
|
||||||
|
"max_heat_w": ("heat_kw", "Most heat output, kW")}
|
||||||
|
# the owner's ratings (the panel's Devices page, devices-specs.json) and
|
||||||
|
# where to enter each: (rating, its label on the panel, the mapping's param)
|
||||||
|
BATTERY_RATINGS = {"capacity": ("battery_kwh", "Battery capacity, kWh", "capacity_wh"),
|
||||||
|
"power": ("battery_kw", "Battery power, kW", "max_charge_w and max_discharge_w"),
|
||||||
|
"efficiency": ("battery_efficiency_pct", "Battery round-trip efficiency, %", "efficiency")}
|
||||||
|
|
||||||
|
|
||||||
def ev_volts(params: dict, purpose: str) -> float:
|
def ev_volts(params: dict, purpose: str) -> float:
|
||||||
"""A charger's volts x phases (W per A) for a purpose: "plan" (the
|
"""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,
|
plan's power per A) or "order" (the grid order's current bound,
|
||||||
@@ -148,18 +214,83 @@ class Control:
|
|||||||
entity_range: dict = field(default_factory=dict) # quantity -> (lo, hi) the device itself reports (a climate's)
|
entity_range: dict = field(default_factory=dict) # quantity -> (lo, hi) the device itself reports (a climate's)
|
||||||
switchover: dict = field(default_factory=dict) # heating/cooling pair on one climate (switchover.py)
|
switchover: dict = field(default_factory=dict) # heating/cooling pair on one climate (switchover.py)
|
||||||
name: str = "" # the owner's name for it (mapping `name`), else label() guesses
|
name: str = "" # the owner's name for it (mapping `name`), else label() guesses
|
||||||
|
# the owner's ratings of its device (devices-specs.json: battery_kwh, ...)
|
||||||
|
# and that device's name, set by the controller; the device's own
|
||||||
|
# reported ratings (its capacity/rated_power/efficiency entities) by
|
||||||
|
# inputs.device_state
|
||||||
|
rated: dict = field(default_factory=dict)
|
||||||
|
rated_device: str = ""
|
||||||
|
reported: dict = field(default_factory=dict)
|
||||||
|
|
||||||
def label(self) -> str:
|
def label(self) -> str:
|
||||||
"""A name for people: the mapping's, else the id in words
|
"""A name for people: the mapping's, else the id in words
|
||||||
("living_room_cooling": "Living room cooling")."""
|
("living_room_cooling": "Living room cooling")."""
|
||||||
return self.name or (self.id.replace("_", " ").strip().capitalize() or self.id)
|
return self.name or (self.id.replace("_", " ").strip().capitalize() or self.id)
|
||||||
|
|
||||||
def p(self, name: str) -> float | str:
|
def p(self, name: str) -> float | str | None:
|
||||||
return self.params.get(name, PARAMS[self.kind][name])
|
"""A param: the mapping's, else the kind's default; a fact without
|
||||||
|
a default (FACTS) is None when the mapping has none."""
|
||||||
|
if name in self.params:
|
||||||
|
return self.params[name]
|
||||||
|
if name in FACTS.get(self.kind, ()):
|
||||||
|
return None
|
||||||
|
return PARAMS[self.kind][name]
|
||||||
|
|
||||||
|
def battery_facts(self) -> tuple[dict, list[str], dict]:
|
||||||
|
"""A battery's capacity_wh, max_charge_w, max_discharge_w and
|
||||||
|
efficiency (one way): the mapping's params, else the device's
|
||||||
|
reported ratings, else the owner's (the panel); never a default.
|
||||||
|
(facts, the missing ones: "capacity", "power", "efficiency", per
|
||||||
|
fact where it came from: mapping | device | owner)."""
|
||||||
|
out, src, rep, own = {}, {}, self.reported or {}, self.rated or {}
|
||||||
|
|
||||||
|
def pick(key, device, owner):
|
||||||
|
for where, v in (("mapping", self.params.get(key)), ("device", device), ("owner", owner)):
|
||||||
|
x = _num(v)
|
||||||
|
if x is not None and x > 0:
|
||||||
|
out[key], src[key] = x, where
|
||||||
|
return
|
||||||
|
kwh, kw, eff_pct = (_num(own.get(k)) for k in ("battery_kwh", "battery_kw", "battery_efficiency_pct"))
|
||||||
|
pick("capacity_wh", rep.get("capacity_wh"), kwh * 1000.0 if kwh else None)
|
||||||
|
pick("max_charge_w", rep.get("rated_power_w"), kw * 1000.0 if kw else None)
|
||||||
|
pick("max_discharge_w", rep.get("rated_power_w"), kw * 1000.0 if kw else None)
|
||||||
|
# round trip -> one way: charging and discharging lose alike (the
|
||||||
|
# optimiser's eff x charge, discharge / eff: eff² round trip)
|
||||||
|
rt = rep.get("efficiency_pct")
|
||||||
|
pick("efficiency", _one_way(rt), _one_way(eff_pct))
|
||||||
|
missing = [m for m, keys in (("capacity", ("capacity_wh",)), ("power", ("max_charge_w", "max_discharge_w")),
|
||||||
|
("efficiency", ("efficiency",))) if any(k not in out for k in keys)]
|
||||||
|
return out, missing, src
|
||||||
|
|
||||||
|
def heat_facts(self) -> tuple[dict, list[str], dict]:
|
||||||
|
"""A heating control's boiler_efficiency (0-1, heat per kWh of gas
|
||||||
|
billed) and max_heat_w where its heat_source needs them (boiler,
|
||||||
|
hybrid: both; district: max_heat_w): the mapping's params, else the
|
||||||
|
device's (its efficiency sensor, in %), else the owner's ratings
|
||||||
|
(boiler_efficiency_pct, heat_kw); never a default. (facts, the
|
||||||
|
missing ones, per fact where it came from)."""
|
||||||
|
src = str(self.p("heat_source") or "electric")
|
||||||
|
need = {"boiler": ("boiler_efficiency", "max_heat_w"), "hybrid": ("boiler_efficiency", "max_heat_w"),
|
||||||
|
"district": ("max_heat_w",)}.get(src, ())
|
||||||
|
out, where, rep, own = {}, {}, self.reported or {}, self.rated or {}
|
||||||
|
eff_pct, kw = _num(own.get("boiler_efficiency_pct")), _num(own.get("heat_kw"))
|
||||||
|
cands = {"boiler_efficiency": (("mapping", self.params.get("boiler_efficiency")),
|
||||||
|
("device", _pct(rep.get("efficiency_pct"))), ("owner", _pct(eff_pct))),
|
||||||
|
"max_heat_w": (("mapping", self.params.get("max_heat_w")), ("owner", kw * 1000.0 if kw else None))}
|
||||||
|
for k in need:
|
||||||
|
for w, v in cands[k]:
|
||||||
|
x = _num(v)
|
||||||
|
if x is not None and x > 0 and (k != "boiler_efficiency" or x <= 1.0):
|
||||||
|
out[k], where[k] = x, w
|
||||||
|
break
|
||||||
|
return out, [k for k in need if k not in out], where
|
||||||
|
|
||||||
def device_range(self, q: str) -> tuple[float, float]:
|
def device_range(self, q: str) -> tuple[float, float]:
|
||||||
if self.kind == "battery" and q == "power_w":
|
if self.kind == "battery" and q == "power_w":
|
||||||
return (-float(self.p("max_discharge_w")), float(self.p("max_charge_w")))
|
# without its power known nothing but 0 may be set (no plan
|
||||||
|
# sets one then; a command is refused by the guard)
|
||||||
|
f, _, _ = self.battery_facts()
|
||||||
|
return (-float(f.get("max_discharge_w") or 0.0), float(f.get("max_charge_w") or 0.0))
|
||||||
if self.kind == "ev" and q == "current_a":
|
if self.kind == "ev" and q == "current_a":
|
||||||
return (0.0, float(self.p("max_current_a")))
|
return (0.0, float(self.p("max_current_a")))
|
||||||
lo, hi = RANGE[self.kind][q]
|
lo, hi = RANGE[self.kind][q]
|
||||||
@@ -203,6 +334,49 @@ def _num(v) -> float | None:
|
|||||||
return x if x == x and abs(x) != float("inf") else None
|
return x if x == x and abs(x) != float("inf") else None
|
||||||
|
|
||||||
|
|
||||||
|
def _pct(v) -> float | None:
|
||||||
|
"""A percentage as a share (0-1], or None."""
|
||||||
|
x = _num(v)
|
||||||
|
return x / 100.0 if x is not None and 0 < x <= 100 else None
|
||||||
|
|
||||||
|
|
||||||
|
def _one_way(round_trip_pct) -> float | None:
|
||||||
|
"""A round-trip efficiency in % as the one-way factor (its square root),
|
||||||
|
or None (not a number in 1..100)."""
|
||||||
|
x = _num(round_trip_pct)
|
||||||
|
return (x / 100.0) ** 0.5 if x is not None and 0 < x <= 100 else None
|
||||||
|
|
||||||
|
|
||||||
|
def _check_heat(cid: str, p: dict, ents: dict):
|
||||||
|
src = p.get("heat_source", "electric")
|
||||||
|
if src not in HEAT_SOURCES:
|
||||||
|
raise ValueError(f"control {cid}: heat_source is one of {', '.join(HEAT_SOURCES)}")
|
||||||
|
curve = p.get("cop_curve") or []
|
||||||
|
if not isinstance(curve, list) or not all(isinstance(x, (list, tuple)) and len(x) == 2 and _num(x[0]) is not None
|
||||||
|
and _num(x[1]) is not None and float(x[1]) > 0 for x in curve):
|
||||||
|
raise ValueError(f"control {cid}: cop_curve is [[outside °C, COP > 0], ...]")
|
||||||
|
opts = p.get("source_options") or {}
|
||||||
|
if not isinstance(opts, dict) or set(opts) - {"heat_pump", "boiler"}:
|
||||||
|
raise ValueError(f"control {cid}: source_options names the select's options for heat_pump and boiler")
|
||||||
|
if src == "hybrid" and ("source" not in ents or set(opts) != {"heat_pump", "boiler"}):
|
||||||
|
raise ValueError(f"control {cid}: a hybrid needs its source select and source_options for heat_pump and boiler")
|
||||||
|
|
||||||
|
|
||||||
|
def cop_at(params: dict, t_out: float) -> float:
|
||||||
|
"""A heat pump's COP at an outside temperature: its datasheet curve
|
||||||
|
(cop_curve, linear between the points, held beyond the ends), else
|
||||||
|
params cop."""
|
||||||
|
pts = sorted((float(a), float(b)) for a, b in (params.get("cop_curve") or []))
|
||||||
|
if not pts:
|
||||||
|
return float(params.get("cop") or PARAMS["heating"]["cop"])
|
||||||
|
if t_out <= pts[0][0]:
|
||||||
|
return pts[0][1]
|
||||||
|
for (t0, c0), (t1, c1) in zip(pts, pts[1:]):
|
||||||
|
if t_out <= t1:
|
||||||
|
return c0 + (c1 - c0) * (t_out - t0) / (t1 - t0) if t1 > t0 else c1
|
||||||
|
return pts[-1][1]
|
||||||
|
|
||||||
|
|
||||||
def _check_cover(cid: str, p: dict):
|
def _check_cover(cid: str, p: dict):
|
||||||
for k in ("open_pct", "shade_pct", "closed_pct"):
|
for k in ("open_pct", "shade_pct", "closed_pct"):
|
||||||
if k in p and (_num(p[k]) is None or not 0 <= float(p[k]) <= 100):
|
if k in p and (_num(p[k]) is None or not 0 <= float(p[k]) <= 100):
|
||||||
@@ -243,11 +417,17 @@ def parse(raw: list | None, reading_assets: set[str], rooms: set[str] | None = N
|
|||||||
if not any(a in ents for a in alts):
|
if not any(a in ents for a in alts):
|
||||||
raise ValueError(f"control {cid}: needs {' or '.join(alts)}")
|
raise ValueError(f"control {cid}: needs {' or '.join(alts)}")
|
||||||
params = dict(c.get("params") or {})
|
params = dict(c.get("params") or {})
|
||||||
unknown = set(params) - set(PARAMS[kind])
|
unknown = set(params) - set(PARAMS[kind]) - set(FACTS.get(kind, ()))
|
||||||
if unknown:
|
if unknown:
|
||||||
raise ValueError(f"control {cid}: unknown params {', '.join(sorted(unknown))}")
|
raise ValueError(f"control {cid}: unknown params {', '.join(sorted(unknown))}")
|
||||||
if kind == "cover":
|
if kind == "cover":
|
||||||
_check_cover(cid, params)
|
_check_cover(cid, params)
|
||||||
|
for k in FACTS.get(kind, ()):
|
||||||
|
if k in params and (_num(params[k]) is None or float(params[k]) <= 0 or
|
||||||
|
(k.endswith("efficiency") and float(params[k]) > 1)):
|
||||||
|
raise ValueError(f"control {cid}: {k} is a number above 0" + (" and at most 1" if k.endswith("efficiency") else ""))
|
||||||
|
if kind == "heating":
|
||||||
|
_check_heat(cid, params, ents)
|
||||||
sw = {}
|
sw = {}
|
||||||
if c.get("switchover") is not None:
|
if c.get("switchover") is not None:
|
||||||
if kind not in ("heating", "cooling") or "climate" not in ents:
|
if kind not in ("heating", "cooling") or "climate" not in ents:
|
||||||
|
|||||||
@@ -42,11 +42,24 @@ control are switched on here as on the Hub's Devices page (the agent starts
|
|||||||
again, as the Hub's tool restarts it); without it the page points to the
|
again, as the Hub's tool restarts it); without it the page points to the
|
||||||
add-on's configuration. Protection (protected / not, never sealed) and a thermostat's heating or
|
add-on's configuration. Protection (protected / not, never sealed) and a thermostat's heating or
|
||||||
cooling choice are the owner's, here as on the Hub.
|
cooling choice are the owner's, here as on the Hub.
|
||||||
|
|
||||||
|
Remote Home Assistant for Cantabile support (item 19; the
|
||||||
|
add-on's option support_ha, off by default): while support has this Home
|
||||||
|
Assistant open (as its own HA user "Cantabile support", an administrator,
|
||||||
|
so it can open these pages too) both pages show it at the top with End
|
||||||
|
now, the add-on's own button: the link (edge/link/hatunnel.go) writes the
|
||||||
|
open sessions and its pid into its state directory (support-ha.json), End
|
||||||
|
now sends it SIGUSR1, as the Hub's panel does, and the link closes the
|
||||||
|
session and disables that user. What the Hub's panel keeps from support
|
||||||
|
stays the owner's here too: reading and control, protection, heating or
|
||||||
|
cooling, a shade's window, indoor or outdoor and the weather choice are
|
||||||
|
refused to the "Cantabile support" user.
|
||||||
"""
|
"""
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import signal
|
||||||
import time
|
import time
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
|
||||||
@@ -61,6 +74,16 @@ from .joinpage import CSS, E, JoinPage
|
|||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
PAGES = ("devices", "building")
|
PAGES = ("devices", "building")
|
||||||
|
# The Home Assistant user Cantabile support signs in as (edge/link/hauser.go
|
||||||
|
# haSupportUsername); HA's ingress names it in X-Remote-User-Name.
|
||||||
|
SUPPORT_USERNAME = "cantabile-support"
|
||||||
|
# Only the owner's (the Hub panel's post_devices refuses them to support).
|
||||||
|
OWNERS_ONLY = {"consent": M("Reading and control are the owner's to switch on."),
|
||||||
|
"protect": M("Protection is the owner's to change."),
|
||||||
|
"switchover": M("Heating or cooling is the owner's to choose."),
|
||||||
|
"shade": M("The shade's room and window are the owner's to set."),
|
||||||
|
"place": M("Indoor or outdoor is the owner's to choose."),
|
||||||
|
"weather": M("The weather forecast is the owner's to choose.")}
|
||||||
SWITCHOVER_WORDS = {"heating": M("Heating"), "cooling": M("Cooling"),
|
SWITCHOVER_WORDS = {"heating": M("Heating"), "cooling": M("Cooling"),
|
||||||
"season": M("By season (heating October to April, cooling May to September)"),
|
"season": M("By season (heating October to April, cooling May to September)"),
|
||||||
"outdoor": M("By outdoor temperature")}
|
"outdoor": M("By outdoor temperature")}
|
||||||
@@ -118,6 +141,7 @@ section.set{border-top:1px solid #d5ded9;margin-top:1rem}section.set h3{margin:.
|
|||||||
.row{display:flex;gap:.6rem}.row>div{flex:1}.right{text-align:right}
|
.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.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.more summary{cursor:pointer;margin:.6rem 0}
|
||||||
|
.box.support{background:#fbe9c9;border-color:#e8c37a}.box.support form{display:inline;margin:0 0 0 .4rem}.box.support button{margin:.3rem 0 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{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}
|
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}
|
@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}
|
||||||
@@ -126,7 +150,8 @@ form.changes:not(:has(input[value=outdoor]:checked)) .outdoor{display:none}}
|
|||||||
.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}
|
.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}
|
.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.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}}"""
|
details.info>.info-body{background:#18221d;color:#e3ebe6;border-color:#2b3a32}details.info>summary{border-color:#9fb0a6;color:#9fb0a6}
|
||||||
|
.box.support{background:#3a2c12;border-color:#6b5222}}"""
|
||||||
# HA's hash (#devices, #building: Cantabile's dashboard) to this page's ?p=
|
# 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||'');"
|
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])}"
|
"if(m&&location.search.indexOf('p=')<0&&location.search.indexOf('code=')<0)location.replace('?p='+m[1])}"
|
||||||
@@ -844,10 +869,10 @@ 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 "")
|
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:
|
def page_devices(v: dict, msg: str = "", err: str = "", banner: str = "") -> str:
|
||||||
about = E(T("Home Assistant's devices that a Cantabile device profile knows. Use one with Cantabile to see it on "
|
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."))
|
"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)]
|
body = [banner + f"<h2>{E(T('Devices'))}{info(about)}</h2>" + messages(msg, err)]
|
||||||
rooms = v.get("rooms") or []
|
rooms = v.get("rooms") or []
|
||||||
if v.get("devices"):
|
if v.get("devices"):
|
||||||
n = len(v["devices"])
|
n = len(v["devices"])
|
||||||
@@ -872,15 +897,15 @@ def page_devices(v: dict, msg: str = "", err: str = "") -> str:
|
|||||||
return html(T("Cantabile: devices"), "".join(body), "devices")
|
return html(T("Cantabile: devices"), "".join(body), "devices")
|
||||||
|
|
||||||
|
|
||||||
def page_device(v: dict, dev_id: str, msg: str = "", err: str = "") -> str:
|
def page_device(v: dict, dev_id: str, msg: str = "", err: str = "", banner: str = "") -> str:
|
||||||
"""One device in use: its state and values, then its settings, each
|
"""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."""
|
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)
|
d = next((x for x in v.get("devices") or [] if x["id"] == dev_id), None)
|
||||||
if not d:
|
if not d:
|
||||||
return page_devices(v, msg, err)
|
return page_devices(v, msg, err, banner)
|
||||||
rooms = v.get("rooms") or []
|
rooms = v.get("rooms") or []
|
||||||
vals = device_values(d)
|
vals = device_values(d)
|
||||||
body = (f'<a class="back" href="?p=devices">‹ {E(T("Devices"))}</a><h2>{E(d["name"])}</h2>'
|
body = (banner + 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="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'<div class="box now"><span class="state">{device_state(d)}</span>'
|
||||||
+ (f'<span class="values">{E(vals)}</span>' if vals else "") + "</div>"
|
+ (f'<span class="values">{E(vals)}</span>' if vals else "") + "</div>"
|
||||||
@@ -894,12 +919,57 @@ def page_refused(why: str, page: str = "devices") -> str:
|
|||||||
return html("Cantabile", f"<h2>{E(head)}</h2><div class=\"box err\">{E(why)}</div>", page)
|
return html("Cantabile", f"<h2>{E(head)}</h2><div class=\"box err\">{E(why)}</div>", page)
|
||||||
|
|
||||||
|
|
||||||
|
class SupportSessions:
|
||||||
|
"""Cantabile support's open Home Assistant session, as the link writes
|
||||||
|
it (<link state>/support-ha.json: its pid, the sessions), and End now."""
|
||||||
|
|
||||||
|
def __init__(self, link_state: str, proc: str = "/proc", kill=os.kill):
|
||||||
|
self.path = os.path.join(link_state, "support-ha.json")
|
||||||
|
self.proc, self.kill = proc, kill
|
||||||
|
|
||||||
|
def state(self) -> dict:
|
||||||
|
st = _read_json(self.path, {})
|
||||||
|
return st if isinstance(st, dict) else {}
|
||||||
|
|
||||||
|
def open(self) -> list[dict]:
|
||||||
|
return [x for x in self.state().get("sessions") or [] if isinstance(x, dict)]
|
||||||
|
|
||||||
|
def end(self) -> bool:
|
||||||
|
"""SIGUSR1 to the link (only to a process that is the link): True if sent."""
|
||||||
|
pid = self.state().get("pid")
|
||||||
|
if not isinstance(pid, int) or pid <= 1:
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
with open(os.path.join(self.proc, str(pid), "cmdline"), "rb") as f:
|
||||||
|
if b"cantabile-link" not in f.read().split(b"\0")[0]:
|
||||||
|
return False
|
||||||
|
self.kill(pid, signal.SIGUSR1)
|
||||||
|
except OSError:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def support_banner(sessions: list[dict], page: str) -> str:
|
||||||
|
"""While support has this Home Assistant open: who, how long, End now."""
|
||||||
|
if not sessions:
|
||||||
|
return ""
|
||||||
|
who = ", ".join(sorted({str(x.get("by") or T("Cantabile support")) for x in sessions}))
|
||||||
|
since = min((x.get("since") or 0) for x in sessions)
|
||||||
|
n = max(0, int((time.time() - since) // 60)) if since else 0
|
||||||
|
return (f'<div class="box warn support"><b>{E(T("Cantabile support has this Home Assistant open"))}</b> '
|
||||||
|
+ E(TN("({who}, for {n} minute), signed in as the user \"Cantabile support\". Everything they do is recorded.",
|
||||||
|
"({who}, for {n} minutes), signed in as the user \"Cantabile support\". Everything they do is recorded.",
|
||||||
|
n, who=who))
|
||||||
|
+ form("end-support", hidden(p=page), T("End now"), inline=True) + "</div>")
|
||||||
|
|
||||||
|
|
||||||
class App(JoinPage):
|
class App(JoinPage):
|
||||||
"""The add-on's pages: Devices (the first) and Building or community."""
|
"""The add-on's pages: Devices (the first) and Building or community."""
|
||||||
|
|
||||||
def __init__(self, fleet, devices: Devices, location=None, admin=None):
|
def __init__(self, fleet, devices: Devices, location=None, admin=None):
|
||||||
super().__init__(fleet, **({"location": location} if location else {}))
|
super().__init__(fleet, **({"location": location} if location else {}))
|
||||||
self.devices = devices
|
self.devices = devices
|
||||||
|
self.support = SupportSessions(getattr(fleet, "link_state", "") or os.environ.get("CANTABILE_LINK_STATE", "/data/link"))
|
||||||
# admin(user_id) -> bool: the agent's HA client asks HA (ha.is_admin)
|
# admin(user_id) -> bool: the agent's HA client asks HA (ha.is_admin)
|
||||||
self.admin = admin or self._admin
|
self.admin = admin or self._admin
|
||||||
|
|
||||||
@@ -924,21 +994,51 @@ class App(JoinPage):
|
|||||||
return 200, page_refused(T("Cantabile could not ask Home Assistant who you are ({error}).", error=e), page)
|
return 200, page_refused(T("Cantabile could not ask Home Assistant who you are ({error}).", error=e), page)
|
||||||
if not ok:
|
if not ok:
|
||||||
return 200, page_refused(T("Only an administrator of Home Assistant can use this page."), page)
|
return 200, page_refused(T("Only an administrator of Home Assistant can use this page."), page)
|
||||||
|
f = ({k: v[0] for k, v in urllib.parse.parse_qs(data.decode("utf-8", "replace")).items()}
|
||||||
|
if method == "POST" else {})
|
||||||
|
if f.get("do") == "end-support":
|
||||||
|
# the add-on's End now (any administrator: the owner, or support itself)
|
||||||
|
sent = await asyncio.to_thread(self.support.end)
|
||||||
|
msg, err = ((T("Ended. Cantabile support's session is closed and the user \"Cantabile support\" is disabled "
|
||||||
|
"again (a few seconds)."), "") if sent else
|
||||||
|
("", T("No session to end, or Cantabile's connection is not running.")))
|
||||||
|
if page == "building":
|
||||||
|
return 200, self.with_banner(await asyncio.to_thread(self.get, {}), "building", msg, err)
|
||||||
|
return 200, page_devices(await self.devices.view(), msg, err, self.banner("devices"))
|
||||||
if page == "building":
|
if page == "building":
|
||||||
status, out = await super().respond(method, url, hdr, data, peer)
|
status, out = await super().respond(method, url, hdr, data, peer)
|
||||||
if status == 200:
|
if status == 200:
|
||||||
out = out.replace("</style>", f"\n{MORE_CSS}</style><script>{HASH_SCRIPT}</script>", 1) \
|
out = self.with_banner(out, "building")
|
||||||
.replace("<body>", "<body>" + nav("building"), 1)
|
|
||||||
return status, out
|
return status, out
|
||||||
if url.path not in ("/", "") or method not in ("GET", "POST"):
|
if url.path not in ("/", "") or method not in ("GET", "POST"):
|
||||||
return 404, T("Not found")
|
return 404, T("Not found")
|
||||||
dev_id = (q.get("d") or [""])[0]
|
dev_id = (q.get("d") or [""])[0]
|
||||||
|
banner = self.banner("devices")
|
||||||
if method == "GET":
|
if method == "GET":
|
||||||
v = await self.devices.view()
|
v = await self.devices.view()
|
||||||
return 200, page_device(v, dev_id) if dev_id else page_devices(v)
|
return 200, page_device(v, dev_id, banner=banner) if dev_id else page_devices(v, banner=banner)
|
||||||
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"
|
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)
|
if self.is_support(hdr) and f.get("do") in OWNERS_ONLY:
|
||||||
|
err = T(OWNERS_ONLY[f["do"]])
|
||||||
|
v = await self.devices.view()
|
||||||
|
return 200, page_device(v, f.get("id", ""), err=err, banner=banner) if f.get("id") else \
|
||||||
|
page_devices(v, err=err, banner=banner)
|
||||||
|
return 200, (await self.post_devices(f, by)).replace("<body>" + nav("devices"), "<body>" + nav("devices") + banner, 1)
|
||||||
|
|
||||||
|
def is_support(self, hdr: dict) -> bool:
|
||||||
|
"""The request is the "Cantabile support" HA user's (HA's ingress
|
||||||
|
names it: its username, or its id while a session is open)."""
|
||||||
|
uid = hdr.get("x-remote-user-id", "")
|
||||||
|
return (hdr.get("x-remote-user-name", "") == SUPPORT_USERNAME
|
||||||
|
or bool(uid) and uid in {x.get("ha_user") for x in self.support.open()})
|
||||||
|
|
||||||
|
def banner(self, page: str) -> str:
|
||||||
|
return support_banner(self.support.open(), page)
|
||||||
|
|
||||||
|
def with_banner(self, out: str, page: str, msg: str = "", err: str = "") -> str:
|
||||||
|
"""JoinPage's page with this app's style, nav and support banner."""
|
||||||
|
return out.replace("</style>", f"\n{MORE_CSS}</style><script>{HASH_SCRIPT}</script>", 1) \
|
||||||
|
.replace("<body>", "<body>" + nav(page) + self.banner(page) + messages(msg, err), 1)
|
||||||
|
|
||||||
async def post_devices(self, f: dict, by: str = "owner") -> str:
|
async def post_devices(self, f: dict, by: str = "owner") -> str:
|
||||||
dv, do, msg, err = self.devices, f.get("do", ""), "", ""
|
dv, do, msg, err = self.devices, f.get("do", ""), "", ""
|
||||||
|
|||||||
@@ -102,6 +102,12 @@ def actions_for(c, want, col, level="plan", why="") -> list[Action]:
|
|||||||
act("climate", "set_temperature", {"temperature": float(v)}, float(v), "temp_c")
|
act("climate", "set_temperature", {"temperature": float(v)}, float(v), "temp_c")
|
||||||
elif v in ("on", "off"):
|
elif v in ("on", "off"):
|
||||||
switch("switch", v == "on")
|
switch("switch", v == "on")
|
||||||
|
# a hybrid's heat source (optimizer: the heat pump or the boiler, the cheaper heat now)
|
||||||
|
src = want.get("source") if isinstance(want, dict) else None
|
||||||
|
if c.kind == "heating" and src and "source" in e:
|
||||||
|
opt = (c.p("source_options") or {}).get(src)
|
||||||
|
if opt and col.state(e["source"]) != str(opt):
|
||||||
|
act("source", "select_option", {"option": str(opt)}, str(opt))
|
||||||
elif c.kind == "ev":
|
elif c.kind == "ev":
|
||||||
v = want.get("set") if isinstance(want, dict) else want
|
v = want.get("set") if isinstance(want, dict) else want
|
||||||
if v == "off":
|
if v == "off":
|
||||||
@@ -166,6 +172,13 @@ class Executor:
|
|||||||
if cmd:
|
if cmd:
|
||||||
self.commands.pop(c.id, None)
|
self.commands.pop(c.id, None)
|
||||||
fresh = self.plan is not None and now - self.plan.get("made", 0) <= self.stale and self.inputs_ok
|
fresh = self.plan is not None and now - self.plan.get("made", 0) <= self.stale and self.inputs_ok
|
||||||
|
if fresh and c.id in (self.plan.get("refused") or {}):
|
||||||
|
# not planned: a room's fallback schedule (optimizer.fallback_entries),
|
||||||
|
# else its safe default (a battery without its facts, docs/parameters.md U2)
|
||||||
|
e = _plan_value(self.plan, c.id, now)
|
||||||
|
if e is not None:
|
||||||
|
return e, "plan", e.get("why", "")
|
||||||
|
return safe_want(c), "plan", "not planned: " + str(self.plan["refused"][c.id].get("why", ""))
|
||||||
if fresh:
|
if fresh:
|
||||||
e = _plan_value(self.plan, c.id, now)
|
e = _plan_value(self.plan, c.id, now)
|
||||||
return e, "plan", (e or {}).get("why", "")
|
return e, "plan", (e or {}).get("why", "")
|
||||||
|
|||||||
@@ -22,6 +22,16 @@ metered). The kinds:
|
|||||||
with the Mieterstrom pricing) times the site's calibration
|
with the Mieterstrom pricing) times the site's calibration
|
||||||
(pvcalib.py).
|
(pvcalib.py).
|
||||||
|
|
||||||
|
The series (SERIES_FEATURES; methods.md B1-B2, B13-B16): electric `load`,
|
||||||
|
`base_load`, `pv` (W); thermal `heat` (heat delivered for space heating,
|
||||||
|
W) and `dhw` (heat for hot water, W) with the weather, the wind, the room
|
||||||
|
model's heat loss (`loss`: the site's fitted UA times the indoor less the
|
||||||
|
outdoor temperature) and the expected occupancy (`occ`: the occupancy
|
||||||
|
series' hour-of-week profile); `occupancy` (the share of a site's presence
|
||||||
|
sensors that see someone, 0..1) and `ev_plug` (cars plugged in at the
|
||||||
|
site's chargers, 0..1 per charger) on the calendar alone. The same kinds
|
||||||
|
and the same code for each: a new series is data and features.
|
||||||
|
|
||||||
A model is a JSON-able dict ("params"); fit_* makes it, predict() uses it.
|
A model is a JSON-able dict ("params"); fit_* makes it, predict() uses it.
|
||||||
intraday() carries the last hours' error against the meter into the next
|
intraday() carries the last hours' error against the meter into the next
|
||||||
hours (the box every 15 minutes; methods.md B10).
|
hours (the box every 15 minutes; methods.md B10).
|
||||||
@@ -54,6 +64,16 @@ MIN_GBM_SLOTS = 96 # a day of data at least
|
|||||||
LOAD_FEATURES = ["hod", "dow", "off", "how", "lag7", "temp", "ghi", "cloud", "hdd", "special", "doy_sin", "doy_cos",
|
LOAD_FEATURES = ["hod", "dow", "off", "how", "lag7", "temp", "ghi", "cloud", "hdd", "special", "doy_sin", "doy_cos",
|
||||||
"daylight"]
|
"daylight"]
|
||||||
PV_FEATURES = ["hod", "how", "lag7", "temp", "ghi", "cloud", "elev", "az", "phys", "doy_sin", "doy_cos", "daylight"]
|
PV_FEATURES = ["hod", "how", "lag7", "temp", "ghi", "cloud", "elev", "az", "phys", "doy_sin", "doy_cos", "daylight"]
|
||||||
|
HEAT_FEATURES = ["hod", "dow", "off", "how", "lag7", "temp", "ghi", "cloud", "hdd", "wind", "special", "doy_sin", "doy_cos",
|
||||||
|
"daylight", "loss", "occ"]
|
||||||
|
DHW_FEATURES = ["hod", "dow", "off", "how", "lag7", "temp", "special", "doy_sin", "doy_cos", "occ"]
|
||||||
|
PRESENCE_FEATURES = ["hod", "dow", "off", "how", "lag7", "special", "doy_sin", "doy_cos"]
|
||||||
|
SERIES_FEATURES = {"pv": PV_FEATURES, "heat": HEAT_FEATURES, "dhw": DHW_FEATURES, "occupancy": PRESENCE_FEATURES,
|
||||||
|
"ev_plug": PRESENCE_FEATURES}
|
||||||
|
# shares, not watts: occupancy 0..1, ev_plug the cars plugged in (0..1 per
|
||||||
|
# charger); their nMAE's floor and their profiles' digits differ (level_floor)
|
||||||
|
FRACTION_SERIES = ("occupancy", "ev_plug")
|
||||||
|
UPPER = {"occupancy": 1.0} # a share of the sensors: never above 1
|
||||||
FEATURES_V1 = {"load": ["hod", "dow", "off", "how", "lag7", "temp", "ghi", "cloud", "hdd"], # before 2026-11-04
|
FEATURES_V1 = {"load": ["hod", "dow", "off", "how", "lag7", "temp", "ghi", "cloud", "hdd"], # before 2026-11-04
|
||||||
"pv": ["hod", "how", "lag7", "temp", "ghi", "cloud", "elev", "az", "phys"]}
|
"pv": ["hod", "how", "lag7", "temp", "ghi", "cloud", "elev", "az", "phys"]}
|
||||||
CATEGORICAL = ("special",)
|
CATEGORICAL = ("special",)
|
||||||
@@ -63,13 +83,33 @@ CATEGORICAL = ("special",)
|
|||||||
# (measured 2026-11-04, building's synthetic site: a cold snap 12 K below the
|
# (measured 2026-11-04, building's synthetic site: a cold snap 12 K below the
|
||||||
# coldest day trained on, held-out nMAE 29.5 % -> 6.4 %; an ordinary winter
|
# coldest day trained on, held-out nMAE 29.5 % -> 6.4 %; an ordinary winter
|
||||||
# week 3.8 % -> 3.3 %; methods.md B10)
|
# week 3.8 % -> 3.3 %; methods.md B10)
|
||||||
LINEAR_SERIES: tuple = ("load", "base_load")
|
LINEAR_SERIES: tuple = ("load", "base_load", "heat")
|
||||||
|
# ... but only with this much history (days between the first and last
|
||||||
|
# metered slot): on a few days linear leaves overfit (measured on the sim
|
||||||
|
# farm 2026-11-04, ~3.6 days: nMAE 1.07 with them, 0.58 without)
|
||||||
|
LINEAR_MIN_DAYS = 14.0
|
||||||
# recency (methods.md B10): a slot's weight max(0.5^(age/30 d), 0.25 x the
|
# recency (methods.md B10): a slot's weight max(0.5^(age/30 d), 0.25 x the
|
||||||
# season's likeness (cos of the age over a year, last year's same weeks
|
# season's likeness (cos of the age over a year, last year's same weeks
|
||||||
# count), 0.05)
|
# count), 0.05)
|
||||||
HALF_LIFE_D, SEASON_W, FLOOR_W = 30.0, 0.25, 0.05
|
HALF_LIFE_D, SEASON_W, FLOOR_W = 30.0, 0.25, 0.05
|
||||||
|
|
||||||
|
|
||||||
|
def feature_names(series: str) -> list[str]:
|
||||||
|
"""A series' current features (SERIES_FEATURES; the load's for the rest)."""
|
||||||
|
return list(SERIES_FEATURES.get(series, LOAD_FEATURES))
|
||||||
|
|
||||||
|
|
||||||
|
def level_floor(series: str) -> float:
|
||||||
|
"""The metered mean under which nMAE is not given (metrics): 1 W for
|
||||||
|
a power, 0.01 for a share (FRACTION_SERIES)."""
|
||||||
|
return 0.01 if series in FRACTION_SERIES else 1.0
|
||||||
|
|
||||||
|
|
||||||
|
def digits(series: str) -> int:
|
||||||
|
"""The decimals an hour-of-week profile keeps: 1 for watts, 3 for a share."""
|
||||||
|
return 3 if series in FRACTION_SERIES else 1
|
||||||
|
|
||||||
|
|
||||||
# ---- the calendar -------------------------------------------------------------
|
# ---- the calendar -------------------------------------------------------------
|
||||||
|
|
||||||
def _easter(y: int) -> date:
|
def _easter(y: int) -> date:
|
||||||
@@ -209,6 +249,14 @@ class Weather:
|
|||||||
elev: np.ndarray
|
elev: np.ndarray
|
||||||
az: np.ndarray
|
az: np.ndarray
|
||||||
hours: list = field(default_factory=list) # the records (pvforecast.Hour), for pvforecast
|
hours: list = field(default_factory=list) # the records (pvforecast.Hour), for pvforecast
|
||||||
|
wind: np.ndarray | None = None # m/s, the record of the slot's hour (None: no record had it)
|
||||||
|
|
||||||
|
def slice(self, sl: slice) -> "Weather":
|
||||||
|
return Weather(self.temp[sl], self.ghi[sl], self.cloud[sl], self.elev[sl], self.az[sl], self.hours,
|
||||||
|
None if self.wind is None else self.wind[sl])
|
||||||
|
|
||||||
|
def wind_or_nan(self, n: int) -> np.ndarray:
|
||||||
|
return np.full(n, np.nan) if self.wind is None else self.wind
|
||||||
|
|
||||||
|
|
||||||
def sun(t: np.ndarray, lat: float | None, lon: float | None) -> tuple[np.ndarray, np.ndarray]:
|
def sun(t: np.ndarray, lat: float | None, lon: float | None) -> tuple[np.ndarray, np.ndarray]:
|
||||||
@@ -228,7 +276,7 @@ def weather(t: np.ndarray, hours: list, lat: float | None, lon: float | None) ->
|
|||||||
pvforecast.forecast does), else a clear sky dimmed by the cloud cover."""
|
pvforecast.forecast does), else a clear sky dimmed by the cloud cover."""
|
||||||
n = len(t)
|
n = len(t)
|
||||||
el, az = sun(t, lat, lon)
|
el, az = sun(t, lat, lon)
|
||||||
temp, ghi, cloud = np.full(n, np.nan), np.full(n, np.nan), np.full(n, np.nan)
|
temp, ghi, cloud, wind = np.full(n, np.nan), np.full(n, np.nan), np.full(n, np.nan), np.full(n, np.nan)
|
||||||
if not hours:
|
if not hours:
|
||||||
return Weather(temp, ghi, cloud, el, az, [])
|
return Weather(temp, ghi, cloud, el, az, [])
|
||||||
by_end = {}
|
by_end = {}
|
||||||
@@ -249,6 +297,8 @@ def weather(t: np.ndarray, hours: list, lat: float | None, lon: float | None) ->
|
|||||||
continue
|
continue
|
||||||
if h.cloud_pct is not None:
|
if h.cloud_pct is not None:
|
||||||
cloud[i] = h.cloud_pct
|
cloud[i] = h.cloud_pct
|
||||||
|
if getattr(h, "wind_ms", None) is not None:
|
||||||
|
wind[i] = h.wind_ms
|
||||||
if lat is None or lon is None:
|
if lat is None or lon is None:
|
||||||
if h.solar_kwh_m2 is not None:
|
if h.solar_kwh_m2 is not None:
|
||||||
ghi[i] = h.solar_kwh_m2 * 1000.0
|
ghi[i] = h.solar_kwh_m2 * 1000.0
|
||||||
@@ -264,14 +314,15 @@ def weather(t: np.ndarray, hours: list, lat: float | None, lon: float | None) ->
|
|||||||
ghi[i] = h.solar_kwh_m2 * 1000.0 * (4 * mine / s) if s > 0 else 0.0
|
ghi[i] = h.solar_kwh_m2 * 1000.0 * (4 * mine / s) if s > 0 else 0.0
|
||||||
elif h.cloud_pct is not None:
|
elif h.cloud_pct is not None:
|
||||||
ghi[i] = pvforecast.cloud_dimmed(pvforecast.clear_sky_ghi(el[i]), h.cloud_pct)
|
ghi[i] = pvforecast.cloud_dimmed(pvforecast.clear_sky_ghi(el[i]), h.cloud_pct)
|
||||||
return Weather(temp, ghi, cloud, el, az, list(hours))
|
return Weather(temp, ghi, cloud, el, az, list(hours), wind if not np.isnan(wind).all() else None)
|
||||||
|
|
||||||
|
|
||||||
# ---- metrics ------------------------------------------------------------------
|
# ---- metrics ------------------------------------------------------------------
|
||||||
|
|
||||||
def metrics(y: np.ndarray, p: np.ndarray) -> dict:
|
def metrics(y: np.ndarray, p: np.ndarray, floor: float = 1.0) -> dict:
|
||||||
"""MAE and RMSE (W) and nMAE (MAE over the mean metered value) on the
|
"""MAE and RMSE (W) and nMAE (MAE over the mean metered value) on the
|
||||||
slots where both exist; nMAE None when the metered mean is ~0 (a night)."""
|
slots where both exist; nMAE None when the metered mean is ~0 (a night;
|
||||||
|
under `floor`: level_floor)."""
|
||||||
m = ~(np.isnan(y) | np.isnan(p))
|
m = ~(np.isnan(y) | np.isnan(p))
|
||||||
if not m.any():
|
if not m.any():
|
||||||
return {"n": 0, "mae": None, "rmse": None, "nmae": None}
|
return {"n": 0, "mae": None, "rmse": None, "nmae": None}
|
||||||
@@ -279,13 +330,14 @@ def metrics(y: np.ndarray, p: np.ndarray) -> dict:
|
|||||||
mae = float(np.abs(e).mean())
|
mae = float(np.abs(e).mean())
|
||||||
level = float(np.abs(y[m]).mean())
|
level = float(np.abs(y[m]).mean())
|
||||||
return {"n": int(m.sum()), "mae": mae, "rmse": float(np.sqrt((e ** 2).mean())),
|
return {"n": int(m.sum()), "mae": mae, "rmse": float(np.sqrt((e ** 2).mean())),
|
||||||
"nmae": mae / level if level > 1.0 else None}
|
"nmae": mae / level if level > floor else None}
|
||||||
|
|
||||||
|
|
||||||
# ---- how: the hour-of-week profile -----------------------------------------------
|
# ---- how: the hour-of-week profile -----------------------------------------------
|
||||||
|
|
||||||
def fit_how(t: np.ndarray, y: np.ndarray, tz: str, weights: np.ndarray | None = None) -> dict:
|
def fit_how(t: np.ndarray, y: np.ndarray, tz: str, weights: np.ndarray | None = None, ndigits: int = 1) -> dict:
|
||||||
"""weights: per slot (recency_weights); None: all alike."""
|
"""weights: per slot (recency_weights); None: all alike. ndigits: the
|
||||||
|
values' decimals (digits(series): a share keeps 3)."""
|
||||||
cal = calendar(t, tz)
|
cal = calendar(t, tz)
|
||||||
vals: list[float | None] = []
|
vals: list[float | None] = []
|
||||||
have = ~np.isnan(y)
|
have = ~np.isnan(y)
|
||||||
@@ -299,7 +351,7 @@ def fit_how(t: np.ndarray, y: np.ndarray, tz: str, weights: np.ndarray | None =
|
|||||||
if v is None: # the same hour on the other weekdays, else the mean
|
if v is None: # the same hour on the other weekdays, else the mean
|
||||||
same = [vals[d * 24 + h % 24] for d in range(7) if vals[d * 24 + h % 24] is not None]
|
same = [vals[d * 24 + h % 24] for d in range(7) if vals[d * 24 + h % 24] is not None]
|
||||||
v = sum(same) / len(same) if same else overall
|
v = sum(same) / len(same) if same else overall
|
||||||
out.append(round(max(v, 0.0), 1))
|
out.append(round(max(v, 0.0), ndigits))
|
||||||
return {"kind": "how", "values": out, "tz": tz}
|
return {"kind": "how", "values": out, "tz": tz}
|
||||||
|
|
||||||
|
|
||||||
@@ -321,10 +373,13 @@ def _lag7(t: np.ndarray, y: np.ndarray, hist_t: np.ndarray | None, hist_y: np.nd
|
|||||||
|
|
||||||
def features(series: str, t: np.ndarray, w: Weather, how_p: dict, tz: str, lag7: np.ndarray,
|
def features(series: str, t: np.ndarray, w: Weather, how_p: dict, tz: str, lag7: np.ndarray,
|
||||||
phys: np.ndarray | None, names: list[str] | None = None, lat: float | None = None,
|
phys: np.ndarray | None, names: list[str] | None = None, lat: float | None = None,
|
||||||
state: str | None = None, kept: list | None = None) -> tuple[np.ndarray, list[str]]:
|
state: str | None = None, kept: list | None = None, room: dict | None = None,
|
||||||
|
occ: list | None = None) -> tuple[np.ndarray, list[str]]:
|
||||||
"""The feature matrix; names: a model's own list (default: the
|
"""The feature matrix; names: a model's own list (default: the
|
||||||
series' current one). kept: the special days' types the model learnt
|
series' current one). kept: the special days' types the model learnt
|
||||||
(the others count as normal)."""
|
(the others count as normal). room: {"ua_w_per_k", "t_in_c"} (the heat
|
||||||
|
loss UA x (indoor - outdoor), W; methods.md B13), occ: the occupancy's
|
||||||
|
168 hour-of-week values (the expected share present); NaN without them."""
|
||||||
cal = calendar(t, tz, state)
|
cal = calendar(t, tz, state)
|
||||||
special = cal["special"]
|
special = cal["special"]
|
||||||
if kept is not None:
|
if kept is not None:
|
||||||
@@ -334,11 +389,23 @@ def features(series: str, t: np.ndarray, w: Weather, how_p: dict, tz: str, lag7:
|
|||||||
"how": how_values(how_p, t, tz), "lag7": lag7, "temp": w.temp, "ghi": w.ghi, "cloud": w.cloud,
|
"how": how_values(how_p, t, tz), "lag7": lag7, "temp": w.temp, "ghi": w.ghi, "cloud": w.cloud,
|
||||||
"hdd": np.maximum(15.0 - w.temp, 0.0), "elev": w.elev, "az": w.az,
|
"hdd": np.maximum(15.0 - w.temp, 0.0), "elev": w.elev, "az": w.az,
|
||||||
"phys": phys if phys is not None else np.full(len(t), np.nan), "special": special,
|
"phys": phys if phys is not None else np.full(len(t), np.nan), "special": special,
|
||||||
"doy_sin": np.sin(ang), "doy_cos": np.cos(ang), "daylight": daylight_h(cal["doy"], lat)}
|
"doy_sin": np.sin(ang), "doy_cos": np.cos(ang), "daylight": daylight_h(cal["doy"], lat),
|
||||||
names = list(names) if names is not None else (PV_FEATURES if series == "pv" else LOAD_FEATURES)
|
"wind": w.wind_or_nan(len(t)), "loss": heat_loss(room, w.temp),
|
||||||
|
"occ": how_values({"values": occ}, t, tz) if occ and len(occ) == 168 else np.full(len(t), np.nan)}
|
||||||
|
names = list(names) if names is not None else feature_names(series)
|
||||||
return np.column_stack([cols[c] for c in names]), names
|
return np.column_stack([cols[c] for c in names]), names
|
||||||
|
|
||||||
|
|
||||||
|
def heat_loss(room: dict | None, temp: np.ndarray) -> np.ndarray:
|
||||||
|
"""The room model's heat loss per slot (W): UA (W/K) x (indoor -
|
||||||
|
outdoor), never below 0; NaN without a fitted UA or an indoor
|
||||||
|
temperature (methods.md B13.1)."""
|
||||||
|
ua, tin = (room or {}).get("ua_w_per_k"), (room or {}).get("t_in_c")
|
||||||
|
if not ua or tin is None:
|
||||||
|
return np.full(len(temp), np.nan)
|
||||||
|
return np.maximum(float(ua) * (float(tin) - temp), 0.0)
|
||||||
|
|
||||||
|
|
||||||
def special_kept(t: np.ndarray, y: np.ndarray, tz: str, state: str | None = None, at_least: int = 2) -> list[int]:
|
def special_kept(t: np.ndarray, y: np.ndarray, tz: str, state: str | None = None, at_least: int = 2) -> list[int]:
|
||||||
"""The special days' types with at least 2 metered days in (t, y)."""
|
"""The special days' types with at least 2 metered days in (t, y)."""
|
||||||
cal = calendar(t, tz, state)
|
cal = calendar(t, tz, state)
|
||||||
@@ -354,28 +421,41 @@ def special_kept(t: np.ndarray, y: np.ndarray, tz: str, state: str | None = None
|
|||||||
def fit_gbm(series: str, t: np.ndarray, y: np.ndarray, w: Weather, tz: str, lag7: np.ndarray,
|
def fit_gbm(series: str, t: np.ndarray, y: np.ndarray, w: Weather, tz: str, lag7: np.ndarray,
|
||||||
phys: np.ndarray | None, lat: float | None = None, state: str | None = None,
|
phys: np.ndarray | None, lat: float | None = None, state: str | None = None,
|
||||||
weights: np.ndarray | None = None, linear: bool | None = None,
|
weights: np.ndarray | None = None, linear: bool | None = None,
|
||||||
names: list[str] | None = None) -> dict | None:
|
names: list[str] | None = None, base: np.ndarray | None = None, room: dict | None = None,
|
||||||
|
occ: list | None = None) -> dict | None:
|
||||||
"""weights: per slot (recency_weights; None: all alike); linear: leaves
|
"""weights: per slot (recency_weights; None: all alike); linear: leaves
|
||||||
with linear models (default: series in LINEAR_SERIES); names: the
|
with linear models (default: series in LINEAR_SERIES); names: the
|
||||||
features (default: the series' current list)."""
|
features (default: the series' current list); base: a forecast per slot
|
||||||
|
the trees learn the difference to (LightGBM's init_score: the cloud
|
||||||
|
teacher's, methods.md B11), then params["base"] and predict_gbm needs it.
|
||||||
|
room, occ: the heat series' room model and occupancy profile
|
||||||
|
(features), kept in the params where the features use them."""
|
||||||
import lightgbm as lgb
|
import lightgbm as lgb
|
||||||
have = ~np.isnan(y)
|
have = ~np.isnan(y) if base is None else ~(np.isnan(y) | np.isnan(base))
|
||||||
if have.sum() < MIN_GBM_SLOTS:
|
if have.sum() < MIN_GBM_SLOTS:
|
||||||
return None
|
return None
|
||||||
how_p = fit_how(t, y, tz, weights)
|
how_p = fit_how(t, y, tz, weights, digits(series))
|
||||||
kept = special_kept(t, y, tz, state)
|
kept = special_kept(t, y, tz, state)
|
||||||
X, names = features(series, t, w, how_p, tz, lag7, phys, names, lat, state, kept)
|
X, names = features(series, t, w, how_p, tz, lag7, phys, names, lat, state, kept, room, occ)
|
||||||
params = dict(GBM_PARAMS)
|
params = dict(GBM_PARAMS)
|
||||||
if linear if linear is not None else series in LINEAR_SERIES:
|
span_days = float(t[have][-1] - t[have][0]) / 86400.0 if have.any() else 0.0
|
||||||
|
if linear if linear is not None else (series in LINEAR_SERIES and span_days >= LINEAR_MIN_DAYS):
|
||||||
params["linear_tree"] = True
|
params["linear_tree"] = True
|
||||||
cat = [c for c in CATEGORICAL if c in names]
|
cat = [c for c in CATEGORICAL if c in names]
|
||||||
ds = lgb.Dataset(X[have], y[have], feature_name=names, categorical_feature=cat or "auto",
|
ds = lgb.Dataset(X[have], y[have], feature_name=names, categorical_feature=cat or "auto",
|
||||||
weight=None if weights is None else np.asarray(weights, dtype=float)[have])
|
weight=None if weights is None else np.asarray(weights, dtype=float)[have],
|
||||||
|
init_score=None if base is None else np.asarray(base, dtype=float)[have])
|
||||||
m = lgb.train(params, ds, num_boost_round=GBM_ROUNDS)
|
m = lgb.train(params, ds, num_boost_round=GBM_ROUNDS)
|
||||||
out = {"kind": "gbm", "model": m.model_to_string(), "features": names, "how": how_p["values"], "tz": tz,
|
out = {"kind": "gbm", "model": m.model_to_string(), "features": names, "how": how_p["values"], "tz": tz,
|
||||||
"cap_w": float(np.nanpercentile(y[have], 99.9)) * 1.2 if have.any() else None}
|
"cap_w": float(np.nanpercentile(y[have], 99.9)) * 1.2 if have.any() else None}
|
||||||
if "special" in names:
|
if "special" in names:
|
||||||
out["special_kept"] = kept
|
out["special_kept"] = kept
|
||||||
|
if "loss" in names and room and room.get("ua_w_per_k") and room.get("t_in_c") is not None:
|
||||||
|
out["room"] = {"ua_w_per_k": round(float(room["ua_w_per_k"]), 3), "t_in_c": round(float(room["t_in_c"]), 2)}
|
||||||
|
if "occ" in names and occ and len(occ) == 168:
|
||||||
|
out["occ"] = [round(float(x), 3) for x in occ]
|
||||||
|
if base is not None:
|
||||||
|
out["base"] = "teacher"
|
||||||
if params.get("linear_tree"):
|
if params.get("linear_tree"):
|
||||||
out["linear"] = True
|
out["linear"] = True
|
||||||
return out
|
return out
|
||||||
@@ -410,23 +490,30 @@ def box_params(params: dict) -> dict:
|
|||||||
|
|
||||||
|
|
||||||
def predict_gbm(params: dict, series: str, t: np.ndarray, w: Weather, lag7: np.ndarray,
|
def predict_gbm(params: dict, series: str, t: np.ndarray, w: Weather, lag7: np.ndarray,
|
||||||
phys: np.ndarray | None, lat: float | None = None, state: str | None = None) -> np.ndarray:
|
phys: np.ndarray | None, lat: float | None = None, state: str | None = None,
|
||||||
|
base: np.ndarray | None = None) -> np.ndarray:
|
||||||
tz = params.get("tz", "UTC")
|
tz = params.get("tz", "UTC")
|
||||||
names = params["features"]
|
names = params["features"]
|
||||||
if not isinstance(names, list) or any(n not in FEATURE_NAMES for n in names):
|
if not isinstance(names, list) or any(n not in FEATURE_NAMES for n in names):
|
||||||
raise ValueError("gbm: features this code does not know")
|
raise ValueError("gbm: features this code does not know")
|
||||||
X, names = features(series, t, w, {"values": params["how"]}, tz, lag7, phys, names, lat, state,
|
X, names = features(series, t, w, {"values": params["how"]}, tz, lag7, phys, names, lat, state,
|
||||||
params.get("special_kept"))
|
params.get("special_kept"), params.get("room"), params.get("occ"))
|
||||||
p = np.asarray(_predictor(params).predict(X), dtype=float)
|
p = np.asarray(_predictor(params).predict(X), dtype=float)
|
||||||
|
if params.get("base"): # trees on a base forecast (fit_gbm's base): the sum is the difference
|
||||||
|
if base is None:
|
||||||
|
raise ValueError("gbm: trained on a base forecast, none given")
|
||||||
|
p = np.asarray(base, dtype=float) + p
|
||||||
p = np.maximum(p, 0.0)
|
p = np.maximum(p, 0.0)
|
||||||
if params.get("cap_w"):
|
if params.get("cap_w"):
|
||||||
p = np.minimum(p, params["cap_w"])
|
p = np.minimum(p, params["cap_w"])
|
||||||
|
if series in UPPER:
|
||||||
|
p = np.minimum(p, UPPER[series])
|
||||||
if series == "pv":
|
if series == "pv":
|
||||||
p = np.where(w.elev <= 0, 0.0, p) if not np.isnan(w.elev).all() else p
|
p = np.where(w.elev <= 0, 0.0, p) if not np.isnan(w.elev).all() else p
|
||||||
return p
|
return p
|
||||||
|
|
||||||
|
|
||||||
FEATURE_NAMES = frozenset(LOAD_FEATURES) | frozenset(PV_FEATURES) | {"doy"}
|
FEATURE_NAMES = frozenset(LOAD_FEATURES) | frozenset(PV_FEATURES) | frozenset(HEAT_FEATURES) | frozenset(DHW_FEATURES) | {"doy"}
|
||||||
|
|
||||||
|
|
||||||
# ---- one entry point -----------------------------------------------------------
|
# ---- one entry point -----------------------------------------------------------
|
||||||
@@ -443,6 +530,12 @@ class Site:
|
|||||||
azimuth: float = 180.0
|
azimuth: float = 180.0
|
||||||
pvgis_monthly: list | None = None # PVGIS's kWh per kWp per month (pvcalib.pvgis_monthly)
|
pvgis_monthly: list | None = None # PVGIS's kWh per kWp per month (pvcalib.pvgis_monthly)
|
||||||
state: str | None = None # the German state (NW, BY, ...): carnival, school holidays; None: unknown
|
state: str | None = None # the German state (NW, BY, ...): carnival, school holidays; None: unknown
|
||||||
|
# the heat series (methods.md B13): the room model's UA (W/K) and the
|
||||||
|
# indoor temperature (°C) its loss uses, the occupancy's hour-of-week
|
||||||
|
# profile (168 shares); kept in a gbm's params when it is fitted
|
||||||
|
ua_w_per_k: float | None = None
|
||||||
|
t_in_c: float | None = None
|
||||||
|
occ_how: list | None = None
|
||||||
|
|
||||||
|
|
||||||
def phys_prior(t: np.ndarray, w: Weather, s: Site) -> np.ndarray | None:
|
def phys_prior(t: np.ndarray, w: Weather, s: Site) -> np.ndarray | None:
|
||||||
@@ -454,15 +547,17 @@ def phys_prior(t: np.ndarray, w: Weather, s: Site) -> np.ndarray | None:
|
|||||||
|
|
||||||
|
|
||||||
def fit(kind: str, series: str, t: np.ndarray, y: np.ndarray, w: Weather, s: Site,
|
def fit(kind: str, series: str, t: np.ndarray, y: np.ndarray, w: Weather, s: Site,
|
||||||
hist_t: np.ndarray | None = None, hist_y: np.ndarray | None = None) -> dict | None:
|
hist_t: np.ndarray | None = None, hist_y: np.ndarray | None = None, base: np.ndarray | None = None) -> dict | None:
|
||||||
"""A model of this kind on (t, y), or None where the kind cannot be made
|
"""A model of this kind on (t, y), or None where the kind cannot be made
|
||||||
(too little data, PV physics without a location or weather). Recent
|
(too little data, PV physics without a location or weather). Recent
|
||||||
slots weigh more (recency_weights; pvcalib has its own)."""
|
slots weigh more (recency_weights; pvcalib has its own)."""
|
||||||
if kind == "how":
|
if kind == "how":
|
||||||
return fit_how(t, y, s.tz, recency_weights(t)) if (~np.isnan(y)).any() else None
|
return fit_how(t, y, s.tz, recency_weights(t), digits(series)) if (~np.isnan(y)).any() else None
|
||||||
if kind == "gbm":
|
if kind == "gbm":
|
||||||
prior = phys_prior(t, w, s) if series == "pv" else None
|
prior = phys_prior(t, w, s) if series == "pv" else None
|
||||||
return fit_gbm(series, t, y, w, s.tz, _lag7(t, y, hist_t, hist_y), prior, s.lat, s.state, recency_weights(t))
|
room = {"ua_w_per_k": s.ua_w_per_k, "t_in_c": s.t_in_c} if s.ua_w_per_k else None
|
||||||
|
return fit_gbm(series, t, y, w, s.tz, _lag7(t, y, hist_t, hist_y), prior, s.lat, s.state, recency_weights(t),
|
||||||
|
base=base, room=room, occ=s.occ_how)
|
||||||
if kind == "pvphys":
|
if kind == "pvphys":
|
||||||
if series != "pv":
|
if series != "pv":
|
||||||
return None
|
return None
|
||||||
@@ -478,7 +573,7 @@ def fit(kind: str, series: str, t: np.ndarray, y: np.ndarray, w: Weather, s: Sit
|
|||||||
|
|
||||||
|
|
||||||
def predict(params: dict, series: str, t: np.ndarray, w: Weather, s: Site,
|
def predict(params: dict, series: str, t: np.ndarray, w: Weather, s: Site,
|
||||||
hist_t: np.ndarray | None = None, hist_y: np.ndarray | None = None) -> np.ndarray:
|
hist_t: np.ndarray | None = None, hist_y: np.ndarray | None = None, base: np.ndarray | None = None) -> np.ndarray:
|
||||||
"""The model's forecast per slot (W). hist_*: metered history before t
|
"""The model's forecast per slot (W). hist_*: metered history before t
|
||||||
(the week-ago lag; NaN where unknown)."""
|
(the week-ago lag; NaN where unknown)."""
|
||||||
kind = params["kind"]
|
kind = params["kind"]
|
||||||
@@ -487,7 +582,7 @@ def predict(params: dict, series: str, t: np.ndarray, w: Weather, s: Site,
|
|||||||
if kind == "gbm":
|
if kind == "gbm":
|
||||||
prior = phys_prior(t, w, s) if series == "pv" else None
|
prior = phys_prior(t, w, s) if series == "pv" else None
|
||||||
nan = np.full(len(t), np.nan)
|
nan = np.full(len(t), np.nan)
|
||||||
return predict_gbm(params, series, t, w, _lag7(t, nan, hist_t, hist_y), prior, s.lat, s.state)
|
return predict_gbm(params, series, t, w, _lag7(t, nan, hist_t, hist_y), prior, s.lat, s.state, base)
|
||||||
if kind == "pvphys":
|
if kind == "pvphys":
|
||||||
ss = Site(s.tz, s.lat, s.lon, s.kwp, params.get("tilt", s.tilt), params.get("azimuth", s.azimuth), state=s.state)
|
ss = Site(s.tz, s.lat, s.lon, s.kwp, params.get("tilt", s.tilt), params.get("azimuth", s.azimuth), state=s.state)
|
||||||
prior = phys_prior(t, w, ss)
|
prior = phys_prior(t, w, ss)
|
||||||
|
|||||||
@@ -154,7 +154,8 @@ class Guard:
|
|||||||
tr.commanded[a.entity] = (now, a.value)
|
tr.commanded[a.entity] = (now, a.value)
|
||||||
c = self.controls.get(a.control)
|
c = self.controls.get(a.control)
|
||||||
boiler = c is not None and c.kind in ("heating", "cooling") and a.channel == "switch"
|
boiler = c is not None and c.kind in ("heating", "cooling") and a.channel == "switch"
|
||||||
tr.changed["set" if a.quantity or boiler else "mode"] = now
|
# a hybrid's heat source: its own hold (steady.py _hold_source)
|
||||||
|
tr.changed["source" if a.channel == "source" else "set" if a.quantity or boiler else "mode"] = now
|
||||||
if a.domain == "cover":
|
if a.domain == "cover":
|
||||||
tr.moved = now
|
tr.moved = now
|
||||||
if a.quantity == "":
|
if a.quantity == "":
|
||||||
|
|||||||
@@ -424,4 +424,23 @@ MESSAGES = {
|
|||||||
'heat delivered (thermal)': 'gelieferte Wärme (thermisch)',
|
'heat delivered (thermal)': 'gelieferte Wärme (thermisch)',
|
||||||
'heat energy (thermal)': 'Wärmemenge (thermisch)',
|
'heat energy (thermal)': 'Wärmemenge (thermisch)',
|
||||||
'heat {v} (thermal)': 'Wärme {v} (thermisch)',
|
'heat {v} (thermal)': 'Wärme {v} (thermisch)',
|
||||||
|
# remote Home Assistant for Cantabile support (devpage.py)
|
||||||
|
"Cantabile support has this Home Assistant open": "Der Cantabile-Support hat dieses Home Assistant geöffnet",
|
||||||
|
"Cantabile support": "Cantabile-Support",
|
||||||
|
("({who}, for {n} minute), signed in as the user \"Cantabile support\". Everything they do is recorded.",
|
||||||
|
"({who}, for {n} minutes), signed in as the user \"Cantabile support\". Everything they do is recorded."):
|
||||||
|
("({who}, seit {n} Minute), angemeldet als Benutzer „Cantabile support“. Alles, was er tut, wird aufgezeichnet.",
|
||||||
|
"({who}, seit {n} Minuten), angemeldet als Benutzer „Cantabile support“. Alles, was er tut, wird aufgezeichnet."),
|
||||||
|
"End now": "Jetzt beenden",
|
||||||
|
"Ended. Cantabile support's session is closed and the user \"Cantabile support\" is disabled again (a few seconds).":
|
||||||
|
"Beendet. Die Sitzung des Cantabile-Supports ist geschlossen und der Benutzer „Cantabile support“ wieder "
|
||||||
|
"deaktiviert (in wenigen Sekunden).",
|
||||||
|
"No session to end, or Cantabile's connection is not running.":
|
||||||
|
"Keine Sitzung zu beenden, oder Cantabiles Verbindung läuft nicht.",
|
||||||
|
"Reading and control are the owner's to switch on.": "Auslesen und Steuerung schaltet nur der Eigentümer ein.",
|
||||||
|
"Protection is the owner's to change.": "Den Schutz ändert nur der Eigentümer.",
|
||||||
|
"Heating or cooling is the owner's to choose.": "Heizen oder Kühlen wählt der Eigentümer.",
|
||||||
|
"The shade's room and window are the owner's to set.": "Raum und Fenster der Beschattung legt der Eigentümer fest.",
|
||||||
|
"Indoor or outdoor is the owner's to choose.": "Innen oder außen wählt der Eigentümer.",
|
||||||
|
"The weather forecast is the owner's to choose.": "Die Wettervorhersage wählt der Eigentümer.",
|
||||||
}
|
}
|
||||||
@@ -3,8 +3,9 @@
|
|||||||
inputs' entities (presence, temperatures, humidity, outside temperature,
|
inputs' entities (presence, temperatures, humidity, outside temperature,
|
||||||
sun, weather), a short local history of load and PV (the fallback when no
|
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. A
|
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
|
room's R and C: the mapping's, else the box's or the cloud's fit (the
|
||||||
the defaults; sources.rooms says which per control.
|
model bundle's rooms); none: the room is not planned (room_refusal) and keeps a fallback schedule; sources.rooms says which per
|
||||||
|
control.
|
||||||
|
|
||||||
None of this leaves the site; only the plan does (controller.py).
|
None of this leaves the site; only the plan does (controller.py).
|
||||||
|
|
||||||
@@ -284,10 +285,11 @@ def device_state(c, col: Collector, now: float) -> dict:
|
|||||||
e = c.entities
|
e = c.entities
|
||||||
st = {}
|
st = {}
|
||||||
if c.kind == "battery":
|
if c.kind == "battery":
|
||||||
soc = col.value(e.get("soc", ""))
|
# the state of charge from its sensor only (no estimate: docs/parameters.md U2)
|
||||||
st["soc_pct"] = soc if soc is not None else float(c.p("soc_pct"))
|
st["soc_pct"] = col.value(e.get("soc", ""))
|
||||||
st["soc_estimated"] = soc is None
|
|
||||||
st["charge_from_grid"] = col.state(e.get("charge_from_grid", "")) == "on"
|
st["charge_from_grid"] = col.state(e.get("charge_from_grid", "")) == "on"
|
||||||
|
if hasattr(c, "reported"):
|
||||||
|
c.reported = battery_reported(c, col)
|
||||||
elif c.kind in ("heating", "cooling"):
|
elif c.kind in ("heating", "cooling"):
|
||||||
t = col.value(e.get("temp", ""))
|
t = col.value(e.get("temp", ""))
|
||||||
if t is None and e.get("climate"):
|
if t is None and e.get("climate"):
|
||||||
@@ -298,6 +300,12 @@ def device_state(c, col: Collector, now: float) -> dict:
|
|||||||
st["hvac"] = col.state(e["climate"])
|
st["hvac"] = col.state(e["climate"])
|
||||||
else:
|
else:
|
||||||
st["on"] = col.state(e.get("switch", "")) == "on"
|
st["on"] = col.state(e.get("switch", "")) == "on"
|
||||||
|
if c.kind == "heating" and e.get("source"):
|
||||||
|
# a hybrid's source now, in the plan's words (heat_pump | boiler)
|
||||||
|
opts = {str(v): k for k, v in (c.p("source_options") or {}).items()}
|
||||||
|
st["source"] = opts.get(str(col.state(e["source"])))
|
||||||
|
if c.kind == "heating" and e.get("efficiency") and hasattr(c, "reported"):
|
||||||
|
c.reported = {"efficiency_pct": col.value(e["efficiency"])}
|
||||||
elif c.kind == "ev":
|
elif c.kind == "ev":
|
||||||
conn = e.get("connected")
|
conn = e.get("connected")
|
||||||
st["connected"] = col.state(conn) == "on" if conn else True
|
st["connected"] = col.state(conn) == "on" if conn else True
|
||||||
@@ -317,6 +325,105 @@ def device_state(c, col: Collector, now: float) -> dict:
|
|||||||
return st
|
return st
|
||||||
|
|
||||||
|
|
||||||
|
def _scaled(col: Collector, eid: str, units: dict) -> float | None:
|
||||||
|
"""An entity's value converted by its unit (units: unit -> factor; a
|
||||||
|
unit not in it: None, never a guess at the scale)."""
|
||||||
|
v = col.value(eid) if eid else None
|
||||||
|
if v is None:
|
||||||
|
return None
|
||||||
|
f = units.get(col.attr(eid, "unit_of_measurement"))
|
||||||
|
return v * f if f is not None else None
|
||||||
|
|
||||||
|
|
||||||
|
def battery_reported(c, col: Collector) -> dict:
|
||||||
|
"""The battery's own ratings where its device reports them (the
|
||||||
|
control's capacity, rated_power and efficiency entities): capacity_wh,
|
||||||
|
rated_power_w, efficiency_pct (round trip); absent where it does not."""
|
||||||
|
e, out = c.entities, {}
|
||||||
|
cap = _scaled(col, e.get("capacity", ""), {"kWh": 1000.0, "Wh": 1.0})
|
||||||
|
pw = _scaled(col, e.get("rated_power", ""), {"kW": 1000.0, "W": 1.0})
|
||||||
|
eff = _scaled(col, e.get("efficiency", ""), {"%": 1.0})
|
||||||
|
for k, v in (("capacity_wh", cap), ("rated_power_w", pw), ("efficiency_pct", eff)):
|
||||||
|
if v is not None and v > 0:
|
||||||
|
out[k] = v
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def battery_refusal(c, st: dict) -> dict | None:
|
||||||
|
"""Why a battery is not planned (docs/parameters.md U2), or None: its
|
||||||
|
capacity, power or efficiency unknown (the mapping, the device and its
|
||||||
|
owner's ratings have none), or no state of charge from its sensor.
|
||||||
|
{"why": the reasons, "where": where to enter what is missing,
|
||||||
|
"missing": [...]}."""
|
||||||
|
from .controls import BATTERY_RATINGS
|
||||||
|
_, missing, _ = c.battery_facts()
|
||||||
|
why = [f"battery {m} unknown" for m in missing]
|
||||||
|
if st.get("soc_pct") is None:
|
||||||
|
why.append("SoC unknown (no state-of-charge sensor)" if not c.entities.get("soc")
|
||||||
|
else "SoC unknown (the state-of-charge sensor has no value)")
|
||||||
|
if not why:
|
||||||
|
return None
|
||||||
|
where = ""
|
||||||
|
if missing:
|
||||||
|
labels = ", ".join(BATTERY_RATINGS[m][1] for m in missing)
|
||||||
|
params = ", ".join(BATTERY_RATINGS[m][2] for m in missing)
|
||||||
|
dev = getattr(c, "rated_device", "") or ""
|
||||||
|
where = (f"on the panel: Devices, {dev}, Ratings ({labels})" if dev else
|
||||||
|
f"on the panel: Devices, the battery's device, Ratings ({labels}); or in the mapping's params ({params})")
|
||||||
|
if st.get("soc_pct") is None:
|
||||||
|
where = (where + "; " if where else "") + "map the battery's state-of-charge sensor (soc)"
|
||||||
|
return {"why": "; ".join(why), "where": where, "missing": missing + (["soc"] if st.get("soc_pct") is None else [])}
|
||||||
|
|
||||||
|
|
||||||
|
HYBRID_SWITCH_KEY = "heat.hybrid_switch_eur" # the Parameters page's (via the tariff bundle)
|
||||||
|
|
||||||
|
|
||||||
|
def heating_refusal(c, params: dict, price_gas: list, price_heat: list, prices_status: str,
|
||||||
|
box_params: dict | None = None) -> dict | None:
|
||||||
|
"""Why a heating control is not planned (docs/methods.md E6, the
|
||||||
|
owner's rule: refuse, never guess), or None: a boiler's efficiency or
|
||||||
|
a boiler's / heat station's most heat unknown, no gas price (boiler,
|
||||||
|
hybrid) or heat price (district) for now, a hybrid without an
|
||||||
|
electricity price to compare with. {"why", "where", "missing"}."""
|
||||||
|
from .controls import HEAT_RATINGS
|
||||||
|
src = str(params.get("heat_source") or "electric")
|
||||||
|
if src == "electric":
|
||||||
|
return None
|
||||||
|
_, missing, _ = c.heat_facts()
|
||||||
|
why = [f"{HEAT_RATINGS[m][1].split(',')[0].lower()} unknown" for m in missing]
|
||||||
|
where = []
|
||||||
|
if src in ("heat_pump", "hybrid") and "cop" not in (c.params or {}) and not (c.params or {}).get("cop_curve"):
|
||||||
|
# a heat pump's COP is the device's (its datasheet): no default stands in
|
||||||
|
why.append("heat pump COP unknown")
|
||||||
|
where.append("the mapping's params: cop_curve (the datasheet's COP by outside temperature) or cop")
|
||||||
|
missing = missing + ["cop"]
|
||||||
|
if src == "hybrid" and (box_params or {}).get(HYBRID_SWITCH_KEY) is None:
|
||||||
|
why.append(f"parameter {HYBRID_SWITCH_KEY} not received")
|
||||||
|
where.append("the admin Parameters page (Gas and heat); it reaches the box with its tariff")
|
||||||
|
missing = missing + [HYBRID_SWITCH_KEY]
|
||||||
|
rated = [m for m in missing if m in HEAT_RATINGS]
|
||||||
|
if rated:
|
||||||
|
labels = ", ".join(HEAT_RATINGS[m][1] for m in rated)
|
||||||
|
dev = getattr(c, "rated_device", "") or ""
|
||||||
|
where.append(f"on the panel: Devices, {dev or 'the heating device'}, Ratings ({labels}); or in the mapping's params "
|
||||||
|
f"({', '.join(rated)})" + ("; or map the boiler's efficiency sensor" if "boiler_efficiency" in rated else ""))
|
||||||
|
if src in ("boiler", "hybrid") and (not price_gas or price_gas[0] is None):
|
||||||
|
why.append("no gas price for now")
|
||||||
|
where.append("the site's gas tariff (console: the site's tariff, Gas)")
|
||||||
|
missing = missing + ["gas_price"]
|
||||||
|
if src == "district" and (not price_heat or price_heat[0] is None):
|
||||||
|
why.append("no heat price for now")
|
||||||
|
where.append("the site's heat tariff, or its building's heat price (console: Buildings, rules)")
|
||||||
|
missing = missing + ["heat_price"]
|
||||||
|
if src == "hybrid" and prices_status == "none":
|
||||||
|
why.append("no electricity price to compare the heat pump with")
|
||||||
|
where.append("the site's electricity tariff")
|
||||||
|
missing = missing + ["electricity_price"]
|
||||||
|
if not why:
|
||||||
|
return None
|
||||||
|
return {"why": "; ".join(why), "where": "; ".join(where), "missing": missing}
|
||||||
|
|
||||||
|
|
||||||
SET_POSITION = 4 # HA's CoverEntityFeature.SET_POSITION
|
SET_POSITION = 4 # HA's CoverEntityFeature.SET_POSITION
|
||||||
|
|
||||||
|
|
||||||
@@ -350,6 +457,12 @@ def model_state(now: float, controls, inputs, col: Collector, bundles, history,
|
|||||||
start = int(now // STEP_S * STEP_S)
|
start = int(now // STEP_S * STEP_S)
|
||||||
zone = ZoneInfo(tz or "UTC")
|
zone = ZoneInfo(tz or "UTC")
|
||||||
prices = bundles.prices(start, STEP_S, STEPS, now)
|
prices = bundles.prices(start, STEP_S, STEPS, now)
|
||||||
|
# gas and heat per step (fleet heatprice.go): what a boiler's and district heat cost
|
||||||
|
cp = getattr(bundles, "carrier_prices", None)
|
||||||
|
price_gas = cp("gas", start, STEP_S, STEPS, now) if cp else [None] * STEPS
|
||||||
|
price_heat = cp("heat", start, STEP_S, STEPS, now) if cp else [None] * STEPS
|
||||||
|
bp = getattr(bundles, "box_params", None)
|
||||||
|
box_params = bp(now) if bp else {}
|
||||||
# the base load: the house without the planned devices (Finding A2-a), so
|
# 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)
|
# 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
|
# do not count them twice. The model's base_load_w is that; its load_w
|
||||||
@@ -431,7 +544,11 @@ def model_state(now: float, controls, inputs, col: Collector, bundles, history,
|
|||||||
continue
|
continue
|
||||||
h = datetime.fromtimestamp(start + k * STEP_S, timezone.utc).astimezone(zone).hour
|
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)
|
occupied.append(1 if occupied_hours[0] <= h < occupied_hours[1] else 0)
|
||||||
devices, rooms_src, phases_src = [], {}, {}
|
devices, rooms_src, phases_src, refused, battery_src, heat_src = [], {}, {}, {}, {}, {}
|
||||||
|
# a room's COP and comfort temperatures: the mapping's (the device's facts, the owner's), or the
|
||||||
|
# defaults (optimizer.shift_envelope publishes no shift from defaults); rooms not planned
|
||||||
|
# (room_refusal) with what their fallback schedule needs
|
||||||
|
cop_src, band_src, fallback = {}, {}, []
|
||||||
one_room = sum(c.kind in ("heating", "cooling") for c in controls) == 1
|
one_room = sum(c.kind in ("heating", "cooling") for c in controls) == 1
|
||||||
for c in controls:
|
for c in controls:
|
||||||
if hasattr(c, "learn_range"):
|
if hasattr(c, "learn_range"):
|
||||||
@@ -448,26 +565,87 @@ def model_state(now: float, controls, inputs, col: Collector, bundles, history,
|
|||||||
{"phases": None, "source": "assumed"})
|
{"phases": None, "source": "assumed"})
|
||||||
params["phases"] = info["phases"]
|
params["phases"] = info["phases"]
|
||||||
phases_src[c.id] = {"phases": info["phases"], "source": info["source"]}
|
phases_src[c.id] = {"phases": info["phases"], "source": info["source"]}
|
||||||
|
st = device_state(c, col, now)
|
||||||
if c.kind in ("heating", "cooling"):
|
if c.kind in ("heating", "cooling"):
|
||||||
rooms_src[c.id] = _room_rc(c, params, bundles, one_room, now)
|
# the room's and the device's facts the mapping gives (no defaults: controls.FACTS)
|
||||||
|
params.update({k: float(c.params[k]) for k in ("r_k_per_kw", "c_kwh_per_k", "max_power_w", "cop")
|
||||||
|
if k in c.params})
|
||||||
|
source = "electric" if c.kind == "cooling" else str(params.get("heat_source") or "electric")
|
||||||
|
if c.kind == "heating" and source == "electric" and "cop" not in params:
|
||||||
|
params["cop"] = 1.0 # a resistive heater: all its electricity becomes heat (physics)
|
||||||
|
src, power = _room_rc(c, params, bundles, one_room, now)
|
||||||
|
rooms_src[c.id] = src
|
||||||
|
# an electric fit runs in electric equivalents: the COP cancels (_room_rc)
|
||||||
|
cop_src[c.id] = ("mapping" if "cop" in c.params or c.params.get("cop_curve") else
|
||||||
|
"electric heater" if c.kind == "heating" and source == "electric" else
|
||||||
|
"electric fit" if power == "electric" and "cop" in params else "none")
|
||||||
|
# comfort/setback/setup: the mapping's, else the box's copy of the Parameters rows
|
||||||
|
# comfort.heating_setpoints / comfort.cooling_setpoints (docs/parameters.md C7)
|
||||||
|
band_src[c.id] = "mapping" if any(k in c.params for k in ("comfort_c", "setback_c", "setup_c")) else "defaults"
|
||||||
|
# what is missing for the room and for its heat source (methods E6)
|
||||||
|
nos = [room_refusal(c, st, src, power, source)]
|
||||||
|
if c.kind == "heating":
|
||||||
|
nos.append(heating_refusal(c, params, price_gas, price_heat, getattr(prices, "status", "tariff"),
|
||||||
|
box_params))
|
||||||
|
nos = [n for n in nos if n is not None]
|
||||||
|
if nos:
|
||||||
|
# not planned: its limits and a fallback schedule (the owner's or
|
||||||
|
# installer's preferences: optimizer.fallback_entries)
|
||||||
|
no = {"why": "; ".join(n["why"] for n in nos if n.get("why")),
|
||||||
|
"where": "; ".join(n["where"] for n in nos if n.get("where")),
|
||||||
|
"missing": [m for n in nos for m in n.get("missing") or []]}
|
||||||
|
refused[c.id] = dict(no, kind=c.kind, name=c.label() if hasattr(c, "label") else c.id)
|
||||||
|
fallback.append({"id": c.id, "kind": c.kind, "params": params, "state": st, "bounds": bounds,
|
||||||
|
"channels": sorted(c.writable())})
|
||||||
|
continue
|
||||||
|
if c.kind == "heating" and hasattr(c, "heat_facts"):
|
||||||
|
facts, _, fsrc = c.heat_facts()
|
||||||
|
params.update(facts)
|
||||||
|
if facts:
|
||||||
|
heat_src[c.id] = fsrc
|
||||||
|
if c.kind == "battery":
|
||||||
|
no = battery_refusal(c, st)
|
||||||
|
if no is not None:
|
||||||
|
refused[c.id] = dict(no, kind="battery", name=c.label() if hasattr(c, "label") else c.id)
|
||||||
|
continue
|
||||||
|
facts, _, fsrc = c.battery_facts()
|
||||||
|
params.update(facts)
|
||||||
|
battery_src[c.id] = fsrc
|
||||||
|
# the battery's power from its facts (device_range ran before them in bounds)
|
||||||
|
b = c.bounds("power_w", (extra_limits or {}).get(c.id))
|
||||||
|
bounds["power_w"] = [b.lo, b.hi, b.lo_by, b.hi_by]
|
||||||
devices.append({"id": c.id, "kind": c.kind, "params": params,
|
devices.append({"id": c.id, "kind": c.kind, "params": params,
|
||||||
"state": device_state(c, col, now), "bounds": bounds,
|
"state": st, "bounds": bounds,
|
||||||
"channels": sorted(c.writable())})
|
"channels": sorted(c.writable())})
|
||||||
clouds = _hourly(weather_forecast or [], "cloud_coverage", start, STEPS)
|
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
|
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)
|
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,
|
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,
|
"price_import": prices.import_ct, "price_export": prices.export_ct, "price_note": prices.note,
|
||||||
|
"price_gas": price_gas, "price_heat": price_heat, "params": box_params,
|
||||||
|
# the tariff's state (bundles.Prices): status tariff | none, why none, the steps it prices
|
||||||
|
"prices": {"status": getattr(prices, "status", "tariff"), "reason": getattr(prices, "reason", ""),
|
||||||
|
"tariff": getattr(prices, "tariff_id", ""), "expired_at": getattr(prices, "expired_at", 0),
|
||||||
|
"priced_steps": getattr(prices, "priced_steps", STEPS)},
|
||||||
"load_w": load, "pv_w": pv, "t_out": t_out, "sun": sun, "occupied": occupied, "sky": sky,
|
"load_w": load, "pv_w": pv, "t_out": t_out, "sun": sun, "occupied": occupied, "sky": sky,
|
||||||
"ghi_w_m2": ghi, "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(),
|
"sources": {"load": load_src, "pv": pv_src, "t_out": t_src, "model": bundles.model_id(),
|
||||||
"sky": "weather forecast" if clouds else "clear", "rooms": rooms_src, "ghi": ghi_src,
|
"sky": "weather forecast" if clouds else "clear", "rooms": rooms_src, "ghi": ghi_src,
|
||||||
|
"rooms_cop": cop_src, "rooms_band": band_src,
|
||||||
"ev_phases": phases_src,
|
"ev_phases": phases_src,
|
||||||
|
**({"battery": battery_src} if battery_src else {}),
|
||||||
|
**({"heating": heat_src} if heat_src else {}),
|
||||||
**({"intraday": intraday} if intraday else {}),
|
**({"intraday": intraday} if intraday else {}),
|
||||||
# per series the steps from the cloud's forecast, the box's own model
|
# per series the steps from the cloud's forecast, the box's own model
|
||||||
# (its kind and weather) and the hour-of-week profile (methods.md B9)
|
# (its kind and weather) and the hour-of-week profile (methods.md B9)
|
||||||
"forecast": {k: v for k, v in fc_src.items() if v}},
|
"forecast": {k: v for k, v in fc_src.items() if v}},
|
||||||
"devices": devices, "building": building or {}, "grid_order": grid_order or {},
|
"devices": devices, "building": building or {}, "grid_order": grid_order or {},
|
||||||
|
# controls not planned and why (docs/parameters.md U2: a battery without its facts;
|
||||||
|
# a room without its R/C fit, power, COP or temperature)
|
||||||
|
"refused": refused,
|
||||||
|
# the rooms among them: their limits and params for a fallback schedule
|
||||||
|
# (preferences.apply gives their schedule, optimizer.fallback_entries the setpoints)
|
||||||
|
"fallback": fallback,
|
||||||
# the market's contracts in force as sources and sinks (shadow, the market's step 2)
|
# 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 []}
|
"market": bundles.market_for_model(start, STEP_S, STEPS, now) if hasattr(bundles, "market_for_model") else []}
|
||||||
if grid_max_w:
|
if grid_max_w:
|
||||||
@@ -508,12 +686,21 @@ def irradiance(start: int, location: dict | None, sky: list[float],
|
|||||||
|
|
||||||
|
|
||||||
def _room_rc(c, params: dict, bundles, only_one: bool, now: float) -> str:
|
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
|
"""A heating or cooling control's R and C: the mapping's own (both), else
|
||||||
model's room (bundles.room_rc), else the defaults. A room fitted against
|
the model's room (the box's fit in force, else the cloud's:
|
||||||
the electric power: C * cop and R / cop (the same RC; the optimiser's
|
bundles.room_rc), else none (never the built-in defaults: the room is
|
||||||
heat is cop * P). Into params; where they came from."""
|
not planned, room_refusal). A room fitted against the
|
||||||
if "r_k_per_kw" in c.params or "c_kwh_per_k" in c.params:
|
electric power: C * cop and R / cop (the same RC; the optimiser's heat
|
||||||
return "mapping"
|
is cop * P): the COP cancels, so without the mapping's cop the model
|
||||||
|
runs in electric equivalents (cop 1 by construction, not a default).
|
||||||
|
Into params; (where they came from: mapping | box model | model |
|
||||||
|
partial (the mapping sets one of the two) | none, the power the fit is
|
||||||
|
against: thermal | electric | "")."""
|
||||||
|
has = [k for k in ("r_k_per_kw", "c_kwh_per_k") if k in c.params]
|
||||||
|
if len(has) == 2:
|
||||||
|
return "mapping", "thermal"
|
||||||
|
if has:
|
||||||
|
return "partial", ""
|
||||||
# the box's own fit where it is in force (boxtrain.py), else the cloud's
|
# the box's own fit where it is in force (boxtrain.py), else the cloud's
|
||||||
own = getattr(bundles, "own", None)
|
own = getattr(bundles, "own", None)
|
||||||
rc = own.room_rc(c.id, str(c.params.get("area") or ""), only_one) if own is not None else None
|
rc = own.room_rc(c.id, str(c.params.get("area") or ""), only_one) if own is not None else None
|
||||||
@@ -522,12 +709,17 @@ def _room_rc(c, params: dict, bundles, only_one: bool, now: float) -> str:
|
|||||||
get = getattr(bundles, "room_rc", None)
|
get = getattr(bundles, "room_rc", None)
|
||||||
rc = get(c.id, str(c.params.get("area") or ""), only_one, now) if get else None
|
rc = get(c.id, str(c.params.get("area") or ""), only_one, now) if get else None
|
||||||
if rc is None:
|
if rc is None:
|
||||||
return "defaults"
|
return "none", ""
|
||||||
r, cap = rc["r_k_per_kw"], rc["c_kwh_per_k"]
|
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
|
gain, solar = rc.get("gain_w") or 0.0, rc.get("solar_m2") or 0.0
|
||||||
cop = number(params.get("cop")) or 1.0
|
electric = str(params.get("heat_source") or "electric") in ("electric", "heat_pump")
|
||||||
if rc.get("power") == "electric" and cop > 0:
|
if rc.get("power") == "electric" and electric:
|
||||||
# fitted in electric equivalents: the heat is cop times (like C)
|
# fitted in electric equivalents: the heat is cop times (like C); without
|
||||||
|
# the mapping's COP (a cooler, or a heat pump with its curve alone) the
|
||||||
|
# model stays in electric equivalents (cop 1)
|
||||||
|
cop = number(params.get("cop")) or 1.0
|
||||||
|
if c.kind == "cooling" or "cop" in params:
|
||||||
|
params["cop"] = cop
|
||||||
r, cap = r / cop, cap * cop
|
r, cap = r / cop, cap * cop
|
||||||
gain = [g * cop for g in gain] if isinstance(gain, list) else gain * cop
|
gain = [g * cop for g in gain] if isinstance(gain, list) else gain * cop
|
||||||
solar *= cop
|
solar *= cop
|
||||||
@@ -538,7 +730,51 @@ def _room_rc(c, params: dict, bundles, only_one: bool, now: float) -> str:
|
|||||||
params["internal_gain_w"] = gain
|
params["internal_gain_w"] = gain
|
||||||
if "solar_m2" not in c.params:
|
if "solar_m2" not in c.params:
|
||||||
params["solar_m2"] = solar
|
params["solar_m2"] = solar
|
||||||
return src
|
return src, ("electric" if rc.get("power") == "electric" else "thermal")
|
||||||
|
|
||||||
|
|
||||||
|
def room_refusal(c, st: dict, rc: str, power: str, source: str = "electric") -> dict | None:
|
||||||
|
"""Why a heating or cooling room is not planned (docs/parameters.md E3: R/C measured, power and COP the device's), or
|
||||||
|
None: no R/C (no fit, the mapping sets neither, or only one of the
|
||||||
|
two), the heater's or cooler's electric power unknown (max_power_w),
|
||||||
|
the COP unknown where the model needs it (the mapping's R/C or a fit
|
||||||
|
against the heat; an electric fit needs none), or no room temperature
|
||||||
|
now. {"why", "where", "missing": [...]} (the plan adds "fallback":
|
||||||
|
schedule | own | none). The room keeps its limits, and its thermostat
|
||||||
|
follows the owner's or installer's schedule (preferences) meanwhile,
|
||||||
|
else its own setpoint (optimizer.fallback_entries)."""
|
||||||
|
why, where, missing = [], [], []
|
||||||
|
if rc == "partial":
|
||||||
|
have = "r_k_per_kw" if "r_k_per_kw" in c.params else "c_kwh_per_k"
|
||||||
|
lack = "c_kwh_per_k" if have == "r_k_per_kw" else "r_k_per_kw"
|
||||||
|
why.append(f"the mapping sets {have} but not {lack}")
|
||||||
|
where.append("the mapping's params: both r_k_per_kw and c_kwh_per_k, or neither (the room's fit is used)")
|
||||||
|
missing.append(lack)
|
||||||
|
elif rc == "none":
|
||||||
|
why.append("no R/C fit for this room yet (no room model)")
|
||||||
|
where.append("nothing to do: the box fits the room from its measured temperature and heating (nightly, once "
|
||||||
|
"there are a few days of history); or the installer enters r_k_per_kw and c_kwh_per_k in the "
|
||||||
|
"mapping's params")
|
||||||
|
missing += ["r_k_per_kw", "c_kwh_per_k"]
|
||||||
|
# the electric power: a cooler's, and a heater's that runs on electricity (a boiler's or a heat
|
||||||
|
# station's heat is its max_heat_w: heating_refusal)
|
||||||
|
if "max_power_w" not in c.params and (c.kind == "cooling" or source in ("electric", "heat_pump", "hybrid")):
|
||||||
|
why.append(f"the {'heating' if c.kind == 'heating' else 'cooling'}'s electric power unknown")
|
||||||
|
where.append("the mapping's params (max_power_w, W)")
|
||||||
|
missing.append("max_power_w")
|
||||||
|
# a cooler's COP (a heater's: electric 1, a heat pump's or hybrid's: heating_refusal)
|
||||||
|
if c.kind == "cooling" and rc in ("mapping", "model", "box model") and power != "electric" and "cop" not in c.params:
|
||||||
|
why.append("COP unknown (heat per kWh of electricity)")
|
||||||
|
where.append("the mapping's params (cop)")
|
||||||
|
missing.append("cop")
|
||||||
|
if st.get("temp_c") is None:
|
||||||
|
why.append("room temperature unknown" + (" (no temperature sensor)" if not c.entities.get("temp") and
|
||||||
|
not c.entities.get("climate") else " (no value now)"))
|
||||||
|
where.append("map the room's temperature sensor (temp), or a thermostat that reports its temperature")
|
||||||
|
missing.append("temp")
|
||||||
|
if not why:
|
||||||
|
return None
|
||||||
|
return {"why": "; ".join(why), "where": "; ".join(where), "missing": missing}
|
||||||
|
|
||||||
|
|
||||||
def _quantities(c) -> list[str]:
|
def _quantities(c) -> list[str]:
|
||||||
|
|||||||
@@ -64,13 +64,29 @@ def battery_ratings(controls) -> dict:
|
|||||||
if "capacity_wh" in c.params:
|
if "capacity_wh" in c.params:
|
||||||
kwh += float(c.params["capacity_wh"]) / 1000
|
kwh += float(c.params["capacity_wh"]) / 1000
|
||||||
if "max_charge_w" in c.params or "max_discharge_w" in c.params:
|
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
|
kw += max(float(c.params.get("max_charge_w") or 0), float(c.params.get("max_discharge_w") or 0)) / 1000
|
||||||
out = {"battery_kwh": round(kwh, 3)} if kwh > 0 else {}
|
out = {"battery_kwh": round(kwh, 3)} if kwh > 0 else {}
|
||||||
if out and kw > 0:
|
if out and kw > 0:
|
||||||
out["battery_kw"] = round(kw, 3)
|
out["battery_kw"] = round(kw, 3)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def heating_ratings(controls) -> dict:
|
||||||
|
"""A heating control's boiler efficiency as its mapping states it
|
||||||
|
(params boiler_efficiency, 0-1; controls.heat_facts' first source):
|
||||||
|
the cloud's gas forecast divides the heat forecast by it (methods.md
|
||||||
|
B14). Never a default."""
|
||||||
|
for c in controls:
|
||||||
|
if c.kind == "heating" and "boiler_efficiency" in c.params:
|
||||||
|
try:
|
||||||
|
v = float(c.params["boiler_efficiency"])
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
continue
|
||||||
|
if 0 < v <= 1:
|
||||||
|
return {"boiler_efficiency": round(v, 4)}
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
def build(mapping, registries: dict, specs: dict, policy: dict, now: float, ev_phases: dict | None = None) -> dict:
|
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),
|
"""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)."""
|
told on the device of the control ("1 phase, learned" in the console)."""
|
||||||
@@ -129,6 +145,7 @@ def build(mapping, registries: dict, specs: dict, policy: dict, now: float, ev_p
|
|||||||
hd = devs.get(d) or {}
|
hd = devs.get(d) or {}
|
||||||
mine = {control_of[e] for e in eids if e in control_of}
|
mine = {control_of[e] for e in eids if e in control_of}
|
||||||
ratings = battery_ratings([controls[i] for i in sorted(mine - rated)])
|
ratings = battery_ratings([controls[i] for i in sorted(mine - rated)])
|
||||||
|
ratings.update(heating_ratings([controls[i] for i in sorted(mine - rated)]))
|
||||||
rated |= mine
|
rated |= mine
|
||||||
if not d and not ratings:
|
if not d and not ratings:
|
||||||
continue # entities without a device: nothing to tell but a battery's ratings
|
continue # entities without a device: nothing to tell but a battery's ratings
|
||||||
|
|||||||
@@ -40,11 +40,13 @@ from . import fcmodels, pvcalib, pvforecast
|
|||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
SLOT_S = 900
|
SLOT_S = 900
|
||||||
SERIES = {"load_w": "load", "pv_w": "pv", "base_load_w": "base_load"} # the bundle's names: fcmodels'
|
SERIES = {"load_w": "load", "pv_w": "pv", "base_load_w": "base_load", # the bundle's names: fcmodels'
|
||||||
|
# the carriers' (methods.md B13-B16): the box carries their models, it plans on none of them yet
|
||||||
|
"heat_w": "heat", "dhw_w": "dhw", "occupancy": "occupancy", "ev_plug": "ev_plug"}
|
||||||
# the agent's history (inputs.History) a series' lag comes from: its load_w
|
# the agent's history (inputs.History) a series' lag comes from: its load_w
|
||||||
# is the base load already (the house without the planned devices), which
|
# is the base load already (the house without the planned devices), which
|
||||||
# is the load where the box plans none (the only case its load_w is used)
|
# is the load where the box plans none (the only case its load_w is used)
|
||||||
HISTORY = {"load_w": "load_w", "base_load_w": "load_w", "pv_w": "pv_w"}
|
HISTORY = {"load_w": "load_w", "base_load_w": "load_w", "pv_w": "pv_w"} # the carriers': none kept (no lag)
|
||||||
SITE_FIELDS = {"tz": str, "lat": float, "lon": float, "kwp": float, "tilt": float, "azimuth": float, "state": str}
|
SITE_FIELDS = {"tz": str, "lat": float, "lon": float, "kwp": float, "tilt": float, "azimuth": float, "state": str}
|
||||||
|
|
||||||
|
|
||||||
@@ -62,7 +64,7 @@ class Model:
|
|||||||
|
|
||||||
def __init__(self, name: str, d: dict):
|
def __init__(self, name: str, d: dict):
|
||||||
if name not in SERIES or not isinstance(d, dict) or not isinstance(d.get("params"), dict):
|
if name not in SERIES or not isinstance(d, dict) or not isinstance(d.get("params"), dict):
|
||||||
raise ValueError("a model: {params, site} for load_w, pv_w or base_load_w")
|
raise ValueError("a model: {params, site} for " + ", ".join(SERIES))
|
||||||
self.name, self.series = name, SERIES[name]
|
self.name, self.series = name, SERIES[name]
|
||||||
p, s = dict(d["params"]), d.get("site") or {}
|
p, s = dict(d["params"]), d.get("site") or {}
|
||||||
if not isinstance(s, dict):
|
if not isinstance(s, dict):
|
||||||
@@ -88,9 +90,10 @@ class Model:
|
|||||||
elif self.kind == "gbm":
|
elif self.kind == "gbm":
|
||||||
# the features it was trained with: today's, or those before the
|
# the features it was trained with: today's, or those before the
|
||||||
# special days and the yearly rhythm (fcmodels.FEATURES_V1)
|
# special days and the yearly rhythm (fcmodels.FEATURES_V1)
|
||||||
key = "pv" if self.series == "pv" else "load"
|
want = fcmodels.feature_names(self.series)
|
||||||
want = fcmodels.PV_FEATURES if key == "pv" else fcmodels.LOAD_FEATURES
|
v1 = fcmodels.FEATURES_V1.get("pv" if self.series == "pv" else "load") if self.series in ("load", "base_load", "pv") \
|
||||||
if p.get("features") not in (want, fcmodels.FEATURES_V1[key]) or not _numbers(p.get("how"), 168) \
|
else None
|
||||||
|
if p.get("features") not in (want, v1) or not _numbers(p.get("how"), 168) \
|
||||||
or "model" in p or not all(isinstance(k, int) for k in p.get("special_kept") or []):
|
or "model" in p or not all(isinstance(k, int) for k in p.get("special_kept") or []):
|
||||||
raise ValueError(f"gbm: the features {', '.join(want)}, 168 hour-of-week values, trees")
|
raise ValueError(f"gbm: the features {', '.join(want)}, 168 hour-of-week values, trees")
|
||||||
if p.get("cap_w") is not None and not _num(p["cap_w"]):
|
if p.get("cap_w") is not None and not _num(p["cap_w"]):
|
||||||
@@ -148,12 +151,12 @@ def ha_hours(forecast: list[dict] | None) -> list:
|
|||||||
|
|
||||||
|
|
||||||
def cloud_hours(recs) -> list:
|
def cloud_hours(recs) -> list:
|
||||||
"""The bundle's "weather": [{"end", "solar", "cloud", "temp"}]."""
|
"""The bundle's "weather": [{"end", "solar", "cloud", "temp", "wind"}]."""
|
||||||
out = []
|
out = []
|
||||||
for r in recs if isinstance(recs, list) else []:
|
for r in recs if isinstance(recs, list) else []:
|
||||||
if not isinstance(r, dict) or not isinstance(r.get("end"), int) or r["end"] % 3600:
|
if not isinstance(r, dict) or not isinstance(r.get("end"), int) or r["end"] % 3600:
|
||||||
continue
|
continue
|
||||||
v = [float(r[k]) if _num(r.get(k)) else None for k in ("solar", "cloud", "temp")]
|
v = [float(r[k]) if _num(r.get(k)) else None for k in ("solar", "cloud", "temp", "wind")]
|
||||||
out.append(pvforecast.Hour(datetime.fromtimestamp(r["end"], timezone.utc), *v))
|
out.append(pvforecast.Hour(datetime.fromtimestamp(r["end"], timezone.utc), *v))
|
||||||
return out
|
return out
|
||||||
|
|
||||||
@@ -211,7 +214,7 @@ def predict(model: Model, t: list[int], history, ha_forecast: list[dict] | None,
|
|||||||
covered = [((ts // 3600 + 1) * 3600) in src for ts in t]
|
covered = [((ts // 3600 + 1) * 3600) in src for ts in t]
|
||||||
s = model.site
|
s = model.site
|
||||||
w = fcmodels.weather(ta, hours, s.lat, s.lon)
|
w = fcmodels.weather(ta, hours, s.lat, s.lon)
|
||||||
ht, hy = _history(history, HISTORY[model.name], int(ta[0]) - 7 * 86400, int(ta[0]))
|
ht, hy = _history(history, HISTORY.get(model.name, ""), int(ta[0]) - 7 * 86400, int(ta[0]))
|
||||||
p = fcmodels.predict(model.params, model.series, ta, w, s, ht, hy) if any(covered) else np.full(len(t), np.nan)
|
p = fcmodels.predict(model.params, model.series, ta, w, s, ht, hy) if any(covered) else np.full(len(t), np.nan)
|
||||||
clim = model.climate(ta) if not all(covered) else None
|
clim = model.climate(ta) if not all(covered) else None
|
||||||
out: list[float | None] = []
|
out: list[float | None] = []
|
||||||
|
|||||||
@@ -6,7 +6,29 @@ every 15 minutes in a subprocess with a time limit (controller.py):
|
|||||||
|
|
||||||
Objective: the grid's cost (the tariff's import and export prices) plus
|
Objective: the grid's cost (the tariff's import and export prices) plus
|
||||||
penalties for comfort the plan gives up and for the floors and limits it
|
penalties for comfort the plan gives up and for the floors and limits it
|
||||||
would cross. Floors and limits are soft here, with a penalty far above any
|
would cross.
|
||||||
|
|
||||||
|
Prices (docs/parameters.md U1; no price is ever made up):
|
||||||
|
- no tariff (none received, expired, none for now: state["prices"]
|
||||||
|
status "none"): the plan is made without prices, for comfort, the
|
||||||
|
limits and the owner's musts only. The meter's energy and the devices'
|
||||||
|
energy carry a tiny engineering weight (NO_PRICE_EUR_KWH: of plans
|
||||||
|
otherwise equal, the one that moves the least energy), so nothing is
|
||||||
|
shifted, pre-heated, stored or exported for a price; the battery stays
|
||||||
|
in its own mode unless a reserve the owner set needs charging; the
|
||||||
|
market's contracts are not planned (nothing to compare them with).
|
||||||
|
The plan's cost and baseline are None and its price_note says why.
|
||||||
|
- a tariff that prices only the first n steps (beyond its end, or a gap):
|
||||||
|
the plan covers those n steps only (_cut), never a step without a
|
||||||
|
price as if it were free.
|
||||||
|
- a step without an export price: export is unpriced there, it carries
|
||||||
|
no value (neither earned nor charged, coefficient 0) - not "0 ct" as a
|
||||||
|
fact: the plan exports what it cannot use, but never discharges a
|
||||||
|
battery or plans anything to export, and the meter's import/export
|
||||||
|
switch treats it as worth nothing. Curtailing (spill) has its own small
|
||||||
|
engineering cost (SPILL_EUR_KWH), so PV is never curtailed while the
|
||||||
|
meter can take it, whatever export is worth; a negative export price
|
||||||
|
(a real one) still curtails. Floors and limits are soft here, with a penalty far above any
|
||||||
price, so there is always a plan (a room already below its floor must be
|
price, so there is always a plan (a room already below its floor must be
|
||||||
able to come back); the executor's guard enforces them hard on every action.
|
able to come back); the executor's guard enforces them hard on every action.
|
||||||
|
|
||||||
@@ -19,7 +41,13 @@ Device models (one function each, small and replaceable):
|
|||||||
(internal_gain_w, solar_m2 x the irradiance; the sun's only
|
(internal_gain_w, solar_m2 x the irradiance; the sun's only
|
||||||
where the room has no cover in the plan, which carries it));
|
where the room has no cover in the plan, which carries it));
|
||||||
comfort target when occupied, setback/setup otherwise; a
|
comfort target when occupied, setback/setup otherwise; a
|
||||||
switched boiler is on/off (binary)
|
switched boiler is on/off (binary). Only with its R and C (the
|
||||||
|
mapping's both, or a fit: never built-in defaults), its electric
|
||||||
|
power (max_power_w), its COP where the model needs it and its
|
||||||
|
temperature now; else the room is not planned (inputs.
|
||||||
|
room_refusal): its thermostat follows the owner's
|
||||||
|
or installer's schedule within the room's limits
|
||||||
|
(fallback_entries), else keeps its own setpoint
|
||||||
ev the energy needed by the departure time, between min and max
|
ev the energy needed by the departure time, between min and max
|
||||||
current (binary), only while connected
|
current (binary), only while connected
|
||||||
cover the share of the sun's heat into its room (0 closed .. 1 open);
|
cover the share of the sun's heat into its room (0 closed .. 1 open);
|
||||||
@@ -28,6 +56,22 @@ Device models (one function each, small and replaceable):
|
|||||||
season, quiet hours) its positions are fixed to it and its plan
|
season, quiet hours) its positions are fixed to it and its plan
|
||||||
entries are the schedule's moves
|
entries are the schedule's moves
|
||||||
|
|
||||||
|
Heat by its source (docs/methods.md E6; params.heat_source, the prices
|
||||||
|
state["price_gas"] and state["price_heat"] per step, fleet heatprice.go):
|
||||||
|
electric / heat_pump electric W through the meter at the import price; heat
|
||||||
|
= COP x W (params cop, or cop_curve at the step's
|
||||||
|
outside temperature)
|
||||||
|
boiler gas W (billed, Hs) at the gas price; heat = the
|
||||||
|
boiler's efficiency x W; up to max_heat_w of heat
|
||||||
|
district heat W at the heat price; up to max_heat_w
|
||||||
|
hybrid both, one at a time (a binary per step: the heat pump
|
||||||
|
or the boiler), each change of source costing the
|
||||||
|
Parameters page's heat.hybrid_switch_eur (state
|
||||||
|
"params", from the tariff bundle: no flapping)
|
||||||
|
The plan's cost then counts gas and heat too (gas_eur, heat_eur). A control
|
||||||
|
whose price or facts are missing never reaches the plan (inputs.py
|
||||||
|
heating_refusal: refused with the reason).
|
||||||
|
|
||||||
The grid operator's limit (gridorder.py, state["grid_order"]: §14a through
|
The grid operator's limit (gridorder.py, state["grid_order"]: §14a through
|
||||||
the EEBUS bridge): the controllable consumers may draw at most
|
the EEBUS bridge): the controllable consumers may draw at most
|
||||||
limit_w from the grid beyond the household's own net load, i.e. the import
|
limit_w from the grid beyond the household's own net load, i.e. the import
|
||||||
@@ -70,7 +114,7 @@ from zoneinfo import ZoneInfo
|
|||||||
import highspy
|
import highspy
|
||||||
from highspy.highs import highs_linear_expression as Lin
|
from highspy.highs import highs_linear_expression as Lin
|
||||||
|
|
||||||
from .controls import ev_volts
|
from .controls import cop_at, ev_volts
|
||||||
from .evphases import words as phase_words
|
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)
|
PENALTY_LIMIT = 5.0 # EUR per unit and step across a floor or limit (K, % of a battery, kWh)
|
||||||
@@ -91,6 +135,10 @@ CHANGE_EUR_KW = 0.002 # EUR per kW a device's power changes from one step to
|
|||||||
# boiler must not be switched back and forth)
|
# 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
|
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
|
MARKET_MAX_W = 100000.0 # W: a contract without a maximum, and the meter's import/export switch
|
||||||
|
NO_PRICE_EUR_KWH = 1e-4 # EUR per kWh through the meter and per kWh a device uses, without a tariff
|
||||||
|
# only (ENG: a tie-break toward the least energy, not a price)
|
||||||
|
SPILL_EUR_KWH = 1e-3 # EUR per kWh of PV curtailed (ENG: never curtail while the meter takes it;
|
||||||
|
# above NO_PRICE_EUR_KWH so export wins over spill without a tariff)
|
||||||
GRID_MAX_W = 43470.0 # W: the site's connection when the state does not say (state["grid_max_w"],
|
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,
|
# config grid_max_w): a German house connection's usual 63 A main fuse,
|
||||||
# 3 x 230 V x 63 A
|
# 3 x 230 V x 63 A
|
||||||
@@ -105,6 +153,11 @@ class Model:
|
|||||||
self.flows = [[] for _ in range(self.T)] # (+1 into the house's balance | -1 out of it, var)
|
self.flows = [[] for _ in range(self.T)] # (+1 into the house's balance | -1 out of it, var)
|
||||||
self.cost = [] # (coef, var)
|
self.cost = [] # (coef, var)
|
||||||
self.devices = {} # id -> (kind, vars for the plan)
|
self.devices = {} # id -> (kind, vars for the plan)
|
||||||
|
# False: no tariff, no prices (_prepare); a state not prepared: priced
|
||||||
|
# where it has a tariff and an import price for every step
|
||||||
|
self.priced = s["priced"] if "priced" in s else (
|
||||||
|
(s.get("prices") or {}).get("status", "tariff") != "none" and
|
||||||
|
all(v is not None for v in (s.get("price_import") or [None])[:self.T]))
|
||||||
|
|
||||||
def var(self, lo=0.0, hi=highspy.kHighsInf, cost=0.0, integer=False):
|
def var(self, lo=0.0, hi=highspy.kHighsInf, cost=0.0, integer=False):
|
||||||
v = self.h.addVariable(lb=lo, ub=hi, type=highspy.HighsVarType.kInteger if integer else
|
v = self.h.addVariable(lb=lo, ub=hi, type=highspy.HighsVarType.kInteger if integer else
|
||||||
@@ -121,9 +174,17 @@ class Model:
|
|||||||
self.h.addConstr(d - b + a >= 0)
|
self.h.addConstr(d - b + a >= 0)
|
||||||
self.h.addConstr(d + b - a >= 0)
|
self.h.addConstr(d + b - a >= 0)
|
||||||
|
|
||||||
def eur_wh(self, t: int, price_ct: float) -> float:
|
def eur_wh(self, t: int, price_ct: float | None) -> float:
|
||||||
"""EUR for one W held for one step at this price."""
|
"""EUR for one W held for one step at this price (None: no price, 0)."""
|
||||||
return self.dt / 1000.0 * price_ct / 100.0
|
return 0.0 if price_ct is None else self.dt / 1000.0 * price_ct / 100.0
|
||||||
|
|
||||||
|
def use_cost(self, t: int = 0) -> float:
|
||||||
|
"""Without a tariff: a device's energy's tie-break weight per W and
|
||||||
|
step (NO_PRICE_EUR_KWH; rising a little over the horizon, so of
|
||||||
|
equal plans the earlier wins); with one 0 (its prices count)."""
|
||||||
|
if self.priced:
|
||||||
|
return 0.0
|
||||||
|
return self.dt / 1000.0 * NO_PRICE_EUR_KWH * (1.0 + t / max(1, self.T))
|
||||||
|
|
||||||
# -- the house -------------------------------------------------------
|
# -- the house -------------------------------------------------------
|
||||||
def grid(self):
|
def grid(self):
|
||||||
@@ -143,9 +204,14 @@ class Model:
|
|||||||
for t in range(self.T):
|
for t in range(self.T):
|
||||||
net = s["load_w"][t] - s["pv_w"][t]
|
net = s["load_w"][t] - s["pv_w"][t]
|
||||||
hi_in, hi_out = max(cap, net), max(cap, -net)
|
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]))
|
if self.priced:
|
||||||
go = self.var(0.0, hi_out, cost=-self.eur_wh(t, s["price_export"][t]))
|
gi = self.var(0.0, hi_in, cost=self.eur_wh(t, s["price_import"][t]))
|
||||||
spill = self.var(0.0, max(0.0, s["pv_w"][t]))
|
# an unpriced export (None) carries no value: coefficient 0
|
||||||
|
go = self.var(0.0, hi_out, cost=-self.eur_wh(t, s["price_export"][t]))
|
||||||
|
else:
|
||||||
|
gi = self.var(0.0, hi_in, cost=self.dt / 1000.0 * NO_PRICE_EUR_KWH)
|
||||||
|
go = self.var(0.0, hi_out, cost=self.dt / 1000.0 * NO_PRICE_EUR_KWH)
|
||||||
|
spill = self.var(0.0, max(0.0, s["pv_w"][t]), cost=self.dt / 1000.0 * SPILL_EUR_KWH)
|
||||||
self.gin.append(gi)
|
self.gin.append(gi)
|
||||||
self.gout.append(go)
|
self.gout.append(go)
|
||||||
self.gcap.append((hi_in, hi_out))
|
self.gcap.append((hi_in, hi_out))
|
||||||
@@ -155,8 +221,11 @@ class Model:
|
|||||||
"""The meter's import/export switch where the feed-in pays at least
|
"""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)."""
|
the import price (grid), outside the steps in skip (market's own)."""
|
||||||
s = self.s
|
s = self.s
|
||||||
|
if not self.priced:
|
||||||
|
return # both carry the same tiny weight: doing both never pays
|
||||||
for t in range(self.T):
|
for t in range(self.T):
|
||||||
if t in skip or s["price_export"][t] < s["price_import"][t]:
|
pe = s["price_export"][t] if s["price_export"][t] is not None else 0.0 # unpriced: worth nothing
|
||||||
|
if t in skip or pe < s["price_import"][t]:
|
||||||
continue
|
continue
|
||||||
hi_in, hi_out = self.gcap[t]
|
hi_in, hi_out = self.gcap[t]
|
||||||
imp = self.var(0.0, 1.0, integer=True)
|
imp = self.var(0.0, 1.0, integer=True)
|
||||||
@@ -261,7 +330,7 @@ class Model:
|
|||||||
max_ch = max(0.0, min(float(p["max_charge_w"]), phi))
|
max_ch = max(0.0, min(float(p["max_charge_w"]), phi))
|
||||||
max_dis = max(0.0, min(float(p["max_discharge_w"]), -plo))
|
max_dis = max(0.0, min(float(p["max_discharge_w"]), -plo))
|
||||||
soc0 = cap * min(max(float(st["soc_pct"]), 0.0), 100.0) / 100.0
|
soc0 = cap * min(max(float(st["soc_pct"]), 0.0), 100.0) / 100.0
|
||||||
mean_price = sum(self.s["price_import"]) / self.T
|
mean_price = sum(self.s["price_import"]) / self.T if self.priced else None
|
||||||
soc, prev, ch, dis = [], soc0, [], []
|
soc, prev, ch, dis = [], soc0, [], []
|
||||||
pref = d.get("pref") # the owner's reserve per step (preferences.py)
|
pref = d.get("pref") # the owner's reserve per step (preferences.py)
|
||||||
floors = []
|
floors = []
|
||||||
@@ -270,8 +339,8 @@ class Model:
|
|||||||
if pref and not (pref.get("off") is not None and pref["group"][t] == pref["off"]):
|
if pref and not (pref.get("off") is not None and pref["group"][t] == pref["off"]):
|
||||||
lo_t = min(max(lo_pct, pref["floor_pct"][t]), hi_pct)
|
lo_t = min(max(lo_pct, pref["floor_pct"][t]), hi_pct)
|
||||||
floors.append(lo_t)
|
floors.append(lo_t)
|
||||||
c = self.var(0.0, max_ch)
|
c = self.var(0.0, max_ch, cost=self.use_cost(t))
|
||||||
x = self.var(0.0, max_dis)
|
x = self.var(0.0, max_dis, cost=self.use_cost(t))
|
||||||
z = self.var(0.0, 1.0, integer=True)
|
z = self.var(0.0, 1.0, integer=True)
|
||||||
self.h.addConstr(c - max_ch * z <= 0)
|
self.h.addConstr(c - max_ch * z <= 0)
|
||||||
self.h.addConstr(x + max_dis * z <= max_dis)
|
self.h.addConstr(x + max_dis * z <= max_dis)
|
||||||
@@ -286,17 +355,24 @@ class Model:
|
|||||||
ch.append(c)
|
ch.append(c)
|
||||||
dis.append(x)
|
dis.append(x)
|
||||||
prev = e
|
prev = e
|
||||||
# energy left at the end is worth the mean price: no draining at the horizon
|
# energy left at the end is worth the mean price: no draining at the
|
||||||
short = self.var(cost=mean_price / 100.0 / 1000.0)
|
# horizon (without a tariff nothing is drained for a price anyway)
|
||||||
self.h.addConstr(prev + short >= min(soc0, hi_pct / 100.0 * cap))
|
if mean_price is not None:
|
||||||
|
short = self.var(cost=mean_price / 100.0 / 1000.0)
|
||||||
|
self.h.addConstr(prev + short >= min(soc0, hi_pct / 100.0 * cap))
|
||||||
self.smooth([c - x for c, x in zip(ch, dis)])
|
self.smooth([c - x for c, x in zip(ch, dis)])
|
||||||
self.devices[d["id"]] = ("battery", {"ch": ch, "dis": dis, "soc": soc, "cap": cap, "max_dis": max_dis,
|
self.devices[d["id"]] = ("battery", {"ch": ch, "dis": dis, "soc": soc, "cap": cap, "max_dis": max_dis,
|
||||||
"floor_wh": lo_pct / 100.0 * cap, "floors": floors})
|
"floor_wh": lo_pct / 100.0 * cap, "floors": floors,
|
||||||
|
"max_ch": max_ch, "eff": eff, "soc0": soc0, "hi_wh": hi_pct / 100.0 * cap})
|
||||||
|
|
||||||
def room(self, d, cooling: bool, gains):
|
def room(self, d, cooling: bool, gains):
|
||||||
p, st, b = d["params"], d["state"], d["bounds"]
|
p, st, b = d["params"], d["state"], d["bounds"]
|
||||||
r, c = float(p["r_k_per_kw"]), float(p["c_kwh_per_k"])
|
r, c = float(p["r_k_per_kw"]), float(p["c_kwh_per_k"])
|
||||||
pmax, cop = float(p["max_power_w"]), float(p["cop"])
|
# the device's electric power and COP: facts (inputs.room_refusal / heating_refusal left out
|
||||||
|
# a room without them); a boiler or a heat station draws no electricity in the plan
|
||||||
|
elec = cooling or str(p.get("heat_source") or "electric") in ("electric", "heat_pump", "hybrid")
|
||||||
|
pmax = float(p["max_power_w"]) if elec else 0.0
|
||||||
|
cop = float(p["cop"]) if p.get("cop") is not None else 0.0 # 0: none (a curve alone, or no electricity)
|
||||||
lo, hi = b["temp_c"][0], b["temp_c"][1]
|
lo, hi = b["temp_c"][0], b["temp_c"][1]
|
||||||
comfort = float(p["comfort_c"])
|
comfort = float(p["comfort_c"])
|
||||||
other = float(p["setup_c"] if cooling else p["setback_c"])
|
other = float(p["setup_c"] if cooling else p["setback_c"])
|
||||||
@@ -304,6 +380,21 @@ class Model:
|
|||||||
t0 = float(t0) if t0 is not None else comfort
|
t0 = float(t0) if t0 is not None else comfort
|
||||||
switched = not cooling and "switch" in d["channels"] and "climate" not in d["channels"]
|
switched = not cooling and "switch" in d["channels"] and "climate" not in d["channels"]
|
||||||
temps, powers, prev = [], [], t0
|
temps, powers, prev = [], [], t0
|
||||||
|
band = [] # per step the owner's band (the shift envelope's: shift_envelope)
|
||||||
|
# what makes the heat (module docstring; inputs.py refused it where a price or fact is missing)
|
||||||
|
src = "electric" if cooling else str(p.get("heat_source") or "electric")
|
||||||
|
# facts and parameters, never defaults: inputs.heating_refusal left out a control without them
|
||||||
|
eff = float(p["boiler_efficiency"]) if src in ("boiler", "hybrid") else None
|
||||||
|
qmax = float(p["max_heat_w"]) if src in ("boiler", "hybrid", "district") else 0.0
|
||||||
|
gmax = qmax / eff if eff else 0.0
|
||||||
|
switch_eur = float((self.s.get("params") or {})["heat.hybrid_switch_eur"]) if src == "hybrid" else 0.0
|
||||||
|
gas = self.s.get("price_gas") or [None] * self.T
|
||||||
|
dhp = self.s.get("price_heat") or [None] * self.T
|
||||||
|
t_out = self.s["t_out"]
|
||||||
|
cops = [cop_at(p, t_out[t]) if p.get("cop_curve") else cop for t in range(self.T)]
|
||||||
|
els, gss, dhs, prev_u = [], [], [], None
|
||||||
|
if src == "hybrid" and st.get("source") in ("heat_pump", "boiler"):
|
||||||
|
prev_u = 1.0 if st["source"] == "heat_pump" else 0.0 # the source now: a change from it counts
|
||||||
a = self.dt / (r * c)
|
a = self.dt / (r * c)
|
||||||
internal = self.hourly(p.get("internal_gain_w") or 0.0)
|
internal = self.hourly(p.get("internal_gain_w") or 0.0)
|
||||||
solar = float(p.get("solar_m2") or 0.0)
|
solar = float(p.get("solar_m2") or 0.0)
|
||||||
@@ -311,21 +402,52 @@ class Model:
|
|||||||
pref = d.get("pref") # the owner's schedule and overrides (preferences.py)
|
pref = d.get("pref") # the owner's schedule and overrides (preferences.py)
|
||||||
cap = (self.s.get("building") or {}).get("thermostat_cap")
|
cap = (self.s.get("building") or {}).get("thermostat_cap")
|
||||||
for t in range(self.T):
|
for t in range(self.T):
|
||||||
if switched:
|
terms = [] # (W of heat per W of input, the input)
|
||||||
u = self.var(0.0, 1.0, integer=True)
|
pw = pg = ph = None
|
||||||
pw = self.var(0.0, pmax)
|
if src in ("electric", "heat_pump", "hybrid"):
|
||||||
self.h.addConstr(pw - pmax * u == 0)
|
if switched and src != "hybrid":
|
||||||
else:
|
u = self.var(0.0, 1.0, integer=True)
|
||||||
pw = self.var(0.0, pmax)
|
pw = self.var(0.0, pmax, cost=self.use_cost(t))
|
||||||
self.flows[t].append((-1, pw))
|
self.h.addConstr(pw - pmax * u == 0)
|
||||||
|
else:
|
||||||
|
pw = self.var(0.0, pmax, cost=self.use_cost(t))
|
||||||
|
self.flows[t].append((-1, pw))
|
||||||
|
terms.append((cops[t], pw))
|
||||||
|
if src in ("boiler", "hybrid"):
|
||||||
|
cost = self.eur_wh(t, gas[t]) # the gas price per W of gas for the step
|
||||||
|
if switched and src == "boiler":
|
||||||
|
u = self.var(0.0, 1.0, integer=True)
|
||||||
|
pg = self.var(0.0, gmax, cost=cost)
|
||||||
|
self.h.addConstr(pg - gmax * u == 0)
|
||||||
|
else:
|
||||||
|
pg = self.var(0.0, gmax, cost=cost)
|
||||||
|
terms.append((eff, pg))
|
||||||
|
if src == "district":
|
||||||
|
ph = self.var(0.0, qmax, cost=self.eur_wh(t, dhp[t]))
|
||||||
|
terms.append((1.0, ph))
|
||||||
|
if src == "hybrid":
|
||||||
|
hu = self.var(0.0, 1.0, integer=True) # 1: the heat pump, 0: the boiler
|
||||||
|
self.h.addConstr(pw - pmax * hu <= 0)
|
||||||
|
self.h.addConstr(pg + gmax * hu <= gmax)
|
||||||
|
if prev_u is not None:
|
||||||
|
sw = self.var(cost=switch_eur)
|
||||||
|
self.h.addConstr(sw - hu + prev_u >= 0)
|
||||||
|
self.h.addConstr(sw + hu - prev_u >= 0)
|
||||||
|
prev_u = hu
|
||||||
|
els.append(pw)
|
||||||
|
gss.append(pg)
|
||||||
|
dhs.append(ph)
|
||||||
|
heat = sum((k * v for k, v in terms), start=Lin())
|
||||||
|
main = pw if pw is not None else pg if pg is not None else ph
|
||||||
tt = self.var(-50.0, 80.0)
|
tt = self.var(-50.0, 80.0)
|
||||||
sign = -1.0 if cooling else 1.0
|
sign = -1.0 if cooling else 1.0
|
||||||
covers = gains(t)
|
covers = gains(t)
|
||||||
gain = sum((g * gx for g, gx in covers), start=Lin())
|
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
|
# 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)
|
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)
|
# T[t+1] = T[t] (1 - a) + a T_out + dt/C (± heat/1000 + sun gain/1000 + fixed gains/1000);
|
||||||
self.h.addConstr(tt - (1 - a) * prev - sign * self.dt / c * cop / 1000.0 * pw
|
# heat: COP x electric W, efficiency x gas W, district W (module docstring)
|
||||||
|
self.h.addConstr(tt - (1 - a) * prev - sign * self.dt / c / 1000.0 * heat
|
||||||
- self.dt / c / 1000.0 * gain == a * self.s["t_out"][t] + self.dt / c / 1000.0 * fixed)
|
- 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]
|
occ = self.s["occupied"][t + 1] if t + 1 < self.T else self.s["occupied"][t]
|
||||||
want, must = (comfort if occ else other), None
|
want, must = (comfort if occ else other), None
|
||||||
@@ -335,6 +457,13 @@ class Model:
|
|||||||
want, must = pref["base"][t], None # pref_costs: the plan without this group
|
want, must = pref["base"][t], None # pref_costs: the plan without this group
|
||||||
target = min(max(want, lo), hi)
|
target = min(max(want, lo), hi)
|
||||||
capped = bool(cap) and cap["from_step"] <= t < cap["to_step"]
|
capped = bool(cap) and cap["from_step"] <= t < cap["to_step"]
|
||||||
|
# heating: from its target (and must) up to its upper bound (under a building's cap); cooling mirrored
|
||||||
|
if cooling:
|
||||||
|
b_hi = min(target, must) if must is not None else target
|
||||||
|
band.append((max(lo, cap["value"]) if capped else lo, max(min(b_hi, hi), lo)))
|
||||||
|
else:
|
||||||
|
b_lo = max(target, must) if must is not None else target
|
||||||
|
band.append((min(max(b_lo, lo), hi), min(hi, cap["value"]) if capped else hi))
|
||||||
if must is not None:
|
if must is not None:
|
||||||
# a bound inside the bounds in force, and under a building's cap
|
# a bound inside the bounds in force, and under a building's cap
|
||||||
mv = min(max(must, lo), hi)
|
mv = min(max(must, lo), hi)
|
||||||
@@ -361,11 +490,19 @@ class Model:
|
|||||||
else:
|
else:
|
||||||
self.h.addConstr(tt - miss_cap <= cap["value"]) # heating: not above it
|
self.h.addConstr(tt - miss_cap <= cap["value"]) # heating: not above it
|
||||||
temps.append(tt)
|
temps.append(tt)
|
||||||
powers.append(pw)
|
powers.append(main)
|
||||||
prev = tt
|
prev = tt
|
||||||
self.smooth(powers)
|
self.smooth(powers if src != "hybrid" else [a_ + b_ for a_, b_ in zip(els, gss)])
|
||||||
|
# power: the electric input (None for gas and district heat: no electricity to shed or price);
|
||||||
|
# gas, district: their inputs; heat: per step the heat terms
|
||||||
self.devices[d["id"]] = ("cooling" if cooling else "heating",
|
self.devices[d["id"]] = ("cooling" if cooling else "heating",
|
||||||
{"temp": temps, "power": powers, "pmax": pmax, "switched": switched})
|
{"temp": temps, "power": els, "gas": gss, "district": dhs, "src": src,
|
||||||
|
"cops": cops, "eff": eff,
|
||||||
|
"pmax": {"electric": pmax, "heat_pump": pmax, "hybrid": pmax, "boiler": gmax,
|
||||||
|
"district": qmax}[src], "switched": switched,
|
||||||
|
# the shift envelope (electric sources only): Wh of electricity per K at the mean COP
|
||||||
|
"keep": 1 - a, "wh_per_k": 1000.0 * c / (sum(cops) / len(cops)) if cops and min(cops) > 0 else 0.0,
|
||||||
|
"band": band})
|
||||||
|
|
||||||
def hourly(self, v) -> list[float]:
|
def hourly(self, v) -> list[float]:
|
||||||
"""A number, or 24 by local hour, per step."""
|
"""A number, or 24 by local hour, per step."""
|
||||||
@@ -397,7 +534,7 @@ class Model:
|
|||||||
powers.append(None)
|
powers.append(None)
|
||||||
continue
|
continue
|
||||||
y = self.var(0.0, 1.0, integer=True)
|
y = self.var(0.0, 1.0, integer=True)
|
||||||
pw = self.var(0.0, imax * volts)
|
pw = self.var(0.0, imax * volts, cost=self.use_cost(t))
|
||||||
self.h.addConstr(pw - imax * volts * y <= 0)
|
self.h.addConstr(pw - imax * volts * y <= 0)
|
||||||
self.h.addConstr(pw - imin * volts * y >= 0)
|
self.h.addConstr(pw - imin * volts * y >= 0)
|
||||||
self.flows[t].append((-1, pw))
|
self.flows[t].append((-1, pw))
|
||||||
@@ -417,7 +554,9 @@ class Model:
|
|||||||
self.h.addConstr(total + short >= need)
|
self.h.addConstr(total + short >= need)
|
||||||
self.smooth([p for p in powers if p is not None])
|
self.smooth([p for p in powers if p is not None])
|
||||||
self.devices[d["id"]] = ("ev", {"power": powers, "volts": volts, "dep": dep,
|
self.devices[d["id"]] = ("ev", {"power": powers, "volts": volts, "dep": dep,
|
||||||
"pmax": imax * volts if imax >= imin else 0.0})
|
"pmax": imax * volts if imax >= imin else 0.0, "need": need,
|
||||||
|
# the ready-by and the energy known (shift_envelope): not the defaults
|
||||||
|
"known": ev_known(st, pref)})
|
||||||
|
|
||||||
def cover(self, d, room_ids):
|
def cover(self, d, room_ids):
|
||||||
p, b = d["params"], d["bounds"]
|
p, b = d["params"], d["bounds"]
|
||||||
@@ -488,6 +627,19 @@ class Model:
|
|||||||
return self.h.modelStatusToString(status)
|
return self.h.modelStatusToString(status)
|
||||||
|
|
||||||
|
|
||||||
|
def ev_known(st: dict, pref: dict | None) -> bool:
|
||||||
|
"""A car's ready-by and energy are facts: the ready-by the owner's
|
||||||
|
preference or the car's (its charger's departure entity), the energy
|
||||||
|
the car's (energy-needed entity) or the owner's (kWh, or % with the
|
||||||
|
car's state of charge and capacity). The mapping's defaults (need_wh,
|
||||||
|
departure) are not; a preference in % without the car's charge falls
|
||||||
|
back to them (preferences.py), so it is not either."""
|
||||||
|
dep = bool(pref) or st.get("departure") is not None
|
||||||
|
need = st.get("need_wh") is not None or bool(pref) and (
|
||||||
|
pref.get("kwh") is not None or (pref.get("soc_pct") is not None and bool(pref.get("capacity_wh"))))
|
||||||
|
return dep and need
|
||||||
|
|
||||||
|
|
||||||
def departure_step(s: dict, hhmm: str) -> int:
|
def departure_step(s: dict, hhmm: str) -> int:
|
||||||
"""The step of the next departure (local time), within the horizon."""
|
"""The step of the next departure (local time), within the horizon."""
|
||||||
zone = ZoneInfo(s.get("tz") or "UTC")
|
zone = ZoneInfo(s.get("tz") or "UTC")
|
||||||
@@ -503,8 +655,55 @@ def _round(v: float, step: float) -> float:
|
|||||||
return round(v / step) * step
|
return round(v / step) * step
|
||||||
|
|
||||||
|
|
||||||
|
def _prepare(s: dict) -> dict:
|
||||||
|
"""The state as the plan takes it (U1): without a tariff (status none,
|
||||||
|
or no import price for the first step) priced False and no market; a
|
||||||
|
tariff that prices only the first n steps: the horizon cut to them."""
|
||||||
|
pi = s.get("price_import") or []
|
||||||
|
status = (s.get("prices") or {}).get("status", "tariff")
|
||||||
|
# gas and heat: a boiler's or district heat's steps only while their price lasts
|
||||||
|
n = s["steps"]
|
||||||
|
srcs = {str((d.get("params") or {}).get("heat_source") or "electric") for d in s.get("devices") or [] if d["kind"] == "heating"}
|
||||||
|
for key, need in (("price_gas", {"boiler", "hybrid"}), ("price_heat", {"district"})):
|
||||||
|
xs = s.get(key) or []
|
||||||
|
if srcs & need and xs:
|
||||||
|
n = min(n, next((k for k, v in enumerate(xs) if v is None), len(xs)))
|
||||||
|
if n == 0:
|
||||||
|
n = s["steps"] # refused upstream (inputs.heating_refusal): nothing to cut for
|
||||||
|
if status == "none" or not pi or pi[0] is None:
|
||||||
|
s = dict(s, priced=False, market=[])
|
||||||
|
return _cut(s, n) if n < s["steps"] else s
|
||||||
|
n = min(n, next((k for k, v in enumerate(pi) if v is None), len(pi)))
|
||||||
|
s = dict(s, priced=True)
|
||||||
|
return _cut(s, n) if n < s["steps"] else s
|
||||||
|
|
||||||
|
|
||||||
|
def _cut(s: dict, n: int) -> dict:
|
||||||
|
"""The state for its first n steps: every per-step list (any list of
|
||||||
|
the horizon's length, at any depth) cut to n, the step ranges (from_step,
|
||||||
|
to_step, dep_step) kept inside it (a car's ready-by beyond it: "beyond")."""
|
||||||
|
T = s["steps"]
|
||||||
|
|
||||||
|
def cut(x):
|
||||||
|
if isinstance(x, list):
|
||||||
|
return [cut(v) for v in (x[:n] if len(x) == T else x)]
|
||||||
|
if isinstance(x, dict):
|
||||||
|
y = {k: cut(v) for k, v in x.items()}
|
||||||
|
for k in ("from_step", "to_step"):
|
||||||
|
if isinstance(y.get(k), int):
|
||||||
|
y[k] = min(y[k], n)
|
||||||
|
if isinstance(y.get("dep_step"), int) and y["dep_step"] > n:
|
||||||
|
y["dep_step"], y["beyond"] = n, True
|
||||||
|
return y
|
||||||
|
return x
|
||||||
|
out = cut(s)
|
||||||
|
out["steps"] = n
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
def plan(s: dict, time_limit: float = 30.0) -> dict:
|
def plan(s: dict, time_limit: float = 30.0) -> dict:
|
||||||
t0 = time.time()
|
t0 = time.time()
|
||||||
|
s = _prepare(s)
|
||||||
m = Model(s)
|
m = Model(s)
|
||||||
status = m.solve(time_limit)
|
status = m.solve(time_limit)
|
||||||
val = m.h.getSolution().col_value
|
val = m.h.getSolution().col_value
|
||||||
@@ -522,22 +721,46 @@ def plan(s: dict, time_limit: float = 30.0) -> dict:
|
|||||||
devices[d["id"]] = entries
|
devices[d["id"]] = entries
|
||||||
market = market_plan(s, m, x)
|
market = market_plan(s, m, x)
|
||||||
energy = energy_of(s, m, x, market)
|
energy = energy_of(s, m, x, market)
|
||||||
|
heat_cost = carrier_costs(s, m, x)
|
||||||
|
if energy is not None:
|
||||||
|
energy += heat_cost["gas_eur"] + heat_cost["heat_eur"]
|
||||||
|
base = baseline(s, m, x)
|
||||||
out = {"v": 1, "made": int(time.time()), "start": start, "step_s": step, "status": status,
|
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),
|
"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),
|
# without a tariff no cost: None, never a cost at a made-up price
|
||||||
|
"cost_eur": round(energy, 2) if energy is not None else None,
|
||||||
|
"baseline_eur": round(base, 2) if base is not None else None,
|
||||||
|
"priced": m.priced, "steps": T, "price_state": s.get("prices") or {},
|
||||||
"price_note": s.get("price_note", ""), "sources": s.get("sources", {})}
|
"price_note": s.get("price_note", ""), "sources": s.get("sources", {})}
|
||||||
|
if heat_cost["gas_eur"] or heat_cost["heat_eur"]:
|
||||||
|
out["gas_eur"], out["heat_eur"] = round(heat_cost["gas_eur"], 2), round(heat_cost["heat_eur"], 2)
|
||||||
|
# rooms not planned (inputs.room_refusal): the thermostat's
|
||||||
|
# fallback schedule, and which one in their refusal
|
||||||
|
refused = copy.deepcopy(s.get("refused") or {})
|
||||||
|
for d in s.get("fallback") or []:
|
||||||
|
entries = fallback_entries(s, d)
|
||||||
|
if entries:
|
||||||
|
devices[d["id"]] = entries
|
||||||
|
if d["id"] in refused:
|
||||||
|
refused[d["id"]]["fallback"] = ("schedule" if entries else
|
||||||
|
"own" if "climate" in d.get("channels", []) else "none")
|
||||||
|
if refused:
|
||||||
|
out["refused"] = refused # controls not planned, and why (U2, E6)
|
||||||
if market:
|
if market:
|
||||||
out["market"] = market
|
out["market"] = market
|
||||||
if any(d.get("pref") for d in s["devices"]):
|
if any(d.get("pref") for d in s["devices"]) or any(d.get("pref") for d in s.get("fallback") or []):
|
||||||
out["prefs"] = pref_feedback(s, m, x)
|
out["prefs"] = pref_feedback(s, m, x) + fallback_feedback(s)
|
||||||
if s.get("prefs_cost"):
|
if s.get("prefs_cost") and energy is not None:
|
||||||
out["prefs_cost"] = pref_costs(s, energy, t0 + time_limit)
|
out["prefs_cost"] = pref_costs(s, energy, t0 + time_limit)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
def energy_of(s, m, x, market=None) -> float:
|
def energy_of(s, m, x, market=None) -> float:
|
||||||
"""The plan's cost for power (EUR): the grid's import less its feed-in,
|
"""The plan's cost for power (EUR): the grid's import less its feed-in
|
||||||
and the market's contracts."""
|
(an unpriced export counts nothing), and the market's contracts; None
|
||||||
|
without a tariff."""
|
||||||
|
if not m.priced:
|
||||||
|
return None
|
||||||
pi, pe = s["price_import"], s["price_export"]
|
pi, pe = s["price_import"], s["price_export"]
|
||||||
e = sum(m.eur_wh(t, pi[t]) * x(m.gin[t]) - m.eur_wh(t, pe[t]) * x(m.gout[t]) for t in range(m.T))
|
e = sum(m.eur_wh(t, pi[t]) * x(m.gin[t]) - m.eur_wh(t, pe[t]) * x(m.gout[t]) for t in range(m.T))
|
||||||
return e + sum(c["cost_eur"] for c in (market if market is not None else market_plan(s, m, x)))
|
return e + sum(c["cost_eur"] for c in (market if market is not None else market_plan(s, m, x)))
|
||||||
@@ -646,7 +869,7 @@ def _ev_feedback(s, d, pref, v, x, m) -> dict:
|
|||||||
short = max(0.0, pref["need_wh"] - wh)
|
short = max(0.0, pref["need_wh"] - wh)
|
||||||
pi = s["price_import"]
|
pi = s["price_import"]
|
||||||
f["planned_kwh"] = round(wh / 1000.0, 2)
|
f["planned_kwh"] = round(wh / 1000.0, 2)
|
||||||
f["charge_eur"] = round(sum(m.eur_wh(t, pi[t]) * pw[t] for t in range(dep)), 2)
|
f["charge_eur"] = round(sum(m.eur_wh(t, pi[t]) * pw[t] for t in range(dep)), 2) if m.priced else None
|
||||||
reached = ""
|
reached = ""
|
||||||
if pref.get("soc_pct") is not None and pref.get("capacity_wh"):
|
if pref.get("soc_pct") is not None and pref.get("capacity_wh"):
|
||||||
pct = min(100.0, float(pref["soc_pct"]) + 100.0 * wh / pref["capacity_wh"])
|
pct = min(100.0, float(pref["soc_pct"]) + 100.0 * wh / pref["capacity_wh"])
|
||||||
@@ -752,26 +975,34 @@ def market_plan(s, m, x) -> list[dict]:
|
|||||||
vs += (e * pi[t] - pay * price) / 100.0
|
vs += (e * pi[t] - pay * price) / 100.0
|
||||||
else:
|
else:
|
||||||
cost -= e * price / 100.0
|
cost -= e * price / 100.0
|
||||||
vs += e * (price - pe[t]) / 100.0
|
vs += e * (price - (pe[t] or 0.0)) / 100.0 # an unpriced export: worth nothing
|
||||||
out.append({"id": c["id"], "name": c.get("name", ""), "direction": c["direction"], "price_ct": c["price_ct"],
|
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),
|
"steps": steps, "planned_kwh": round(kwh, 4), "paid_kwh": round(paid_kwh, 4),
|
||||||
"cost_eur": round(cost, 4), "vs_grid_eur": round(vs, 4)})
|
"cost_eur": round(cost, 4), "vs_grid_eur": round(vs, 4)})
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
FLEX_STEPS = 96 # 24 h at 15 min: what a building plans with
|
FLEX_STEPS = 96 # 24 h at 15 min: what a building plans with (shed_w, grid_w)
|
||||||
|
SHIFT_STEPS = 192 # 48 h: the shift envelope (the band prices' horizon)
|
||||||
|
SHIFT_DEVICES = 16 # devices in a shift envelope at most (the fleet's storage bound)
|
||||||
|
|
||||||
|
|
||||||
def flexibility(m, x) -> dict:
|
def flexibility(m, x) -> dict:
|
||||||
"""What the flat could do for its building (the building
|
"""What the flat could do for its building (the building
|
||||||
asks for flexibility, it doesn't cap averages; building/README.md hole
|
asks for flexibility, it doesn't cap averages; building/README.md hole
|
||||||
3), per step for the next 24 h: grid_w, the planned import; shed_w, how
|
3), per step for the next 24 h: grid_w, the planned import through the
|
||||||
much of it the controllable devices (heating, cooling, EV, the battery's
|
meter (the grid's and a market contract's: a flat supplied by its
|
||||||
charging) could drop in that step, plus what the battery could still
|
building's pool imports through a contract, and its gin alone was 0, so
|
||||||
discharge (its spare power, bounded by the energy above its floor): what
|
shed_w was 0 too: docs/methods.md A11); shed_w, how much of it the
|
||||||
a load_limit can take away without touching the household's own use."""
|
controllable devices (heating, cooling, EV, the battery's charging)
|
||||||
|
could drop in that step, plus what the battery could still discharge
|
||||||
|
(its spare power, bounded by the energy above its floor): what a
|
||||||
|
load_limit can take away without touching the household's own use.
|
||||||
|
shift (48 h, shift_envelope): the energy the devices can move, for a
|
||||||
|
building's price bands; absent where no device's facts are known."""
|
||||||
n = min(m.T, FLEX_STEPS)
|
n = min(m.T, FLEX_STEPS)
|
||||||
grid = [round(x(m.gin[t])) for t in range(n)]
|
bought = [x(m.gin[t]) + sum((x(v) for v in m.buy[t]), 0.0) for t in range(m.T)]
|
||||||
|
grid = [round(bought[t]) for t in range(n)]
|
||||||
shed = [0.0] * n
|
shed = [0.0] * n
|
||||||
for kind, v in m.devices.values():
|
for kind, v in m.devices.values():
|
||||||
if v is None:
|
if v is None:
|
||||||
@@ -785,9 +1016,110 @@ def flexibility(m, x) -> dict:
|
|||||||
for t in range(min(n, len(cols or []))):
|
for t in range(min(n, len(cols or []))):
|
||||||
if cols[t] is not None and hasattr(cols[t], "index"): # an EV's steps after departure are empty
|
if cols[t] is not None and hasattr(cols[t], "index"): # an EV's steps after departure are empty
|
||||||
shed[t] += max(x(cols[t]), 0.0)
|
shed[t] += max(x(cols[t]), 0.0)
|
||||||
# net_w: import less export, the member's draw as planned (the box's forecast of it upward, boxtrain.py)
|
out = {"grid_w": grid, "shed_w": [round(min(sh, g)) for sh, g in zip(shed, grid)],
|
||||||
return {"grid_w": grid, "shed_w": [round(min(sh, g)) for sh, g in zip(shed, grid)],
|
"net_w": [round(bought[t] - x(m.gout[t]) - sum((x(v) for v in m.sell[t]), 0.0)) for t in range(n)]}
|
||||||
"net_w": [round(x(m.gin[t]) - x(m.gout[t])) for t in range(n)]}
|
sh = shift_envelope(m, x)
|
||||||
|
if sh:
|
||||||
|
out["shift"] = sh
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def _x(x, v) -> float:
|
||||||
|
"""A step's value: a variable's, or 0 where there is none (an EV's steps after departure)."""
|
||||||
|
return x(v) if v is not None and hasattr(v, "index") else 0.0
|
||||||
|
|
||||||
|
|
||||||
|
def shift_envelope(m, x) -> dict | None:
|
||||||
|
"""The energy each controllable device can move over the next 48 h,
|
||||||
|
from what the plan already knows (docs/methods.md A11), for the
|
||||||
|
building's price bands (building/mieterstrom.py: the flats' agents
|
||||||
|
re-plan against the band prices; the building models that response
|
||||||
|
with this). Per device, per step (W, Wh; the step is step_s):
|
||||||
|
|
||||||
|
battery base_w (the planned grid-side power: charge > 0, discharge
|
||||||
|
< 0), soc_wh (the state of charge at the start), lo_wh per
|
||||||
|
step (its floor: the bounds and the owner's reserve), max_wh
|
||||||
|
(its ceiling), ch_w, dis_w (its power), eta (one way), end_wh (the planned
|
||||||
|
last state of charge: the building keeps at least it)
|
||||||
|
ev, heating, cooling
|
||||||
|
base_w (planned), up_w / down_w (the most it can draw above /
|
||||||
|
below base_w), lo_wh / hi_wh (the bounds of the moved energy
|
||||||
|
store S_t = keep * S_(t-1) + (p_t - base_t) * dt, S_(-1) = 0),
|
||||||
|
keep (1 a lossless store: a car; a room's heat decays toward
|
||||||
|
its plan: 1 - dt / (R C))
|
||||||
|
|
||||||
|
Published only from facts: a battery with its ratings (a battery
|
||||||
|
without them is not planned at all: U2); a car plugged in whose
|
||||||
|
ready-by and energy are the owner's preference or the car's own (never
|
||||||
|
the defaults); a room whose R and C are fitted (or the mapping's) with
|
||||||
|
the device's COP (the mapping's, or none needed: a fit against the
|
||||||
|
electric power) and the owner's band (preferences, or the mapping's
|
||||||
|
comfort temperatures); a room without them is not planned at all. None of them: None."""
|
||||||
|
s = m.s
|
||||||
|
n = min(m.T, SHIFT_STEPS)
|
||||||
|
if n <= 0:
|
||||||
|
return None
|
||||||
|
dt = m.dt
|
||||||
|
src = s.get("sources") or {}
|
||||||
|
rooms, cops, bands = src.get("rooms") or {}, src.get("rooms_cop") or {}, src.get("rooms_band") or {}
|
||||||
|
out = []
|
||||||
|
for d in s["devices"]:
|
||||||
|
kind, v = m.devices.get(d["id"], (d["kind"], None))
|
||||||
|
if v is None:
|
||||||
|
continue
|
||||||
|
if kind == "battery":
|
||||||
|
base = [_x(x, v["ch"][t]) - _x(x, v["dis"][t]) for t in range(n)]
|
||||||
|
out.append({"id": d["id"], "kind": "battery", "base_w": [round(b) for b in base],
|
||||||
|
"soc_wh": round(v["soc0"]), "lo_wh": [round(v["floors"][t] / 100.0 * v["cap"]) for t in range(n)],
|
||||||
|
"max_wh": round(v["hi_wh"]), "ch_w": round(v["max_ch"]), "dis_w": round(v["max_dis"]),
|
||||||
|
"eta": round(v["eff"], 4), "end_wh": round(_x(x, v["soc"][n - 1]))})
|
||||||
|
elif kind == "ev":
|
||||||
|
if not v.get("known") or v["pmax"] <= 0:
|
||||||
|
continue
|
||||||
|
p = [_x(x, v["power"][t]) for t in range(n)]
|
||||||
|
dep = min(v["dep"], n)
|
||||||
|
if dep <= 0 or sum(p) <= 0 and v["need"] <= 0:
|
||||||
|
continue
|
||||||
|
up = [max(0.0, v["pmax"] - p[t]) if t < dep else 0.0 for t in range(n)]
|
||||||
|
down = [p[t] if t < dep else 0.0 for t in range(n)]
|
||||||
|
lo, hi, cum = [], [], 0.0
|
||||||
|
for t in range(n):
|
||||||
|
cum += p[t] * dt
|
||||||
|
if t < dep - 1: # before the ready-by: between nothing and the need delivered
|
||||||
|
lo.append(-cum)
|
||||||
|
hi.append(max(0.0, v["need"] - cum))
|
||||||
|
else: # at and after it: what the plan delivers (a ready-by beyond the horizon: no less by its end)
|
||||||
|
end = v["need"] - cum
|
||||||
|
lo.append(min(0.0, end) if v["dep"] <= n else 0.0)
|
||||||
|
hi.append(max(0.0, end))
|
||||||
|
out.append({"id": d["id"], "kind": "ev", "base_w": [round(a) for a in p], "up_w": [round(a) for a in up],
|
||||||
|
"down_w": [round(a) for a in down], "lo_wh": [round(a) for a in lo], "hi_wh": [round(a) for a in hi],
|
||||||
|
"keep": 1.0})
|
||||||
|
elif kind in ("heating", "cooling"):
|
||||||
|
if v.get("switched"):
|
||||||
|
continue # an on/off boiler: all or nothing, not a band of power
|
||||||
|
if v.get("src", "electric") not in ("electric", "heat_pump"):
|
||||||
|
continue # gas, district heat or a hybrid's choice: no electricity to shift as planned
|
||||||
|
if rooms.get(d["id"]) not in ("mapping", "model", "box model") or \
|
||||||
|
cops.get(d["id"]) not in ("mapping", "electric fit", "electric heater") or v["wh_per_k"] <= 0:
|
||||||
|
continue # no fit or no COP: the heat it can hold is not known
|
||||||
|
if not d.get("pref") and bands.get(d["id"]) != "mapping":
|
||||||
|
continue # no band of the owner's
|
||||||
|
p = [_x(x, v["power"][t]) for t in range(n)]
|
||||||
|
temps = [_x(x, v["temp"][t]) for t in range(n)]
|
||||||
|
k, sign = v["wh_per_k"], (-1.0 if kind == "cooling" else 1.0)
|
||||||
|
lo, hi = [], []
|
||||||
|
for t in range(n):
|
||||||
|
blo, bhi = v["band"][t]
|
||||||
|
a, b = sorted((sign * (blo - temps[t]) * k, sign * (bhi - temps[t]) * k))
|
||||||
|
lo.append(min(a, 0.0)) # the plan's own temperature is always inside
|
||||||
|
hi.append(max(b, 0.0))
|
||||||
|
out.append({"id": d["id"], "kind": kind, "base_w": [round(a) for a in p],
|
||||||
|
"up_w": [round(max(0.0, v["pmax"] - a)) for a in p], "down_w": [round(a) for a in p],
|
||||||
|
"lo_wh": [round(a) for a in lo], "hi_wh": [round(a) for a in hi], "keep": round(v["keep"], 6)})
|
||||||
|
if not out:
|
||||||
|
return None
|
||||||
|
return {"v": 1, "start": s["start"], "step_s": s["step_s"], "steps": n, "devices": out[:SHIFT_DEVICES]}
|
||||||
|
|
||||||
|
|
||||||
def _charge_why(pi, t) -> str:
|
def _charge_why(pi, t) -> str:
|
||||||
@@ -808,12 +1140,93 @@ def _charge_why(pi, t) -> str:
|
|||||||
|
|
||||||
def _key(e) -> tuple:
|
def _key(e) -> tuple:
|
||||||
"""What makes a plan entry a change: a battery's self-consumption is one
|
"""What makes a plan entry a change: a battery's self-consumption is one
|
||||||
state whatever its power (it follows the house)."""
|
state whatever its power (it follows the house); a hybrid's source."""
|
||||||
return ("auto",) if e.get("auto") else (e["set"], e.get("grid"))
|
return ("auto",) if e.get("auto") else (e["set"], e.get("grid"), e.get("source"))
|
||||||
|
|
||||||
|
|
||||||
|
def carrier_costs(s, m, x) -> dict:
|
||||||
|
"""What the plan's gas and district heat cost (EUR), at their prices."""
|
||||||
|
gas, dh = s.get("price_gas") or [], s.get("price_heat") or []
|
||||||
|
out = {"gas_eur": 0.0, "heat_eur": 0.0}
|
||||||
|
for kind, v in m.devices.values():
|
||||||
|
if kind != "heating" or not v:
|
||||||
|
continue
|
||||||
|
for key, prices, col in (("gas_eur", gas, "gas"), ("heat_eur", dh, "district")):
|
||||||
|
for t, var in enumerate(v.get(col) or []):
|
||||||
|
if var is not None and t < len(prices) and prices[t] is not None:
|
||||||
|
out[key] += m.eur_wh(t, prices[t]) * x(var)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def _inputs_w(v, t, x) -> float:
|
||||||
|
"""A heating or cooling device's input in step t (W: electric, gas or
|
||||||
|
district heat; a hybrid's both)."""
|
||||||
|
return sum(x(c[t]) for c in (v.get("power"), v.get("gas"), v.get("district")) if c and c[t] is not None)
|
||||||
|
|
||||||
|
|
||||||
GRID_ZERO_W = 50.0 # the meter's import and export below this: the battery balances the house
|
GRID_ZERO_W = 50.0 # the meter's import and export below this: the battery balances the house
|
||||||
|
|
||||||
|
KEEP_OWN = "release" # executor.RELEASE: the thermostat keeps its own setpoint (nothing written)
|
||||||
|
|
||||||
|
|
||||||
|
def fallback_entries(s, d) -> list[dict]:
|
||||||
|
"""A room not planned (inputs.room_refusal): its
|
||||||
|
thermostat's fallback schedule, no optimisation. Per step the target of
|
||||||
|
the owner's (or installer's) schedule and overrides in force
|
||||||
|
(preferences.room_series: a block's temperature, a must at least it;
|
||||||
|
outside the blocks the setback (heating) / setup (cooling); a boost;
|
||||||
|
away at the setback), rounded to the half degree and held inside the
|
||||||
|
room's bounds (the owner's and installer's limits, the device's range).
|
||||||
|
Without blocks (an override alone) the steps outside the override keep
|
||||||
|
the thermostat's own setpoint (KEEP_OWN). No schedule, or a switched
|
||||||
|
heater without a thermostat: none (the executor's safe value: the
|
||||||
|
device as it is)."""
|
||||||
|
pref = d.get("pref")
|
||||||
|
if not pref or "climate" not in (d.get("channels") or []):
|
||||||
|
return []
|
||||||
|
cooling = d["kind"] == "cooling"
|
||||||
|
lo, hi = d["bounds"]["temp_c"][0], d["bounds"]["temp_c"][1]
|
||||||
|
lo_h = math.ceil(lo * 2) / 2 if lo is not None else None
|
||||||
|
hi_h = math.floor(hi * 2) / 2 if hi is not None else None
|
||||||
|
groups = pref["groups"]
|
||||||
|
blocks = any(g["kind"] == "block" for g in groups)
|
||||||
|
out = []
|
||||||
|
for t in range(min(s["steps"], len(pref["target"]))):
|
||||||
|
g = pref["group"][t]
|
||||||
|
if g == -1 and not blocks:
|
||||||
|
e = {"set": KEEP_OWN, "why": "fallback: the thermostat's own setpoint (room not optimised, no schedule)"}
|
||||||
|
else:
|
||||||
|
want = float(pref["target"][t])
|
||||||
|
must = pref["must"][t]
|
||||||
|
if must is not None:
|
||||||
|
want = min(want, float(must)) if cooling else max(want, float(must))
|
||||||
|
v = _round(want, 0.5)
|
||||||
|
if lo_h is not None:
|
||||||
|
v = max(v, lo_h)
|
||||||
|
if hi_h is not None:
|
||||||
|
v = min(v, hi_h)
|
||||||
|
what = groups[g]["text"] if g >= 0 else ("setup" if cooling else "setback") + " outside your schedule"
|
||||||
|
e = {"set": v, "why": f"fallback schedule (room not optimised): {what}"}
|
||||||
|
if not out or _key(out[-1]) != _key(e):
|
||||||
|
out.append({"t": s["start"] + t * s["step_s"], **e})
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def fallback_feedback(s) -> list[dict]:
|
||||||
|
"""A not-planned room's preferences (fallback_entries): not optimised,
|
||||||
|
the thermostat set to them; met unknown (no plan of the room to tell)."""
|
||||||
|
out = []
|
||||||
|
for d in s.get("fallback") or []:
|
||||||
|
pref = d.get("pref")
|
||||||
|
if not pref or "climate" not in (d.get("channels") or []):
|
||||||
|
continue
|
||||||
|
for gi, g in enumerate(pref["groups"]):
|
||||||
|
out.append({"control": d["id"], "group": g["group"], "kind": g["kind"], "how": g["how"],
|
||||||
|
"label": g["text"], "met": None,
|
||||||
|
"detail": "room not optimised: set as the thermostat's schedule"
|
||||||
|
if gi in pref["group"] else "not in the next 48 h"})
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
def _entry(d, kind, v, t, x, s, pi, grid_w=(1e9, 1e9)):
|
def _entry(d, kind, v, t, x, s, pi, grid_w=(1e9, 1e9)):
|
||||||
"""What a device is to do in step t, and why (a few words). grid_w:
|
"""What a device is to do in step t, and why (a few words). grid_w:
|
||||||
@@ -826,6 +1239,12 @@ def _entry(d, kind, v, t, x, s, pi, grid_w=(1e9, 1e9)):
|
|||||||
net = _round(net, 50.0)
|
net = _round(net, 50.0)
|
||||||
surplus = s["pv_w"][t] - s["load_w"][t]
|
surplus = s["pv_w"][t] - s["load_w"][t]
|
||||||
grid = net > max(0.0, surplus) + 50
|
grid = net > max(0.0, surplus) + 50
|
||||||
|
if not s.get("priced", True):
|
||||||
|
# no tariff: the battery in its own mode, unless the owner's
|
||||||
|
# reserve needs charging from the grid
|
||||||
|
if grid:
|
||||||
|
return {"set": net, "grid": True, "why": "charge to the reserve you set (no tariff)"}
|
||||||
|
return {"set": net, "grid": False, "auto": True, "why": "no tariff: the battery in its own mode"}
|
||||||
if net > 0:
|
if net > 0:
|
||||||
why = _charge_why(pi, t) if grid else "PV surplus"
|
why = _charge_why(pi, t) if grid else "PV surplus"
|
||||||
elif net < 0:
|
elif net < 0:
|
||||||
@@ -846,7 +1265,17 @@ def _entry(d, kind, v, t, x, s, pi, grid_w=(1e9, 1e9)):
|
|||||||
return {"set": net, "grid": grid, "why": why}
|
return {"set": net, "grid": grid, "why": why}
|
||||||
if kind in ("heating", "cooling"):
|
if kind in ("heating", "cooling"):
|
||||||
temp = x(v["temp"][t])
|
temp = x(v["temp"][t])
|
||||||
pw = x(v["power"][t])
|
pw = _inputs_w(v, t, x)
|
||||||
|
src = v.get("src", "electric")
|
||||||
|
# the price that buys the heat now, for the words
|
||||||
|
pr = {"boiler": s.get("price_gas"), "district": s.get("price_heat")}.get(src) or pi
|
||||||
|
pr_t = pr[t] if t < len(pr) else None
|
||||||
|
source = None
|
||||||
|
if src == "hybrid":
|
||||||
|
hp_w, gas_w = x(v["power"][t]), x(v["gas"][t])
|
||||||
|
source = "heat_pump" if hp_w >= gas_w and hp_w > 50 else "boiler" if gas_w > 50 else None
|
||||||
|
if source == "boiler":
|
||||||
|
pr_t = (s.get("price_gas") or [None] * (t + 1))[t]
|
||||||
ahead = s["occupied"][min(t + 4, s["steps"] - 1)]
|
ahead = s["occupied"][min(t + 4, s["steps"] - 1)]
|
||||||
pref = d.get("pref")
|
pref = d.get("pref")
|
||||||
if pref:
|
if pref:
|
||||||
@@ -856,7 +1285,7 @@ def _entry(d, kind, v, t, x, s, pi, grid_w=(1e9, 1e9)):
|
|||||||
tg, k0 = pref["target"], max(t - 1, 0)
|
tg, k0 = pref["target"], max(t - 1, 0)
|
||||||
occ = sign * (tg[k0] - pref["base"][k0]) > 0
|
occ = sign * (tg[k0] - pref["base"][k0]) > 0
|
||||||
ahead = any(sign * (tg[k] - tg[k0]) > 0 for k in range(t, min(t + 4, len(tg))))
|
ahead = any(sign * (tg[k] - tg[k0]) > 0 for k in range(t, min(t + 4, len(tg))))
|
||||||
if kind == "heating" and v["switched"]:
|
if kind == "heating" and v["switched"] and src != "hybrid":
|
||||||
on = pw > 0.5 * v["pmax"]
|
on = pw > 0.5 * v["pmax"]
|
||||||
why = ("pre-heat before someone is home" if on and not occ and ahead else
|
why = ("pre-heat before someone is home" if on and not occ and ahead else
|
||||||
"keep the room warm" if on else "off: warm enough or away")
|
"keep the room warm" if on else "off: warm enough or away")
|
||||||
@@ -885,8 +1314,13 @@ def _entry(d, kind, v, t, x, s, pi, grid_w=(1e9, 1e9)):
|
|||||||
want = max(want, float(pref["must"][t]))
|
want = max(want, float(pref["must"][t]))
|
||||||
floor_t = min(max(math.floor(want * 2) / 2, math.ceil(lo * 2) / 2), math.floor(hi * 2) / 2)
|
floor_t = min(max(math.floor(want * 2) / 2, math.ceil(lo * 2) / 2), math.floor(hi * 2) / 2)
|
||||||
return {"set": min(target, floor_t), "why": "setback: away or night" if not occ else "warm enough"}
|
return {"set": min(target, floor_t), "why": "setback: away or night" if not occ else "warm enough"}
|
||||||
return {"set": target, "why": f"pre-heat while power is cheap ({pi[t]:.1f} ct)" if not occ and ahead
|
what = {"boiler": "gas", "district": "heat"}.get(src if src != "hybrid" else (source or ""), "power")
|
||||||
else "keep the room warm"}
|
e = {"set": target, "why": f"pre-heat while {what} is cheap ({pr_t:.1f} ct)"
|
||||||
|
if not occ and ahead and pr_t is not None else "keep the room warm"}
|
||||||
|
if source:
|
||||||
|
e["source"] = source
|
||||||
|
e["why"] += "; " + ("the heat pump: its heat is cheaper now" if source == "heat_pump" else "the boiler: its heat is cheaper now")
|
||||||
|
return e
|
||||||
if kind == "ev":
|
if kind == "ev":
|
||||||
p = v["power"][t]
|
p = v["power"][t]
|
||||||
if p is None:
|
if p is None:
|
||||||
@@ -894,6 +1328,10 @@ def _entry(d, kind, v, t, x, s, pi, grid_w=(1e9, 1e9)):
|
|||||||
amps = round(x(p) / v["volts"])
|
amps = round(x(p) / v["volts"])
|
||||||
ph = ((s.get("sources") or {}).get("ev_phases") or {}).get(d["id"])
|
ph = ((s.get("sources") or {}).get("ev_phases") or {}).get(d["id"])
|
||||||
ph = f"; {phase_words(ph)}" if ph else ""
|
ph = f"; {phase_words(ph)}" if ph else ""
|
||||||
|
if pi[t] is None:
|
||||||
|
if amps < 1:
|
||||||
|
return {"set": "off", "why": "not needed now (no tariff)" + ph}
|
||||||
|
return {"set": amps, "why": "charge for the departure (no tariff)" + ph}
|
||||||
if amps < 1:
|
if amps < 1:
|
||||||
return {"set": "off", "why": "wait for cheaper hours" + ph}
|
return {"set": "off", "why": "wait for cheaper hours" + ph}
|
||||||
return {"set": amps, "why": f"cheapest hours before departure ({pi[t]:.1f} ct){ph}"}
|
return {"set": amps, "why": f"cheapest hours before departure ({pi[t]:.1f} ct){ph}"}
|
||||||
@@ -918,7 +1356,10 @@ def _entry(d, kind, v, t, x, s, pi, grid_w=(1e9, 1e9)):
|
|||||||
def baseline(s, m, x) -> float:
|
def baseline(s, m, x) -> float:
|
||||||
"""An estimate of the cost without the plan: the house's net load at its
|
"""An estimate of the cost without the plan: the house's net load at its
|
||||||
prices, no battery, and the devices' energy (as the plan uses it) at the
|
prices, no battery, and the devices' energy (as the plan uses it) at the
|
||||||
mean import price, the EV's as soon as it is plugged in."""
|
mean import price, the EV's as soon as it is plugged in; None without
|
||||||
|
a tariff."""
|
||||||
|
if not m.priced:
|
||||||
|
return None
|
||||||
T, pi, pe = m.T, s["price_import"], s["price_export"]
|
T, pi, pe = m.T, s["price_import"], s["price_export"]
|
||||||
cost = 0.0
|
cost = 0.0
|
||||||
for t in range(T):
|
for t in range(T):
|
||||||
@@ -929,7 +1370,13 @@ def baseline(s, m, x) -> float:
|
|||||||
if not v:
|
if not v:
|
||||||
continue
|
continue
|
||||||
if kind in ("heating", "cooling"):
|
if kind in ("heating", "cooling"):
|
||||||
cost += sum(m.eur_wh(t, mean) * x(v["power"][t]) for t in range(T))
|
# each input as the plan uses it at its mean price (electricity, gas, district heat)
|
||||||
|
cost += sum(m.eur_wh(t, mean) * x(v["power"][t]) for t in range(T) if v["power"][t] is not None)
|
||||||
|
for col, key in (("gas", "price_gas"), ("district", "price_heat")):
|
||||||
|
ps = [p for p in (s.get(key) or [])[:T] if p is not None]
|
||||||
|
if ps and any(c is not None for c in v.get(col) or []):
|
||||||
|
mp = sum(ps) / len(ps)
|
||||||
|
cost += sum(m.eur_wh(t, mp) * x(c) for t, c in enumerate(v[col]) if c is not None)
|
||||||
elif kind == "ev":
|
elif kind == "ev":
|
||||||
wh = sum(m.dt * x(p) for p in v["power"] if p is not None)
|
wh = sum(m.dt * x(p) for p in v["power"] if p is not None)
|
||||||
asap = pi[:max(1, math.ceil(wh / max(1.0, v["volts"] * 16) / m.dt))]
|
asap = pi[:max(1, math.ceil(wh / max(1.0, v["volts"] * 16) / m.dt))]
|
||||||
|
|||||||
@@ -419,6 +419,13 @@ class Preferences:
|
|||||||
self.costs: dict = {"at": 0, "of": 0.0, "eur_day": {}} # the musts' costs, as of a plan
|
self.costs: dict = {"at": 0, "of": 0.0, "eur_day": {}} # the musts' costs, as of a plan
|
||||||
self.feedback: list[dict] = []
|
self.feedback: list[dict] = []
|
||||||
self.errors: list[dict] = [] # the last refused changes (the panel shows them)
|
self.errors: list[dict] = [] # the last refused changes (the panel shows them)
|
||||||
|
# the plan's state for the panel (docs/parameters.md U1, U2): planned with
|
||||||
|
# prices or not and why, the controls not planned and why
|
||||||
|
self.planning: dict = {}
|
||||||
|
# the owner's own tariff as on the bill, from the panel, for the cloud
|
||||||
|
# (the controller sends it through the link: op own_tariff) and its state
|
||||||
|
self.outbox: list[dict] = []
|
||||||
|
self.own_tariff: dict = {}
|
||||||
try:
|
try:
|
||||||
with open(self.path) as f:
|
with open(self.path) as f:
|
||||||
raw = json.load(f)
|
raw = json.load(f)
|
||||||
@@ -596,11 +603,42 @@ class Preferences:
|
|||||||
os.remove(p)
|
os.remove(p)
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
if isinstance(msg, dict):
|
if isinstance(msg, dict) and "own_tariff" in msg:
|
||||||
|
ok, why = self.take_own_tariff(msg)
|
||||||
|
log.info("own tariff from the panel: %s", why)
|
||||||
|
n += 1
|
||||||
|
elif isinstance(msg, dict):
|
||||||
self.apply_msg(msg, controls, tz, "owner", "panel:" + str(msg.get("by") or "owner")[:40])
|
self.apply_msg(msg, controls, tz, "owner", "panel:" + str(msg.get("by") or "owner")[:40])
|
||||||
n += 1
|
n += 1
|
||||||
return n
|
return n
|
||||||
|
|
||||||
|
def take_own_tariff(self, msg: dict) -> tuple[bool, str]:
|
||||||
|
"""The owner's own tariff as on the bill (the panel, when the box has
|
||||||
|
no tariff): {"own_tariff": {"import_ct", "export_ct"?, "valid_from"?},
|
||||||
|
"by"}: checked here, then sent to the cloud (outbox), which keeps it as
|
||||||
|
the site's own price (fleet site_tariffs) unless the site already has
|
||||||
|
a tariff chosen there, and sends the tariff bundle back. Gross ct/kWh."""
|
||||||
|
o = msg.get("own_tariff") or {}
|
||||||
|
now = int(self.clock())
|
||||||
|
|
||||||
|
def ct(v, lo, hi):
|
||||||
|
try:
|
||||||
|
x = float(v)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return None
|
||||||
|
return x if lo <= x <= hi and x == x else None
|
||||||
|
imp = ct(o.get("import_ct"), 0.0, 1000.0)
|
||||||
|
exp = ct(o.get("export_ct"), -100.0, 1000.0) if o.get("export_ct") not in (None, "") else None
|
||||||
|
if imp is None or imp == 0:
|
||||||
|
self.own_tariff = {"state": "refused", "why": "import price: ct/kWh, above 0 and at most 1000", "at": now}
|
||||||
|
return False, self.own_tariff["why"]
|
||||||
|
out = {"v": 1, "import_ct": imp, "by": str(msg.get("by") or "owner")[:40], "at": now}
|
||||||
|
if exp is not None:
|
||||||
|
out["export_ct"] = exp
|
||||||
|
self.outbox.append(out)
|
||||||
|
self.own_tariff = {"state": "sent", "import_ct": imp, "export_ct": exp, "at": now}
|
||||||
|
return True, f"sent: {imp:g} ct/kWh" + (f", export {exp:g} ct/kWh" if exp is not None else "")
|
||||||
|
|
||||||
def apply_msg(self, msg: dict, controls: dict, tz: str, level: str, by: str) -> tuple[bool, str]:
|
def apply_msg(self, msg: dict, controls: dict, tz: str, level: str, by: str) -> tuple[bool, str]:
|
||||||
"""One change in the inbox's form (the panel's, the console's)."""
|
"""One change in the inbox's form (the panel's, the console's)."""
|
||||||
now = self.clock()
|
now = self.clock()
|
||||||
@@ -663,6 +701,13 @@ class Preferences:
|
|||||||
p = None
|
p = None
|
||||||
if p is not None:
|
if p is not None:
|
||||||
d["pref"] = p
|
d["pref"] = p
|
||||||
|
# rooms not planned (inputs.room_refusal): their schedule as the
|
||||||
|
# thermostat's fallback (optimizer.fallback_entries)
|
||||||
|
for d in state.get("fallback") or []:
|
||||||
|
if d["id"] in controls:
|
||||||
|
p = room_series(d, self.value(f"room:{d['id']}"), ovs, state, ends)
|
||||||
|
if p is not None:
|
||||||
|
d["pref"] = p
|
||||||
state["prefs_cost"] = bool(any(d.get("pref") for d in state["devices"])) and (
|
state["prefs_cost"] = bool(any(d.get("pref") for d in state["devices"])) and (
|
||||||
self.changed_at > self.costs.get("of", 0) or now - self.costs.get("at", 0) >= COST_EVERY_S)
|
self.changed_at > self.costs.get("of", 0) or now - self.costs.get("at", 0) >= COST_EVERY_S)
|
||||||
return state
|
return state
|
||||||
@@ -688,6 +733,9 @@ class Preferences:
|
|||||||
f["text"] = feedback_words(f)
|
f["text"] = feedback_words(f)
|
||||||
fb.append(f)
|
fb.append(f)
|
||||||
self.feedback = fb
|
self.feedback = fb
|
||||||
|
self.planning = {"priced": plan.get("priced", True), "note": plan.get("price_note", ""),
|
||||||
|
"prices": plan.get("price_state") or {}, "refused": plan.get("refused") or {},
|
||||||
|
"made": plan.get("made")}
|
||||||
plan["prefs"] = fb
|
plan["prefs"] = fb
|
||||||
plan.pop("prefs_cost", None)
|
plan.pop("prefs_cost", None)
|
||||||
return fb
|
return fb
|
||||||
@@ -717,7 +765,8 @@ class Preferences:
|
|||||||
b = c.bounds(q)
|
b = c.bounds(q)
|
||||||
cl.append({"id": c.id, "kind": c.kind, "name": c.label(), "bounds": [b.lo, b.hi],
|
cl.append({"id": c.id, "kind": c.kind, "name": c.label(), "bounds": [b.lo, b.hi],
|
||||||
"setback_c": _p(c, "setback_c"), "setup_c": _p(c, "setup_c"), "comfort_c": _p(c, "comfort_c")})
|
"setback_c": _p(c, "setback_c"), "setup_c": _p(c, "setup_c"), "comfort_c": _p(c, "comfort_c")})
|
||||||
v = dict(self.public(), controls=cl, mode=mode, errors=self.errors, ha=self.ha, made=int(self.clock()))
|
v = dict(self.public(), controls=cl, mode=mode, errors=self.errors, ha=self.ha, made=int(self.clock()),
|
||||||
|
planning=self.planning, own_tariff=self.own_tariff)
|
||||||
try:
|
try:
|
||||||
tmp = self.view_path + ".tmp"
|
tmp = self.view_path + ".tmp"
|
||||||
with open(tmp, "w") as f:
|
with open(tmp, "w") as f:
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ class Hour:
|
|||||||
solar_kwh_m2: float | None = None
|
solar_kwh_m2: float | None = None
|
||||||
cloud_pct: float | None = None
|
cloud_pct: float | None = None
|
||||||
temp_c: float | None = None
|
temp_c: float | None = None
|
||||||
|
wind_ms: float | None = None # mean wind speed, m/s (DWD through Bright Sky; the heat forecast, methods.md B13)
|
||||||
|
|
||||||
|
|
||||||
def sun_position(t: datetime, lat: float, lon: float) -> tuple[float, float]:
|
def sun_position(t: datetime, lat: float, lon: float) -> tuple[float, float]:
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ Per 15-minute step of the plan, for a shade with its window's orientation
|
|||||||
cooling season the sun on the facade and the room warm (its
|
cooling season the sun on the facade and the room warm (its
|
||||||
temperature within 1 K of the setpoint, or outside
|
temperature within 1 K of the setpoint, or outside
|
||||||
24 °C or more) or the next 3 hours dear (their mean
|
24 °C or more) or the next 3 hours dear (their mean
|
||||||
price 10 % above the horizon's): close to shade_pct
|
price 10 % above the horizon's, priced steps only:
|
||||||
|
no price, no dear hours): close to shade_pct
|
||||||
(someone home: some daylight) or closed_pct (nobody
|
(someone home: some daylight) or closed_pct (nobody
|
||||||
home); open again (open_pct) when the sun has left
|
home); open again (open_pct) when the sun has left
|
||||||
heating season the sun on the facade (sunny, outside below 18 °C):
|
heating season the sun on the facade (sunny, outside below 18 °C):
|
||||||
@@ -154,8 +155,12 @@ def schedule(d: dict, s: dict, ctx: dict | None = None) -> dict:
|
|||||||
min_move = float(p.get("min_move_s", 1800))
|
min_move = float(p.get("min_move_s", 1800))
|
||||||
night_ok = bool(float(p.get("night_moves", 0) or 0))
|
night_ok = bool(float(p.get("night_moves", 0) or 0))
|
||||||
qf, qt = str(p.get("quiet_from", "22:00")), str(p.get("quiet_to", "07:00"))
|
qf, qt = str(p.get("quiet_from", "22:00")), str(p.get("quiet_to", "07:00"))
|
||||||
prices = s.get("price_import") or [0.0] * T
|
# the tariff's prices as they are: None where it has none (no tariff, or
|
||||||
mean = sum(prices) / len(prices) if prices else 0.0
|
# the steps past its end, bundles.prices). The dear hours are judged on
|
||||||
|
# the priced steps only; without a price there are none (no stand-in)
|
||||||
|
prices = list(s.get("price_import") or [])
|
||||||
|
priced = [x for x in prices if x is not None]
|
||||||
|
mean = sum(priced) / len(priced) if priced else 0.0
|
||||||
sky = s.get("sky") or [1.0] * T
|
sky = s.get("sky") or [1.0] * T
|
||||||
t_out = s.get("t_out") or [10.0] * T
|
t_out = s.get("t_out") or [10.0] * T
|
||||||
occupied = s.get("occupied") or [1] * T
|
occupied = s.get("occupied") or [1] * T
|
||||||
@@ -180,7 +185,7 @@ def schedule(d: dict, s: dict, ctx: dict | None = None) -> dict:
|
|||||||
season = ctx.get("season") or season_of(local.month)
|
season = ctx.get("season") or season_of(local.month)
|
||||||
want, why, kind = None, "", None
|
want, why, kind = None, "", None
|
||||||
if season == "cooling":
|
if season == "cooling":
|
||||||
ahead = prices[k:k + 12]
|
ahead = [x for x in prices[k:k + 12] if x is not None]
|
||||||
dear = bool(ahead) and mean > 0 and sum(ahead) / len(ahead) > 1.1 * mean
|
dear = bool(ahead) and mean > 0 and sum(ahead) / len(ahead) > 1.1 * mean
|
||||||
warm = (temp is not None and setpoint is not None and temp >= setpoint - 1.0) or \
|
warm = (temp is not None and setpoint is not None and temp >= setpoint - 1.0) or \
|
||||||
(k < len(t_out) and t_out[k] >= 24.0)
|
(k < len(t_out) and t_out[k] >= 24.0)
|
||||||
|
|||||||
@@ -89,7 +89,8 @@ def _val(c, e):
|
|||||||
|
|
||||||
def _big(c, old, new) -> bool:
|
def _big(c, old, new) -> bool:
|
||||||
if c.kind == "battery":
|
if c.kind == "battery":
|
||||||
top = max(float(c.p("max_charge_w")), float(c.p("max_discharge_w")))
|
f, _, _ = c.battery_facts()
|
||||||
|
top = max(f.get("max_charge_w") or 0.0, f.get("max_discharge_w") or 0.0)
|
||||||
return abs(new - old) >= max(MIN_BATTERY_W, BATTERY_SHARE * top) - 1e-9
|
return abs(new - old) >= max(MIN_BATTERY_W, BATTERY_SHARE * top) - 1e-9
|
||||||
if c.kind in ("heating", "cooling"):
|
if c.kind in ("heating", "cooling"):
|
||||||
return abs(new - old) >= TEMP_K - 1e-9
|
return abs(new - old) >= TEMP_K - 1e-9
|
||||||
@@ -129,9 +130,10 @@ def stabilise(plan: dict, state: dict, controls, col, last_set=None, now: float
|
|||||||
In place; returns the plan."""
|
In place; returns the plan."""
|
||||||
now = plan.get("made", 0) if now is None else now
|
now = plan.get("made", 0) if now is None else now
|
||||||
step = plan.get("step_s", 900)
|
step = plan.get("step_s", 900)
|
||||||
end = plan.get("start", now) + step * state.get("steps", 192)
|
end = plan.get("start", now) + step * plan.get("steps", state.get("steps", 192))
|
||||||
prices = state.get("price_import") or []
|
# the prices the plan had (none without a tariff: no gain to weigh then)
|
||||||
feed = state.get("price_export") or []
|
prices = [x for x in state.get("price_import") or [] if x is not None]
|
||||||
|
feed = [x for x in state.get("price_export") or [] if x is not None]
|
||||||
spread = (max(prices) - min(prices + feed)) if prices else 0.0 # ct per kWh, at most
|
spread = (max(prices) - min(prices + feed)) if prices else 0.0 # ct per kWh, at most
|
||||||
devs = {d["id"]: d for d in state.get("devices") or []}
|
devs = {d["id"]: d for d in state.get("devices") or []}
|
||||||
kept_n = 0
|
kept_n = 0
|
||||||
@@ -158,6 +160,9 @@ def stabilise(plan: dict, state: dict, controls, col, last_set=None, now: float
|
|||||||
out = []
|
out = []
|
||||||
for i, e in enumerate(raw):
|
for i, e in enumerate(raw):
|
||||||
new = _val(c, e)
|
new = _val(c, e)
|
||||||
|
if new == "release":
|
||||||
|
out.append(e) # nothing written (a room's fallback: its own setpoint)
|
||||||
|
continue
|
||||||
if _same(cur, new):
|
if _same(cur, new):
|
||||||
kind = None
|
kind = None
|
||||||
elif isinstance(cur, str) or isinstance(new, str):
|
elif isinstance(cur, str) or isinstance(new, str):
|
||||||
@@ -198,11 +203,13 @@ def stabilise(plan: dict, state: dict, controls, col, last_set=None, now: float
|
|||||||
changed[kind] = t
|
changed[kind] = t
|
||||||
if kind == "mode" and c.kind != "battery":
|
if kind == "mode" and c.kind != "battery":
|
||||||
changed["set"] = t
|
changed["set"] = t
|
||||||
|
_hold_source(c, out, col, hold["mode"], last_set, now)
|
||||||
# a plan lists changes only
|
# a plan lists changes only
|
||||||
merged = []
|
merged = []
|
||||||
for e in out:
|
for e in out:
|
||||||
if merged and _same(_val(c, merged[-1]), _val(c, e)) and \
|
if merged and _same(_val(c, merged[-1]), _val(c, e)) and \
|
||||||
(merged[-1].get("grid") == e.get("grid") or _val(c, e) == AUTO):
|
(merged[-1].get("grid") == e.get("grid") or _val(c, e) == AUTO) and \
|
||||||
|
merged[-1].get("source") == e.get("source"):
|
||||||
continue
|
continue
|
||||||
merged.append(e)
|
merged.append(e)
|
||||||
plan["devices"][c.id] = merged
|
plan["devices"][c.id] = merged
|
||||||
@@ -210,6 +217,31 @@ def stabilise(plan: dict, state: dict, controls, col, last_set=None, now: float
|
|||||||
return plan
|
return plan
|
||||||
|
|
||||||
|
|
||||||
|
def _hold_source(c, entries, col, hold_s, last_set, now):
|
||||||
|
"""A hybrid's heat source (a plan entry's "source") changes only after
|
||||||
|
the hold of a mode (params.hold_mode_s) since its last change: from the
|
||||||
|
agent's last command (the guard's record, kind "source"), then from the
|
||||||
|
plan's own changes. A held entry keeps the source in force, its why
|
||||||
|
says so. In place."""
|
||||||
|
if c.kind != "heating" or "source" not in c.entities:
|
||||||
|
return
|
||||||
|
opts = {str(v): k for k, v in (c.p("source_options") or {}).items()}
|
||||||
|
cur = opts.get(str(col.state(c.entities["source"])))
|
||||||
|
t_last = last_set(c.id, "source") if last_set else None
|
||||||
|
t_last = t_last if t_last is not None else -1e18
|
||||||
|
for e in entries:
|
||||||
|
new = e.get("source")
|
||||||
|
if new is None or new == cur:
|
||||||
|
continue
|
||||||
|
t = max(e["t"], now)
|
||||||
|
if cur is not None and t - t_last < hold_s:
|
||||||
|
e["planned_source"], e["source"] = new, cur
|
||||||
|
e["why"] = (e.get("why", "") + "; " if e.get("why") else "") + \
|
||||||
|
f"source kept: changed {int((t - t_last) // 60)} min ago (hold {hold_s / 60:.0f} min)"
|
||||||
|
continue
|
||||||
|
cur, t_last = new, t
|
||||||
|
|
||||||
|
|
||||||
def _amount(c, a, b) -> str:
|
def _amount(c, a, b) -> str:
|
||||||
unit = {"battery": "W", "heating": "K", "cooling": "K", "ev": "A"}[c.kind]
|
unit = {"battery": "W", "heating": "K", "cooling": "K", "ev": "A"}[c.kind]
|
||||||
d = abs(b - a)
|
d = abs(b - a)
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ class SeriesResult:
|
|||||||
|
|
||||||
|
|
||||||
def wslice(w: fcmodels.Weather, sl: slice) -> fcmodels.Weather:
|
def wslice(w: fcmodels.Weather, sl: slice) -> fcmodels.Weather:
|
||||||
return fcmodels.Weather(w.temp[sl], w.ghi[sl], w.cloud[sl], w.elev[sl], w.az[sl], w.hours)
|
return w.slice(sl)
|
||||||
|
|
||||||
|
|
||||||
# ---- one series, champion against challengers -----------------------------------
|
# ---- one series, champion against challengers -----------------------------------
|
||||||
@@ -136,13 +136,13 @@ def train_series(series: str, t: np.ndarray, y: np.ndarray, w: fcmodels.Weather,
|
|||||||
res.runs.append(Run(series, kind, {}, None, note=f"not trained: {why}"))
|
res.runs.append(Run(series, kind, {}, None, note=f"not trained: {why}"))
|
||||||
continue
|
continue
|
||||||
pred = fcmodels.predict(p, series, t[te], wslice(wt, te), site, hist_t, hist_y)
|
pred = fcmodels.predict(p, series, t[te], wslice(wt, te), site, hist_t, hist_y)
|
||||||
res.runs.append(Run(series, kind, fcmodels.metrics(y[te], pred), p))
|
res.runs.append(Run(series, kind, fcmodels.metrics(y[te], pred, fcmodels.level_floor(series)), p))
|
||||||
base = next((r for r in res.runs if r.kind == "how" and r.params), None)
|
base = next((r for r in res.runs if r.kind == "how" and r.params), None)
|
||||||
res.baseline = base.metrics if base else None
|
res.baseline = base.metrics if base else None
|
||||||
if champion is not None:
|
if champion is not None:
|
||||||
try:
|
try:
|
||||||
pred = fcmodels.predict(champion, series, t[te], wslice(wt, te), site, hist_t, hist_y)
|
pred = fcmodels.predict(champion, series, t[te], wslice(wt, te), site, hist_t, hist_y)
|
||||||
res.champion = fcmodels.metrics(y[te], pred)
|
res.champion = fcmodels.metrics(y[te], pred, fcmodels.level_floor(series))
|
||||||
except Exception as e: # a champion that can no longer run is replaced
|
except Exception as e: # a champion that can no longer run is replaced
|
||||||
res.champion = None
|
res.champion = None
|
||||||
res.note = f"the model in force could not be scored ({e}): replaced"
|
res.note = f"the model in force could not be scored ({e}): replaced"
|
||||||
@@ -183,7 +183,7 @@ def score_params(params: dict, series: str, t: np.ndarray, y: np.ndarray, w: fcm
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.warning("benchmark %s: %s", series, e)
|
log.warning("benchmark %s: %s", series, e)
|
||||||
return None
|
return None
|
||||||
return fcmodels.metrics(y[te], pred)
|
return fcmodels.metrics(y[te], pred, fcmodels.level_floor(series))
|
||||||
|
|
||||||
|
|
||||||
# ---- the rooms ---------------------------------------------------------------------
|
# ---- the rooms ---------------------------------------------------------------------
|
||||||
|
|||||||
@@ -5,13 +5,14 @@ module git.setti.solutions/daniel/cantabile/edge/link
|
|||||||
go 1.26.0
|
go 1.26.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/coder/websocket v1.8.15
|
||||||
|
github.com/creack/pty v1.1.24
|
||||||
github.com/nats-io/nats.go v1.54.0
|
github.com/nats-io/nats.go v1.54.0
|
||||||
github.com/nats-io/nkeys v0.4.16
|
github.com/nats-io/nkeys v0.4.16
|
||||||
golang.org/x/crypto v0.58.0
|
golang.org/x/crypto v0.58.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/creack/pty v1.1.24 // indirect
|
|
||||||
github.com/klauspost/compress v1.20.0 // indirect
|
github.com/klauspost/compress v1.20.0 // indirect
|
||||||
github.com/nats-io/nuid v1.0.1 // indirect
|
github.com/nats-io/nuid v1.0.1 // indirect
|
||||||
golang.org/x/sys v0.49.0 // indirect
|
golang.org/x/sys v0.49.0 // indirect
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
github.com/coder/websocket v1.8.15 h1:6B2JPeOGlpff2Uz6vOEH1Vzpi0iUz20A+lPVhPHtNUA=
|
||||||
|
github.com/coder/websocket v1.8.15/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
|
||||||
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
|
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
|
||||||
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
|
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
|
||||||
github.com/klauspost/compress v1.20.0 h1:a3C1ke2ohxFymNlb2HWAHjDeKCI90scRskErZkR0ezA=
|
github.com/klauspost/compress v1.20.0 h1:a3C1ke2ohxFymNlb2HWAHjDeKCI90scRskErZkR0ezA=
|
||||||
|
|||||||
@@ -0,0 +1,396 @@
|
|||||||
|
// Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
|
||||||
|
|
||||||
|
//go:build hae2e
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
// The box's side of remote HA's end-to-end test against a real Home
|
||||||
|
// Assistant (platform/ci/ha-remote-e2e.sh runs it with the fleet's side,
|
||||||
|
// platform/services/fleet/hae2e_test.go): HA onboarded as ha-setup does it
|
||||||
|
// (an owner, and the agent's own administrator with a long-lived token),
|
||||||
|
// then this process is the box: the signed ha-open command and the
|
||||||
|
// tunnel's messages over NATS, exactly as the link takes them (haOpenWith,
|
||||||
|
// haFrameWith), with HA on the box's network. "e2e.owner-end" stands in
|
||||||
|
// for the owner's End now (SIGUSR1 on a box).
|
||||||
|
//
|
||||||
|
// HAE2E_MODE=addon: the HA add-on's path (CANTABILE_HA_REMOTE=1 with the
|
||||||
|
// Supervisor's token, as addon-run sets it): HA at homeassistant:8123 on
|
||||||
|
// the test's network (an alias of the HA container) and the admin commands
|
||||||
|
// through ws://supervisor/core/websocket, here a stand-in for the
|
||||||
|
// Supervisor's WebSocket proxy (fakeSupervisor: as supervisor/api/proxy.py
|
||||||
|
// does it: auth_required with HA's version, the add-on's token, then every
|
||||||
|
// message passed on except supervisor/ and hassio/ commands, which are
|
||||||
|
// refused). Its token at HA is the agent's administrator's (the real
|
||||||
|
// Supervisor's user is HA's own "Supervisor", also an administrator). The
|
||||||
|
// owner's End now is the add-on page's (devpage.py): SIGUSR1 to the pid in
|
||||||
|
// the link's support-ha.json, which must list the session while it is open.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/json"
|
||||||
|
"encoding/pem"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/coder/websocket"
|
||||||
|
"github.com/nats-io/nats.go"
|
||||||
|
)
|
||||||
|
|
||||||
|
// fakeSupervisor: the Supervisor's WebSocket proxy for add-ons
|
||||||
|
// (/core/websocket), on :80 of the name "supervisor"; seen: the command
|
||||||
|
// types it passed to HA.
|
||||||
|
type fakeSupervisor struct {
|
||||||
|
addonToken, coreWS, coreToken string
|
||||||
|
mu sync.Mutex
|
||||||
|
seen []string
|
||||||
|
refused []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeSupervisor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path != "/core/websocket" {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx := r.Context()
|
||||||
|
// Core first: its version for the add-on's greeting (the Supervisor knows it)
|
||||||
|
core, _, err := websocket.Dial(ctx, f.coreWS, nil)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "core", 502)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer core.CloseNow()
|
||||||
|
core.SetReadLimit(64 << 20)
|
||||||
|
var hello struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Version string `json:"ha_version"`
|
||||||
|
}
|
||||||
|
_, b, err := core.Read(ctx)
|
||||||
|
if err != nil || json.Unmarshal(b, &hello) != nil {
|
||||||
|
http.Error(w, "core", 502)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c, err := websocket.Accept(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer c.CloseNow()
|
||||||
|
c.SetReadLimit(64 << 20)
|
||||||
|
wj := func(conn *websocket.Conn, v any) error {
|
||||||
|
b, _ := json.Marshal(v)
|
||||||
|
return conn.Write(ctx, websocket.MessageText, b)
|
||||||
|
}
|
||||||
|
wj(c, map[string]any{"type": "auth_required", "ha_version": hello.Version})
|
||||||
|
actx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||||
|
_, b, err = c.Read(actx)
|
||||||
|
cancel()
|
||||||
|
var auth struct {
|
||||||
|
Type, AccessToken, APIPassword string
|
||||||
|
}
|
||||||
|
var am map[string]any
|
||||||
|
json.Unmarshal(b, &am)
|
||||||
|
auth.AccessToken, _ = am["access_token"].(string)
|
||||||
|
auth.APIPassword, _ = am["api_password"].(string)
|
||||||
|
tok := auth.AccessToken
|
||||||
|
if tok == "" {
|
||||||
|
tok = auth.APIPassword
|
||||||
|
}
|
||||||
|
if err != nil || tok != f.addonToken {
|
||||||
|
wj(c, map[string]any{"type": "auth_invalid", "message": "Invalid access"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// the Supervisor's own sign-in at Core
|
||||||
|
wj(core, map[string]any{"type": "auth", "access_token": f.coreToken})
|
||||||
|
if _, b, err = core.Read(ctx); err != nil || !strings.Contains(string(b), "auth_ok") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
wj(c, map[string]any{"type": "auth_ok", "ha_version": hello.Version})
|
||||||
|
done := make(chan struct{}, 2)
|
||||||
|
go func() { // Core -> add-on: as it is
|
||||||
|
defer func() { done <- struct{}{} }()
|
||||||
|
for {
|
||||||
|
t, b, err := core.Read(ctx)
|
||||||
|
if err != nil || c.Write(ctx, t, b) != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
go func() { // add-on -> Core: supervisor/ and hassio/ refused
|
||||||
|
defer func() { done <- struct{}{} }()
|
||||||
|
for {
|
||||||
|
t, b, err := c.Read(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var m map[string]any
|
||||||
|
if json.Unmarshal(b, &m) != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
typ, _ := m["type"].(string)
|
||||||
|
if typ == "" || strings.HasPrefix(typ, "supervisor/") || strings.HasPrefix(typ, "hassio/") {
|
||||||
|
f.mu.Lock()
|
||||||
|
f.refused = append(f.refused, typ)
|
||||||
|
f.mu.Unlock()
|
||||||
|
wj(c, map[string]any{"id": m["id"], "type": "result", "success": false,
|
||||||
|
"error": map[string]any{"code": "unauthorized", "message": "Unauthorized"}})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
f.mu.Lock()
|
||||||
|
f.seen = append(f.seen, typ)
|
||||||
|
f.mu.Unlock()
|
||||||
|
if core.Write(ctx, t, b) != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
<-done
|
||||||
|
}
|
||||||
|
|
||||||
|
func e2eWait(t *testing.T, path string, d time.Duration) []byte {
|
||||||
|
end := time.Now().Add(d)
|
||||||
|
for time.Now().Before(end) {
|
||||||
|
if b, err := os.ReadFile(path); err == nil && len(b) > 0 {
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
t.Fatalf("no %s", path)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func e2ePost(t *testing.T, base, path string, body any, form url.Values, token string, out any) int {
|
||||||
|
var req *http.Request
|
||||||
|
if form != nil {
|
||||||
|
req, _ = http.NewRequest("POST", base+path, strings.NewReader(form.Encode()))
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
} else {
|
||||||
|
b, _ := json.Marshal(body)
|
||||||
|
req, _ = http.NewRequest("POST", base+path, strings.NewReader(string(b)))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
}
|
||||||
|
if token != "" {
|
||||||
|
req.Header.Set("Authorization", "Bearer "+token)
|
||||||
|
}
|
||||||
|
res, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s: %v", path, err)
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
if out != nil {
|
||||||
|
json.NewDecoder(res.Body).Decode(out)
|
||||||
|
}
|
||||||
|
return res.StatusCode
|
||||||
|
}
|
||||||
|
|
||||||
|
// e2eOnboard: HA's owner and the agent's administrator (as ha-setup).
|
||||||
|
func e2eOnboard(t *testing.T, addr string) string {
|
||||||
|
base := "http://" + addr
|
||||||
|
end := time.Now().Add(5 * time.Minute)
|
||||||
|
for {
|
||||||
|
res, err := http.Get(base + "/api/onboarding")
|
||||||
|
if err == nil {
|
||||||
|
res.Body.Close()
|
||||||
|
if res.StatusCode == 200 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if time.Now().After(end) {
|
||||||
|
t.Fatal("HA did not start")
|
||||||
|
}
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
}
|
||||||
|
cid := "http://cantabile.local/"
|
||||||
|
var u struct {
|
||||||
|
AuthCode string `json:"auth_code"`
|
||||||
|
}
|
||||||
|
if st := e2ePost(t, base, "/api/onboarding/users", map[string]any{"client_id": cid, "name": "Owner", "username": "owner",
|
||||||
|
"password": randomSecret(), "language": "en"}, nil, "", &u); st != 200 {
|
||||||
|
t.Fatalf("onboarding users: %d", st)
|
||||||
|
}
|
||||||
|
var tok haTokens
|
||||||
|
e2ePost(t, base, "/auth/token", nil, url.Values{"grant_type": {"authorization_code"}, "code": {u.AuthCode}, "client_id": {cid}}, "", &tok)
|
||||||
|
for _, step := range []string{"core_config", "analytics"} {
|
||||||
|
e2ePost(t, base, "/api/onboarding/"+step, map[string]any{}, nil, tok.AccessToken, nil)
|
||||||
|
}
|
||||||
|
e2ePost(t, base, "/api/onboarding/integration", map[string]any{"client_id": cid, "redirect_uri": cid + "?auth_callback=1"}, nil, tok.AccessToken, nil)
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||||
|
defer cancel()
|
||||||
|
ws, err := haDial(ctx, "ws://"+addr+"/api/websocket", tok.AccessToken)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
r, err := ws.call(ctx, "config/auth/create", map[string]any{"name": "Cantabile Edge Agent", "group_ids": []string{"system-admin"}, "local_only": true})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var cr struct{ User haUserRow }
|
||||||
|
json.Unmarshal(r, &cr)
|
||||||
|
pw := randomSecret()
|
||||||
|
if _, err := ws.call(ctx, "config/auth_provider/homeassistant/create", map[string]any{"user_id": cr.User.ID, "username": "cantabile-agent", "password": pw}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
ws.close()
|
||||||
|
cfg := haTunnelCfg{httpBase: base}
|
||||||
|
at, err := cfg.haLoginAs(ctx, "http://cantabile.local", "cantabile-agent", pw)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
aws, err := haDial(ctx, "ws://"+addr+"/api/websocket", at.AccessToken)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer aws.close()
|
||||||
|
r, err = aws.call(ctx, "auth/long_lived_access_token", map[string]any{"client_name": "Cantabile Edge Agent", "lifespan": 30})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var long string
|
||||||
|
json.Unmarshal(r, &long)
|
||||||
|
return long
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHAE2EBox(t *testing.T) {
|
||||||
|
dir, addr, box := os.Getenv("HAE2E_DIR"), os.Getenv("HAE2E_HA_ADDR"), os.Getenv("HAE2E_BOX")
|
||||||
|
if dir == "" {
|
||||||
|
t.Skip("HAE2E_DIR not set")
|
||||||
|
}
|
||||||
|
token := e2eOnboard(t, addr)
|
||||||
|
os.WriteFile(dir+"/agent-token.tmp", []byte(token), 0o600)
|
||||||
|
os.Rename(dir+"/agent-token.tmp", dir+"/agent-token")
|
||||||
|
t.Setenv("CANTABILE_HA_REMOTE", "1")
|
||||||
|
addon := os.Getenv("HAE2E_MODE") == "addon"
|
||||||
|
var sv *fakeSupervisor
|
||||||
|
if addon {
|
||||||
|
// as addon-run starts the link: the option on, the Supervisor's
|
||||||
|
// token, nothing else (HA and the proxy by their names)
|
||||||
|
t.Setenv("CANTABILE_HUB", "")
|
||||||
|
t.Setenv("SUPERVISOR_TOKEN", randomSecret())
|
||||||
|
t.Setenv("CANTABILE_LINK_STATE", t.TempDir())
|
||||||
|
sv = &fakeSupervisor{addonToken: os.Getenv("SUPERVISOR_TOKEN"), coreWS: "ws://" + addr + "/api/websocket", coreToken: token}
|
||||||
|
go http.ListenAndServe(":80", sv)
|
||||||
|
// the add-on's End now (devpage.py): SIGUSR1 to the link
|
||||||
|
usr1 := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(usr1, syscall.SIGUSR1)
|
||||||
|
go func() {
|
||||||
|
for range usr1 {
|
||||||
|
haCloseAll("ended by the owner")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
writeHAState() // the link's start
|
||||||
|
} else {
|
||||||
|
t.Setenv("CANTABILE_HA_ADDR", addr)
|
||||||
|
t.Setenv("CANTABILE_HA_TOKEN_FILE", dir+"/agent-token")
|
||||||
|
}
|
||||||
|
cfg, ok := haConfig()
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("no config")
|
||||||
|
}
|
||||||
|
if addon && (cfg.addr != "homeassistant:8123" || cfg.adminWS != "ws://supervisor/core/websocket") {
|
||||||
|
t.Fatalf("not the add-on's path: %+v", cfg)
|
||||||
|
}
|
||||||
|
if addon {
|
||||||
|
// the Supervisor refuses its own commands through this proxy (as the real one)
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
w, err := haDial(ctx, cfg.adminWS, os.Getenv("SUPERVISOR_TOKEN"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := w.call(ctx, "supervisor/api", map[string]any{"endpoint": "/info", "method": "get"}); err == nil ||
|
||||||
|
!strings.Contains(err.Error(), "unauthorized") {
|
||||||
|
t.Errorf("supervisor/ through the proxy: %v", err)
|
||||||
|
}
|
||||||
|
w.close()
|
||||||
|
if _, err := haDial(ctx, cfg.adminWS, "not-the-token"); err == nil {
|
||||||
|
t.Error("the proxy took another token")
|
||||||
|
}
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
addonState := func() (st struct {
|
||||||
|
Pid int
|
||||||
|
Sessions []map[string]any
|
||||||
|
}) {
|
||||||
|
b, _ := os.ReadFile(haStateFile())
|
||||||
|
json.Unmarshal(b, &st)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := cfg.haLeftover(context.Background()); err != nil { // the link's start
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
blk, _ := pem.Decode(e2eWait(t, dir+"/commands-pub.pem", 5*time.Minute))
|
||||||
|
pub, err := x509.ParsePKIXPublicKey(blk.Bytes)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
nc, err := nats.Connect(os.Getenv("HAE2E_NATS"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer nc.Close()
|
||||||
|
send := func(m map[string]any) {
|
||||||
|
b, _ := json.Marshal(m)
|
||||||
|
nc.Publish("ctb.site."+box+".ha", b)
|
||||||
|
}
|
||||||
|
nc.Subscribe("ctb.cmd."+box, func(m *nats.Msg) {
|
||||||
|
var w struct{ T string }
|
||||||
|
json.Unmarshal(m.Data, &w)
|
||||||
|
if w.T == "ha" {
|
||||||
|
haFrameWith(m.Data, send)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c, err := checkCommand(pub, box, m.Data, time.Now())
|
||||||
|
if err != nil {
|
||||||
|
t.Logf("command refused: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ok, msg := false, "unknown command"
|
||||||
|
if c.Cmd == "ha-open" {
|
||||||
|
ok, msg = haOpenWith(cfg, c, send)
|
||||||
|
if addon && ok {
|
||||||
|
if st := addonState(); st.Pid != os.Getpid() || len(st.Sessions) != 1 || st.Sessions[0]["kind"] != "ha" {
|
||||||
|
t.Errorf("the add-on's pages are not told of the session: %+v", st)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b, _ := json.Marshal(map[string]any{"v": 1, "id": c.ID, "ok": ok, "result": map[string]any{"message": msg}})
|
||||||
|
nc.Publish("ctb.site."+box+".result", b)
|
||||||
|
})
|
||||||
|
nc.Subscribe("e2e.owner-end", func(*nats.Msg) {
|
||||||
|
if !addon {
|
||||||
|
haCloseAll("ended by the owner")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// the add-on page's End now: the pid from the link's file, SIGUSR1
|
||||||
|
if st := addonState(); st.Pid > 1 && len(st.Sessions) == 1 {
|
||||||
|
syscall.Kill(st.Pid, syscall.SIGUSR1)
|
||||||
|
} else {
|
||||||
|
t.Errorf("End now: no session in the link's file: %+v", st)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
nc.Flush()
|
||||||
|
os.WriteFile(dir+"/box-ready", []byte("1"), 0o644)
|
||||||
|
e2eWait(t, dir+"/done", 20*time.Minute)
|
||||||
|
haCloseAll("test over")
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
if addon {
|
||||||
|
if st := addonState(); len(st.Sessions) != 0 {
|
||||||
|
t.Errorf("sessions left in the link's file: %+v", st)
|
||||||
|
}
|
||||||
|
sv.mu.Lock()
|
||||||
|
seen := strings.Join(sv.seen, " ")
|
||||||
|
sv.mu.Unlock()
|
||||||
|
for _, want := range []string{"config/auth/list", "config/auth/create", "config/auth/update",
|
||||||
|
"config/auth_provider/homeassistant/create", "config/auth_provider/homeassistant/delete"} {
|
||||||
|
if !strings.Contains(seen, want) {
|
||||||
|
t.Errorf("%s did not go through the Supervisor's proxy", want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Logf("through the Supervisor's proxy: %d commands", len(sv.seen))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,636 @@
|
|||||||
|
// Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log/slog"
|
||||||
|
"net"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Remote Home Assistant for Cantabile support (item 19): HA's
|
||||||
|
// whole interface (its pages, scripts, API and WebSocket) through the box's
|
||||||
|
// link, for one support session at a time. Opened like the terminal and the
|
||||||
|
// panel: a signed "ha-open" command with a random session key, on the
|
||||||
|
// owner's permission in a support request (until it ends) or for the
|
||||||
|
// Cantabile team (remote-shell, 1 hour); 15 minutes without a request from
|
||||||
|
// support close it, and so does the owner's End now (SIGUSR1, closeAll).
|
||||||
|
//
|
||||||
|
// The tunnel: Cantabile's console serves the session on its own host name
|
||||||
|
// (HA cannot run under a sub-path) and passes each browser connection as a
|
||||||
|
// stream: {"t":"ha", "k":"o"} opens one to HA's HTTP port, "d" carries bytes
|
||||||
|
// (base64, at most haChunk), "a" gives the sender credit back (each side
|
||||||
|
// sends at most haWindow bytes a stream before the other has taken them),
|
||||||
|
// "x" closes it; "c" closes the session. Every message from the console
|
||||||
|
// carries the session key's MAC and a sequence number (a replay or a gap is
|
||||||
|
// refused: a gap resets the open streams). The box answers on
|
||||||
|
// ctb.site.<id>.ha the same way ("e": HA closed its side). What goes through
|
||||||
|
// is HTTP/1.1 as the console writes it (it records each request: method,
|
||||||
|
// path, status, bytes; never bodies) and, after an upgrade, a WebSocket's
|
||||||
|
// bytes. The box limits the streams (haStreamsMax) and its upload to the
|
||||||
|
// console (bytes/s, the command's "rate" within haRateMin..haRateCap).
|
||||||
|
//
|
||||||
|
// Sign-in: the "Cantabile support" HA user (hauser.go), tokens for the
|
||||||
|
// session's origin, sent once to the console ({"k":"tok"}), which hands
|
||||||
|
// them to the support browser and keeps them nowhere else.
|
||||||
|
|
||||||
|
const (
|
||||||
|
haChunk = 32 << 10 // bytes per "d" message (base64: ~44 KB; a box may send 256 KiB)
|
||||||
|
haWindow = 256 << 10 // bytes in flight per stream and direction
|
||||||
|
haStreamsMax = 96 // open streams per session (a browser loads HA's scripts in parallel)
|
||||||
|
haRateDef = 1 << 20 // the box's upload to the console, bytes/s
|
||||||
|
haRateMin = 64 << 10
|
||||||
|
haRateCap = 8 << 20
|
||||||
|
haDialWait = 5 * time.Second
|
||||||
|
haSignInWait = 45 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
// haTunnelCfg: where HA is from the link, and the agent's admin token.
|
||||||
|
type haTunnelCfg struct {
|
||||||
|
addr string // HA's HTTP port: host:port
|
||||||
|
httpBase string // http://<addr>
|
||||||
|
adminWS string // the WebSocket the admin token works on
|
||||||
|
adminToken func() (string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// haConfig: the Hub (HA on 127.0.0.1:8123, the agent's token from
|
||||||
|
// ha-setup); an HA add-on site only when its owner turned the option on
|
||||||
|
// (addon-run sets CANTABILE_HA_REMOTE=1): Core at homeassistant:8123 on the
|
||||||
|
// Supervisor's network, the admin commands through the Supervisor's proxy
|
||||||
|
// with the add-on's own token. CANTABILE_HA_ADDR, CANTABILE_HA_ADMIN_WS and
|
||||||
|
// CANTABILE_HA_TOKEN_FILE override (tests).
|
||||||
|
func haConfig() (haTunnelCfg, bool) {
|
||||||
|
var c haTunnelCfg
|
||||||
|
switch {
|
||||||
|
case isHub():
|
||||||
|
c.addr, c.adminWS = "127.0.0.1:8123", "ws://127.0.0.1:8123/api/websocket"
|
||||||
|
c.adminToken = fileToken("/data/cantabile/edge/ha-token")
|
||||||
|
case os.Getenv("CANTABILE_HA_REMOTE") == "1" && os.Getenv("SUPERVISOR_TOKEN") != "":
|
||||||
|
c.addr, c.adminWS = "homeassistant:8123", "ws://supervisor/core/websocket"
|
||||||
|
sv := os.Getenv("SUPERVISOR_TOKEN")
|
||||||
|
c.adminToken = func() (string, error) { return sv, nil }
|
||||||
|
case os.Getenv("CANTABILE_HA_REMOTE") == "1" && os.Getenv("CANTABILE_HA_ADDR") != "":
|
||||||
|
// tests: everything from the overrides below
|
||||||
|
default:
|
||||||
|
return c, false
|
||||||
|
}
|
||||||
|
if v := os.Getenv("CANTABILE_HA_ADDR"); v != "" {
|
||||||
|
c.addr, c.adminWS = v, "ws://"+v+"/api/websocket"
|
||||||
|
}
|
||||||
|
if v := os.Getenv("CANTABILE_HA_ADMIN_WS"); v != "" {
|
||||||
|
c.adminWS = v
|
||||||
|
}
|
||||||
|
if v := os.Getenv("CANTABILE_HA_TOKEN_FILE"); v != "" {
|
||||||
|
c.adminToken = fileToken(v)
|
||||||
|
}
|
||||||
|
if c.adminToken == nil {
|
||||||
|
return c, false
|
||||||
|
}
|
||||||
|
c.httpBase = "http://" + c.addr
|
||||||
|
return c, true
|
||||||
|
}
|
||||||
|
|
||||||
|
type haMsg struct {
|
||||||
|
T string `json:"t"` // "ha"
|
||||||
|
S string `json:"s"`
|
||||||
|
N int64 `json:"n"`
|
||||||
|
K string `json:"k"` // o d a x c
|
||||||
|
I int64 `json:"i,omitempty"`
|
||||||
|
D string `json:"d,omitempty"`
|
||||||
|
W int64 `json:"w,omitempty"`
|
||||||
|
MAC string `json:"mac"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// haMAC: what the console signs each message with (session key).
|
||||||
|
func haMAC(key []byte, m haMsg) string {
|
||||||
|
h := hmac.New(sha256.New, key)
|
||||||
|
fmt.Fprintf(h, "ha|%s|%d|%s|%d|%s|%d", m.S, m.N, m.K, m.I, m.D, m.W)
|
||||||
|
return hex.EncodeToString(h.Sum(nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
type haSession struct {
|
||||||
|
id, by, consent, origin string
|
||||||
|
key []byte
|
||||||
|
until, started time.Time
|
||||||
|
cfg haTunnelCfg
|
||||||
|
send func(map[string]any) // to the console (ctb.site.<id>.ha)
|
||||||
|
user *haUser
|
||||||
|
rate *haRate
|
||||||
|
once sync.Once
|
||||||
|
gone chan struct{}
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
|
seqIn int64
|
||||||
|
seqOut int64
|
||||||
|
last time.Time
|
||||||
|
streams map[int64]*haStream
|
||||||
|
}
|
||||||
|
|
||||||
|
type haStream struct {
|
||||||
|
id int64
|
||||||
|
s *haSession
|
||||||
|
in chan []byte // from the console, to HA
|
||||||
|
done chan struct{}
|
||||||
|
once sync.Once
|
||||||
|
mu sync.Mutex
|
||||||
|
cond *sync.Cond
|
||||||
|
credit int64 // bytes this side may still send
|
||||||
|
conn net.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
haMu sync.Mutex
|
||||||
|
haSessions = map[string]*haSession{}
|
||||||
|
)
|
||||||
|
|
||||||
|
// out: one message to the console, numbered (under s.mu: the numbers go
|
||||||
|
// out in order).
|
||||||
|
func (s *haSession) out(m map[string]any) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
s.seqOut++
|
||||||
|
m["s"], m["n"] = s.id, s.seqOut
|
||||||
|
s.send(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
// haList: the open sessions for the owner's banner (writeTermState).
|
||||||
|
func haList() []map[string]any {
|
||||||
|
haMu.Lock()
|
||||||
|
defer haMu.Unlock()
|
||||||
|
out := []map[string]any{}
|
||||||
|
for _, s := range haSessions {
|
||||||
|
m := map[string]any{"by": s.by, "since": s.started.Unix(), "consent": s.consent, "kind": "ha"}
|
||||||
|
s.mu.Lock()
|
||||||
|
if s.user != nil {
|
||||||
|
m["ha_user"] = s.user.id // the add-on's pages know the support user by it (devpage.py)
|
||||||
|
}
|
||||||
|
s.mu.Unlock()
|
||||||
|
out = append(out, m)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// haStateFile: where an HA add-on site's link tells the add-on's own
|
||||||
|
// pages (edge/cantabile_edge/devpage.py) that support has this Home
|
||||||
|
// Assistant open: the link's state directory, which the agent (the same
|
||||||
|
// user in the add-on's container) reads; their End now sends the link
|
||||||
|
// SIGUSR1 (its pid is in the file), as the Hub's panel does.
|
||||||
|
func haStateFile() string {
|
||||||
|
return filepath.Join(env("CANTABILE_LINK_STATE", "/data/link"), "support-ha.json")
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeHAState: the open sessions for the owner: on the Hub the panel's
|
||||||
|
// banner (writeTermState); on an HA add-on site the add-on's pages.
|
||||||
|
func writeHAState() {
|
||||||
|
if isHub() {
|
||||||
|
writeTermState()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b, _ := json.Marshal(map[string]any{"pid": os.Getpid(), "user": haSupportName, "sessions": haList()})
|
||||||
|
f := haStateFile()
|
||||||
|
if os.WriteFile(f+".tmp", b, 0o644) == nil {
|
||||||
|
os.Rename(f+".tmp", f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// haOrigin: the session's own origin as the console sent it: https (or
|
||||||
|
// http on a *.localhost name, for tests), a host whose first label is
|
||||||
|
// s-<session>, nothing else.
|
||||||
|
func haOrigin(origin, id string) bool {
|
||||||
|
u, err := url.Parse(origin)
|
||||||
|
if err != nil || u.User != nil || u.Path != "" || u.RawQuery != "" || u.Fragment != "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
h := u.Hostname()
|
||||||
|
if !strings.HasPrefix(h, "s-"+id+".") || strings.ToLower(h) != h {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return u.Scheme == "https" || (u.Scheme == "http" && strings.HasSuffix(h, ".localhost"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// haOpen: the "ha-open" command (signed): the session, the support user
|
||||||
|
// signed in, its tokens to the console.
|
||||||
|
func (l *link) haOpen(c command) (bool, string) {
|
||||||
|
cfg, ok := haConfig()
|
||||||
|
if !ok {
|
||||||
|
return false, "remote Home Assistant is not enabled on this site"
|
||||||
|
}
|
||||||
|
return haOpenWith(cfg, c, func(m map[string]any) { l.publish("ha", m) })
|
||||||
|
}
|
||||||
|
|
||||||
|
func haOpenWith(cfg haTunnelCfg, c command, send func(map[string]any)) (bool, string) {
|
||||||
|
id, keyHex, origin := c.Args["session"], c.Args["key"], c.Args["origin"]
|
||||||
|
key, err := hex.DecodeString(keyHex)
|
||||||
|
if err != nil || len(key) != 32 || id == "" || len(id) > 64 {
|
||||||
|
return false, "bad session"
|
||||||
|
}
|
||||||
|
if !haOrigin(origin, id) {
|
||||||
|
return false, "bad origin"
|
||||||
|
}
|
||||||
|
until := time.Now().Add(termMax)
|
||||||
|
if c.Args["consent"] != "" {
|
||||||
|
u, err := strconv.ParseInt(c.Args["until"], 10, 64)
|
||||||
|
if err != nil || time.Now().Unix() >= u {
|
||||||
|
return false, "the owner's permission has ended"
|
||||||
|
}
|
||||||
|
until = time.Unix(u, 0)
|
||||||
|
}
|
||||||
|
rate, _ := strconv.Atoi(c.Args["rate"])
|
||||||
|
if rate == 0 {
|
||||||
|
rate = haRateDef
|
||||||
|
}
|
||||||
|
rate = min(max(rate, haRateMin), haRateCap)
|
||||||
|
s := &haSession{id: id, by: c.Args["by"], consent: c.Args["consent"], origin: origin, key: key, until: until,
|
||||||
|
started: time.Now(), last: time.Now(), cfg: cfg, send: send, rate: newHARate(rate), gone: make(chan struct{}),
|
||||||
|
streams: map[int64]*haStream{}}
|
||||||
|
// One session a box (the support user is one HA user: a second
|
||||||
|
// session's End would end the first's sign-in).
|
||||||
|
haMu.Lock()
|
||||||
|
for _, o := range haSessions {
|
||||||
|
haMu.Unlock()
|
||||||
|
if o.id == id {
|
||||||
|
return true, "already open"
|
||||||
|
}
|
||||||
|
return false, "Home Assistant is already open for Cantabile support (" + o.by + ")"
|
||||||
|
}
|
||||||
|
haSessions[id] = s
|
||||||
|
haMu.Unlock()
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), haSignInWait)
|
||||||
|
hu, tok, err := cfg.haSignIn(ctx, origin)
|
||||||
|
cancel()
|
||||||
|
if err != nil {
|
||||||
|
haMu.Lock()
|
||||||
|
delete(haSessions, id)
|
||||||
|
haMu.Unlock()
|
||||||
|
slog.Warn("remote HA: sign-in failed", "session", id, "error", err)
|
||||||
|
return false, "the support user could not sign in to Home Assistant: " + err.Error()
|
||||||
|
}
|
||||||
|
s.mu.Lock()
|
||||||
|
s.user = hu
|
||||||
|
s.mu.Unlock()
|
||||||
|
b, _ := json.Marshal(tok)
|
||||||
|
s.out(map[string]any{"k": "tok", "d": base64.StdEncoding.EncodeToString(b)})
|
||||||
|
writeHAState()
|
||||||
|
why := "Cantabile support opened your Home Assistant through Cantabile's console (" + s.by + "), signed in as \"" + haSupportName + "\""
|
||||||
|
if s.consent != "" {
|
||||||
|
why += ", as you allowed in request " + s.consent
|
||||||
|
}
|
||||||
|
event("remote-ha", why)
|
||||||
|
slog.Info("remote HA open", "session", id, "by", s.by)
|
||||||
|
go s.watch()
|
||||||
|
return true, "Home Assistant open"
|
||||||
|
}
|
||||||
|
|
||||||
|
// haEnd: why a session must close now ("" while it may stay open).
|
||||||
|
func haEnd(last, until, now time.Time) string {
|
||||||
|
switch {
|
||||||
|
case now.Sub(last) > termIdle:
|
||||||
|
return "closed after 15 minutes without a request"
|
||||||
|
case !now.Before(until):
|
||||||
|
return "closed: its time (or the owner's permission) ended"
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *haSession) watch() {
|
||||||
|
t := time.NewTicker(10 * time.Second)
|
||||||
|
defer t.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-s.gone:
|
||||||
|
return
|
||||||
|
case <-t.C:
|
||||||
|
}
|
||||||
|
s.mu.Lock()
|
||||||
|
last := s.last
|
||||||
|
s.mu.Unlock()
|
||||||
|
if why := haEnd(last, s.until, time.Now()); why != "" {
|
||||||
|
s.close(why)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// close: every stream closed at once (HA's WebSocket with it), the console
|
||||||
|
// told, then the support user disabled (its tokens removed by HA).
|
||||||
|
func (s *haSession) close(why string) {
|
||||||
|
s.once.Do(func() {
|
||||||
|
haMu.Lock()
|
||||||
|
delete(haSessions, s.id)
|
||||||
|
haMu.Unlock()
|
||||||
|
close(s.gone)
|
||||||
|
s.mu.Lock()
|
||||||
|
all := make([]*haStream, 0, len(s.streams))
|
||||||
|
for _, st := range s.streams {
|
||||||
|
all = append(all, st)
|
||||||
|
}
|
||||||
|
s.mu.Unlock()
|
||||||
|
for _, st := range all {
|
||||||
|
st.close(false)
|
||||||
|
}
|
||||||
|
s.out(map[string]any{"k": "end", "why": why})
|
||||||
|
writeHAState()
|
||||||
|
event("remote-ha", "Cantabile support's Home Assistant session closed ("+why+")")
|
||||||
|
slog.Info("remote HA closed", "session", s.id, "why", why)
|
||||||
|
if s.user == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
added, err := s.cfg.haEnd(ctx, s.user)
|
||||||
|
cancel()
|
||||||
|
if err != nil {
|
||||||
|
// tried again by haRetryDisable; its tokens stop at the latest then
|
||||||
|
slog.Error("remote HA: the support user is not disabled yet", "session", s.id, "error", err)
|
||||||
|
go haRetryDisable(s.cfg, s.user.id)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
msg := "the Home Assistant user \"" + haSupportName + "\" is disabled again"
|
||||||
|
if len(added) > 0 {
|
||||||
|
msg += "; users added during the session: " + strings.Join(added, ", ")
|
||||||
|
}
|
||||||
|
event("remote-ha", msg)
|
||||||
|
s.out(map[string]any{"k": "rep", "disabled": true, "users_added": added})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func haRetryDisable(cfg haTunnelCfg, id string) {
|
||||||
|
for i := 0; i < 30; i++ {
|
||||||
|
time.Sleep(20 * time.Second)
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
err := cfg.haDisable(ctx, id)
|
||||||
|
cancel()
|
||||||
|
if err == nil {
|
||||||
|
event("remote-ha", "the Home Assistant user \""+haSupportName+"\" is disabled again")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// haCloseAll: the owner pressed End now.
|
||||||
|
func haCloseAll(why string) {
|
||||||
|
haMu.Lock()
|
||||||
|
var all []*haSession
|
||||||
|
for _, s := range haSessions {
|
||||||
|
all = append(all, s)
|
||||||
|
}
|
||||||
|
haMu.Unlock()
|
||||||
|
for _, s := range all {
|
||||||
|
s.close(why)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// haStartup: a session open when the link stopped left the support user
|
||||||
|
// enabled: disabled as soon as HA answers.
|
||||||
|
func haStartup() {
|
||||||
|
cfg, ok := haConfig()
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
for i := 0; i < 60; i++ {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
err := cfg.haLeftover(ctx)
|
||||||
|
cancel()
|
||||||
|
if err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(30 * time.Second)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
// haFrame: one tunnel message from the console (checked with the key).
|
||||||
|
func (l *link) haFrame(raw []byte) {
|
||||||
|
haFrameWith(raw, func(m map[string]any) { l.publish("ha", m) })
|
||||||
|
}
|
||||||
|
|
||||||
|
func haFrameWith(raw []byte, send func(map[string]any)) {
|
||||||
|
var m haMsg
|
||||||
|
if json.Unmarshal(raw, &m) != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
haMu.Lock()
|
||||||
|
s := haSessions[m.S]
|
||||||
|
haMu.Unlock()
|
||||||
|
if s == nil {
|
||||||
|
if m.S != "" && (m.K == "o" || m.K == "d") { // traffic for a session gone: the console should stop
|
||||||
|
send(map[string]any{"s": m.S, "k": "end", "why": "no such session on the box"})
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !hmac.Equal([]byte(haMAC(s.key, m)), []byte(m.MAC)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.mu.Lock()
|
||||||
|
if m.N <= s.seqIn {
|
||||||
|
s.mu.Unlock()
|
||||||
|
return // a replay
|
||||||
|
}
|
||||||
|
gap := m.N != s.seqIn+1
|
||||||
|
s.seqIn = m.N
|
||||||
|
if m.K == "o" || m.K == "d" {
|
||||||
|
s.last = time.Now()
|
||||||
|
}
|
||||||
|
var broken []*haStream
|
||||||
|
if gap { // a message lost: the open streams cannot be trusted
|
||||||
|
for _, st := range s.streams {
|
||||||
|
broken = append(broken, st)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
st := s.streams[m.I]
|
||||||
|
s.mu.Unlock()
|
||||||
|
for _, b := range broken {
|
||||||
|
b.close(true)
|
||||||
|
if b == st {
|
||||||
|
st = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch m.K {
|
||||||
|
case "c":
|
||||||
|
s.close("closed in Cantabile's console")
|
||||||
|
case "o":
|
||||||
|
s.open(m.I)
|
||||||
|
case "d":
|
||||||
|
if st == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
d, err := base64.StdEncoding.DecodeString(m.D)
|
||||||
|
if err != nil || len(d) > haChunk {
|
||||||
|
st.close(true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case st.in <- d:
|
||||||
|
default: // more than its window: the console broke the rules
|
||||||
|
st.close(true)
|
||||||
|
}
|
||||||
|
case "a":
|
||||||
|
if st != nil && m.W > 0 && m.W <= haWindow {
|
||||||
|
st.mu.Lock()
|
||||||
|
st.credit = min(st.credit+m.W, haWindow)
|
||||||
|
st.cond.Broadcast()
|
||||||
|
st.mu.Unlock()
|
||||||
|
}
|
||||||
|
case "x":
|
||||||
|
if st != nil {
|
||||||
|
st.close(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// open: a new stream to HA's HTTP port.
|
||||||
|
func (s *haSession) open(i int64) {
|
||||||
|
s.mu.Lock()
|
||||||
|
if _, dup := s.streams[i]; dup || i <= 0 || len(s.streams) >= haStreamsMax {
|
||||||
|
s.mu.Unlock()
|
||||||
|
s.out(map[string]any{"k": "x", "i": i, "why": "too many streams"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
st := &haStream{id: i, s: s, in: make(chan []byte, 1024), done: make(chan struct{}), credit: haWindow}
|
||||||
|
st.cond = sync.NewCond(&st.mu)
|
||||||
|
s.streams[i] = st
|
||||||
|
s.mu.Unlock()
|
||||||
|
go st.run()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (st *haStream) run() {
|
||||||
|
conn, err := net.DialTimeout("tcp", st.s.cfg.addr, haDialWait)
|
||||||
|
if err != nil {
|
||||||
|
st.close(true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
st.mu.Lock()
|
||||||
|
st.conn = conn
|
||||||
|
st.mu.Unlock()
|
||||||
|
select {
|
||||||
|
case <-st.done: // closed while dialling
|
||||||
|
conn.Close()
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
go st.pump()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-st.done:
|
||||||
|
return
|
||||||
|
case b := <-st.in:
|
||||||
|
conn.SetWriteDeadline(time.Now().Add(time.Minute))
|
||||||
|
if _, err := conn.Write(b); err != nil {
|
||||||
|
st.close(true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
st.s.out(map[string]any{"k": "a", "i": st.id, "w": len(b)})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// pump: HA's answer to the console, within the stream's credit and the
|
||||||
|
// session's rate.
|
||||||
|
func (st *haStream) pump() {
|
||||||
|
buf := make([]byte, haChunk)
|
||||||
|
for {
|
||||||
|
st.mu.Lock()
|
||||||
|
for st.credit <= 0 && !st.closed() {
|
||||||
|
st.cond.Wait()
|
||||||
|
}
|
||||||
|
n := min(st.credit, haChunk)
|
||||||
|
st.mu.Unlock()
|
||||||
|
if st.closed() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
k, err := st.conn.Read(buf[:n])
|
||||||
|
if k > 0 {
|
||||||
|
if !st.s.rate.wait(k, st.done) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
st.mu.Lock()
|
||||||
|
st.credit -= int64(k)
|
||||||
|
st.mu.Unlock()
|
||||||
|
st.s.out(map[string]any{"k": "d", "i": st.id, "d": base64.StdEncoding.EncodeToString(buf[:k])})
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, io.EOF) && !st.closed() {
|
||||||
|
st.s.out(map[string]any{"k": "e", "i": st.id})
|
||||||
|
}
|
||||||
|
st.close(false)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (st *haStream) closed() bool {
|
||||||
|
select {
|
||||||
|
case <-st.done:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// close: tell=true also tells the console (a reset it did not ask for).
|
||||||
|
func (st *haStream) close(tell bool) {
|
||||||
|
st.once.Do(func() {
|
||||||
|
close(st.done)
|
||||||
|
st.mu.Lock()
|
||||||
|
if st.conn != nil {
|
||||||
|
st.conn.Close()
|
||||||
|
}
|
||||||
|
st.cond.Broadcast()
|
||||||
|
st.mu.Unlock()
|
||||||
|
st.s.mu.Lock()
|
||||||
|
delete(st.s.streams, st.id)
|
||||||
|
st.s.mu.Unlock()
|
||||||
|
if tell {
|
||||||
|
st.s.out(map[string]any{"k": "x", "i": st.id})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// haRate: a token bucket of bytes per second (a second's worth of burst).
|
||||||
|
type haRate struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
per float64
|
||||||
|
tokens float64
|
||||||
|
at time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func newHARate(per int) *haRate {
|
||||||
|
return &haRate{per: float64(per), tokens: float64(per), at: time.Now()}
|
||||||
|
}
|
||||||
|
|
||||||
|
// wait: n bytes may go; false if done closed first.
|
||||||
|
func (r *haRate) wait(n int, done <-chan struct{}) bool {
|
||||||
|
for {
|
||||||
|
r.mu.Lock()
|
||||||
|
now := time.Now()
|
||||||
|
r.tokens = min(r.per, r.tokens+now.Sub(r.at).Seconds()*r.per)
|
||||||
|
r.at = now
|
||||||
|
if r.tokens >= float64(n) {
|
||||||
|
r.tokens -= float64(n)
|
||||||
|
r.mu.Unlock()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
need := time.Duration((float64(n) - r.tokens) / r.per * float64(time.Second))
|
||||||
|
r.mu.Unlock()
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
return false
|
||||||
|
case <-time.After(need):
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,713 @@
|
|||||||
|
// Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"log/slog"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/coder/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
// fakeHA: Home Assistant's user admin over its WebSocket, its login flow,
|
||||||
|
// a large page and a WebSocket echo (what the tunnel carries).
|
||||||
|
type fakeHA struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
users map[string]*haUserRow
|
||||||
|
pw map[string]string // username -> password
|
||||||
|
local map[string]bool
|
||||||
|
admin map[string]bool
|
||||||
|
n int
|
||||||
|
big []byte
|
||||||
|
conns int // open echo WebSockets
|
||||||
|
}
|
||||||
|
|
||||||
|
const fakeAdmin = "admin-token-for-tests"
|
||||||
|
|
||||||
|
func newFakeHA() (*fakeHA, *httptest.Server) {
|
||||||
|
f := &fakeHA{users: map[string]*haUserRow{"owner": {ID: "owner", Name: "Owner", Username: "owner", IsOwner: true, IsActive: true}},
|
||||||
|
pw: map[string]string{"owner": "owner-pw"}, local: map[string]bool{}, admin: map[string]bool{}, big: bytes.Repeat([]byte("0123456789abcdef"), 1<<16)}
|
||||||
|
m := http.NewServeMux()
|
||||||
|
m.HandleFunc("/api/websocket", f.ws)
|
||||||
|
m.HandleFunc("/auth/login_flow", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var in map[string]any
|
||||||
|
json.NewDecoder(r.Body).Decode(&in)
|
||||||
|
if !strings.HasPrefix(in["client_id"].(string), "https://s-") || !strings.HasPrefix(in["redirect_uri"].(string), in["client_id"].(string)) {
|
||||||
|
w.WriteHeader(400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
json.NewEncoder(w).Encode(map[string]any{"flow_id": "F1", "type": "form"})
|
||||||
|
})
|
||||||
|
m.HandleFunc("/auth/login_flow/F1", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var in map[string]string
|
||||||
|
json.NewDecoder(r.Body).Decode(&in)
|
||||||
|
f.mu.Lock()
|
||||||
|
ok := in["password"] != "" && f.pw[in["username"]] == in["password"]
|
||||||
|
f.mu.Unlock()
|
||||||
|
if !ok {
|
||||||
|
json.NewEncoder(w).Encode(map[string]any{"type": "form", "errors": map[string]string{"base": "invalid_auth"}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
json.NewEncoder(w).Encode(map[string]any{"type": "create_entry", "result": "CODE1"})
|
||||||
|
})
|
||||||
|
m.HandleFunc("/auth/token", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
r.ParseForm()
|
||||||
|
if r.Form.Get("code") != "CODE1" {
|
||||||
|
w.WriteHeader(400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
json.NewEncoder(w).Encode(map[string]any{"access_token": "AT-very-secret", "refresh_token": "RT-very-secret", "expires_in": 1800, "token_type": "Bearer"})
|
||||||
|
})
|
||||||
|
m.HandleFunc("/big", func(w http.ResponseWriter, r *http.Request) { w.Write(f.big) })
|
||||||
|
m.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
c, err := websocket.Accept(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
f.mu.Lock()
|
||||||
|
f.conns++
|
||||||
|
f.mu.Unlock()
|
||||||
|
defer func() { f.mu.Lock(); f.conns--; f.mu.Unlock() }()
|
||||||
|
for {
|
||||||
|
t, b, err := c.Read(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Write(context.Background(), t, b)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return f, httptest.NewServer(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeHA) ws(w http.ResponseWriter, r *http.Request) {
|
||||||
|
c, err := websocket.Accept(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
write := func(v any) { b, _ := json.Marshal(v); c.Write(ctx, websocket.MessageText, b) }
|
||||||
|
write(map[string]any{"type": "auth_required"})
|
||||||
|
var a map[string]any
|
||||||
|
_, b, _ := c.Read(ctx)
|
||||||
|
json.Unmarshal(b, &a)
|
||||||
|
if a["access_token"] != fakeAdmin {
|
||||||
|
write(map[string]any{"type": "auth_invalid"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
write(map[string]any{"type": "auth_ok"})
|
||||||
|
for {
|
||||||
|
_, b, err := c.Read(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var m map[string]any
|
||||||
|
json.Unmarshal(b, &m)
|
||||||
|
id := m["id"]
|
||||||
|
ok := func(res any) { write(map[string]any{"id": id, "type": "result", "success": true, "result": res}) }
|
||||||
|
bad := func(code string) {
|
||||||
|
write(map[string]any{"id": id, "type": "result", "success": false, "error": map[string]string{"code": code}})
|
||||||
|
}
|
||||||
|
f.mu.Lock()
|
||||||
|
switch m["type"] {
|
||||||
|
case "config/auth/list":
|
||||||
|
var out []haUserRow
|
||||||
|
for _, u := range f.users {
|
||||||
|
out = append(out, *u)
|
||||||
|
}
|
||||||
|
ok(out)
|
||||||
|
case "config/auth/create":
|
||||||
|
f.n++
|
||||||
|
u := &haUserRow{ID: "u" + string(rune('0'+f.n)), Name: m["name"].(string), IsActive: true}
|
||||||
|
f.users[u.ID] = u
|
||||||
|
f.local[u.ID] = m["local_only"] == true
|
||||||
|
f.admin[u.ID] = len(m["group_ids"].([]any)) == 1 && m["group_ids"].([]any)[0] == "system-admin"
|
||||||
|
ok(map[string]any{"user": u})
|
||||||
|
case "config/auth/update":
|
||||||
|
u := f.users[m["user_id"].(string)]
|
||||||
|
if u == nil {
|
||||||
|
bad("not_found")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if v, has := m["is_active"].(bool); has {
|
||||||
|
if u.IsOwner && !v {
|
||||||
|
bad("invalid")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
u.IsActive = v
|
||||||
|
}
|
||||||
|
if v, has := m["local_only"].(bool); has {
|
||||||
|
f.local[u.ID] = v
|
||||||
|
}
|
||||||
|
ok(nil)
|
||||||
|
case "config/auth_provider/homeassistant/create":
|
||||||
|
u := f.users[m["user_id"].(string)]
|
||||||
|
if u == nil || u.Username != "" {
|
||||||
|
bad("credentials_exist")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
u.Username = m["username"].(string)
|
||||||
|
f.pw[u.Username] = m["password"].(string)
|
||||||
|
ok(nil)
|
||||||
|
case "config/auth_provider/homeassistant/delete":
|
||||||
|
name := m["username"].(string)
|
||||||
|
var who *haUserRow
|
||||||
|
for _, u := range f.users {
|
||||||
|
if u.Username == name {
|
||||||
|
who = u
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if who != nil && who.IsOwner {
|
||||||
|
bad("cannot_delete_owner_credentials")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if who != nil {
|
||||||
|
who.Username = ""
|
||||||
|
}
|
||||||
|
delete(f.pw, name)
|
||||||
|
ok(nil)
|
||||||
|
default:
|
||||||
|
bad("unknown_command")
|
||||||
|
}
|
||||||
|
f.mu.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeHA) support() haUserRow {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
for _, u := range f.users {
|
||||||
|
if u.Name == haSupportName {
|
||||||
|
return *u
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return haUserRow{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// console: the console's side of the protocol, in memory.
|
||||||
|
type console struct {
|
||||||
|
t *testing.T
|
||||||
|
sid string
|
||||||
|
key []byte
|
||||||
|
sendMu sync.Mutex
|
||||||
|
mu sync.Mutex
|
||||||
|
n int64
|
||||||
|
msgs chan map[string]any
|
||||||
|
pipes map[int64]chan []byte // per stream, in order (nil: closed)
|
||||||
|
sent map[int64]int64
|
||||||
|
tokens []byte
|
||||||
|
ended chan string
|
||||||
|
report chan map[string]any
|
||||||
|
noAck bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func newConsole(t *testing.T, sid string) *console {
|
||||||
|
k := make([]byte, 32)
|
||||||
|
for i := range k {
|
||||||
|
k[i] = byte(i + 1)
|
||||||
|
}
|
||||||
|
c := &console{t: t, sid: sid, key: k, msgs: make(chan map[string]any, 4096), pipes: map[int64]chan []byte{},
|
||||||
|
sent: map[int64]int64{}, ended: make(chan string, 4), report: make(chan map[string]any, 4)}
|
||||||
|
go c.loop()
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *console) fromBox(m map[string]any) {
|
||||||
|
b, _ := json.Marshal(m) // as over NATS
|
||||||
|
var v map[string]any
|
||||||
|
json.Unmarshal(b, &v)
|
||||||
|
c.msgs <- v
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *console) loop() {
|
||||||
|
for m := range c.msgs {
|
||||||
|
i := int64(0)
|
||||||
|
if f, ok := m["i"].(float64); ok {
|
||||||
|
i = int64(f)
|
||||||
|
}
|
||||||
|
c.mu.Lock()
|
||||||
|
p := c.pipes[i]
|
||||||
|
c.mu.Unlock()
|
||||||
|
switch m["k"] {
|
||||||
|
case "tok":
|
||||||
|
c.mu.Lock()
|
||||||
|
c.tokens, _ = base64.StdEncoding.DecodeString(m["d"].(string))
|
||||||
|
c.mu.Unlock()
|
||||||
|
case "d":
|
||||||
|
d, _ := base64.StdEncoding.DecodeString(m["d"].(string))
|
||||||
|
c.mu.Lock()
|
||||||
|
c.sent[i] += int64(len(d))
|
||||||
|
c.mu.Unlock()
|
||||||
|
if p != nil {
|
||||||
|
p <- d
|
||||||
|
}
|
||||||
|
case "e", "x":
|
||||||
|
if p != nil {
|
||||||
|
c.mu.Lock()
|
||||||
|
delete(c.pipes, i)
|
||||||
|
c.mu.Unlock()
|
||||||
|
close(p)
|
||||||
|
}
|
||||||
|
case "end": // as the console does: every stream of the session closed
|
||||||
|
c.mu.Lock()
|
||||||
|
for k, p := range c.pipes {
|
||||||
|
delete(c.pipes, k)
|
||||||
|
close(p)
|
||||||
|
}
|
||||||
|
c.mu.Unlock()
|
||||||
|
c.ended <- m["why"].(string)
|
||||||
|
case "rep":
|
||||||
|
c.report <- m
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// frame: one signed message to the box.
|
||||||
|
func (c *console) frame(k string, i int64, d string, w int64) {
|
||||||
|
c.sendMu.Lock() // in order, as the console publishes them
|
||||||
|
defer c.sendMu.Unlock()
|
||||||
|
c.mu.Lock()
|
||||||
|
c.n++
|
||||||
|
m := haMsg{T: "ha", S: c.sid, N: c.n, K: k, I: i, D: d, W: w}
|
||||||
|
c.mu.Unlock()
|
||||||
|
m.MAC = haMAC(c.key, m)
|
||||||
|
b, _ := json.Marshal(m)
|
||||||
|
haFrameWith(b, c.fromBox)
|
||||||
|
}
|
||||||
|
|
||||||
|
// conn: a stream as a net.Conn (an HTTP or WebSocket client dials it).
|
||||||
|
type tconn struct {
|
||||||
|
c *console
|
||||||
|
i int64
|
||||||
|
pr *io.PipeReader
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tconn) Read(p []byte) (int, error) { return t.pr.Read(p) }
|
||||||
|
func (t *tconn) Write(p []byte) (int, error) {
|
||||||
|
n0 := len(p)
|
||||||
|
for len(p) > 0 {
|
||||||
|
n := min(len(p), haChunk)
|
||||||
|
t.c.frame("d", t.i, base64.StdEncoding.EncodeToString(p[:n]), 0)
|
||||||
|
p = p[n:]
|
||||||
|
}
|
||||||
|
return n0, nil
|
||||||
|
}
|
||||||
|
func (t *tconn) Close() error { t.c.frame("x", t.i, "", 0); t.pr.Close(); return nil }
|
||||||
|
func (t *tconn) LocalAddr() net.Addr { return &net.TCPAddr{} }
|
||||||
|
func (t *tconn) RemoteAddr() net.Addr { return &net.TCPAddr{} }
|
||||||
|
func (t *tconn) SetDeadline(time.Time) error { return nil }
|
||||||
|
func (t *tconn) SetReadDeadline(time.Time) error { return nil }
|
||||||
|
func (t *tconn) SetWriteDeadline(time.Time) error { return nil }
|
||||||
|
|
||||||
|
func (c *console) dial(i int64) net.Conn {
|
||||||
|
pr, pw := io.Pipe()
|
||||||
|
ch := make(chan []byte, 1024)
|
||||||
|
c.mu.Lock()
|
||||||
|
c.pipes[i] = ch
|
||||||
|
c.mu.Unlock()
|
||||||
|
go func() { // in order to the reader, then credit back (unless the test holds it)
|
||||||
|
for d := range ch {
|
||||||
|
pw.Write(d)
|
||||||
|
c.mu.Lock()
|
||||||
|
noAck := c.noAck
|
||||||
|
c.mu.Unlock()
|
||||||
|
if !noAck {
|
||||||
|
c.frame("a", i, "", int64(len(d)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pw.Close()
|
||||||
|
}()
|
||||||
|
c.frame("o", i, "", 0)
|
||||||
|
return &tconn{c: c, i: i, pr: pr}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *console) client() *http.Client {
|
||||||
|
var next int64 = 100
|
||||||
|
var mu sync.Mutex
|
||||||
|
return &http.Client{Transport: &http.Transport{DisableKeepAlives: true, DialContext: func(context.Context, string, string) (net.Conn, error) {
|
||||||
|
mu.Lock()
|
||||||
|
next++
|
||||||
|
i := next
|
||||||
|
mu.Unlock()
|
||||||
|
return c.dial(i), nil
|
||||||
|
}}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func haCmd(sid, origin string, extra map[string]string) command {
|
||||||
|
k := make([]byte, 32)
|
||||||
|
for i := range k {
|
||||||
|
k[i] = byte(i + 1)
|
||||||
|
}
|
||||||
|
a := map[string]string{"session": sid, "key": hex.EncodeToString(k), "by": "Ana (support)", "origin": origin}
|
||||||
|
for x, v := range extra {
|
||||||
|
a[x] = v
|
||||||
|
}
|
||||||
|
return command{ID: "c-" + sid, Cmd: "ha-open", Args: a}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHAOrigin(t *testing.T) {
|
||||||
|
id := "0f0e0d0c-0000-4000-8000-000000000001"
|
||||||
|
for o, want := range map[string]bool{
|
||||||
|
"https://s-" + id + ".support.example.net": true,
|
||||||
|
"https://s-" + id + ".support.example.net:8443": true,
|
||||||
|
"http://s-" + id + ".support.localhost:18443": true,
|
||||||
|
"http://s-" + id + ".support.example.net": false, // http only on localhost
|
||||||
|
"https://s-other.support.example.net": false,
|
||||||
|
"https://x.s-" + id + ".support.example.net": false,
|
||||||
|
"https://s-" + id + ".support.example.net/": false,
|
||||||
|
"https://user@s-" + id + ".support.example.net": false,
|
||||||
|
"https://S-" + strings.ToUpper(id) + ".example.net": false,
|
||||||
|
"https://s-" + id + ".support.example.net/?x=1": false,
|
||||||
|
"javascript://s-" + id + ".support.example.net": false,
|
||||||
|
"https://s-" + id + ".support.example.net#fragment": false,
|
||||||
|
"https://s-" + id + "evil.example.net": false,
|
||||||
|
"https://s-" + id + ".support.example.net\n.foo.com": false,
|
||||||
|
} {
|
||||||
|
if haOrigin(o, id) != want {
|
||||||
|
t.Errorf("haOrigin(%q) = %v", o, !want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The MAC as the console computes it (platform/services/fleet/haproxy_test.go
|
||||||
|
// has the same vector).
|
||||||
|
func TestHAMACVector(t *testing.T) {
|
||||||
|
m := haMsg{S: "sess", N: 7, K: "d", I: 3, D: "aGVsbG8="}
|
||||||
|
if got := haMAC([]byte("0123456789abcdef0123456789abcdef"), m); got != "f13d6ee375af9a65f2aa6f9d825f50d2b6932db6329bad76a628a0c4339401f0" {
|
||||||
|
t.Fatalf("haMAC = %s", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHAEnd(t *testing.T) {
|
||||||
|
now := time.Now()
|
||||||
|
if haEnd(now.Add(-14*time.Minute), now.Add(time.Hour), now) != "" {
|
||||||
|
t.Error("closed while in use")
|
||||||
|
}
|
||||||
|
if !strings.Contains(haEnd(now.Add(-16*time.Minute), now.Add(time.Hour), now), "15 minutes") {
|
||||||
|
t.Error("idle not closed")
|
||||||
|
}
|
||||||
|
if !strings.Contains(haEnd(now, now, now), "ended") {
|
||||||
|
t.Error("until not kept")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The whole session against a fake HA: the support user (admin, local
|
||||||
|
// only, not the owner) signed in, one session a box, a large answer under
|
||||||
|
// flow control, a WebSocket, a replay and a bad MAC ignored, the console's
|
||||||
|
// close, the user disabled and without a password after, no token in the
|
||||||
|
// box's logs.
|
||||||
|
func TestHASession(t *testing.T) {
|
||||||
|
var logs bytes.Buffer
|
||||||
|
old := slog.Default()
|
||||||
|
slog.SetDefault(slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelDebug})))
|
||||||
|
defer slog.SetDefault(old)
|
||||||
|
f, srv := newFakeHA()
|
||||||
|
defer srv.Close()
|
||||||
|
addr := strings.TrimPrefix(srv.URL, "http://")
|
||||||
|
cfg := haTunnelCfg{addr: addr, httpBase: srv.URL, adminWS: "ws://" + addr + "/api/websocket",
|
||||||
|
adminToken: func() (string, error) { return fakeAdmin, nil }}
|
||||||
|
|
||||||
|
sid := "0f0e0d0c-0000-4000-8000-000000000001"
|
||||||
|
origin := "https://s-" + sid + ".support.example.net"
|
||||||
|
con := newConsole(t, sid)
|
||||||
|
if ok, msg := haOpenWith(cfg, haCmd(sid, origin, map[string]string{"consent": "ABCD1234", "until": "1"}), con.fromBox); ok {
|
||||||
|
t.Fatalf("opened after the owner's permission ended: %s", msg)
|
||||||
|
}
|
||||||
|
if ok, msg := haOpenWith(cfg, haCmd(sid, "https://s-other.example.net", nil), con.fromBox); ok || msg != "bad origin" {
|
||||||
|
t.Fatalf("another origin: %v %s", ok, msg)
|
||||||
|
}
|
||||||
|
if ok, msg := haOpenWith(cfg, haCmd(sid, origin, map[string]string{"rate": "200000000"}), con.fromBox); !ok {
|
||||||
|
t.Fatalf("open: %s", msg)
|
||||||
|
}
|
||||||
|
defer haCloseAll("test over")
|
||||||
|
su := f.support()
|
||||||
|
if su.ID == "" || !su.IsActive || su.Username != haSupportUsername || su.IsOwner || !f.admin[su.ID] || !f.local[su.ID] {
|
||||||
|
t.Fatalf("support user: %+v admin %v local %v", su, f.admin[su.ID], f.local[su.ID])
|
||||||
|
}
|
||||||
|
if f.users["owner"].Username != "owner" || f.pw["owner"] != "owner-pw" {
|
||||||
|
t.Fatal("the owner's login changed")
|
||||||
|
}
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
con.mu.Lock()
|
||||||
|
tok := string(con.tokens)
|
||||||
|
con.mu.Unlock()
|
||||||
|
if !strings.Contains(tok, "AT-very-secret") || !strings.Contains(tok, "RT-very-secret") {
|
||||||
|
t.Fatalf("tokens: %q", tok)
|
||||||
|
}
|
||||||
|
// A second session on the box: refused.
|
||||||
|
sid2 := "0f0e0d0c-0000-4000-8000-000000000002"
|
||||||
|
if ok, msg := haOpenWith(cfg, haCmd(sid2, "https://s-"+sid2+".support.example.net", nil), con.fromBox); ok || !strings.Contains(msg, "already open") {
|
||||||
|
t.Fatalf("second session: %v %s", ok, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A large answer, read through the tunnel.
|
||||||
|
cl := con.client()
|
||||||
|
res, err := cl.Get("http://ha/big")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
body, _ := io.ReadAll(res.Body)
|
||||||
|
res.Body.Close()
|
||||||
|
if !bytes.Equal(body, f.big) {
|
||||||
|
t.Fatalf("big: %d bytes", len(body))
|
||||||
|
}
|
||||||
|
// Flow control: without credit the box stops at its window.
|
||||||
|
con.mu.Lock()
|
||||||
|
con.noAck = true
|
||||||
|
con.mu.Unlock()
|
||||||
|
c := con.dial(7)
|
||||||
|
c.Write([]byte("GET /big HTTP/1.1\r\nHost: ha\r\nConnection: close\r\n\r\n"))
|
||||||
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
con.mu.Lock()
|
||||||
|
sent := con.sent[7]
|
||||||
|
con.noAck = false
|
||||||
|
con.mu.Unlock()
|
||||||
|
if sent != haWindow {
|
||||||
|
t.Fatalf("sent %d without credit, want the window %d", sent, haWindow)
|
||||||
|
}
|
||||||
|
c.Close()
|
||||||
|
|
||||||
|
// A WebSocket through it.
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
ws, _, err := websocket.Dial(ctx, "ws://ha/echo", &websocket.DialOptions{HTTPClient: cl})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
ws.Write(ctx, websocket.MessageText, []byte(`{"type":"ping"}`))
|
||||||
|
if _, b, err := ws.Read(ctx); err != nil || string(b) != `{"type":"ping"}` {
|
||||||
|
t.Fatalf("echo: %s %v", b, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A bad MAC and a replayed close: ignored.
|
||||||
|
m := haMsg{T: "ha", S: sid, N: 99999, K: "c", MAC: strings.Repeat("0", 64)}
|
||||||
|
b, _ := json.Marshal(m)
|
||||||
|
haFrameWith(b, con.fromBox)
|
||||||
|
con.mu.Lock()
|
||||||
|
m = haMsg{T: "ha", S: sid, N: 1, K: "c"} // an old number
|
||||||
|
con.mu.Unlock()
|
||||||
|
m.MAC = haMAC(con.key, m)
|
||||||
|
b, _ = json.Marshal(m)
|
||||||
|
haFrameWith(b, con.fromBox)
|
||||||
|
haMu.Lock()
|
||||||
|
open := haSessions[sid] != nil
|
||||||
|
haMu.Unlock()
|
||||||
|
if !open {
|
||||||
|
t.Fatal("a bad MAC or a replay closed the session")
|
||||||
|
}
|
||||||
|
if l := haList(); len(l) != 1 || l[0]["kind"] != "ha" {
|
||||||
|
t.Fatalf("banner: %v", l)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The console closes it: the WebSocket ends, the user is disabled.
|
||||||
|
con.frame("c", 0, "", 0)
|
||||||
|
select {
|
||||||
|
case why := <-con.ended:
|
||||||
|
if !strings.Contains(why, "console") {
|
||||||
|
t.Errorf("why: %s", why)
|
||||||
|
}
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
t.Fatal("no end")
|
||||||
|
}
|
||||||
|
rctx, rcancel := context.WithTimeout(ctx, 2*time.Second)
|
||||||
|
if _, _, err := ws.Read(rctx); err == nil || rctx.Err() != nil {
|
||||||
|
t.Fatalf("the WebSocket stayed open: %v", err)
|
||||||
|
}
|
||||||
|
rcancel()
|
||||||
|
select {
|
||||||
|
case <-con.report:
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
t.Fatal("no report")
|
||||||
|
}
|
||||||
|
if su := f.support(); su.IsActive || su.Username != "" {
|
||||||
|
t.Fatalf("after the end: %+v", su)
|
||||||
|
}
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
f.mu.Lock()
|
||||||
|
echo := f.conns
|
||||||
|
f.mu.Unlock()
|
||||||
|
if echo != 0 {
|
||||||
|
t.Errorf("%d WebSockets still open at HA", echo)
|
||||||
|
}
|
||||||
|
// Traffic for the ended session: told it is gone.
|
||||||
|
con.frame("o", 50, "", 0)
|
||||||
|
select {
|
||||||
|
case why := <-con.ended:
|
||||||
|
if !strings.Contains(why, "no such session") {
|
||||||
|
t.Errorf("after the end: %s", why)
|
||||||
|
}
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
t.Fatal("no answer for a gone session")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Again, ended by the owner (End now): a new password, the same user.
|
||||||
|
con2 := newConsole(t, sid2)
|
||||||
|
if ok, msg := haOpenWith(cfg, haCmd(sid2, "https://s-"+sid2+".support.example.net", nil), con2.fromBox); !ok {
|
||||||
|
t.Fatalf("open again: %s", msg)
|
||||||
|
}
|
||||||
|
if su2 := f.support(); su2.ID != su.ID || !su2.IsActive {
|
||||||
|
t.Fatalf("a new user instead of the same: %+v", su2)
|
||||||
|
}
|
||||||
|
haCloseAll("ended by the owner")
|
||||||
|
if why := <-con2.ended; why != "ended by the owner" {
|
||||||
|
t.Errorf("why: %s", why)
|
||||||
|
}
|
||||||
|
<-con2.report
|
||||||
|
if su := f.support(); su.IsActive {
|
||||||
|
t.Fatal("still active after the owner's End")
|
||||||
|
}
|
||||||
|
for _, s := range []string{"AT-very-secret", "RT-very-secret", "CODE1"} {
|
||||||
|
if strings.Contains(logs.String(), s) {
|
||||||
|
t.Errorf("%s in the box's log", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f.mu.Lock()
|
||||||
|
for _, p := range f.pw {
|
||||||
|
if p != "owner-pw" && strings.Contains(logs.String(), p) {
|
||||||
|
t.Error("the support user's password in the log")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// A disabled-at-start leftover: the link's start disables an active
|
||||||
|
// support user (a session open when it stopped).
|
||||||
|
func TestHALeftover(t *testing.T) {
|
||||||
|
f, srv := newFakeHA()
|
||||||
|
defer srv.Close()
|
||||||
|
addr := strings.TrimPrefix(srv.URL, "http://")
|
||||||
|
cfg := haTunnelCfg{addr: addr, httpBase: srv.URL, adminWS: "ws://" + addr + "/api/websocket",
|
||||||
|
adminToken: func() (string, error) { return fakeAdmin, nil }}
|
||||||
|
f.users["s"] = &haUserRow{ID: "s", Name: haSupportName, Username: haSupportUsername, IsActive: true}
|
||||||
|
f.pw[haSupportUsername] = "left-over"
|
||||||
|
if err := cfg.haLeftover(context.Background()); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if u := f.support(); u.IsActive || u.Username != "" {
|
||||||
|
t.Fatalf("left over: %+v", u)
|
||||||
|
}
|
||||||
|
// Never the owner, even with the support user's name.
|
||||||
|
f2, srv2 := newFakeHA()
|
||||||
|
defer srv2.Close()
|
||||||
|
f2.users["owner"].Name = haSupportName
|
||||||
|
f2.users["owner"].Username = "owner"
|
||||||
|
addr2 := strings.TrimPrefix(srv2.URL, "http://")
|
||||||
|
cfg2 := haTunnelCfg{addr: addr2, httpBase: srv2.URL, adminWS: "ws://" + addr2 + "/api/websocket",
|
||||||
|
adminToken: func() (string, error) { return fakeAdmin, nil }}
|
||||||
|
if err := cfg2.haLeftover(context.Background()); err != nil || !f2.users["owner"].IsActive {
|
||||||
|
t.Fatalf("the owner: %v %+v", err, f2.users["owner"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// An HA add-on site: the add-on's option and the Supervisor's token give
|
||||||
|
// HA on the Supervisor's network and the admin commands through its proxy;
|
||||||
|
// without the option nothing (the default).
|
||||||
|
func TestHAConfigAddon(t *testing.T) {
|
||||||
|
t.Setenv("CANTABILE_HUB", "")
|
||||||
|
t.Setenv("CANTABILE_HA_ADDR", "")
|
||||||
|
t.Setenv("CANTABILE_HA_ADMIN_WS", "")
|
||||||
|
t.Setenv("CANTABILE_HA_TOKEN_FILE", "")
|
||||||
|
t.Setenv("SUPERVISOR_TOKEN", "sv-token")
|
||||||
|
t.Setenv("CANTABILE_HA_REMOTE", "")
|
||||||
|
if _, ok := haConfig(); ok {
|
||||||
|
t.Fatal("remote HA without the owner's option")
|
||||||
|
}
|
||||||
|
t.Setenv("CANTABILE_HA_REMOTE", "1")
|
||||||
|
c, ok := haConfig()
|
||||||
|
if !ok || c.addr != "homeassistant:8123" || c.httpBase != "http://homeassistant:8123" || c.adminWS != "ws://supervisor/core/websocket" {
|
||||||
|
t.Fatalf("add-on: %v %+v", ok, c)
|
||||||
|
}
|
||||||
|
if tok, _ := c.adminToken(); tok != "sv-token" {
|
||||||
|
t.Fatal("not the Supervisor's token")
|
||||||
|
}
|
||||||
|
t.Setenv("SUPERVISOR_TOKEN", "")
|
||||||
|
if _, ok := haConfig(); ok {
|
||||||
|
t.Fatal("remote HA without the Supervisor's token")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The add-on's pages learn of a session from the link's state directory
|
||||||
|
// (support-ha.json: the link's pid for End now, the sessions), and that it
|
||||||
|
// ended.
|
||||||
|
func TestHAAddonStateFile(t *testing.T) {
|
||||||
|
t.Setenv("CANTABILE_HUB", "")
|
||||||
|
dir := t.TempDir()
|
||||||
|
t.Setenv("CANTABILE_LINK_STATE", dir)
|
||||||
|
_, srv := newFakeHA()
|
||||||
|
defer srv.Close()
|
||||||
|
addr := strings.TrimPrefix(srv.URL, "http://")
|
||||||
|
cfg := haTunnelCfg{addr: addr, httpBase: srv.URL, adminWS: "ws://" + addr + "/api/websocket",
|
||||||
|
adminToken: func() (string, error) { return fakeAdmin, nil }}
|
||||||
|
read := func() (st struct {
|
||||||
|
Pid int
|
||||||
|
User string
|
||||||
|
Sessions []map[string]any
|
||||||
|
}) {
|
||||||
|
b, err := os.ReadFile(filepath.Join(dir, "support-ha.json"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
json.Unmarshal(b, &st)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeHAState() // the link's start
|
||||||
|
if st := read(); st.Pid != os.Getpid() || len(st.Sessions) != 0 {
|
||||||
|
t.Fatalf("at start: %+v", st)
|
||||||
|
}
|
||||||
|
sid := "0f0e0d0c-0000-4000-8000-0000000000a1"
|
||||||
|
con := newConsole(t, sid)
|
||||||
|
if ok, msg := haOpenWith(cfg, haCmd(sid, "https://s-"+sid+".support.example.net", map[string]string{"by": "Ana (Cantabile)"}), con.fromBox); !ok {
|
||||||
|
t.Fatalf("open: %s", msg)
|
||||||
|
}
|
||||||
|
st := read()
|
||||||
|
if len(st.Sessions) != 1 || st.Sessions[0]["kind"] != "ha" || st.Sessions[0]["by"] != "Ana (Cantabile)" || st.User != haSupportName ||
|
||||||
|
st.Sessions[0]["ha_user"] == nil || st.Sessions[0]["ha_user"] == "" {
|
||||||
|
t.Fatalf("open: %+v", st)
|
||||||
|
}
|
||||||
|
haCloseAll("ended by the owner")
|
||||||
|
<-con.ended
|
||||||
|
if st := read(); len(st.Sessions) != 0 {
|
||||||
|
t.Fatalf("after the end: %+v", st)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTPS on HA's own port: the support person is told why, not a bare
|
||||||
|
// transport error.
|
||||||
|
func TestHAPlainHTTPOnly(t *testing.T) {
|
||||||
|
// as aiohttp with an ssl_context answers plain HTTP: a TLS alert, closed
|
||||||
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer ln.Close()
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
c, err := ln.Accept()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Read(make([]byte, 4096))
|
||||||
|
c.Write([]byte{0x15, 0x03, 0x01, 0x00, 0x02, 0x02, 0x46})
|
||||||
|
c.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
addr := ln.Addr().String()
|
||||||
|
cfg := haTunnelCfg{addr: addr, httpBase: "http://" + addr}
|
||||||
|
_, err = cfg.haLogin(context.Background(), "https://s-x.support.example.net", "pw")
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "ssl_certificate") {
|
||||||
|
t.Fatalf("got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,381 @@
|
|||||||
|
// Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/coder/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The "Cantabile support" user in Home Assistant (item 19):
|
||||||
|
// support signs in to the owner's HA as this user, never as the owner and
|
||||||
|
// never with the owner's password. The box keeps it, with the agent's HA
|
||||||
|
// administrator token (the Hub: /data/cantabile/edge/ha-token, made by
|
||||||
|
// ha-setup; an HA add-on site: the Supervisor's token, through the
|
||||||
|
// Supervisor's WebSocket proxy).
|
||||||
|
//
|
||||||
|
// - An administrator, not the owner: what support looks into (integrations,
|
||||||
|
// devices and entities, HA's logs, repairs, restart, backups, add-ons on
|
||||||
|
// an HA OS site) is admin-only in HA; a plain user sees dashboards only.
|
||||||
|
// HA lets only the owner change another user's password
|
||||||
|
// (admin_change_password is owner-only since 2026.x), so this user cannot
|
||||||
|
// change or learn the owner's.
|
||||||
|
// - local_only: it signs in only from the box's own network (the box's link
|
||||||
|
// is its only way in; HA's remote access through Nabu Casa refuses it).
|
||||||
|
// - Between sessions it is disabled and has no password: a session gives it
|
||||||
|
// a new random password (its credentials made again), signs it in once
|
||||||
|
// through HA's own login flow on the box, and throws the password away;
|
||||||
|
// only the tokens go to the support browser. At the end it is disabled
|
||||||
|
// again (HA removes every refresh token of a disabled user, which also
|
||||||
|
// closes its WebSocket connections) and its credentials removed.
|
||||||
|
|
||||||
|
const (
|
||||||
|
haSupportName = "Cantabile support"
|
||||||
|
haSupportUsername = "cantabile-support"
|
||||||
|
)
|
||||||
|
|
||||||
|
// haUser: the HA side of one session.
|
||||||
|
type haUser struct {
|
||||||
|
id string // the support user's id
|
||||||
|
before map[string]bool // the users there were at the start
|
||||||
|
}
|
||||||
|
|
||||||
|
// haTokens: what HA's /auth/token answers (passed on to the browser).
|
||||||
|
type haTokens struct {
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
RefreshToken string `json:"refresh_token"`
|
||||||
|
ExpiresIn int `json:"expires_in"`
|
||||||
|
TokenType string `json:"token_type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// haWS: a short WebSocket conversation with HA (commands, one at a time).
|
||||||
|
type haWS struct {
|
||||||
|
c *websocket.Conn
|
||||||
|
id int
|
||||||
|
}
|
||||||
|
|
||||||
|
func haDial(ctx context.Context, wsURL, token string) (*haWS, error) {
|
||||||
|
c, _, err := websocket.Dial(ctx, wsURL, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Home Assistant's WebSocket: %w", err)
|
||||||
|
}
|
||||||
|
c.SetReadLimit(16 << 20)
|
||||||
|
w := &haWS{c: c}
|
||||||
|
var hello struct{ Type string }
|
||||||
|
if err := w.read(ctx, &hello); err != nil || hello.Type != "auth_required" {
|
||||||
|
c.CloseNow()
|
||||||
|
return nil, errors.New("Home Assistant did not greet")
|
||||||
|
}
|
||||||
|
if err := w.write(ctx, map[string]any{"type": "auth", "access_token": token}); err != nil {
|
||||||
|
c.CloseNow()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := w.read(ctx, &hello); err != nil || hello.Type != "auth_ok" {
|
||||||
|
c.CloseNow()
|
||||||
|
return nil, errors.New("Home Assistant did not accept the agent's token") // never the token itself
|
||||||
|
}
|
||||||
|
return w, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *haWS) read(ctx context.Context, v any) error {
|
||||||
|
_, b, err := w.c.Read(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return json.Unmarshal(b, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *haWS) write(ctx context.Context, v any) error {
|
||||||
|
b, _ := json.Marshal(v)
|
||||||
|
return w.c.Write(ctx, websocket.MessageText, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *haWS) close() { w.c.Close(websocket.StatusNormalClosure, "") }
|
||||||
|
|
||||||
|
// call: one command; its result, or HA's error.
|
||||||
|
func (w *haWS) call(ctx context.Context, typ string, args map[string]any) (json.RawMessage, error) {
|
||||||
|
w.id++
|
||||||
|
m := map[string]any{"id": w.id, "type": typ}
|
||||||
|
for k, v := range args {
|
||||||
|
m[k] = v
|
||||||
|
}
|
||||||
|
if err := w.write(ctx, m); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
var r struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Result json.RawMessage `json:"result"`
|
||||||
|
Error struct{ Code, Message string }
|
||||||
|
}
|
||||||
|
if err := w.read(ctx, &r); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if r.ID != w.id || r.Type != "result" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !r.Success {
|
||||||
|
return nil, &haErr{typ, r.Error.Code, r.Error.Message}
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type haErr struct{ cmd, code, msg string }
|
||||||
|
|
||||||
|
func (e *haErr) Error() string { return e.cmd + ": " + e.code + " " + e.msg }
|
||||||
|
|
||||||
|
type haUserRow struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
IsOwner bool `json:"is_owner"`
|
||||||
|
IsActive bool `json:"is_active"`
|
||||||
|
SystemGenerated bool `json:"system_generated"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func haUsers(ctx context.Context, w *haWS) ([]haUserRow, error) {
|
||||||
|
r, err := w.call(ctx, "config/auth/list", nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var us []haUserRow
|
||||||
|
return us, json.Unmarshal(r, &us)
|
||||||
|
}
|
||||||
|
|
||||||
|
// haFindSupport: the support user among HA's users (by its username, else
|
||||||
|
// its name); never the owner or a user HA made itself.
|
||||||
|
func haFindSupport(us []haUserRow) *haUserRow {
|
||||||
|
for i, u := range us {
|
||||||
|
if u.Username == haSupportUsername && !u.IsOwner && !u.SystemGenerated {
|
||||||
|
return &us[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i, u := range us {
|
||||||
|
if u.Name == haSupportName && u.Username == "" && !u.IsOwner && !u.SystemGenerated {
|
||||||
|
return &us[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func randomSecret() string {
|
||||||
|
b := make([]byte, 32)
|
||||||
|
rand.Read(b)
|
||||||
|
return hex.EncodeToString(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// haSignIn: the support user, enabled with a new password, signed in for
|
||||||
|
// the session's origin (HA's client id is the origin, as its own frontend
|
||||||
|
// uses it there). The password is thrown away.
|
||||||
|
func (c haTunnelCfg) haSignIn(ctx context.Context, origin string) (*haUser, *haTokens, error) {
|
||||||
|
tok, err := c.adminToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
w, err := haDial(ctx, c.adminWS, tok)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
defer w.close()
|
||||||
|
us, err := haUsers(ctx, w)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
hu := &haUser{before: map[string]bool{}}
|
||||||
|
for _, u := range us {
|
||||||
|
hu.before[u.ID] = true
|
||||||
|
}
|
||||||
|
su := haFindSupport(us)
|
||||||
|
if su == nil {
|
||||||
|
r, err := w.call(ctx, "config/auth/create", map[string]any{"name": haSupportName,
|
||||||
|
"group_ids": []string{"system-admin"}, "local_only": true})
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
var cr struct{ User haUserRow }
|
||||||
|
if json.Unmarshal(r, &cr) != nil || cr.User.ID == "" {
|
||||||
|
return nil, nil, errors.New("Home Assistant made no support user")
|
||||||
|
}
|
||||||
|
hu.id = cr.User.ID
|
||||||
|
hu.before[hu.id] = true // ours, not one support added
|
||||||
|
} else {
|
||||||
|
hu.id = su.ID
|
||||||
|
if su.Username != "" { // last session's password (normally gone already)
|
||||||
|
if _, err := w.call(ctx, "config/auth_provider/homeassistant/delete", map[string]any{"username": su.Username}); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err := w.call(ctx, "config/auth/update", map[string]any{"user_id": hu.id, "name": haSupportName, "is_active": true,
|
||||||
|
"group_ids": []string{"system-admin"}, "local_only": true}); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
pw := randomSecret()
|
||||||
|
if _, err := w.call(ctx, "config/auth_provider/homeassistant/create", map[string]any{"user_id": hu.id,
|
||||||
|
"username": haSupportUsername, "password": pw}); err != nil {
|
||||||
|
c.haDisable(context.Background(), hu.id)
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
t, err := c.haLogin(ctx, origin, pw)
|
||||||
|
if err != nil {
|
||||||
|
c.haDisable(context.Background(), hu.id)
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
return hu, t, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// haLogin: HA's own login flow (as its sign-in page does it), on the box.
|
||||||
|
func (c haTunnelCfg) haLogin(ctx context.Context, origin, pw string) (*haTokens, error) {
|
||||||
|
return c.haLoginAs(ctx, origin, haSupportUsername, pw)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c haTunnelCfg) haLoginAs(ctx context.Context, origin, username, pw string) (*haTokens, error) {
|
||||||
|
cid := origin + "/"
|
||||||
|
var flow struct {
|
||||||
|
FlowID string `json:"flow_id"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Result string `json:"result"`
|
||||||
|
Errors map[string]string
|
||||||
|
}
|
||||||
|
if err := c.haPost(ctx, "/auth/login_flow", map[string]any{"client_id": cid, "handler": []any{"homeassistant", nil},
|
||||||
|
"redirect_uri": origin + "/?auth_callback=1"}, &flow); err != nil {
|
||||||
|
var ue *url.Error
|
||||||
|
if errors.As(err, &ue) && !ue.Timeout() {
|
||||||
|
// HA's port did not speak plain HTTP: on an HA add-on site most
|
||||||
|
// likely an ssl_certificate in HA's http: configuration (the
|
||||||
|
// tunnel goes to HA's own port on the Supervisor's network).
|
||||||
|
return nil, fmt.Errorf("Home Assistant did not answer over plain HTTP at %s (HTTPS on HA's own port, "+
|
||||||
|
"ssl_certificate in its http: configuration, is not supported for remote access): %w", c.addr, err)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if flow.FlowID == "" {
|
||||||
|
return nil, errors.New("Home Assistant's login flow did not start")
|
||||||
|
}
|
||||||
|
if err := c.haPost(ctx, "/auth/login_flow/"+flow.FlowID, map[string]any{"client_id": cid,
|
||||||
|
"username": username, "password": pw}, &flow); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if flow.Type != "create_entry" || flow.Result == "" {
|
||||||
|
return nil, fmt.Errorf("Home Assistant refused the support user's sign-in (%s %v)", flow.Type, flow.Errors)
|
||||||
|
}
|
||||||
|
form := url.Values{"grant_type": {"authorization_code"}, "code": {flow.Result}, "client_id": {cid}}
|
||||||
|
req, _ := http.NewRequestWithContext(ctx, "POST", c.httpBase+"/auth/token", strings.NewReader(form.Encode()))
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
res, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
var t haTokens
|
||||||
|
if res.StatusCode != 200 || json.NewDecoder(io.LimitReader(res.Body, 64<<10)).Decode(&t) != nil || t.AccessToken == "" || t.RefreshToken == "" {
|
||||||
|
return nil, fmt.Errorf("Home Assistant gave no token (%d)", res.StatusCode)
|
||||||
|
}
|
||||||
|
if t.TokenType == "" {
|
||||||
|
t.TokenType = "Bearer"
|
||||||
|
}
|
||||||
|
return &t, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c haTunnelCfg) haPost(ctx context.Context, path string, body, out any) error {
|
||||||
|
b, _ := json.Marshal(body)
|
||||||
|
req, _ := http.NewRequestWithContext(ctx, "POST", c.httpBase+path, strings.NewReader(string(b)))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
res, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
if res.StatusCode != 200 {
|
||||||
|
return fmt.Errorf("Home Assistant %s: %d", path, res.StatusCode)
|
||||||
|
}
|
||||||
|
return json.NewDecoder(io.LimitReader(res.Body, 64<<10)).Decode(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// haDisable: the support user disabled (HA removes its refresh tokens and
|
||||||
|
// so closes its connections) and its password removed; the users added
|
||||||
|
// since the start (support may have made some: the owner should know).
|
||||||
|
func (c haTunnelCfg) haDisable(ctx context.Context, id string) error {
|
||||||
|
_, err := c.haEnd(ctx, &haUser{id: id})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c haTunnelCfg) haEnd(ctx context.Context, hu *haUser) ([]string, error) {
|
||||||
|
tok, err := c.adminToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
w, err := haDial(ctx, c.adminWS, tok)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer w.close()
|
||||||
|
if _, err := w.call(ctx, "config/auth/update", map[string]any{"user_id": hu.id, "is_active": false}); err != nil {
|
||||||
|
var he *haErr
|
||||||
|
if !errors.As(err, &he) || he.code != "not_found" { // someone removed it: nothing to disable
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
us, err := haUsers(ctx, w)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var added []string
|
||||||
|
for _, u := range us {
|
||||||
|
if u.ID == hu.id && u.Username != "" {
|
||||||
|
w.call(ctx, "config/auth_provider/homeassistant/delete", map[string]any{"username": u.Username})
|
||||||
|
}
|
||||||
|
if hu.before != nil && !hu.before[u.ID] && !u.SystemGenerated {
|
||||||
|
added = append(added, u.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return added, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// haLeftover: at the link's start (a session may have been open when it
|
||||||
|
// stopped): the support user disabled, if HA has one active.
|
||||||
|
func (c haTunnelCfg) haLeftover(ctx context.Context) error {
|
||||||
|
tok, err := c.adminToken()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w, err := haDial(ctx, c.adminWS, tok)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
us, err := haUsers(ctx, w)
|
||||||
|
w.close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if su := haFindSupport(us); su != nil && (su.IsActive || su.Username != "") {
|
||||||
|
return c.haDisable(ctx, su.ID)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// fileToken: a token read from a file each time (a new agent token is
|
||||||
|
// written by ha-setup agent-token).
|
||||||
|
func fileToken(path string) func() (string, error) {
|
||||||
|
return func() (string, error) {
|
||||||
|
b, err := os.ReadFile(path)
|
||||||
|
if t := strings.TrimSpace(string(b)); err == nil && t != "" {
|
||||||
|
return t, nil
|
||||||
|
}
|
||||||
|
return "", errors.New("no Home Assistant token for the agent on this box")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,6 +13,8 @@ package main
|
|||||||
// agent -> link {"op":"plan"|"action","data":{...}} (published, no answer;
|
// agent -> link {"op":"plan"|"action","data":{...}} (published, no answer;
|
||||||
// plans at most every 5 min, actions at most 120 an hour)
|
// plans at most every 5 min, actions at most 120 an hour)
|
||||||
// agent -> link {"op":"inventory","data":{...}} (the devices' specs; published at most every 10 min)
|
// agent -> link {"op":"inventory","data":{...}} (the devices' specs; published at most every 10 min)
|
||||||
|
// agent -> link {"op":"own_tariff","data":{...}} (the owner's own price
|
||||||
|
// from the panel, docs/parameters.md U1; at most every minute)
|
||||||
// agent -> link {"op":"state"} -> {"op":"state","ok":<NATS connected>}
|
// agent -> link {"op":"state"} -> {"op":"state","ok":<NATS connected>}
|
||||||
// link -> agent {"op":"command","id":"<cmd id>","cmd":"...","args":{...}}
|
// link -> agent {"op":"command","id":"<cmd id>","cmd":"...","args":{...}}
|
||||||
// agent -> link {"op":"result","id":"<cmd id>","ok":true,"message":"..."}
|
// agent -> link {"op":"result","id":"<cmd id>","ok":true,"message":"..."}
|
||||||
@@ -42,6 +44,7 @@ type local struct {
|
|||||||
rmu sync.Mutex
|
rmu sync.Mutex
|
||||||
lastPlan time.Time
|
lastPlan time.Time
|
||||||
lastInv time.Time
|
lastInv time.Time
|
||||||
|
lastOwn time.Time
|
||||||
actions []time.Time
|
actions []time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,6 +68,13 @@ func (lc *local) allow(op string, now time.Time) bool {
|
|||||||
lc.lastPlan = now
|
lc.lastPlan = now
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
if op == "own_tariff" {
|
||||||
|
if !lc.lastOwn.IsZero() && now.Sub(lc.lastOwn) < time.Minute {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
lc.lastOwn = now
|
||||||
|
return true
|
||||||
|
}
|
||||||
if op == "inventory" {
|
if op == "inventory" {
|
||||||
if !lc.lastInv.IsZero() && now.Sub(lc.lastInv) < inventoryEvery {
|
if !lc.lastInv.IsZero() && now.Sub(lc.lastInv) < inventoryEvery {
|
||||||
return false
|
return false
|
||||||
@@ -191,7 +201,7 @@ func (lc *local) serve(c net.Conn) {
|
|||||||
if len(m.Data) > 0 {
|
if len(m.Data) > 0 {
|
||||||
lc.l.publish(m.Op, m.Data)
|
lc.l.publish(m.Op, m.Data)
|
||||||
}
|
}
|
||||||
case "plan", "action", "inventory":
|
case "plan", "action", "inventory", "own_tariff":
|
||||||
if len(m.Data) > 0 && lc.allow(m.Op, time.Now()) {
|
if len(m.Data) > 0 && lc.allow(m.Op, time.Now()) {
|
||||||
lc.l.publish(m.Op, m.Data)
|
lc.l.publish(m.Op, m.Data)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -183,7 +183,7 @@ func run() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer l.loc.close()
|
defer l.loc.close()
|
||||||
if isHub() { // the owner's End now in the panel: support's sessions close
|
if _, ok := haConfig(); ok || isHub() { // the owner's End now (the Hub's panel): support's sessions close
|
||||||
usr1 := make(chan os.Signal, 1)
|
usr1 := make(chan os.Signal, 1)
|
||||||
signal.Notify(usr1, syscall.SIGUSR1)
|
signal.Notify(usr1, syscall.SIGUSR1)
|
||||||
go func() {
|
go func() {
|
||||||
@@ -191,8 +191,14 @@ func run() error {
|
|||||||
l.closeAll()
|
l.closeAll()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
writeTermState()
|
if isHub() {
|
||||||
writePanelState()
|
writeTermState()
|
||||||
|
writePanelState()
|
||||||
|
}
|
||||||
|
if !isHub() {
|
||||||
|
writeHAState() // an HA add-on site: none open now (a file left by the last run said otherwise)
|
||||||
|
}
|
||||||
|
haStartup() // remote HA (hatunnel.go): a support user left enabled is disabled
|
||||||
}
|
}
|
||||||
go l.loop(ctx)
|
go l.loop(ctx)
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
@@ -756,6 +762,9 @@ func (l *link) heartbeat() map[string]any {
|
|||||||
if p := l.pathInfo(); p != nil {
|
if p := l.pathInfo(); p != nil {
|
||||||
hb["path"] = p
|
hb["path"] = p
|
||||||
}
|
}
|
||||||
|
if _, ok := haConfig(); ok { // support may open its Home Assistant (hatunnel.go)
|
||||||
|
hb["ha_remote"] = true
|
||||||
|
}
|
||||||
// the bundles this link takes (bundles.go): as large, and as text
|
// the bundles this link takes (bundles.go): as large, and as text
|
||||||
hb["bundle_max"], hb["bundle_text"] = bundleMax, true
|
hb["bundle_max"], hb["bundle_text"] = bundleMax, true
|
||||||
c := consents(env("CANTABILE_CONSENT_FILE",
|
c := consents(env("CANTABILE_CONSENT_FILE",
|
||||||
@@ -882,9 +891,13 @@ func checkCommand(key crypto.PublicKey, site string, data []byte, now time.Time)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *link) onCommand(m *nats.Msg) {
|
func (l *link) onCommand(m *nats.Msg) {
|
||||||
|
var w struct{ T string }
|
||||||
|
json.Unmarshal(m.Data, &w)
|
||||||
|
if w.T == "ha" { // remote HA's tunnel: checked with the session key (hatunnel.go)
|
||||||
|
l.haFrame(m.Data)
|
||||||
|
return
|
||||||
|
}
|
||||||
if isHub() {
|
if isHub() {
|
||||||
var w struct{ T string }
|
|
||||||
json.Unmarshal(m.Data, &w)
|
|
||||||
switch w.T {
|
switch w.T {
|
||||||
case "term": // terminal keystrokes: checked with the session key (terminal.go)
|
case "term": // terminal keystrokes: checked with the session key (terminal.go)
|
||||||
l.termInput(m.Data)
|
l.termInput(m.Data)
|
||||||
@@ -930,6 +943,9 @@ func (l *link) do(c command) (bool, string, func()) {
|
|||||||
switch c.Cmd {
|
switch c.Cmd {
|
||||||
case "site-moved", "restore-offer": // a reinstall merged into its earlier site (reinstall.go)
|
case "site-moved", "restore-offer": // a reinstall merged into its earlier site (reinstall.go)
|
||||||
return l.reinstallCmd(c)
|
return l.reinstallCmd(c)
|
||||||
|
case "ha-open": // remote HA for support (hatunnel.go): the Hub and HA add-on sites that allow it
|
||||||
|
ok, msg := l.haOpen(c)
|
||||||
|
return ok, msg, nil
|
||||||
case "refresh":
|
case "refresh":
|
||||||
l.publish("hb", l.heartbeat())
|
l.publish("hb", l.heartbeat())
|
||||||
return true, "heartbeat sent", nil
|
return true, "heartbeat sent", nil
|
||||||
|
|||||||
@@ -109,6 +109,7 @@ func writeTermState() {
|
|||||||
}
|
}
|
||||||
termsMu.Unlock()
|
termsMu.Unlock()
|
||||||
list = append(list, panelSessions()...) // Cantabile support in the panel (panel.go)
|
list = append(list, panelSessions()...) // Cantabile support in the panel (panel.go)
|
||||||
|
list = append(list, haList()...) // and in Home Assistant (hatunnel.go)
|
||||||
b, _ := json.Marshal(map[string]any{"sessions": list})
|
b, _ := json.Marshal(map[string]any{"sessions": list})
|
||||||
os.WriteFile(runDir+"/agent-terminal.json.tmp", b, 0o644)
|
os.WriteFile(runDir+"/agent-terminal.json.tmp", b, 0o644)
|
||||||
os.Rename(runDir+"/agent-terminal.json.tmp", runDir+"/agent-terminal.json")
|
os.Rename(runDir+"/agent-terminal.json.tmp", runDir+"/agent-terminal.json")
|
||||||
@@ -126,6 +127,7 @@ func (l *link) closeAll() {
|
|||||||
l.termClose(s, "ended by the owner")
|
l.termClose(s, "ended by the owner")
|
||||||
}
|
}
|
||||||
l.closePanels()
|
l.closePanels()
|
||||||
|
haCloseAll("ended by the owner")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *link) termOpen(c command) (bool, string) {
|
func (l *link) termOpen(c command) (bool, string) {
|
||||||
|
|||||||
@@ -79,7 +79,9 @@ def test_model_state_plans_on_the_base_load(tmp_path):
|
|||||||
col = Collector()
|
col = Collector()
|
||||||
watts(col, sensor__load=6400, sensor__hp=4000, sensor__living_temp=15)
|
watts(col, sensor__load=6400, sensor__hp=4000, sensor__living_temp=15)
|
||||||
ctl = [Control("living", "heating", {"climate": "climate.living", "temp": "sensor.living_temp"}, True,
|
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})]
|
{"max_power_w": 4000.0, "comfort_c": 22.0, "setback_c": 22.0,
|
||||||
|
# the installer's room (never the built-in R/C)
|
||||||
|
"r_k_per_kw": 10.0, "c_kwh_per_k": 3.0, "cop": 1.0})]
|
||||||
b = Bundles(str(tmp_path / "none"), SITE)
|
b = Bundles(str(tmp_path / "none"), SITE)
|
||||||
s = model_state(START, ctl, m.inputs, col, b, None, "UTC", None)
|
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"
|
assert s["load_w"] == [2400.0] * len(s["load_w"]) and s["sources"]["load"] == "now"
|
||||||
@@ -136,9 +138,11 @@ def test_bundle_carries_the_gains(tmp_path):
|
|||||||
bad = room_model(tmp_path, gain_w=[1.0] * 5)
|
bad = room_model(tmp_path, gain_w=[1.0] * 5)
|
||||||
assert bad.room_rc("living") is None and "gain_w" in bad.model.error
|
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
|
# 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})
|
hp = Control("living", "heating", {"climate": "climate.living"}, True, {"cop": 3.0, "max_power_w": 2000.0})
|
||||||
b = room_model(tmp_path, gain_w=100.0, solar_m2=2.0, power="electric")
|
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,
|
col = Collector()
|
||||||
|
col.on_entities({"a": {"climate.living": {"s": "heat", "a": {"current_temperature": 20.0}}}})
|
||||||
|
s = model_state(START, [hp], site_mapping(tmp_path, [], []).inputs, col, b, None, "UTC", None,
|
||||||
location={"latitude": 52.5, "longitude": 13.4})
|
location={"latitude": 52.5, "longitude": 13.4})
|
||||||
p = s["devices"][0]["params"]
|
p = s["devices"][0]["params"]
|
||||||
assert (p["internal_gain_w"], p["solar_m2"]) == (300.0, 6.0) and s["sources"]["rooms"]["living"] == "model"
|
assert (p["internal_gain_w"], p["solar_m2"]) == (300.0, 6.0) and s["sources"]["rooms"]["living"] == "model"
|
||||||
|
|||||||
@@ -112,45 +112,51 @@ def test_mapping_v3_refuses(tmp_path, bad, why):
|
|||||||
|
|
||||||
|
|
||||||
# -- bundles ------------------------------------------------------------------
|
# -- bundles ------------------------------------------------------------------
|
||||||
def test_tariff_and_fallbacks(tmp_path):
|
def test_tariff_as_it_is_and_none_without(tmp_path):
|
||||||
b = Bundles(str(tmp_path), SITE, default_price_ct=33)
|
"""docs/parameters.md U1: no default price, no last price held: without
|
||||||
|
a tariff (never, expired, another site's) status none and no prices;
|
||||||
|
a step the tariff has no price for has none (the plan covers the priced
|
||||||
|
steps); an export without a price is unpriced (None), not 0 ct."""
|
||||||
|
b = Bundles(str(tmp_path), SITE)
|
||||||
start = 1_800_000_000 // 900 * 900
|
start = 1_800_000_000 // 900 * 900
|
||||||
p = b.prices(start, 900, 4, start)
|
p = b.prices(start, 900, 4, start)
|
||||||
assert p.import_ct == [33] * 4 and "default" in p.note
|
assert (p.status, p.reason) == ("none", "never") and p.import_ct == [None] * 4 and p.export_ct == [None] * 4
|
||||||
|
assert p.note.startswith("no tariff: price optimisation off (no tariff received yet")
|
||||||
t = {"v": 1, "kind": "tariff", "id": "t1", "site": SITE, "issued": start, "start": start, "step_s": 900,
|
t = {"v": 1, "kind": "tariff", "id": "t1", "site": SITE, "issued": start, "start": start, "step_s": 900,
|
||||||
"steps": 2, "expires": start + 7200, "currency": "EUR", "unit": "ct/kWh",
|
"steps": 2, "expires": start + 7200, "currency": "EUR", "unit": "ct/kWh",
|
||||||
"import": [10, None], "export": [5, 5]}
|
"import": [10, None], "export": [5, 5]}
|
||||||
(tmp_path / "tariff.json").write_text(json.dumps(t))
|
(tmp_path / "tariff.json").write_text(json.dumps(t))
|
||||||
p = b.prices(start, 900, 4, start)
|
p = b.prices(start, 900, 4, start)
|
||||||
assert p.import_ct == [10, 10, 10, 10] and p.export_ct == [5, 5, 0, 0] and "3 of 4" in p.note
|
assert p.import_ct == [10, None, None, None] and p.export_ct == [5, 5, None, None]
|
||||||
p = b.prices(start, 900, 2, start + 7200) # expired: the flat price, said
|
assert p.status == "tariff" and p.priced_steps == 1 and "the plan covers those" in p.note
|
||||||
assert p.import_ct == [10, 10] and "expired" in p.note
|
p = b.prices(start, 900, 2, start + 7200) # expired: no price at all, said with the time
|
||||||
os.remove(tmp_path / "tariff.json")
|
assert (p.status, p.reason, p.tariff_id) == ("none", "expired", "t1") and p.import_ct == [None, None]
|
||||||
assert "last known flat" in b.prices(start, 900, 1, start).note
|
assert "the tariff t1 expired at" in p.note
|
||||||
|
os.remove(tmp_path / "tariff.json") # gone: never a last price held
|
||||||
|
assert b.prices(start, 900, 1, start).reason == "never"
|
||||||
t["site"] = "cd" * 16 # another site's file: not taken
|
t["site"] = "cd" * 16 # another site's file: not taken
|
||||||
(tmp_path / "tariff.json").write_text(json.dumps(t))
|
(tmp_path / "tariff.json").write_text(json.dumps(t))
|
||||||
assert b.prices(start, 900, 1, start).note.startswith("no tariff")
|
assert b.prices(start, 900, 1, start).reason == "other_site"
|
||||||
|
t.update(site=SITE, start=start + 900, id="t10") # no price for now: none
|
||||||
|
(tmp_path / "tariff.json").write_text(json.dumps(t))
|
||||||
|
assert b.prices(start, 900, 2, start).reason == "no_price_now"
|
||||||
|
|
||||||
|
|
||||||
def test_feed_in_at_zero_is_said(tmp_path):
|
def test_export_unpriced_is_said(tmp_path):
|
||||||
"""Finding A1-a: where no export price is known the feed-in is 0 ct, and
|
b = Bundles(str(tmp_path), SITE)
|
||||||
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
|
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,
|
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",
|
"steps": 4, "expires": start + 7200, "currency": "EUR", "unit": "ct/kWh",
|
||||||
"import": [10, 10, 10, 10], "export": [5, 5, 5, 5]}
|
"import": [10, 10, 10, 10], "export": [5, 5, 5, 5]}
|
||||||
(tmp_path / "tariff.json").write_text(json.dumps(t))
|
(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 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]
|
t["export"] = [5, None, None, None]
|
||||||
(tmp_path / "tariff.json").write_text(json.dumps(t))
|
(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
|
p = b.prices(start, 900, 4, start)
|
||||||
|
assert p.export_ct == [5, None, None, None] and "no export price in 3 of 4 steps: export unpriced" in p.note
|
||||||
del t["export"]
|
del t["export"]
|
||||||
(tmp_path / "tariff.json").write_text(json.dumps(t))
|
(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"
|
assert b.prices(start, 900, 4, start).note == "tariff t1; no export price: export unpriced (no value)"
|
||||||
|
|
||||||
|
|
||||||
def test_model_bundle(tmp_path):
|
def test_model_bundle(tmp_path):
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import pytest
|
|||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from cantabile_edge import i18n, propose
|
from cantabile_edge import i18n, propose
|
||||||
from cantabile_edge.devpage import App, Devices
|
from cantabile_edge.devpage import App, Devices, SupportSessions
|
||||||
from cantabile_edge.joinpage import Fleet
|
from cantabile_edge.joinpage import Fleet
|
||||||
from cantabile_edge.mapping import Mapping
|
from cantabile_edge.mapping import Mapping
|
||||||
|
|
||||||
@@ -302,6 +302,77 @@ def test_german(tmp_path):
|
|||||||
assert "Use with Cantabile" not in out
|
assert "Use with Cantabile" not in out
|
||||||
|
|
||||||
|
|
||||||
|
def test_support_session_banner_end_and_owners_choices(tmp_path):
|
||||||
|
"""Remote HA for support on an add-on site: while the link says support
|
||||||
|
has this Home Assistant open, both pages show it with End now (SIGUSR1
|
||||||
|
to the link, only to the link); the support user cannot change what is
|
||||||
|
the owner's (as on the Hub's panel)."""
|
||||||
|
import signal
|
||||||
|
import time
|
||||||
|
s = Site(tmp_path)
|
||||||
|
link, proc, killed = tmp_path / "link", tmp_path / "proc", []
|
||||||
|
link.mkdir()
|
||||||
|
(proc / "4242").mkdir(parents=True)
|
||||||
|
(proc / "4242" / "cmdline").write_bytes(b"cantabile-link\0")
|
||||||
|
(proc / "77").mkdir()
|
||||||
|
(proc / "77" / "cmdline").write_bytes(b"python3\0-m\0cantabile_edge\0")
|
||||||
|
s.app.support = SupportSessions(str(link), proc=str(proc), kill=lambda pid, sig: killed.append((pid, sig)))
|
||||||
|
state = link / "support-ha.json"
|
||||||
|
assert "Cantabile support has this Home Assistant open" not in s.req() # no file: none
|
||||||
|
state.write_text(json.dumps({"pid": 4242, "sessions": []}))
|
||||||
|
assert "Cantabile support has this Home Assistant open" not in s.req()
|
||||||
|
state.write_text(json.dumps({"pid": 4242, "user": "Cantabile support", "sessions": [
|
||||||
|
{"by": "Ana (Cantabile)", "since": int(time.time()) - 300, "consent": "", "kind": "ha"}]}))
|
||||||
|
out = s.req()
|
||||||
|
assert "Cantabile support has this Home Assistant open" in out and "Ana (Cantabile), for 5 minutes" in out
|
||||||
|
assert 'value="end-support"' in out and "End now" in out
|
||||||
|
assert "Cantabile support has this Home Assistant open" in s.req(query="p=building")
|
||||||
|
|
||||||
|
def german():
|
||||||
|
i18n.use("de")
|
||||||
|
return s.req()
|
||||||
|
import contextvars
|
||||||
|
out = contextvars.copy_context().run(german)
|
||||||
|
assert "Der Cantabile-Support hat dieses Home Assistant geöffnet" in out and "seit 5 Minuten" in out
|
||||||
|
assert "Jetzt beenden" in out
|
||||||
|
# the owner's End now (any administrator): the link gets SIGUSR1
|
||||||
|
out = s.req("POST", "p=devices", {"do": "end-support"})
|
||||||
|
assert "Ended. Cantabile support's session is closed" in out and killed == [(4242, signal.SIGUSR1)]
|
||||||
|
out = s.req("POST", "p=building", {"do": "end-support"})
|
||||||
|
assert "Ended." in out and "<nav>" in out and len(killed) == 2
|
||||||
|
# never another process: a pid that is not the link
|
||||||
|
state.write_text(json.dumps({"pid": 77, "sessions": [{"by": "Ana", "since": 1, "kind": "ha"}]}))
|
||||||
|
assert "No session to end" in s.req("POST", "p=devices", {"do": "end-support"}) and len(killed) == 2
|
||||||
|
state.write_text(json.dumps({"pid": 4242, "sessions": [{"by": "Ana", "since": 1, "kind": "ha"}]}))
|
||||||
|
# the support user (an HA administrator) may look and add, not decide what is the owner's
|
||||||
|
s.give_consent(share=True, control=True)
|
||||||
|
s.post(do="use", ha_device=AC, profile="ha-thermostat")
|
||||||
|
dev_id = next(iter(s.devices()))
|
||||||
|
sup = {"x-remote-user-id": "u-support", "x-remote-user-name": "cantabile-support",
|
||||||
|
"x-remote-user-display-name": "Cantabile support"}
|
||||||
|
s.ha.admins.add("u-support")
|
||||||
|
ingress = "172.30.32.2"
|
||||||
|
frag = s.tmp / "mapping.d" / f"{dev_id}.yaml"
|
||||||
|
for form, why in (({"do": "consent", "id": dev_id, "read": "1", "control": "1"}, "Reading and control are the owner"),
|
||||||
|
({"do": "protect", "id": dev_id, "target": f"device:{dev_id}", "level": "none"}, "Protection is the owner"),
|
||||||
|
({"do": "switchover", "id": dev_id, "mode": "cooling"}, "Heating or cooling is the owner"),
|
||||||
|
({"do": "weather", "id": dev_id}, "The weather forecast is the owner")):
|
||||||
|
before = (s.state / "devices.json").read_text(), frag.read_text()
|
||||||
|
out = s.req("POST", f"p=devices&d={dev_id}", form, sup, ingress)
|
||||||
|
assert why in out and "Cantabile support has this Home Assistant open" in out, form
|
||||||
|
assert before == ((s.state / "devices.json").read_text(), frag.read_text()), form
|
||||||
|
# the owner still can
|
||||||
|
s.req("POST", f"p=devices&d={dev_id}", {"do": "consent", "id": dev_id, "read": "1", "control": "0"})
|
||||||
|
assert s.devices()[dev_id] != json.loads(before[0])[dev_id]
|
||||||
|
# known by its id too while a session is open (the link names it)
|
||||||
|
state.write_text(json.dumps({"pid": 4242, "sessions": [{"by": "Ana", "since": 1, "kind": "ha", "ha_user": "u-support"}]}))
|
||||||
|
out = s.req("POST", f"p=devices&d={dev_id}", {"do": "protect", "id": dev_id, "target": f"device:{dev_id}", "level": "none"},
|
||||||
|
{"x-remote-user-id": "u-support"}, ingress)
|
||||||
|
assert "Protection is the owner" in out
|
||||||
|
# and End now is theirs too
|
||||||
|
assert "Ended." in s.req("POST", "p=devices", {"do": "end-support"}, sup, ingress)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def english():
|
def english():
|
||||||
i18n.use("en")
|
i18n.use("en")
|
||||||
@@ -0,0 +1,212 @@
|
|||||||
|
# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
|
||||||
|
"""Heat by its source (docs/methods.md E6): a boiler priced by gas ÷ its
|
||||||
|
efficiency, district heat by the heat price, a hybrid choosing heat pump or
|
||||||
|
boiler per quarter-hour; refused where a price or a fact is missing."""
|
||||||
|
import json
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from cantabile_edge import bundles as bundles_mod
|
||||||
|
from cantabile_edge import controls, executor, inputs, steady
|
||||||
|
|
||||||
|
highspy = pytest.importorskip("highspy")
|
||||||
|
from cantabile_edge import optimizer # noqa: E402
|
||||||
|
|
||||||
|
START = 1793232000 # 2026-10-29 00:00 UTC
|
||||||
|
|
||||||
|
|
||||||
|
def house(devices, steps=96, imp=30.0, gas=None, heat=None):
|
||||||
|
hour = lambda k: (k // 4 + 1) % 24 # noqa: E731
|
||||||
|
imp = imp if isinstance(imp, list) else [imp] * steps
|
||||||
|
return {"v": 1, "start": START, "step_s": 900, "steps": steps, "tz": "Europe/Berlin",
|
||||||
|
"price_import": imp, "price_export": [8.0] * steps, "price_note": "test",
|
||||||
|
"price_gas": gas if isinstance(gas, list) else [gas] * steps,
|
||||||
|
"price_heat": heat if isinstance(heat, list) else [heat] * steps,
|
||||||
|
"params": {"heat.hybrid_switch_eur": 0.05}, # the Parameters page's seed (fleet, via the bundle)
|
||||||
|
"load_w": [400.0] * steps, "pv_w": [0.0] * steps, "t_out": [0.0] * steps,
|
||||||
|
"sun": [0.0] * steps, "occupied": [1 if 6 <= hour(k) < 22 else 0 for k in range(steps)],
|
||||||
|
"devices": devices}
|
||||||
|
|
||||||
|
|
||||||
|
def room(src, temp=19.0, **extra):
|
||||||
|
p = {"r_k_per_kw": 10, "c_kwh_per_k": 3, "max_power_w": 3000, "comfort_c": 21, "setback_c": 17, "cop": 3.0,
|
||||||
|
"heat_source": src, "source_options": {}, "cop_curve": [], **extra}
|
||||||
|
return {"id": "heating", "kind": "heating", "channels": ["climate"] + (["source"] if src == "hybrid" else []),
|
||||||
|
"params": p, "state": {"temp_c": temp}, "bounds": {"temp_c": [16, 26, "owner", "owner"]}}
|
||||||
|
|
||||||
|
|
||||||
|
def test_boiler_is_priced_by_gas_and_uses_no_electricity():
|
||||||
|
s = house([room("boiler", boiler_efficiency=0.9, max_heat_w=9000)], gas=12.0)
|
||||||
|
p = optimizer.plan(s, 20)
|
||||||
|
assert p["gas_eur"] > 0 and "heat_eur" in p
|
||||||
|
m = optimizer.Model(optimizer._prepare(s))
|
||||||
|
m.solve(20)
|
||||||
|
val = m.h.getSolution().col_value
|
||||||
|
v = m.devices["heating"][1]
|
||||||
|
assert all(e is None for e in v["power"]) # no electric input
|
||||||
|
gas_wh = sum(val[g.index] for g in v["gas"]) * 0.25
|
||||||
|
assert gas_wh > 0
|
||||||
|
# the plan's gas cost is its gas at the gas price
|
||||||
|
assert abs(p["gas_eur"] - round(gas_wh / 1000 * 0.12, 2)) < 0.02
|
||||||
|
# the electricity bill is the house alone (400 W at 30 ct for 24 h = 2.88 EUR)
|
||||||
|
assert abs(p["cost_eur"] - p["gas_eur"] - 2.88) < 0.02
|
||||||
|
|
||||||
|
|
||||||
|
def test_district_heat_by_the_heat_price():
|
||||||
|
p = optimizer.plan(house([room("district", max_heat_w=6000)], heat=11.0), 20)
|
||||||
|
assert p["heat_eur"] > 0 and p.get("gas_eur", 0) == 0
|
||||||
|
assert any(isinstance(e["set"], float) for e in p["devices"]["heating"])
|
||||||
|
|
||||||
|
|
||||||
|
def hybrid_plan(imp, gas):
|
||||||
|
s = house([room("hybrid", boiler_efficiency=0.9, max_heat_w=9000)], imp=imp, gas=gas)
|
||||||
|
return optimizer.plan(s, 20)
|
||||||
|
|
||||||
|
|
||||||
|
def test_hybrid_chooses_the_cheaper_heat():
|
||||||
|
# heat pump: 30 ct / COP 3 = 10 ct per kWh of heat; boiler: 12 ct / 0.9 = 13.3 ct
|
||||||
|
p = hybrid_plan(30.0, 12.0)
|
||||||
|
srcs = {e.get("source") for e in p["devices"]["heating"] if e.get("source")}
|
||||||
|
assert srcs == {"heat_pump"}, p["devices"]["heating"]
|
||||||
|
assert p.get("gas_eur", 0) == 0
|
||||||
|
# dear electricity: 60 / 3 = 20 ct against 13.3 ct: the boiler
|
||||||
|
p = hybrid_plan(60.0, 12.0)
|
||||||
|
srcs = {e.get("source") for e in p["devices"]["heating"] if e.get("source")}
|
||||||
|
assert srcs == {"boiler"}, p["devices"]["heating"]
|
||||||
|
assert p["gas_eur"] > 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_hybrid_switches_with_a_dynamic_price():
|
||||||
|
# cheap power for the first 12 h, dear after: the heat pump first, then the boiler
|
||||||
|
imp = [15.0] * 48 + [70.0] * 48
|
||||||
|
p = hybrid_plan(imp, 12.0)
|
||||||
|
first = [e.get("source") for e in p["devices"]["heating"] if e["t"] < START + 12 * 3600 and e.get("source")]
|
||||||
|
later = [e.get("source") for e in p["devices"]["heating"] if e["t"] >= START + 12 * 3600 and e.get("source")]
|
||||||
|
assert "heat_pump" in first and "boiler" in later, p["devices"]["heating"]
|
||||||
|
# few switches: one each way at most within a price regime
|
||||||
|
changes = sum(1 for a, b in zip(first + later, (first + later)[1:]) if a != b)
|
||||||
|
assert changes <= 3
|
||||||
|
|
||||||
|
|
||||||
|
def test_cop_curve_by_outside_temperature():
|
||||||
|
p = {"cop": 3.0, "cop_curve": [[-7, 2.0], [2, 3.0], [7, 4.0]]}
|
||||||
|
assert controls.cop_at(p, -10) == 2.0 and controls.cop_at(p, 2) == 3.0 and controls.cop_at(p, 4.5) == 3.5
|
||||||
|
assert controls.cop_at({"cop": 2.5, "cop_curve": []}, 0) == 2.5
|
||||||
|
|
||||||
|
|
||||||
|
def parse(params, ents=None):
|
||||||
|
ents = ents or {"climate": "climate.flat"}
|
||||||
|
return controls.parse([{"id": "heating", "kind": "heating", "entities": ents, "params": params}], set())[0]
|
||||||
|
|
||||||
|
|
||||||
|
def test_mapping_checks_the_heat_source():
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
parse({"heat_source": "wood"})
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
parse({"heat_source": "hybrid"}) # no source select, no options
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
parse({"boiler_efficiency": 1.2}) # above 1 (Hs)
|
||||||
|
c = parse({"heat_source": "hybrid", "source_options": {"heat_pump": "Heat pump", "boiler": "Boiler"}},
|
||||||
|
{"climate": "climate.flat", "source": "select.flat_source"})
|
||||||
|
assert c.p("heat_source") == "hybrid"
|
||||||
|
|
||||||
|
|
||||||
|
def test_heat_facts_mapping_device_owner_never_a_default():
|
||||||
|
c = parse({"heat_source": "boiler"})
|
||||||
|
facts, missing, _ = c.heat_facts()
|
||||||
|
assert facts == {} and set(missing) == {"boiler_efficiency", "max_heat_w"}
|
||||||
|
c.reported = {"efficiency_pct": 91.0}
|
||||||
|
c.rated = {"heat_kw": 12}
|
||||||
|
facts, missing, src = c.heat_facts()
|
||||||
|
assert facts == {"boiler_efficiency": 0.91, "max_heat_w": 12000.0} and not missing
|
||||||
|
assert src == {"boiler_efficiency": "device", "max_heat_w": "owner"}
|
||||||
|
c = parse({"heat_source": "boiler", "boiler_efficiency": 0.88, "max_heat_w": 9000})
|
||||||
|
assert c.heat_facts()[0] == {"boiler_efficiency": 0.88, "max_heat_w": 9000.0}
|
||||||
|
# a heat pump needs neither
|
||||||
|
assert parse({"heat_source": "heat_pump"}).heat_facts() == ({}, [], {})
|
||||||
|
|
||||||
|
|
||||||
|
def test_refused_without_efficiency_or_price():
|
||||||
|
c = parse({"heat_source": "boiler", "max_heat_w": 9000})
|
||||||
|
no = inputs.heating_refusal(c, {"heat_source": "boiler"}, [12.0], [None], "tariff")
|
||||||
|
assert no and "efficiency" in no["why"] and "boiler_efficiency" in no["missing"] and "Ratings" in no["where"]
|
||||||
|
c = parse({"heat_source": "boiler", "boiler_efficiency": 0.9, "max_heat_w": 9000})
|
||||||
|
assert inputs.heating_refusal(c, {"heat_source": "boiler"}, [12.0], [None], "tariff") is None
|
||||||
|
no = inputs.heating_refusal(c, {"heat_source": "boiler"}, [None], [None], "tariff")
|
||||||
|
assert no and "no gas price" in no["why"] and "gas tariff" in no["where"]
|
||||||
|
d = parse({"heat_source": "district", "max_heat_w": 6000})
|
||||||
|
no = inputs.heating_refusal(d, {"heat_source": "district"}, [None], [None], "tariff")
|
||||||
|
assert no and "no heat price" in no["why"] and "building" in no["where"]
|
||||||
|
h = parse({"heat_source": "hybrid", "boiler_efficiency": 0.9, "max_heat_w": 9000, "cop_curve": [[-7, 2.5], [7, 3.5]],
|
||||||
|
"source_options": {"heat_pump": "HP", "boiler": "Gas"}}, {"climate": "climate.f", "source": "select.s"})
|
||||||
|
bp = {"heat.hybrid_switch_eur": 0.05}
|
||||||
|
no = inputs.heating_refusal(h, {"heat_source": "hybrid"}, [12.0], [None], "none", bp)
|
||||||
|
assert no and "electricity price" in no["why"]
|
||||||
|
assert inputs.heating_refusal(h, {"heat_source": "hybrid"}, [12.0], [None], "tariff", bp) is None
|
||||||
|
# the Parameters page's switch cost not received (no bundle, or unset): refused, never a constant
|
||||||
|
no = inputs.heating_refusal(h, {"heat_source": "hybrid"}, [12.0], [None], "tariff", {})
|
||||||
|
assert no and "heat.hybrid_switch_eur" in no["why"] and "Parameters page" in no["where"]
|
||||||
|
# a heat pump's COP is its datasheet's: no default
|
||||||
|
hp = parse({"heat_source": "heat_pump"})
|
||||||
|
no = inputs.heating_refusal(hp, {"heat_source": "heat_pump"}, [None], [None], "tariff", bp)
|
||||||
|
assert no and "COP unknown" in no["why"]
|
||||||
|
assert inputs.heating_refusal(parse({"heat_source": "heat_pump", "cop": 3.2}), {"heat_source": "heat_pump"},
|
||||||
|
[None], [None], "tariff", bp) is None
|
||||||
|
assert inputs.heating_refusal(parse({}), {"heat_source": "electric"}, [None], [None], "none") is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_bundle_gas_heat_series(tmp_path):
|
||||||
|
site = "s1"
|
||||||
|
b = {"v": 1, "kind": "tariff", "id": "t1", "site": site, "issued": START, "start": START, "step_s": 900, "steps": 192,
|
||||||
|
"expires": START + 192 * 900, "currency": "EUR", "unit": "ct/kWh", "import": [None] * 192, "export": [None] * 192,
|
||||||
|
"gas": [12.7] * 100 + [None] * 92, "heat": [None] * 192, "estimated": [], "other": {"gas": 12.7}, "sources": []}
|
||||||
|
(tmp_path / "tariff.json").write_text(json.dumps(b))
|
||||||
|
bd = bundles_mod.Bundles(str(tmp_path), site)
|
||||||
|
g = bd.carrier_prices("gas", START, 900, 192, START + 10)
|
||||||
|
assert g[0] == 12.7 and g[99] == 12.7 and g[100] is None
|
||||||
|
assert bd.carrier_prices("heat", START, 900, 192, START + 10) == [None] * 192
|
||||||
|
assert bd.prices(START, 900, 192, START + 10).status == "none" # gas alone: electricity unpriced
|
||||||
|
assert bd.carrier_prices("gas", START, 900, 192, START + 192 * 900) == [None] * 192 # expired
|
||||||
|
assert bd.box_params(START + 10) == {}
|
||||||
|
b["params"] = {"heat.hybrid_switch_eur": 0.2}
|
||||||
|
(tmp_path / "tariff.json").write_text(json.dumps(b))
|
||||||
|
bd = bundles_mod.Bundles(str(tmp_path), site)
|
||||||
|
assert bd.box_params(START + 10) == {"heat.hybrid_switch_eur": 0.2}
|
||||||
|
assert bd.box_params(START + 192 * 900) == {}
|
||||||
|
|
||||||
|
|
||||||
|
class Col:
|
||||||
|
def __init__(self, states):
|
||||||
|
self.s = states
|
||||||
|
|
||||||
|
def state(self, e):
|
||||||
|
return self.s.get(e)
|
||||||
|
|
||||||
|
def attr(self, e, a):
|
||||||
|
return None
|
||||||
|
|
||||||
|
def value(self, e):
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def test_executor_sets_the_hybrid_source():
|
||||||
|
c = parse({"heat_source": "hybrid", "source_options": {"heat_pump": "Heat pump", "boiler": "Gas boiler"}},
|
||||||
|
{"climate": "climate.f", "source": "select.s"})
|
||||||
|
acts = executor.actions_for(c, {"set": 21.0, "source": "boiler"}, Col({"climate.f": "heat", "select.s": "Heat pump"}))
|
||||||
|
sel = [a for a in acts if a.channel == "source"]
|
||||||
|
assert len(sel) == 1 and sel[0].service == "select_option" and sel[0].value == "Gas boiler"
|
||||||
|
acts = executor.actions_for(c, {"set": 21.0, "source": "boiler"}, Col({"climate.f": "heat", "select.s": "Gas boiler"}))
|
||||||
|
assert not [a for a in acts if a.channel == "source"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_steady_holds_the_source():
|
||||||
|
c = parse({"heat_source": "hybrid", "source_options": {"heat_pump": "Heat pump", "boiler": "Gas boiler"}},
|
||||||
|
{"climate": "climate.f", "source": "select.s"})
|
||||||
|
col = Col({"select.s": "Heat pump"})
|
||||||
|
now = START
|
||||||
|
entries = [{"t": now, "set": 21.0, "source": "heat_pump"}, {"t": now + 900, "set": 21.0, "source": "boiler"},
|
||||||
|
{"t": now + 7200, "set": 21.0, "source": "boiler"}]
|
||||||
|
# the agent switched it 10 minutes ago: the boiler waits until the 30 minutes' hold is over
|
||||||
|
steady._hold_source(c, entries, col, 1800.0, lambda cid, k: now - 600 if k == "source" else None, now)
|
||||||
|
assert entries[1]["source"] == "heat_pump" and "source kept" in entries[1]["why"]
|
||||||
|
assert entries[2]["source"] == "boiler"
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
|
# 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
|
"""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
|
that holds 23.5 °C for hours or a full battery is not a stopped sensor (the
|
||||||
clone, 2026-10-30); unavailable is."""
|
clone, 2026-10-30); unavailable is. A battery without its state of charge
|
||||||
|
is not planned (inputs.battery_refusal, docs/parameters.md U2): it does not
|
||||||
|
make the whole plan stale."""
|
||||||
from types import SimpleNamespace
|
from types import SimpleNamespace
|
||||||
|
|
||||||
from cantabile_edge.controller import Controller
|
from cantabile_edge.controller import Controller
|
||||||
@@ -22,4 +24,4 @@ def test_steady_values_are_fresh():
|
|||||||
|
|
||||||
def test_unavailable_is_not():
|
def test_unavailable_is_not():
|
||||||
assert not fresh({"sensor.room": "unavailable", "sensor.soc": "100"})
|
assert not fresh({"sensor.room": "unavailable", "sensor.soc": "100"})
|
||||||
assert not fresh({"sensor.room": "23.5"})
|
assert fresh({"sensor.room": "23.5"}) # the SoC: the battery alone is refused
|
||||||
@@ -12,7 +12,7 @@ import pytest
|
|||||||
from cantabile_edge.bundles import Bundles
|
from cantabile_edge.bundles import Bundles
|
||||||
from cantabile_edge.config import Config
|
from cantabile_edge.config import Config
|
||||||
from cantabile_edge.controller import Controller, write_cell
|
from cantabile_edge.controller import Controller, write_cell
|
||||||
from cantabile_edge.controls import PARAMS, Control
|
from cantabile_edge.controls import Control
|
||||||
from cantabile_edge.inputs import Collector, model_state
|
from cantabile_edge.inputs import Collector, model_state
|
||||||
from cantabile_edge.mapping import Mapping
|
from cantabile_edge.mapping import Mapping
|
||||||
|
|
||||||
@@ -131,44 +131,57 @@ INPUTS = SimpleNamespace(load_power="", outside_temp="", weather="", sun="", pre
|
|||||||
|
|
||||||
|
|
||||||
def heating(cid, **params):
|
def heating(cid, **params):
|
||||||
return Control(cid, "heating", {"climate": f"climate.{cid}"}, True, params)
|
# the heater's power is the installer's (never a default)
|
||||||
|
return Control(cid, "heating", {"climate": f"climate.{cid}"}, True, {"max_power_w": 2000.0, **params})
|
||||||
|
|
||||||
|
|
||||||
def state(b, controls):
|
def warm(*ids):
|
||||||
s = model_state(START, controls, INPUTS, Collector(), b, None, "UTC", None)
|
"""A collector where each climate reports its room at 20 °C."""
|
||||||
return {d["id"]: d["params"] for d in s["devices"]}, s["sources"]
|
col = Collector()
|
||||||
|
col.on_entities({"a": {f"climate.{i}": {"s": "heat", "a": {"current_temperature": 20.0}} for i in ids}})
|
||||||
|
return col
|
||||||
|
|
||||||
|
|
||||||
|
def state(b, controls, full=False):
|
||||||
|
s = model_state(START, controls, INPUTS, warm(*[c.id for c in controls]), b, None, "UTC", None)
|
||||||
|
p = {d["id"]: d["params"] for d in s["devices"]}
|
||||||
|
return (p, s["sources"], s) if full else (p, s["sources"])
|
||||||
|
|
||||||
|
|
||||||
def test_model_state_takes_the_rooms(tmp_path):
|
def test_model_state_takes_the_rooms(tmp_path):
|
||||||
rooms = {"living_room": {"r_k_per_kw": 8, "c_kwh_per_k": 4},
|
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"}}
|
"bedroom": {"r_k_per_kw": 6, "c_kwh_per_k": 2, "power": "electric"}}
|
||||||
b = put(tmp_path, model(rooms=rooms))
|
b = put(tmp_path, model(rooms=rooms))
|
||||||
controls = [heating("living_room_heating"),
|
controls = [heating("living_room_heating", cop=1.0), # a fit against the heat: the COP needed
|
||||||
heating("bedroom", cop=3.0), # a heat pump, fitted on electric power
|
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("office", r_k_per_kw=15.0, c_kwh_per_k=2.5, cop=1.0), # the mapping's own: wins
|
||||||
heating("bath")] # no room: defaults
|
heating("bath")] # no room: not planned
|
||||||
p, src = state(b, controls)
|
p, src, s = state(b, controls, full=True)
|
||||||
assert (p["living_room_heating"]["r_k_per_kw"], p["living_room_heating"]["c_kwh_per_k"]) == (8.0, 4.0)
|
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"]
|
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 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["office"]["r_k_per_kw"], p["office"]["c_kwh_per_k"]) == (15.0, 2.5)
|
||||||
assert p["bath"]["r_k_per_kw"] == PARAMS["heating"]["r_k_per_kw"]
|
assert "bath" not in p and [d["id"] for d in s["fallback"]] == ["bath"]
|
||||||
assert src["rooms"] == {"living_room_heating": "model", "bedroom": "model", "office": "mapping", "bath": "defaults"}
|
assert s["refused"]["bath"]["missing"] == ["r_k_per_kw", "c_kwh_per_k"]
|
||||||
|
assert src["rooms"] == {"living_room_heating": "model", "bedroom": "model", "office": "mapping", "bath": "none"}
|
||||||
assert src["model"] == "m1"
|
assert src["model"] == "m1"
|
||||||
# the mapping's C alone also wins over the model
|
# the mapping's C alone: refused (never the built-in R beside it), the model not mixed in
|
||||||
p, src = state(b, [heating("living_room", c_kwh_per_k=7.0)])
|
p, src, s = state(b, [heating("living_room", c_kwh_per_k=7.0, cop=1.0)], full=True)
|
||||||
assert p["living_room"]["r_k_per_kw"] == PARAMS["heating"]["r_k_per_kw"] and src["rooms"]["living_room"] == "mapping"
|
assert p == {} and src["rooms"]["living_room"] == "partial"
|
||||||
|
assert s["refused"]["living_room"]["missing"] == ["r_k_per_kw"]
|
||||||
|
assert "the mapping sets c_kwh_per_k but not r_k_per_kw" in s["refused"]["living_room"]["why"]
|
||||||
|
|
||||||
|
|
||||||
def test_model_state_only_room_and_no_model(tmp_path):
|
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"}}))
|
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
|
p, src = state(b, [heating("boiler")]) # an electric heater: COP 1 (physics)
|
||||||
assert (p["boiler"]["r_k_per_kw"], p["boiler"]["c_kwh_per_k"]) == (9.0, 5.0) and src["rooms"] == {"boiler": "model"}
|
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, {})
|
assert src["rooms_cop"] == {"boiler": "electric heater"} and p["boiler"]["cop"] == 1.0
|
||||||
|
cool = Control("ac", "cooling", {"climate": "climate.ac"}, True, {"max_power_w": 1500.0})
|
||||||
p, src = state(b, [heating("boiler"), cool]) # two: no guess
|
p, src = state(b, [heating("boiler"), cool]) # two: no guess
|
||||||
assert src["rooms"] == {"boiler": "defaults", "ac": "defaults"}
|
assert src["rooms"] == {"boiler": "none", "ac": "none"} and p == {}
|
||||||
p, src = state(Bundles(str(tmp_path / "none"), SITE), [heating("boiler")])
|
p, src = state(Bundles(str(tmp_path / "none"), SITE), [heating("boiler")])
|
||||||
assert src["rooms"] == {"boiler": "defaults"} and src["model"] == ""
|
assert src["rooms"] == {"boiler": "none"} and src["model"] == "" and p == {}
|
||||||
|
|
||||||
|
|
||||||
# -- the weather cell -----------------------------------------------------------
|
# -- the weather cell -----------------------------------------------------------
|
||||||
|
|||||||
@@ -0,0 +1,147 @@
|
|||||||
|
# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
|
||||||
|
"""A gas boiler's and a hybrid's flat with a shade, planned on a tariff
|
||||||
|
bundle as the cloud sends it (two simulated flats,
|
||||||
|
2026-11-05): import, export and gas for 192 steps from the bundle's start,
|
||||||
|
no district heat price (heat: 192 x None), the hybrid's switch cost among
|
||||||
|
its params. A quarter hour after the bundle's start the plan's 48 h reach
|
||||||
|
past its last price: those steps have none (None, bundles.prices). The
|
||||||
|
shade's "dear hours" summed them and every plan of the flat failed
|
||||||
|
("unsupported operand type(s) for +: 'float' and 'NoneType'"): on any flat
|
||||||
|
with a shade, electric ones too (sim-munich-1, sim-altona-1), not the gas
|
||||||
|
ones only; the dear hours are judged on the priced steps now, and a plan
|
||||||
|
is made. A failed plan is logged with its traceback (cli._Json: "exc")."""
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from cantabile_edge import controller as controller_mod
|
||||||
|
from cantabile_edge.cli import _Json
|
||||||
|
from cantabile_edge.config import Config
|
||||||
|
from cantabile_edge.consent import Consent
|
||||||
|
from cantabile_edge.controller import Controller
|
||||||
|
from cantabile_edge.mapping import Mapping
|
||||||
|
|
||||||
|
highspy = pytest.importorskip("highspy")
|
||||||
|
from cantabile_edge import optimizer # noqa: E402
|
||||||
|
|
||||||
|
SITE = "ab" * 16
|
||||||
|
START = 1793865600 # the bundle's start (2026-11-05 06:00 UTC)
|
||||||
|
NOW = START + 3600 + 30 # an hour later: the horizon's last 4 steps unpriced
|
||||||
|
CAPITAL = {"latitude": 52.37, "longitude": 9.73}
|
||||||
|
|
||||||
|
# the room's R/C (the mapping's: a fitted room's alike) and, for the hybrid's heat pump, its
|
||||||
|
# electric power: without them the room is not planned (room_refusal)
|
||||||
|
ROOM = {"r_k_per_kw": 10, "c_kwh_per_k": 3}
|
||||||
|
|
||||||
|
HEATING = {
|
||||||
|
"boiler": {"id": "heating", "kind": "heating", "consent": True,
|
||||||
|
"entities": {"climate": "climate.boiler", "temp": "sensor.living_temperature"},
|
||||||
|
"params": {"heat_source": "boiler", "boiler_efficiency": 0.9, "max_heat_w": 12000,
|
||||||
|
"comfort_c": 21, "setback_c": 18, **ROOM}},
|
||||||
|
"hybrid": {"id": "heating", "kind": "heating", "consent": True,
|
||||||
|
"entities": {"climate": "climate.hybrid", "temp": "sensor.living_temperature",
|
||||||
|
"source": "select.hybrid_source"},
|
||||||
|
"params": {"heat_source": "hybrid", "boiler_efficiency": 0.9, "max_heat_w": 12000,
|
||||||
|
"cop_curve": [[-7, 2.5], [7, 3.5]],
|
||||||
|
"source_options": {"heat_pump": "Heat pump", "boiler": "Gas boiler"},
|
||||||
|
"max_power_w": 3000, "comfort_c": 21, "setback_c": 18, **ROOM}},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Sink:
|
||||||
|
def __init__(self):
|
||||||
|
self.published = []
|
||||||
|
|
||||||
|
async def publish(self, kind, data):
|
||||||
|
self.published.append((kind, data))
|
||||||
|
|
||||||
|
|
||||||
|
def flat(tmp_path, src, drop=()):
|
||||||
|
heating = dict(HEATING[src], params={k: v for k, v in HEATING[src]["params"].items() if k not in drop})
|
||||||
|
m = {"version": 3, "assets": [],
|
||||||
|
"inputs": {"load_power": "sensor.household_power"},
|
||||||
|
"controls": [heating,
|
||||||
|
{"id": "shades_living", "kind": "cover", "consent": True,
|
||||||
|
"entities": {"cover": "cover.shades_living"}, "params": {"facing": 180}}]}
|
||||||
|
(tmp_path / "mapping.yaml").write_text(yaml.safe_dump(m))
|
||||||
|
bundle = {"v": 1, "kind": "tariff", "id": "t1", "site": SITE, "issued": START, "start": START, "step_s": 900,
|
||||||
|
"steps": 192, "expires": START + 192 * 900, "currency": "EUR", "unit": "ct/kWh",
|
||||||
|
"import": [32.0] * 192, "export": [8.0] * 192, "gas": [12.7] * 192, "heat": [None] * 192,
|
||||||
|
"estimated": [], "other": {}, "sources": [], "params": {"heat.hybrid_switch_eur": 0.05}}
|
||||||
|
(tmp_path / "tariff.json").write_text(json.dumps(bundle))
|
||||||
|
cfg = Config(site_id=SITE, mapping=str(tmp_path / "mapping.yaml"), state_dir=str(tmp_path), dashboards=False,
|
||||||
|
train=False)
|
||||||
|
ctl = Controller(cfg, Mapping.load(cfg.mapping), Consent(cfg.state_dir), Sink(), clock=lambda: NOW)
|
||||||
|
ctl.tz, ctl.location = "Europe/Berlin", CAPITAL
|
||||||
|
ctl.col.on_entities({"a": {
|
||||||
|
"sensor.household_power": {"s": "450", "a": {"unit_of_measurement": "W"}},
|
||||||
|
"sensor.living_temperature": {"s": "20.1", "a": {"unit_of_measurement": "°C"}},
|
||||||
|
"climate.boiler": {"s": "heat", "a": {"temperature": 21, "current_temperature": 20.1}},
|
||||||
|
"climate.hybrid": {"s": "heat", "a": {"temperature": 21, "current_temperature": 20.1}},
|
||||||
|
"select.hybrid_source": {"s": "Heat pump", "a": {"options": ["Heat pump", "Gas boiler"]}},
|
||||||
|
"cover.shades_living": {"s": "open", "a": {"current_position": 100, "supported_features": 15}}}}, NOW)
|
||||||
|
return ctl
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("src", ["boiler", "hybrid"])
|
||||||
|
def test_model_state_past_the_tariffs_end(tmp_path, src):
|
||||||
|
ctl = flat(tmp_path, src)
|
||||||
|
s = ctl.model_state()
|
||||||
|
assert s["price_import"][0] == 32.0 and s["price_import"][-1] is None # the horizon's tail unpriced
|
||||||
|
assert s["price_heat"] == [None] * 192 and s["price_gas"][0] == 12.7
|
||||||
|
assert s["refused"] == {} # the heating is planned
|
||||||
|
shade = next(d for d in s["devices"] if d["id"] == "shades_living")
|
||||||
|
assert len(shade["schedule"]["set"]) == s["steps"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("src", ["boiler", "hybrid"])
|
||||||
|
async def test_without_the_rooms_rc_refused_with_its_reason_the_rest_planned(tmp_path, monkeypatch, src):
|
||||||
|
# no R/C (no fit yet, none in the mapping): the room is refused and says why, never a default R/C;
|
||||||
|
# the shade is planned all the same
|
||||||
|
ctl = flat(tmp_path, src, drop=("r_k_per_kw", "c_kwh_per_k"))
|
||||||
|
s = ctl.model_state()
|
||||||
|
no = s["refused"]["heating"]
|
||||||
|
assert "no R/C fit" in no["why"] and {"r_k_per_kw", "c_kwh_per_k"} <= set(no["missing"])
|
||||||
|
|
||||||
|
async def solve(state, timeout):
|
||||||
|
return optimizer.plan(json.loads(json.dumps(state)), 20)
|
||||||
|
|
||||||
|
monkeypatch.setattr(controller_mod, "solve", solve)
|
||||||
|
ctl.client = object()
|
||||||
|
plan = await ctl.make_plan()
|
||||||
|
assert plan is not None and plan["devices"]["shades_living"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("src", ["boiler", "hybrid"])
|
||||||
|
async def test_a_plan_is_made(tmp_path, monkeypatch, src):
|
||||||
|
ctl = flat(tmp_path, src)
|
||||||
|
|
||||||
|
async def solve(state, timeout): # in-process: the same optimiser, no subprocess
|
||||||
|
return optimizer.plan(json.loads(json.dumps(state)), 20)
|
||||||
|
|
||||||
|
monkeypatch.setattr(controller_mod, "solve", solve)
|
||||||
|
ctl.client = object()
|
||||||
|
plan = await ctl.make_plan()
|
||||||
|
assert plan is not None and ctl.plan_error == "", ctl.plan_error
|
||||||
|
assert plan["devices"]["heating"] and plan["devices"]["shades_living"]
|
||||||
|
if src == "boiler":
|
||||||
|
assert plan["gas_eur"] > 0 # the boiler's heat priced by gas
|
||||||
|
else:
|
||||||
|
assert any(e.get("source") for e in plan["devices"]["heating"]) # the hybrid chooses its source
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_failed_plan_is_logged_with_its_traceback():
|
||||||
|
try:
|
||||||
|
1.0 + None
|
||||||
|
except TypeError as e:
|
||||||
|
r = logging.LogRecord("cantabile_edge.controller", logging.WARNING, __file__, 1, "plan: %s", (e,),
|
||||||
|
exc_info=sys.exc_info())
|
||||||
|
line = json.loads(_Json().format(r))
|
||||||
|
assert line["msg"] == "plan: unsupported operand type(s) for +: 'float' and 'NoneType'"
|
||||||
|
assert line["exc"].startswith("Traceback") and "test_a_failed_plan_is_logged_with_its_traceback" in line["exc"]
|
||||||
|
# without one: the line as before
|
||||||
|
r = logging.LogRecord("x", logging.INFO, __file__, 1, "plan %s", ("p1",), None)
|
||||||
|
assert "exc" not in json.loads(_Json().format(r))
|
||||||
@@ -0,0 +1,260 @@
|
|||||||
|
# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
|
||||||
|
"""Refuse, never guess (docs/parameters.md U1, U2): no price without a
|
||||||
|
tariff, no battery without its facts.
|
||||||
|
|
||||||
|
U1: without a tariff (none received, expired) the plan has no prices: it
|
||||||
|
plans for comfort, the limits and the owner's musts, shifts and stores
|
||||||
|
nothing for a price, has no cost, and says why; a tariff that prices only
|
||||||
|
part of the horizon plans those steps only; an export without a price is
|
||||||
|
worth nothing (no discharging to export, no curtailing for it).
|
||||||
|
U2: a battery is planned only with its capacity, power and efficiency
|
||||||
|
(the mapping, the device, the owner's ratings) and a state of charge from
|
||||||
|
its sensor; else it is refused with the reason and where to enter what is
|
||||||
|
missing.
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from cantabile_edge.bundles import NO_TARIFF, Bundles
|
||||||
|
from cantabile_edge.controls import Control, parse
|
||||||
|
from cantabile_edge.inputs import Collector, model_state
|
||||||
|
from cantabile_edge.mapping import Mapping
|
||||||
|
|
||||||
|
from test_control_parts import SITE
|
||||||
|
|
||||||
|
START = 1_800_000_000 // 3600 * 3600
|
||||||
|
|
||||||
|
|
||||||
|
# -- U2: the battery's facts ---------------------------------------------------------
|
||||||
|
def bat(params=None, ents=None):
|
||||||
|
return Control("battery", "battery", ents or {"setpoint": "number.bat", "soc": "sensor.soc"}, True,
|
||||||
|
dict(params or {}))
|
||||||
|
|
||||||
|
|
||||||
|
def col_with(**vals):
|
||||||
|
col = Collector()
|
||||||
|
col.on_entities({"a": {e.replace("__", "."): {"s": str(v[0]), "a": {"unit_of_measurement": v[1]}}
|
||||||
|
for e, v in vals.items()}})
|
||||||
|
return col
|
||||||
|
|
||||||
|
|
||||||
|
def state(ctl, col, tmp_path, tariff=None):
|
||||||
|
if tariff is not None:
|
||||||
|
(tmp_path / "tariff.json").write_text(json.dumps(tariff))
|
||||||
|
m = Mapping.load(_mapping(tmp_path))
|
||||||
|
return model_state(START, ctl, m.inputs, col, Bundles(str(tmp_path), SITE), None, "UTC", None)
|
||||||
|
|
||||||
|
|
||||||
|
def _mapping(tmp_path):
|
||||||
|
p = tmp_path / "mapping.yaml"
|
||||||
|
p.write_text(yaml.safe_dump({"version": 3, "assets": [{"entity": "sensor.load", "asset": "load.power",
|
||||||
|
"kind": "gauge", "unit": "W", "consent": True}],
|
||||||
|
"inputs": {"load_power": "sensor.load"}, "controls": []}))
|
||||||
|
return str(p)
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_default_battery_facts():
|
||||||
|
c = bat()
|
||||||
|
f, missing, _ = c.battery_facts()
|
||||||
|
assert f == {} and missing == ["capacity", "power", "efficiency"]
|
||||||
|
assert c.p("capacity_wh") is None and c.p("efficiency") is None
|
||||||
|
assert c.device_range("power_w") == (0.0, 0.0) # unknown power: nothing but 0 may be set
|
||||||
|
|
||||||
|
|
||||||
|
def test_battery_without_ratings_is_refused(tmp_path):
|
||||||
|
col = col_with(sensor__soc=(60, "%"), sensor__load=(500, "W"))
|
||||||
|
s = state([bat()], col, tmp_path)
|
||||||
|
assert [d["id"] for d in s["devices"]] == []
|
||||||
|
r = s["refused"]["battery"]
|
||||||
|
assert r["why"] == "battery capacity unknown; battery power unknown; battery efficiency unknown"
|
||||||
|
assert r["where"] == ("on the panel: Devices, the battery's device, Ratings (Battery capacity, kWh, Battery "
|
||||||
|
"power, kW, Battery round-trip efficiency, %); or in the mapping's params (capacity_wh, "
|
||||||
|
"max_charge_w and max_discharge_w, efficiency)")
|
||||||
|
|
||||||
|
|
||||||
|
def test_owner_ratings_plan_the_battery(tmp_path):
|
||||||
|
"""A pilot home's case: the hm8042-hybrid profile's ratings, entered by the
|
||||||
|
owner on the panel (devices-specs.json), make the battery's facts."""
|
||||||
|
c = bat()
|
||||||
|
c.rated, c.rated_device = {"battery_kwh": 10.5, "battery_kw": 5, "battery_efficiency_pct": 90.25}, "HM8042"
|
||||||
|
col = col_with(sensor__soc=(60, "%"), sensor__load=(500, "W"))
|
||||||
|
s = state([c], col, tmp_path)
|
||||||
|
d = s["devices"][0]
|
||||||
|
assert d["params"]["capacity_wh"] == 10500 and d["params"]["max_charge_w"] == 5000
|
||||||
|
assert d["params"]["efficiency"] == pytest.approx(0.95) # one way: the round trip's root
|
||||||
|
assert d["bounds"]["power_w"][:2] == [-5000.0, 5000.0]
|
||||||
|
assert s["sources"]["battery"]["battery"]["capacity_wh"] == "owner"
|
||||||
|
# one rating missing: refused, and the panel's place named with the device
|
||||||
|
c.rated = {"battery_kwh": 10.5, "battery_kw": 5}
|
||||||
|
r = state([c], col, tmp_path)["refused"]["battery"]
|
||||||
|
assert r["why"] == "battery efficiency unknown"
|
||||||
|
assert r["where"] == "on the panel: Devices, HM8042, Ratings (Battery round-trip efficiency, %)"
|
||||||
|
|
||||||
|
|
||||||
|
def test_device_reported_ratings_and_mapping_win(tmp_path):
|
||||||
|
c = bat(ents={"setpoint": "number.bat", "soc": "sensor.soc", "capacity": "sensor.cap",
|
||||||
|
"rated_power": "sensor.pw", "efficiency": "sensor.eff"})
|
||||||
|
c.rated = {"battery_kwh": 99, "battery_kw": 99, "battery_efficiency_pct": 50}
|
||||||
|
col = col_with(sensor__soc=(40, "%"), sensor__cap=(8, "kWh"), sensor__pw=(4, "kW"), sensor__eff=(81, "%"),
|
||||||
|
sensor__load=(500, "W"))
|
||||||
|
d = state([c], col, tmp_path)["devices"][0]
|
||||||
|
assert (d["params"]["capacity_wh"], d["params"]["max_discharge_w"]) == (8000, 4000)
|
||||||
|
assert d["params"]["efficiency"] == pytest.approx(0.9)
|
||||||
|
c.params = {"capacity_wh": 5000.0} # the mapping's own wins
|
||||||
|
assert state([c], col, tmp_path)["devices"][0]["params"]["capacity_wh"] == 5000.0
|
||||||
|
# a capacity in a unit it cannot read is no capacity
|
||||||
|
col = col_with(sensor__soc=(40, "%"), sensor__cap=(8, "Ah"), sensor__pw=(4, "kW"), sensor__eff=(81, "%"),
|
||||||
|
sensor__load=(500, "W"))
|
||||||
|
c.params, c.rated = {}, {}
|
||||||
|
assert state([c], col, tmp_path)["refused"]["battery"]["why"] == "battery capacity unknown"
|
||||||
|
|
||||||
|
|
||||||
|
def test_soc_sensor_missing_refuses(tmp_path):
|
||||||
|
full = {"capacity_wh": 10000, "max_charge_w": 3000, "max_discharge_w": 3000, "efficiency": 0.95}
|
||||||
|
col = col_with(sensor__load=(500, "W"))
|
||||||
|
r = state([bat(full)], col, tmp_path)["refused"]["battery"]
|
||||||
|
assert r["why"] == "SoC unknown (the state-of-charge sensor has no value)" and r["missing"] == ["soc"]
|
||||||
|
r = state([bat(full, {"setpoint": "number.bat"})], col, tmp_path)["refused"]["battery"]
|
||||||
|
assert r["why"] == "SoC unknown (no state-of-charge sensor)"
|
||||||
|
assert r["where"] == "map the battery's state-of-charge sensor (soc)"
|
||||||
|
|
||||||
|
|
||||||
|
def test_mapping_facts_checked():
|
||||||
|
ok = parse([{"id": "b", "kind": "battery", "entities": {"setpoint": "number.b"},
|
||||||
|
"params": {"capacity_wh": 5000, "efficiency": 0.9}}], set())
|
||||||
|
assert ok[0].p("capacity_wh") == 5000
|
||||||
|
with pytest.raises(ValueError, match="efficiency"):
|
||||||
|
parse([{"id": "b", "kind": "battery", "entities": {"setpoint": "number.b"}, "params": {"efficiency": 95}}], set())
|
||||||
|
|
||||||
|
|
||||||
|
def test_refused_battery_takes_its_safe_value():
|
||||||
|
from cantabile_edge.executor import Executor
|
||||||
|
c = bat({"capacity_wh": 1})
|
||||||
|
ex = Executor([c], guard=None, col=Collector(), publish=None, clock=lambda: START)
|
||||||
|
ex.plan = {"made": START, "devices": {}, "refused": {"battery": {"why": "battery power unknown"}}}
|
||||||
|
want, level, why = ex.wanted(c, START)
|
||||||
|
assert want == {"set": 0.0, "grid": False, "auto": True} and why == "not planned: battery power unknown"
|
||||||
|
|
||||||
|
|
||||||
|
# -- U1: no price without a tariff -------------------------------------------------------
|
||||||
|
highspy = pytest.importorskip("highspy")
|
||||||
|
from cantabile_edge import optimizer # noqa: E402
|
||||||
|
|
||||||
|
|
||||||
|
def house(devices, steps=96, price=None, export=None, status="tariff"):
|
||||||
|
hour = lambda k: (k // 4) % 24 # noqa: E731
|
||||||
|
pi = price if price is not None else [12.0 if hour(k) < 6 else 40.0 if 17 <= hour(k) < 21 else 28.0
|
||||||
|
for k in range(steps)]
|
||||||
|
return {"v": 1, "start": START, "step_s": 900, "steps": steps, "tz": "UTC",
|
||||||
|
"price_import": pi, "price_export": export if export is not None else [8.0] * steps,
|
||||||
|
"prices": {"status": status}, "price_note": "test",
|
||||||
|
"load_w": [400.0] * steps, "pv_w": [0.0] * steps, "t_out": [5.0] * steps,
|
||||||
|
"sun": [0.0] * steps, "occupied": [1 if 6 <= hour(k) < 22 else 0 for k in range(steps)], "devices": devices}
|
||||||
|
|
||||||
|
|
||||||
|
def battery(soc=50.0, floor=20):
|
||||||
|
return {"id": "battery", "kind": "battery", "channels": ["setpoint"],
|
||||||
|
"params": {"capacity_wh": 10000, "max_charge_w": 3000, "max_discharge_w": 3000, "efficiency": 0.95},
|
||||||
|
"state": {"soc_pct": soc}, "bounds": {"soc_pct": [floor, 100, "owner", "device"],
|
||||||
|
"power_w": [-3000, 3000, "device", "device"]}}
|
||||||
|
|
||||||
|
|
||||||
|
def heating(temp=19.0):
|
||||||
|
return {"id": "heating", "kind": "heating", "channels": ["climate"],
|
||||||
|
"params": {"r_k_per_kw": 10, "c_kwh_per_k": 3, "max_power_w": 4000, "comfort_c": 21, "setback_c": 17,
|
||||||
|
"cop": 1.0},
|
||||||
|
"state": {"temp_c": temp}, "bounds": {"temp_c": [16, 26, "owner", "owner"]}}
|
||||||
|
|
||||||
|
|
||||||
|
def ev():
|
||||||
|
return {"id": "car", "kind": "ev", "channels": ["switch", "current"],
|
||||||
|
"params": {"need_wh": 6000, "departure": "07:00", "min_current_a": 6, "max_current_a": 16,
|
||||||
|
"phases": 1, "voltage": 230},
|
||||||
|
"state": {"connected": True}, "bounds": {"current_a": [0, 16, "device", "device"]}}
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_tariff_plans_without_prices():
|
||||||
|
s = house([battery(), heating(), ev()], price=[None] * 96, export=[None] * 96, status="none")
|
||||||
|
s["price_note"] = NO_TARIFF + " (no tariff received yet: ...)"
|
||||||
|
p = optimizer.plan(s, 20)
|
||||||
|
assert p["priced"] is False and p["cost_eur"] is None and p["baseline_eur"] is None
|
||||||
|
assert p["price_note"].startswith("no tariff: price optimisation off")
|
||||||
|
# the battery: its own mode throughout, never charged or discharged for a price
|
||||||
|
assert all(e.get("auto") and not e["grid"] for e in p["devices"]["battery"])
|
||||||
|
# the car still charges for its departure (a need, not a price), early and without price words
|
||||||
|
car = [e for e in p["devices"]["car"] if e["set"] != "off"]
|
||||||
|
assert car and all("no tariff" in e["why"] for e in car)
|
||||||
|
# the room is held at its comfort, never pre-heated above it for a price
|
||||||
|
assert all(e["why"] in ("keep the room warm", "setback: away or night", "warm enough") for e in p["devices"]["heating"])
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_tariff_keeps_the_owners_reserve():
|
||||||
|
b = battery(soc=20, floor=10)
|
||||||
|
b["pref"] = {"floor_pct": [60.0] * 96, "group": [0] * 96, "groups": [{"group": "battery:reserve", "text": "reserve 60 %"}]}
|
||||||
|
p = optimizer.plan(house([b], price=[None] * 96, export=[None] * 96, status="none"), 20)
|
||||||
|
assert any(e["grid"] and e["why"] == "charge to the reserve you set (no tariff)" for e in p["devices"]["battery"])
|
||||||
|
|
||||||
|
|
||||||
|
def test_partial_tariff_plans_only_its_steps():
|
||||||
|
pi = [10.0] * 40 + [None] * 56
|
||||||
|
p = optimizer.plan(house([battery()], price=pi), 20)
|
||||||
|
assert p["priced"] is True and p["steps"] == 40 and len(p["flex"]["grid_w"]) == 40
|
||||||
|
assert all(e["t"] < START + 40 * 900 for e in p["devices"]["battery"])
|
||||||
|
|
||||||
|
|
||||||
|
def test_unpriced_export_is_worth_nothing():
|
||||||
|
"""A full battery and PV surplus with no export price: PV is exported
|
||||||
|
(not curtailed: spill has its own cost), the battery is not discharged
|
||||||
|
to export, and the cost counts no feed-in revenue."""
|
||||||
|
s = house([battery(soc=90, floor=10)], price=[30.0] * 96, export=[None] * 96)
|
||||||
|
s["pv_w"] = [3000.0] * 96
|
||||||
|
m = optimizer.Model(optimizer._prepare(s))
|
||||||
|
m.solve(20)
|
||||||
|
val = m.h.getSolution().col_value
|
||||||
|
out = [val[v.index] for v in m.gout]
|
||||||
|
dis = [val[v.index] for v in m.devices["battery"][1]["dis"]]
|
||||||
|
assert max(dis) < 1.0 # nothing discharged to export
|
||||||
|
assert sum(out) > 0 # exported, not curtailed
|
||||||
|
|
||||||
|
|
||||||
|
def test_expired_tariff_through_the_bundle(tmp_path):
|
||||||
|
t = {"v": 1, "kind": "tariff", "id": "t9", "site": SITE, "issued": START - 86400, "start": START - 86400,
|
||||||
|
"step_s": 900, "steps": 4, "expires": START - 3600, "currency": "EUR", "unit": "ct/kWh",
|
||||||
|
"import": [20, 20, 20, 20], "export": [5, 5, 5, 5]}
|
||||||
|
s = state([], col_with(sensor__load=(500, "W")), tmp_path, tariff=t)
|
||||||
|
assert s["prices"]["status"] == "none" and s["prices"]["reason"] == "expired"
|
||||||
|
assert set(s["price_import"]) == {None} and "the tariff t9 expired at" in s["price_note"]
|
||||||
|
|
||||||
|
|
||||||
|
# -- the panel's side: the owner's ratings and own tariff ----------------------------------
|
||||||
|
def test_owner_ratings_from_the_devices_specs(tmp_path):
|
||||||
|
from types import SimpleNamespace
|
||||||
|
from cantabile_edge.controller import Controller
|
||||||
|
(tmp_path / "devices-specs.json").write_text(json.dumps({"v": 1, "devices": {"hm8042-hybrid-1": {
|
||||||
|
"name": "HM8042", "ratings": {"battery_kwh": 10.5, "battery_kw": 5}, "controls": ["battery"]}}}))
|
||||||
|
c, other = bat(), bat()
|
||||||
|
other.id = "battery_2"
|
||||||
|
ctl = SimpleNamespace(cfg=SimpleNamespace(state_dir=str(tmp_path)), controls=[c, other])
|
||||||
|
Controller.owner_ratings(ctl)
|
||||||
|
assert c.rated == {"battery_kwh": 10.5, "battery_kw": 5} and c.rated_device == "HM8042"
|
||||||
|
assert other.rated == {} and other.rated_device == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_own_tariff_from_the_panel(tmp_path):
|
||||||
|
from cantabile_edge.preferences import Preferences
|
||||||
|
p = Preferences(str(tmp_path), clock=lambda: START)
|
||||||
|
(tmp_path / "prefs-inbox").mkdir()
|
||||||
|
(tmp_path / "prefs-inbox" / "1.json").write_text(json.dumps({"by": "owner", "own_tariff": {"import_ct": "31.2",
|
||||||
|
"export_ct": "7.9"}}))
|
||||||
|
(tmp_path / "prefs-inbox" / "2.json").write_text(json.dumps({"by": "owner", "own_tariff": {"import_ct": "-3"}}))
|
||||||
|
assert p.take_inbox({}, "UTC") == 2
|
||||||
|
assert p.outbox == [{"v": 1, "import_ct": 31.2, "by": "owner", "at": START, "export_ct": 7.9}]
|
||||||
|
assert p.own_tariff["state"] == "refused" # the second: refused, the first still sent
|
||||||
|
p.take_plan({"priced": False, "price_note": "no tariff: price optimisation off (...)",
|
||||||
|
"price_state": {"status": "none", "reason": "never"}, "refused": {"battery": {"why": "SoC unknown"}}})
|
||||||
|
p.write_view([])
|
||||||
|
v = json.loads((tmp_path / "preferences-view.json").read_text())
|
||||||
|
assert v["planning"]["priced"] is False and v["planning"]["prices"]["reason"] == "never"
|
||||||
|
assert v["planning"]["refused"]["battery"]["why"] == "SoC unknown" and v["own_tariff"]["state"] == "refused"
|
||||||
@@ -0,0 +1,218 @@
|
|||||||
|
# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
|
||||||
|
"""A room without its facts is not optimised (docs/parameters.md
|
||||||
|
E3: R/C measured, power and COP the device's): never the built-in R/C. The
|
||||||
|
reason (what is missing and where it comes from) goes with the plan to the
|
||||||
|
panel and the console; the room keeps its limits, and its thermostat
|
||||||
|
follows the owner's or installer's schedule meanwhile (optimizer.
|
||||||
|
fallback_entries), else keeps its own setpoint.
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from cantabile_edge import preferences as pm
|
||||||
|
from cantabile_edge import steady
|
||||||
|
from cantabile_edge.bundles import Bundles
|
||||||
|
from cantabile_edge.controls import parse as parse_controls
|
||||||
|
from cantabile_edge.executor import RELEASE, Executor, actions_for
|
||||||
|
from cantabile_edge.inputs import Collector, model_state
|
||||||
|
|
||||||
|
from test_control_parts import SITE
|
||||||
|
from test_model_series import model, put
|
||||||
|
|
||||||
|
START = 1793232000 # 2026-10-29 00:00 UTC, a Thursday (01:00 in Berlin)
|
||||||
|
TZ = "Europe/Berlin"
|
||||||
|
INPUTS = SimpleNamespace(load_power="", outside_temp="", weather="", sun="", presence=[])
|
||||||
|
|
||||||
|
|
||||||
|
class Clock:
|
||||||
|
def __init__(self, t):
|
||||||
|
self.t = t
|
||||||
|
|
||||||
|
def __call__(self):
|
||||||
|
return self.t
|
||||||
|
|
||||||
|
|
||||||
|
def room(cid="living", kind="heating", ents=None, limits=None, **params):
|
||||||
|
raw = {"id": cid, "kind": kind, "entities": ents or {"climate": f"climate.{cid}"}, "params": params}
|
||||||
|
if limits:
|
||||||
|
raw["limits"] = limits
|
||||||
|
return parse_controls([raw], set())[0]
|
||||||
|
|
||||||
|
|
||||||
|
def col(temps: dict | None = None):
|
||||||
|
c = Collector()
|
||||||
|
c.on_entities({"a": {f"climate.{k}": {"s": "heat", "a": {"current_temperature": v, "temperature": 20.0}}
|
||||||
|
for k, v in (temps or {}).items()}})
|
||||||
|
return c
|
||||||
|
|
||||||
|
|
||||||
|
def nobundle(tmp_path):
|
||||||
|
return Bundles(str(tmp_path / "none"), SITE)
|
||||||
|
|
||||||
|
|
||||||
|
def state(controls, collector, bundles, prefs=None):
|
||||||
|
s = model_state(START, controls, INPUTS, collector, bundles, None, TZ, None)
|
||||||
|
if prefs is not None:
|
||||||
|
s = prefs.apply(s, {c.id: c for c in controls}, collector, START)
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
# -- the reasons -----------------------------------------------------------------------
|
||||||
|
def test_no_fit_is_refused_with_the_reason(tmp_path):
|
||||||
|
s = state([room(max_power_w=2000)], col({"living": 20.0}), nobundle(tmp_path))
|
||||||
|
assert s["devices"] == [] and [d["id"] for d in s["fallback"]] == ["living"]
|
||||||
|
r = s["refused"]["living"]
|
||||||
|
assert r["missing"] == ["r_k_per_kw", "c_kwh_per_k"] and r["kind"] == "heating" and r["name"] == "Living"
|
||||||
|
assert "no R/C fit for this room yet" in r["why"] and "nightly" in r["where"]
|
||||||
|
assert s["sources"]["rooms"] == {"living": "none"}
|
||||||
|
# no R and C anywhere for it: the built-in ones are gone (controls.FACTS)
|
||||||
|
assert not {"r_k_per_kw", "c_kwh_per_k"} & set(s["fallback"][0]["params"])
|
||||||
|
assert s["fallback"][0]["params"]["cop"] == 1.0 # an electric heater: physics, not a default
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("params, temps, ents, missing, words", [
|
||||||
|
({}, {"living": 20.0}, None, ["r_k_per_kw", "c_kwh_per_k", "max_power_w"], "heating's electric power unknown"),
|
||||||
|
# a heat pump without its COP (gas-heat's rule, methods E6): one refusal with the room's
|
||||||
|
({"r_k_per_kw": 10, "c_kwh_per_k": 3, "max_power_w": 2000, "heat_source": "heat_pump"}, {"living": 20.0}, None,
|
||||||
|
["cop"], "heat pump COP unknown"),
|
||||||
|
({"heat_source": "heat_pump"}, {"living": 20.0}, None, ["r_k_per_kw", "c_kwh_per_k", "max_power_w", "cop"],
|
||||||
|
"no R/C fit for this room yet"),
|
||||||
|
({"r_k_per_kw": 10, "c_kwh_per_k": 3, "max_power_w": 2000, "cop": 1}, {}, None, ["temp"], "(no value now)"),
|
||||||
|
({"r_k_per_kw": 10, "c_kwh_per_k": 3, "max_power_w": 2000, "cop": 1}, {}, {"switch": "switch.heater"}, ["temp"],
|
||||||
|
"(no temperature sensor)"),
|
||||||
|
({"r_k_per_kw": 10, "max_power_w": 2000, "cop": 1}, {"living": 20.0}, None, ["c_kwh_per_k"],
|
||||||
|
"the mapping sets r_k_per_kw but not c_kwh_per_k"),
|
||||||
|
])
|
||||||
|
def test_each_missing_fact(tmp_path, params, temps, ents, missing, words):
|
||||||
|
s = state([room(ents=ents, **params)], col(temps), nobundle(tmp_path))
|
||||||
|
r = s["refused"]["living"]
|
||||||
|
assert r["missing"] == missing and words in r["why"], r
|
||||||
|
|
||||||
|
|
||||||
|
def test_all_facts_plan_the_room(tmp_path):
|
||||||
|
s = state([room(r_k_per_kw=10, c_kwh_per_k=3, max_power_w=2000, cop=1)], col({"living": 20.0}), nobundle(tmp_path))
|
||||||
|
assert s["refused"] == {} and s["fallback"] == [] and [d["id"] for d in s["devices"]] == ["living"]
|
||||||
|
assert s["sources"]["rooms"] == {"living": "mapping"} and s["sources"]["rooms_cop"] == {"living": "mapping"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_coolers_fit_against_the_heat_needs_the_cop_one_against_power_none(tmp_path):
|
||||||
|
thermal = put(tmp_path, model(rooms={"living": {"r_k_per_kw": 8, "c_kwh_per_k": 4, "power": "thermal"}}))
|
||||||
|
s = state([room(kind="cooling", max_power_w=1500)], col({"living": 20.0}), thermal)
|
||||||
|
assert s["refused"]["living"]["missing"] == ["cop"] and s["sources"]["rooms"] == {"living": "model"}
|
||||||
|
electric = put(tmp_path, model(rooms={"living": {"r_k_per_kw": 8, "c_kwh_per_k": 4, "power": "electric"}}))
|
||||||
|
s = state([room(kind="cooling", max_power_w=1500)], col({"living": 20.0}), electric)
|
||||||
|
assert s["refused"] == {} and s["sources"]["rooms_cop"] == {"living": "electric fit"}
|
||||||
|
# an electric heater with a fit against the heat: COP 1 by physics, planned
|
||||||
|
s = state([room(max_power_w=2000)], col({"living": 20.0}), thermal)
|
||||||
|
assert s["refused"] == {} and s["sources"]["rooms_cop"] == {"living": "electric heater"}
|
||||||
|
p = s["devices"][0]["params"]
|
||||||
|
assert (p["r_k_per_kw"], p["c_kwh_per_k"], p["cop"]) == (8.0, 4.0, 1.0) # electric equivalents
|
||||||
|
|
||||||
|
|
||||||
|
# -- the fallback schedule ---------------------------------------------------------------
|
||||||
|
optimizer = pytest.importorskip("cantabile_edge.optimizer")
|
||||||
|
pytest.importorskip("highspy")
|
||||||
|
|
||||||
|
|
||||||
|
def at(entries, t):
|
||||||
|
cur = None
|
||||||
|
for e in entries:
|
||||||
|
if e["t"] <= t:
|
||||||
|
cur = e
|
||||||
|
return cur
|
||||||
|
|
||||||
|
|
||||||
|
def local_ts(day, hh, mm=0):
|
||||||
|
"""START's Thursday in Berlin (UTC+1 after 25 October) + day days, at hh:mm."""
|
||||||
|
return START - 3600 + day * 86400 + hh * 3600 + mm * 60
|
||||||
|
|
||||||
|
|
||||||
|
def prefs_for(tmp_path, c, value=None, level="owner", now=START):
|
||||||
|
d = tmp_path / "prefs"
|
||||||
|
os.makedirs(d, exist_ok=True)
|
||||||
|
p = pm.Preferences(str(d), Clock(now))
|
||||||
|
if value is not None:
|
||||||
|
ok, why = p.set(f"room:{c.id}", value, "panel:alice", level, c, now)
|
||||||
|
assert ok, why
|
||||||
|
return p
|
||||||
|
|
||||||
|
|
||||||
|
def test_fallback_follows_the_schedule_within_the_limits(tmp_path):
|
||||||
|
c = room(max_power_w=2000, limits={"temp_c": {"owner": [18, 22]}})
|
||||||
|
p = prefs_for(tmp_path, c, {"blocks": [{"days": "daily", "from": "06:00", "to": "22:00", "c": 23},
|
||||||
|
{"days": "daily", "from": "22:00", "to": "01:00", "c": 19, "how": "must"}]})
|
||||||
|
s = state([c], col({"living": 20.0}), nobundle(tmp_path), p)
|
||||||
|
plan = optimizer.plan(s, 20)
|
||||||
|
assert plan["refused"]["living"]["fallback"] == "schedule"
|
||||||
|
e = plan["devices"]["living"]
|
||||||
|
assert at(e, local_ts(0, 7))["set"] == 22.0 # 23 held at the owner's upper limit
|
||||||
|
assert at(e, local_ts(0, 23))["set"] == 19.0 # the must
|
||||||
|
assert at(e, local_ts(0, 3))["set"] == 18.0 # outside: the setback, at the owner's floor
|
||||||
|
assert "fallback schedule (room not optimised)" in at(e, local_ts(0, 7))["why"]
|
||||||
|
assert all(isinstance(x["set"], float) for x in e)
|
||||||
|
# the preferences' feedback: not optimised, set as the thermostat's schedule
|
||||||
|
fb = [f for f in plan["prefs"] if f["control"] == "living"]
|
||||||
|
assert len(fb) == 2 and all(f["met"] is None for f in fb)
|
||||||
|
assert fb[0]["detail"] == "room not optimised: set as the thermostat's schedule"
|
||||||
|
|
||||||
|
|
||||||
|
def test_installer_schedule_is_the_fallback_too(tmp_path):
|
||||||
|
c = room(max_power_w=2000)
|
||||||
|
p = prefs_for(tmp_path, c, {"blocks": [{"days": "daily", "from": "00:00", "to": "00:00", "c": 20.5}]}, "installer")
|
||||||
|
plan = optimizer.plan(state([c], col({"living": 20.0}), nobundle(tmp_path), p), 20)
|
||||||
|
assert {x["set"] for x in plan["devices"]["living"]} == {20.5}
|
||||||
|
|
||||||
|
|
||||||
|
def test_without_a_schedule_the_thermostat_keeps_its_own(tmp_path):
|
||||||
|
c = room(max_power_w=2000)
|
||||||
|
plan = optimizer.plan(state([c], col({"living": 20.0}), nobundle(tmp_path), prefs_for(tmp_path, c)), 20)
|
||||||
|
assert "living" not in plan["devices"] and plan["refused"]["living"]["fallback"] == "own"
|
||||||
|
ex = Executor([c], guard=None, col=col({"living": 20.0}), publish=None, clock=lambda: START)
|
||||||
|
ex.plan = dict(plan, made=START)
|
||||||
|
want, level, why = ex.wanted(c, START)
|
||||||
|
assert want == RELEASE and why.startswith("not planned: no R/C fit")
|
||||||
|
assert actions_for(c, want, ex.col, level, why) == [] # nothing written
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_switched_heater_stays_as_it_is(tmp_path):
|
||||||
|
c = room(ents={"switch": "switch.heater"}, max_power_w=2000)
|
||||||
|
p = prefs_for(tmp_path, c, {"blocks": [{"days": "daily", "from": "06:00", "to": "22:00", "c": 21}]})
|
||||||
|
plan = optimizer.plan(state([c], col(), nobundle(tmp_path), p), 20)
|
||||||
|
assert "living" not in plan["devices"] and plan["refused"]["living"]["fallback"] == "none"
|
||||||
|
|
||||||
|
|
||||||
|
def test_an_override_alone_and_the_executor(tmp_path):
|
||||||
|
c = room(max_power_w=2000, limits={"temp_c": {"owner": [16, 24]}})
|
||||||
|
p = prefs_for(tmp_path, c)
|
||||||
|
ok, _ = p.add_override({"kind": "boost", "control": "living", "c": 23, "hours": 2}, "panel:alice", {"living": c},
|
||||||
|
START)
|
||||||
|
assert ok
|
||||||
|
plan = optimizer.plan(state([c], col({"living": 20.0}), nobundle(tmp_path), p), 20)
|
||||||
|
e = plan["devices"]["living"]
|
||||||
|
assert e[0]["t"] == START and e[0]["set"] == 23.0
|
||||||
|
assert at(e, START + 3 * 3600)["set"] == RELEASE # after the boost: its own setpoint again
|
||||||
|
# steady keeps the fallback's "own setpoint" as it is (nothing written, nothing held)
|
||||||
|
collector = col({"living": 20.0})
|
||||||
|
steady.stabilise(plan, {"steps": 192, "devices": []}, [c], collector, None, START)
|
||||||
|
assert at(plan["devices"]["living"], START + 3 * 3600)["set"] == RELEASE
|
||||||
|
ex = Executor([c], guard=None, col=collector, publish=None, clock=lambda: START)
|
||||||
|
ex.plan = dict(plan, made=START)
|
||||||
|
want, level, why = ex.wanted(c, START)
|
||||||
|
assert want["set"] == 23.0 and "fallback" in why
|
||||||
|
acts = actions_for(c, want, collector, level, why)
|
||||||
|
assert [(a.service, a.value) for a in acts] == [("set_temperature", 23.0)]
|
||||||
|
want, level, why = ex.wanted(c, START + 3 * 3600)
|
||||||
|
assert actions_for(c, want, collector, level, why) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_public_plan_carries_the_refusal(tmp_path):
|
||||||
|
"""The reason travels with the plan (controller.public_plan): fleet keeps it
|
||||||
|
for the console's plan view, the panel reads it from the preferences view."""
|
||||||
|
c = room(max_power_w=2000)
|
||||||
|
p = prefs_for(tmp_path, c, {"blocks": [{"days": "daily", "from": "06:00", "to": "22:00", "c": 21}]})
|
||||||
|
plan = optimizer.plan(state([c], col({"living": 20.0}), nobundle(tmp_path), p), 20)
|
||||||
|
p.take_plan(plan, START)
|
||||||
|
r = p.planning["refused"]["living"]
|
||||||
|
assert r["kind"] == "heating" and r["fallback"] == "schedule" and r["missing"] == ["r_k_per_kw", "c_kwh_per_k"]
|
||||||
@@ -248,6 +248,27 @@ def test_winter_opens_in_the_sun_closes_at_night_never_in_quiet_hours():
|
|||||||
assert moves(shades.schedule(shade(180), state(start, location={}), {}), start) == []
|
assert moves(shades.schedule(shade(180), state(start, location={}), {}), start) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_unpriced_steps_are_no_dear_hours():
|
||||||
|
# the tariff's prices end before the horizon (None after: bundles.prices), or there is none at all
|
||||||
|
# (2026-11-05: every plan of a flat with a shade failed summing them)
|
||||||
|
start = ts(2026, 7, 15)
|
||||||
|
cool = {"temp_c": 21.0, "setpoint_c": 24.0}
|
||||||
|
dear = [30.0] * 64 + [60.0] * 16 + [None] * 16
|
||||||
|
m = moves(shades.schedule(shade(270), state(start, t_out=20.0, prices=dear), cool), start)
|
||||||
|
assert m and m[0][2] == "sun on the west facade: before the dear hours"
|
||||||
|
# no prices: no dear hours (no stand-in price); the warm room still shaded
|
||||||
|
assert moves(shades.schedule(shade(270), state(start, t_out=20.0, prices=[None] * 96), cool), start) == []
|
||||||
|
m = moves(shades.schedule(shade(270), state(start, prices=[None] * 96), {"temp_c": 26.0, "setpoint_c": 24.0}),
|
||||||
|
start)
|
||||||
|
assert m[0][2] == "sun on the west facade"
|
||||||
|
# the winter's rules use no price: the same moves with the tail unpriced
|
||||||
|
jan = ts(2026, 1, 15)
|
||||||
|
full = shades.schedule(shade(180, pos=0), state(jan, t_out=2.0), {"season": "heating"})
|
||||||
|
part = shades.schedule(shade(180, pos=0), state(jan, t_out=2.0, prices=[30.0] * 90 + [None] * 6),
|
||||||
|
{"season": "heating"})
|
||||||
|
assert part["set"] == full["set"]
|
||||||
|
|
||||||
|
|
||||||
def test_minimum_interval_between_moves():
|
def test_minimum_interval_between_moves():
|
||||||
start = ts(2026, 7, 15)
|
start = ts(2026, 7, 15)
|
||||||
flip = [k % 2 for k in range(96)] # occupancy changing every quarter hour
|
flip = [k % 2 for k in range(96)] # occupancy changing every quarter hour
|
||||||
|
|||||||
@@ -0,0 +1,145 @@
|
|||||||
|
# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
|
||||||
|
"""The plan's flexibility for the building (optimizer.flexibility,
|
||||||
|
shift_envelope; docs/methods.md A11): the shift envelope from a known plan,
|
||||||
|
nothing published without the facts, and the meter's import counted with a
|
||||||
|
market contract (the cause of the zero shed_w on the Mieterstrom flats)."""
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
highspy = pytest.importorskip("highspy")
|
||||||
|
|
||||||
|
from cantabile_edge import optimizer # noqa: E402
|
||||||
|
|
||||||
|
START = 1793232000 # 2026-10-29 00:00 UTC (01:00 in Berlin)
|
||||||
|
|
||||||
|
|
||||||
|
def house(devices, steps=192, sources=None, market=None):
|
||||||
|
hour = lambda k: (k // 4 + 1) % 24 # noqa: E731 (Berlin, winter)
|
||||||
|
price = [12.0 if hour(k) < 6 else 40.0 if 17 <= hour(k) < 21 else 28.0 for k in range(steps)]
|
||||||
|
s = {"v": 1, "start": START, "step_s": 900, "steps": steps, "tz": "Europe/Berlin",
|
||||||
|
"price_import": price, "price_export": [8.0] * steps, "price_note": "test",
|
||||||
|
"load_w": [400.0] * steps, "pv_w": [0.0] * steps, "t_out": [5.0] * steps,
|
||||||
|
"sun": [0.0] * steps, "occupied": [1 if 6 <= hour(k) < 22 else 0 for k in range(steps)],
|
||||||
|
"devices": devices, "sources": sources or {}}
|
||||||
|
if market is not None:
|
||||||
|
s["market"] = market
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
def battery(soc=50, floor=20):
|
||||||
|
return {"id": "battery", "kind": "battery", "channels": ["setpoint"],
|
||||||
|
"params": {"capacity_wh": 10000, "max_charge_w": 3000, "max_discharge_w": 3000, "efficiency": 0.95},
|
||||||
|
"state": {"soc_pct": soc}, "bounds": {"soc_pct": [floor, 100, "owner", "device"],
|
||||||
|
"power_w": [-3000, 3000, "device", "device"]}}
|
||||||
|
|
||||||
|
|
||||||
|
def heating(temp=21.0):
|
||||||
|
return {"id": "heating", "kind": "heating", "channels": ["climate"],
|
||||||
|
"params": {"r_k_per_kw": 10, "c_kwh_per_k": 3, "max_power_w": 4000, "comfort_c": 21, "setback_c": 17,
|
||||||
|
"cop": 3.0},
|
||||||
|
"state": {"temp_c": temp}, "bounds": {"temp_c": [16, 24, "owner", "owner"]}}
|
||||||
|
|
||||||
|
|
||||||
|
def ev(state):
|
||||||
|
return {"id": "ev", "kind": "ev", "channels": ["switch", "current"],
|
||||||
|
"params": {"need_wh": 10000, "departure": "07:00", "min_current_a": 6, "max_current_a": 16,
|
||||||
|
"phases": 1, "voltage": 230},
|
||||||
|
"state": dict({"connected": True}, **state), "bounds": {"current_a": [0, 16, "device", "device"]}}
|
||||||
|
|
||||||
|
|
||||||
|
def shift(p, kind):
|
||||||
|
sh = p["flex"].get("shift") or {}
|
||||||
|
return next((d for d in sh.get("devices", []) if d["kind"] == kind), None)
|
||||||
|
|
||||||
|
|
||||||
|
def test_battery_envelope_from_its_plan():
|
||||||
|
p = optimizer.plan(house([battery()]), 20)
|
||||||
|
sh = p["flex"]["shift"]
|
||||||
|
assert sh["v"] == 1 and sh["start"] == START and sh["step_s"] == 900 and sh["steps"] == 192
|
||||||
|
b = shift(p, "battery")
|
||||||
|
assert b["soc_wh"] == 5000 and b["max_wh"] == 10000 and b["ch_w"] == 3000 and b["dis_w"] == 3000
|
||||||
|
assert b["eta"] == pytest.approx(0.95)
|
||||||
|
assert b["lo_wh"] == [2000] * 192 # the floor, 20 % of 10 kWh
|
||||||
|
assert len(b["base_w"]) == 192
|
||||||
|
# base_w is the plan's: charging at night, discharging at the evening peak
|
||||||
|
assert max(b["base_w"][:20]) > 0 and min(b["base_w"][64:80]) < 0
|
||||||
|
# replaying base_w through the battery reaches the published end (no draining)
|
||||||
|
e = b["soc_wh"]
|
||||||
|
for w in b["base_w"]:
|
||||||
|
e += 0.25 * (b["eta"] * max(w, 0) - max(-w, 0) / b["eta"])
|
||||||
|
assert e >= min(b["lo_wh"]) - 5
|
||||||
|
assert e == pytest.approx(b["end_wh"], abs=5)
|
||||||
|
|
||||||
|
|
||||||
|
def test_car_envelope_needs_the_known_ready_by():
|
||||||
|
# the charger reports the departure and the energy needed: published
|
||||||
|
p = optimizer.plan(house([ev({"departure": "07:00", "need_wh": 8000})]), 20)
|
||||||
|
c = shift(p, "ev")
|
||||||
|
assert c is not None and c["keep"] == 1.0
|
||||||
|
delivered = sum(c["base_w"]) * 0.25
|
||||||
|
assert delivered == pytest.approx(8000, rel=0.02)
|
||||||
|
dep = optimizer.departure_step(house([]), "07:00")
|
||||||
|
# before the ready-by: between nothing and the need; from it on: what the plan delivers (no room)
|
||||||
|
assert all(lo <= 0 <= hi for lo, hi in zip(c["lo_wh"], c["hi_wh"]))
|
||||||
|
assert abs(c["lo_wh"][dep]) <= 200 and abs(c["hi_wh"][dep]) <= 200
|
||||||
|
assert c["up_w"][dep] == 0 and c["down_w"][dep] == 0
|
||||||
|
assert c["hi_wh"][0] > 6000 # most of the need can still move
|
||||||
|
# only the defaults (need 10 kWh by 07:00 from the mapping's defaults): nothing published
|
||||||
|
p = optimizer.plan(house([ev({})]), 20)
|
||||||
|
assert shift(p, "ev") is None
|
||||||
|
# not plugged in: nothing (its window is not known)
|
||||||
|
p = optimizer.plan(house([ev({"connected": False, "departure": "07:00", "need_wh": 8000})]), 20)
|
||||||
|
assert "shift" not in p["flex"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_owner_preference_publishes_the_car():
|
||||||
|
d = ev({})
|
||||||
|
d["pref"] = {"group": "ev:ready", "source": "schedule", "dep_step": 24, "at": START + 6 * 3600, "at_local": "Thu 07:00",
|
||||||
|
"need_wh": 6000.0, "how": "must", "max_ct": None, "min_wh": 0.0, "pct": None, "kwh": 6.0,
|
||||||
|
"soc_pct": None, "capacity_wh": None, "note": "", "beyond": False, "text": "Th 07:00 6kWh must"}
|
||||||
|
p = optimizer.plan(house([d]), 20)
|
||||||
|
c = shift(p, "ev")
|
||||||
|
assert c is not None and sum(c["base_w"]) * 0.25 == pytest.approx(6000, rel=0.02)
|
||||||
|
# a ready-by in % without the car's charge: the energy falls back to the default (preferences.py), not published
|
||||||
|
d["pref"] = dict(d["pref"], kwh=None, pct=80)
|
||||||
|
assert optimizer.ev_known({}, d["pref"]) is False
|
||||||
|
assert optimizer.ev_known({"need_wh": 5000}, d["pref"]) is True
|
||||||
|
assert optimizer.ev_known({"departure": "07:00"}, None) is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_room_envelope_needs_fit_cop_and_band():
|
||||||
|
fitted = {"rooms": {"heating": "box model"}, "rooms_cop": {"heating": "mapping"}, "rooms_band": {"heating": "mapping"}}
|
||||||
|
p = optimizer.plan(house([heating()], sources=fitted), 20)
|
||||||
|
h = shift(p, "heating")
|
||||||
|
assert h is not None
|
||||||
|
assert h["keep"] == pytest.approx(1 - 0.25 / (10 * 3), abs=1e-6)
|
||||||
|
assert all(lo <= 0 <= hi for lo, hi in zip(h["lo_wh"], h["hi_wh"]))
|
||||||
|
# 1 K of the room is C / COP = 1 kWh electric: while someone is home never below its 21 °C target (at most
|
||||||
|
# 3 K above it, the 24 °C bound), and never above 24 °C
|
||||||
|
occ = [t for t in range(100, 180) if house([])["occupied"][t + 1]]
|
||||||
|
assert all(h["lo_wh"][t] >= -3000 - 1 for t in occ)
|
||||||
|
assert max(h["hi_wh"][t] for t in occ) > 500
|
||||||
|
assert max(h["hi_wh"]) <= (24 - 16) * 1000 + 1
|
||||||
|
# at night (setback 17 °C) the room may cool further than in the day
|
||||||
|
night = [t for t in range(100, 180) if not house([])["occupied"][t + 1]]
|
||||||
|
assert min(h["lo_wh"][t] for t in night) < min(h["lo_wh"][t] for t in occ)
|
||||||
|
# no fit (the defaults), or no COP of the device's, or no band of the owner's: nothing published
|
||||||
|
for src in ({**fitted, "rooms": {"heating": "defaults"}}, {**fitted, "rooms_cop": {"heating": "default"}},
|
||||||
|
{**fitted, "rooms_band": {"heating": "defaults"}}, {}):
|
||||||
|
p = optimizer.plan(house([heating()], sources=src), 20)
|
||||||
|
assert shift(p, "heating") is None, src
|
||||||
|
|
||||||
|
|
||||||
|
def test_nothing_without_devices():
|
||||||
|
p = optimizer.plan(house([]), 20)
|
||||||
|
assert "shift" not in p["flex"]
|
||||||
|
assert len(p["flex"]["shed_w"]) == 96
|
||||||
|
|
||||||
|
|
||||||
|
def test_import_through_a_contract_counts():
|
||||||
|
# a flat supplied by its building's pool (a market contract it buys on, cheaper than the grid):
|
||||||
|
# its import is the contract's; grid_w (and with it shed_w) was 0 before
|
||||||
|
c = {"id": "pool", "name": "pool", "direction": "buys", "price_ct": 10.0, "max_w": 0.0, "guaranteed_w": 0.0,
|
||||||
|
"minimum_wh": 0.0, "from_step": 0, "to_step": 192}
|
||||||
|
p = optimizer.plan(house([battery()], market=[c]), 20)
|
||||||
|
assert min(p["flex"]["grid_w"]) >= 390 # the house's 400 W at least
|
||||||
|
assert sum(p["flex"]["shed_w"]) > 0
|
||||||
@@ -131,7 +131,9 @@ def test_controller_plans_only_the_active_side(tmp_path, consent): # noqa: F811
|
|||||||
assert "sensor.outside" in ctl.entities() # the mapped outside.temp
|
assert "sensor.outside" in ctl.entities() # the mapped outside.temp
|
||||||
ctl.col.on_entities({"a": {"sensor.outside": {"s": "28", "a": {}}}}, now=0)
|
ctl.col.on_entities({"a": {"sensor.outside": {"s": "28", "a": {}}}}, now=0)
|
||||||
state = ctl.model_state()
|
state = ctl.model_state()
|
||||||
assert [d["id"] for d in state["devices"]] == ["cooling"] # 28 °C in January: cooling
|
# 28 °C in January: cooling (no room model here: not optimised, its fallback)
|
||||||
|
assert [d["id"] for d in state["devices"] + state["fallback"]] == ["cooling"]
|
||||||
|
assert list(state["refused"]) == ["cooling"]
|
||||||
acts = ctl.actuators()
|
acts = ctl.actuators()
|
||||||
assert acts["heating"]["switchover"]["active"] is False and acts["cooling"]["switchover"]["active"] is True
|
assert acts["heating"]["switchover"]["active"] is False and acts["cooling"]["switchover"]["active"] is True
|
||||||
assert json.dumps(acts)
|
assert json.dumps(acts)
|
||||||
|
|||||||
@@ -81,6 +81,11 @@ type Core struct {
|
|||||||
started time.Time
|
started time.Time
|
||||||
sent map[string]string // discovery topics sent -> config
|
sent map[string]string // discovery topics sent -> config
|
||||||
events []string // recent events for the page
|
events []string // recent events for the page
|
||||||
|
// OnNominal: the controllable devices' nominal power changed (0: not
|
||||||
|
// known; the grid identity then offers no LPC: grid.go). Called in a
|
||||||
|
// goroutine (the grid link takes its own lock, then this core's).
|
||||||
|
OnNominal func(w float64)
|
||||||
|
lastNominal float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCore(opts Options, store *Store, backend Backend, profiles map[string]*Profile, signer *Signer) *Core {
|
func NewCore(opts Options, store *Store, backend Backend, profiles map[string]*Profile, signer *Signer) *Core {
|
||||||
@@ -255,6 +260,7 @@ func (c *Core) Update(ski string, useCases []string, values map[string]any) {
|
|||||||
if d.Kind == KindControlBox {
|
if d.Kind == KindControlBox {
|
||||||
c.evaluateGrid()
|
c.evaluateGrid()
|
||||||
}
|
}
|
||||||
|
c.nominalChanged() // a heat pump's nominal_max, a wallbox's current_max or phases came
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- the owner's actions (the page) ------------------------------------------
|
// -- the owner's actions (the page) ------------------------------------------
|
||||||
@@ -295,6 +301,7 @@ func (c *Core) Pair(ski, who string) error {
|
|||||||
c.store.Devices[d.SKI] = d
|
c.store.Devices[d.SKI] = d
|
||||||
c.save()
|
c.save()
|
||||||
c.audit("paired %s (%s %s, SKI %s) by %s", d.Name, d.Brand, d.Model, d.SKI, who)
|
c.audit("paired %s (%s %s, SKI %s) by %s", d.Name, d.Brand, d.Model, d.SKI, who)
|
||||||
|
c.nominalChanged()
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
return c.backend.Pair(d.SKI, d.Kind)
|
return c.backend.Pair(d.SKI, d.Kind)
|
||||||
}
|
}
|
||||||
@@ -332,6 +339,7 @@ func (c *Core) Unpair(id, who string) error {
|
|||||||
delete(c.store.Devices, d.SKI)
|
delete(c.store.Devices, d.SKI)
|
||||||
c.save()
|
c.save()
|
||||||
c.audit("unpaired %s (SKI %s) by %s", d.Name, d.SKI, who)
|
c.audit("unpaired %s (SKI %s) by %s", d.Name, d.SKI, who)
|
||||||
|
c.nominalChanged()
|
||||||
ski := d.SKI
|
ski := d.SKI
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
return c.backend.Unpair(ski)
|
return c.backend.Unpair(ski)
|
||||||
@@ -539,21 +547,84 @@ func (c *Core) LimitWritten(ski string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NominalW: what the bridge says it may draw as a controllable system (the
|
// NominalW: what the bridge declares it may draw as a controllable system
|
||||||
// controllable devices' sum; 32 kW before any is paired).
|
// toward the grid operator's control box (LPC): the controllable devices'
|
||||||
func (c *Core) NominalW() float64 {
|
// sum, each its reported or rated power (maxW). Unknown (0, and why) when no
|
||||||
|
// controllable device is paired or any of them has no known power: then
|
||||||
|
// nothing is declared and the bridge offers no LPC (grid.go), never a
|
||||||
|
// guess (docs/parameters.md U3). missing: the devices without a power.
|
||||||
|
func (c *Core) NominalW() (w float64, missing []string) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
t := 0.0
|
return c.nominal()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Core) nominal() (float64, []string) {
|
||||||
|
t, n, missing := 0.0, 0, []string{}
|
||||||
for _, d := range c.devices {
|
for _, d := range c.devices {
|
||||||
if d.Controllable() {
|
if !d.Controllable() {
|
||||||
t += c.maxW(d)
|
continue
|
||||||
|
}
|
||||||
|
n++
|
||||||
|
if w, ok := c.maxW(d); ok {
|
||||||
|
t += w
|
||||||
|
} else {
|
||||||
|
missing = append(missing, d.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if t == 0 {
|
sort.Strings(missing)
|
||||||
return 32000
|
if n == 0 || len(missing) > 0 {
|
||||||
|
return 0, missing
|
||||||
}
|
}
|
||||||
return t
|
return t, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NominalRefusal: why no nominal power is declared ("" when one is).
|
||||||
|
func NominalRefusal(w float64, missing []string) string {
|
||||||
|
switch {
|
||||||
|
case w > 0:
|
||||||
|
return ""
|
||||||
|
case len(missing) > 0:
|
||||||
|
return "nominal power unknown: enter the controllable devices' ratings (" + strings.Join(missing, ", ") + ")"
|
||||||
|
}
|
||||||
|
return "nominal power unknown: no controllable device (heat pump, wallbox) paired"
|
||||||
|
}
|
||||||
|
|
||||||
|
// nominalChanged: tells the grid identity when the declared power changed
|
||||||
|
// (under c.mu).
|
||||||
|
func (c *Core) nominalChanged() {
|
||||||
|
w, _ := c.nominal()
|
||||||
|
if w == c.lastNominal {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.lastNominal = w
|
||||||
|
if w > 0 {
|
||||||
|
c.audit("LPC: nominal power %.0f W declared to the grid operator's control box", w)
|
||||||
|
} else {
|
||||||
|
c.audit("LPC: no nominal power declared (not known): LPC not offered")
|
||||||
|
}
|
||||||
|
if c.OnNominal != nil {
|
||||||
|
go c.OnNominal(w)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRating: the owner's rating of a controllable device's power (W, 0:
|
||||||
|
// none), where the device reports none.
|
||||||
|
func (c *Core) SetRating(id string, w float64, who string) error {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
d := c.device(id)
|
||||||
|
if d == nil || !d.Paired || !d.Controllable() {
|
||||||
|
return fmt.Errorf("no paired heat pump or wallbox %s", id)
|
||||||
|
}
|
||||||
|
if w < 0 || w > 1e6 || w != w {
|
||||||
|
return fmt.Errorf("%w: a power from 0 to 1000 kW", ErrRefused)
|
||||||
|
}
|
||||||
|
d.RatedW = w
|
||||||
|
c.save()
|
||||||
|
c.audit("rating of %s: %.0f W, by %s", d.Name, w, who)
|
||||||
|
c.nominalChanged()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Core) controlBox() *Device {
|
func (c *Core) controlBox() *Device {
|
||||||
@@ -584,14 +655,17 @@ func (c *Core) evaluateGrid() {
|
|||||||
}
|
}
|
||||||
g := &c.grid
|
g := &c.grid
|
||||||
g.ControlBox, g.Device = cb.SKI, cb.ID
|
g.ControlBox, g.Device = cb.SKI, cb.ID
|
||||||
|
// the control box's own failsafe values (its LPC writes, read back from
|
||||||
|
// the grid identity); until it sent them the law's and the use case's
|
||||||
|
// (law.go), as the grid identity holds them toward it
|
||||||
g.FailsafeW, _ = cb.Number("failsafe_limit")
|
g.FailsafeW, _ = cb.Number("failsafe_limit")
|
||||||
if g.FailsafeW == 0 {
|
if g.FailsafeW <= 0 {
|
||||||
g.FailsafeW = 4200 // §14a's minimum, until the control box writes its own
|
g.FailsafeW = Para14aMinW
|
||||||
}
|
}
|
||||||
if m, ok := cb.Number("failsafe_duration"); ok && m > 0 {
|
if m, ok := cb.Number("failsafe_duration"); ok && m > 0 {
|
||||||
g.FailsafeS = m * 60
|
g.FailsafeS = m * 60
|
||||||
} else {
|
} else {
|
||||||
g.FailsafeS = 7200
|
g.FailsafeS = LPCFailsafeDurationMin.Seconds()
|
||||||
}
|
}
|
||||||
hb := cb.State == "online" && !g.lastHeartbeat.IsZero() && now.Sub(g.lastHeartbeat) <= c.opts.HeartbeatGrace
|
hb := cb.State == "online" && !g.lastHeartbeat.IsZero() && now.Sub(g.lastHeartbeat) <= c.opts.HeartbeatGrace
|
||||||
if !hb && g.lastHeartbeat.IsZero() && now.Sub(c.started) <= c.opts.HeartbeatGrace {
|
if !hb && g.lastHeartbeat.IsZero() && now.Sub(c.started) <= c.opts.HeartbeatGrace {
|
||||||
@@ -655,29 +729,47 @@ func untilText(u int64) string {
|
|||||||
return time.Unix(u, 0).UTC().Format("15:04 UTC")
|
return time.Unix(u, 0).UTC().Format("15:04 UTC")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Core) phases(d *Device) float64 {
|
// reportedPhases: the wallbox's phase count as it reports it (1-3).
|
||||||
|
func reportedPhases(d *Device) (float64, bool) {
|
||||||
if p, ok := d.Number("phases"); ok && p >= 1 && p <= 3 {
|
if p, ok := d.Number("phases"); ok && p >= 1 && p <= 3 {
|
||||||
return p
|
return p, true
|
||||||
}
|
}
|
||||||
return 3
|
return 0, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// maxW: what a controllable device can draw (its share's weight).
|
// phases: for turning a W budget into amps (a limit): the reported count,
|
||||||
func (c *Core) maxW(d *Device) float64 {
|
// else the safe side (OrderPhases: never more than the budget). Never for
|
||||||
|
// declaring a power (maxW).
|
||||||
|
func (c *Core) phases(d *Device) float64 {
|
||||||
|
if p, ok := reportedPhases(d); ok {
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
return OrderPhases
|
||||||
|
}
|
||||||
|
|
||||||
|
// maxW: what a controllable device can draw, and whether it is known: a
|
||||||
|
// heat pump's reported nominal_max, a wallbox's reported current_max times
|
||||||
|
// its reported phases and the voltage, else the owner's rating (RatedW);
|
||||||
|
// else not known (never a guess: docs/parameters.md U3).
|
||||||
|
func (c *Core) maxW(d *Device) (float64, bool) {
|
||||||
switch d.Kind {
|
switch d.Kind {
|
||||||
case KindHeatPump:
|
case KindHeatPump:
|
||||||
if n, ok := d.Number("nominal_max"); ok && n > 0 {
|
if n, ok := d.Number("nominal_max"); ok && n > 0 {
|
||||||
return n
|
return n, true
|
||||||
}
|
}
|
||||||
return 6000
|
|
||||||
case KindWallbox:
|
case KindWallbox:
|
||||||
a, ok := d.Number("current_max")
|
a, ok := d.Number("current_max")
|
||||||
if !ok || a <= 0 {
|
p, okp := reportedPhases(d)
|
||||||
a = 16
|
if ok && a > 0 && okp {
|
||||||
|
return a * c.opts.Voltage * p, true
|
||||||
}
|
}
|
||||||
return a * c.opts.Voltage * c.phases(d)
|
default:
|
||||||
|
return 0, false
|
||||||
}
|
}
|
||||||
return 0
|
if d.RatedW > 0 {
|
||||||
|
return d.RatedW, true
|
||||||
|
}
|
||||||
|
return 0, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// appliedW: what the bridge last allowed a device (its limit, or its whole
|
// appliedW: what the bridge last allowed a device (its limit, or its whole
|
||||||
@@ -693,7 +785,12 @@ func (c *Core) appliedW(d *Device) float64 {
|
|||||||
return a * c.opts.Voltage * c.phases(d)
|
return a * c.opts.Voltage * c.phases(d)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return c.maxW(d)
|
// its whole power; not known: the whole limit is assumed taken by it
|
||||||
|
// (allowedW then leaves the others nothing more: the safe side)
|
||||||
|
if w, ok := c.maxW(d); ok {
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
return c.grid.LimitW
|
||||||
}
|
}
|
||||||
|
|
||||||
// allowedW: the most a device may be given while the grid operator's limit
|
// allowedW: the most a device may be given while the grid operator's limit
|
||||||
@@ -738,12 +835,21 @@ func (c *Core) passOn() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if _, ok := d.Applied["current_limit"]; ok && d.Available["current_limit"] {
|
if _, ok := d.Applied["current_limit"]; ok && d.Available["current_limit"] {
|
||||||
maxA, _ := d.Number("current_max")
|
// back to the wallbox's own maximum, or the current it had
|
||||||
if maxA <= 0 {
|
// before the limit; neither known: left, said (never a guess)
|
||||||
maxA = 16
|
maxA, ok := d.Number("current_max")
|
||||||
|
if !ok || maxA <= 0 {
|
||||||
|
maxA, ok = d.Applied["_before_current"]
|
||||||
|
}
|
||||||
|
if !ok || maxA <= 0 {
|
||||||
|
c.event("%s: the grid limit ended; its current stays at %.0f A: its maximum is not known (set it on the wallbox)",
|
||||||
|
d.Name, d.Applied["current_limit"])
|
||||||
|
delete(d.Applied, "current_limit")
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
if err := c.backend.WriteCurrent(d, maxA); err == nil {
|
if err := c.backend.WriteCurrent(d, maxA); err == nil {
|
||||||
delete(d.Applied, "current_limit")
|
delete(d.Applied, "current_limit")
|
||||||
|
delete(d.Applied, "_before_current")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -766,6 +872,11 @@ func (c *Core) passOn() {
|
|||||||
}
|
}
|
||||||
d.Applied["limit"] = s
|
d.Applied["limit"] = s
|
||||||
} else {
|
} else {
|
||||||
|
if _, had := d.Applied["current_limit"]; !had {
|
||||||
|
if cur, ok := d.Number("current_limit"); ok && cur > 0 {
|
||||||
|
d.Applied["_before_current"] = cur // what to give back when the limit ends
|
||||||
|
}
|
||||||
|
}
|
||||||
a := round(s/(c.opts.Voltage*c.phases(d)), 1)
|
a := round(s/(c.opts.Voltage*c.phases(d)), 1)
|
||||||
what = fmt.Sprintf("current %.0f A", a)
|
what = fmt.Sprintf("current %.0f A", a)
|
||||||
if c.dry(d) == "" {
|
if c.dry(d) == "" {
|
||||||
@@ -801,9 +912,22 @@ func (c *Core) dry(d *Device) string {
|
|||||||
// Shares of a limit over devices (W per device id).
|
// Shares of a limit over devices (W per device id).
|
||||||
func (c *Core) Shares(devs []*Device, limit float64) map[string]float64 {
|
func (c *Core) Shares(devs []*Device, limit float64) map[string]float64 {
|
||||||
out := map[string]float64{}
|
out := map[string]float64{}
|
||||||
total := 0.0
|
// each device's weight: its power; any not known: equal shares (an
|
||||||
|
// engineering rule, not a fact: the sum stays within the limit)
|
||||||
|
weight := map[string]float64{}
|
||||||
|
total, known := 0.0, true
|
||||||
for _, d := range devs {
|
for _, d := range devs {
|
||||||
total += c.maxW(d)
|
w, ok := c.maxW(d)
|
||||||
|
known = known && ok
|
||||||
|
weight[d.ID] = w
|
||||||
|
total += w
|
||||||
|
}
|
||||||
|
if !known {
|
||||||
|
total = 0
|
||||||
|
for _, d := range devs {
|
||||||
|
weight[d.ID] = 1
|
||||||
|
total++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if total <= 0 {
|
if total <= 0 {
|
||||||
return out
|
return out
|
||||||
@@ -811,14 +935,14 @@ func (c *Core) Shares(devs []*Device, limit float64) map[string]float64 {
|
|||||||
spare := 0.0
|
spare := 0.0
|
||||||
heat := 0.0
|
heat := 0.0
|
||||||
for _, d := range devs {
|
for _, d := range devs {
|
||||||
s := limit * c.maxW(d) / total
|
s := limit * weight[d.ID] / total
|
||||||
if total <= limit {
|
if known && total <= limit {
|
||||||
s = c.maxW(d)
|
s = weight[d.ID]
|
||||||
}
|
}
|
||||||
if d.Kind == KindWallbox {
|
if d.Kind == KindWallbox {
|
||||||
minA, ok := d.Number("current_min")
|
minA, ok := d.Number("current_min")
|
||||||
if !ok || minA <= 0 {
|
if !ok || minA <= 0 {
|
||||||
minA = 6
|
minA = IECMinChargeA
|
||||||
}
|
}
|
||||||
a := round(s/(c.opts.Voltage*c.phases(d)), 1)
|
a := round(s/(c.opts.Voltage*c.phases(d)), 1)
|
||||||
if a < minA {
|
if a < minA {
|
||||||
@@ -829,14 +953,18 @@ func (c *Core) Shares(devs []*Device, limit float64) map[string]float64 {
|
|||||||
s = a * c.opts.Voltage * c.phases(d)
|
s = a * c.opts.Voltage * c.phases(d)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
heat += c.maxW(d)
|
heat += weight[d.ID]
|
||||||
}
|
}
|
||||||
out[d.ID] = s
|
out[d.ID] = s
|
||||||
}
|
}
|
||||||
if spare > 0 && heat > 0 {
|
if spare > 0 && heat > 0 {
|
||||||
for _, d := range devs {
|
for _, d := range devs {
|
||||||
if d.Kind != KindWallbox {
|
if d.Kind != KindWallbox {
|
||||||
out[d.ID] = minf(c.maxW(d), out[d.ID]+spare*c.maxW(d)/heat)
|
more := out[d.ID] + spare*weight[d.ID]/heat
|
||||||
|
if w, ok := c.maxW(d); ok {
|
||||||
|
more = minf(w, more)
|
||||||
|
}
|
||||||
|
out[d.ID] = more
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -447,3 +447,91 @@ func TestNoConsentToControl(t *testing.T) {
|
|||||||
t.Fatalf("withdrawn: %s", b)
|
t.Fatalf("withdrawn: %s", b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// docs/parameters.md U3: only known values are declared to the grid
|
||||||
|
// operator's control box; unknown: nothing declared, LPC refused with the
|
||||||
|
// reason; the failsafe values are the control box's, else the law's and the
|
||||||
|
// use case's (law.go).
|
||||||
|
func TestNominalOnlyWhenKnown(t *testing.T) {
|
||||||
|
h := newHarness(t, Options{PassOn: true})
|
||||||
|
if w, missing := h.core.NominalW(); w != 0 || len(missing) != 0 ||
|
||||||
|
NominalRefusal(w, missing) != "nominal power unknown: no controllable device (heat pump, wallbox) paired" {
|
||||||
|
t.Fatalf("nothing paired: %v %v", w, missing)
|
||||||
|
}
|
||||||
|
var told []float64
|
||||||
|
var mu sync.Mutex
|
||||||
|
h.core.OnNominal = func(w float64) { mu.Lock(); told = append(told, w); mu.Unlock() }
|
||||||
|
hp := h.pair("hp01", "Vaillant", "aroTHERM plus", "HeatPumpAppliance", "Heat pump")
|
||||||
|
h.core.Update("hp01", []string{"LPC-EG", "MPC"}, map[string]any{"power": 3000.0})
|
||||||
|
wb := h.pair("wb01", "ABL", "eMH1", "ChargingStation", "Wallbox")
|
||||||
|
h.core.Update("wb01", []string{"EVCC", "OPEV"}, map[string]any{"current_max": 16.0}) // no phases reported
|
||||||
|
w, missing := h.core.NominalW()
|
||||||
|
if w != 0 || strings.Join(missing, ",") != "Heat pump,Wallbox" {
|
||||||
|
t.Fatalf("not known: %v %v", w, missing)
|
||||||
|
}
|
||||||
|
if NominalRefusal(w, missing) != "nominal power unknown: enter the controllable devices' ratings (Heat pump, Wallbox)" {
|
||||||
|
t.Fatal(NominalRefusal(w, missing))
|
||||||
|
}
|
||||||
|
// the heat pump reports its nominal power, the owner rates the wallbox: declared
|
||||||
|
h.core.Update("hp01", nil, map[string]any{"nominal_max": 9000.0})
|
||||||
|
if err := h.core.SetRating(wb.ID, 11000, "owner"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if w, _ := h.core.NominalW(); w != 20000 {
|
||||||
|
t.Fatalf("declared: %v", w)
|
||||||
|
}
|
||||||
|
// the wallbox's own report wins over the rating
|
||||||
|
h.core.Update("wb01", nil, map[string]any{"phases": 1.0})
|
||||||
|
if w, _ := h.core.NominalW(); w != 9000+16*230 {
|
||||||
|
t.Fatalf("reported: %v", w)
|
||||||
|
}
|
||||||
|
if err := h.core.SetRating(hp.ID, -1, "owner"); err == nil {
|
||||||
|
t.Fatal("a negative rating")
|
||||||
|
}
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
// each change told (in goroutines: in any order)
|
||||||
|
seen := map[float64]bool{}
|
||||||
|
for _, w := range told {
|
||||||
|
seen[w] = true
|
||||||
|
}
|
||||||
|
if !seen[20000] || !seen[9000+16*230] {
|
||||||
|
t.Fatalf("the grid identity told: %v", told)
|
||||||
|
}
|
||||||
|
if h.core.lastNominal != 9000+16*230 {
|
||||||
|
t.Fatalf("last: %v", h.core.lastNominal)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFailsafeFromTheControlBoxElseTheLaw(t *testing.T) {
|
||||||
|
h := newHarness(t, Options{})
|
||||||
|
h.pair("cb01", "Theben", "CONEXA control box", "ElectricitySupplySystem", "Control box")
|
||||||
|
h.core.Update("cb01", []string{"LPC-CB"}, map[string]any{"heartbeat": true})
|
||||||
|
if h.core.grid.FailsafeW != Para14aMinW || h.core.grid.FailsafeS != 7200 {
|
||||||
|
t.Fatalf("not sent yet: %+v", h.core.grid)
|
||||||
|
}
|
||||||
|
h.core.Update("cb01", nil, map[string]any{"failsafe_limit": 6000.0, "failsafe_duration": 240.0})
|
||||||
|
if h.core.grid.FailsafeW != 6000 || h.core.grid.FailsafeS != 240*60 {
|
||||||
|
t.Fatalf("the control box's: %+v", h.core.grid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLimitLiftedWithoutAKnownMaximum(t *testing.T) {
|
||||||
|
h := newHarness(t, Options{PassOn: true})
|
||||||
|
h.pair("cb01", "Theben", "CONEXA control box", "ElectricitySupplySystem", "Control box")
|
||||||
|
h.core.Update("cb01", []string{"LPC-CB"}, map[string]any{"heartbeat": true, "grid_limit": 0.0, "grid_limit_active": false})
|
||||||
|
wb := h.pair("wb01", "ABL", "eMH1", "ChargingStation", "Wallbox")
|
||||||
|
h.core.Update("wb01", []string{"EVCC", "OPEV"}, map[string]any{"current_limit": 13.0, "current_min": 6.0})
|
||||||
|
h.be.take()
|
||||||
|
h.core.Update("cb01", nil, map[string]any{"grid_limit": 4200.0, "grid_limit_active": true})
|
||||||
|
// its power unknown: the whole limit as its share, at the safe side's phases (3)
|
||||||
|
if w := h.be.take(); !contains(w, wb.ID+" current 6") {
|
||||||
|
t.Fatalf("limited: %v", w)
|
||||||
|
}
|
||||||
|
h.core.Update("cb01", nil, map[string]any{"grid_limit_active": false})
|
||||||
|
// its maximum unknown: back to the current it had before (13 A), never a made-up 16 A
|
||||||
|
if w := h.be.take(); !contains(w, wb.ID+" current 13") {
|
||||||
|
t.Fatalf("lifted: %v", w)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -197,6 +197,11 @@ func (e *EEBUS) Start(c *Core) error {
|
|||||||
if e.grid, err = startGrid(e, grid); err != nil {
|
if e.grid, err = startGrid(e, grid); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// the declared nominal power follows the devices (core.nominalChanged)
|
||||||
|
gl := e.grid
|
||||||
|
e.core.mu.Lock()
|
||||||
|
e.core.OnNominal = func(w float64) { gl.send(gridMsg{Cmd: "nominal", W: w}) }
|
||||||
|
e.core.mu.Unlock()
|
||||||
e.svc.Start()
|
e.svc.Start()
|
||||||
log.Printf("EEBUS: devices SKI %s (port %d), grid SKI %s (port %d)", e.ski, e.port+1, e.gridSKI, e.port)
|
log.Printf("EEBUS: devices SKI %s (port %d), grid SKI %s (port %d)", e.ski, e.port+1, e.gridSKI, e.port)
|
||||||
go e.poll()
|
go e.poll()
|
||||||
|
|||||||
@@ -285,6 +285,24 @@ func TestSHIPControlBoxAndHeatPump(t *testing.T) {
|
|||||||
d := core.devices[hpSKI]
|
d := core.devices[hpSKI]
|
||||||
return d.State == "online" && d.Available["limit"]
|
return d.State == "online" && d.Available["limit"]
|
||||||
})
|
})
|
||||||
|
// no nominal power the bridge can read from this heat pump (its eg/lpc
|
||||||
|
// characteristic does not come through here): the bridge declares none
|
||||||
|
// and offers no LPC (docs/parameters.md U3) until the owner rates it
|
||||||
|
core.mu.Lock()
|
||||||
|
hpID := core.devices[hpSKI].ID
|
||||||
|
_, known := core.maxW(core.devices[hpSKI])
|
||||||
|
core.mu.Unlock()
|
||||||
|
if !known {
|
||||||
|
if w, missing := core.NominalW(); w != 0 || len(missing) != 1 {
|
||||||
|
t.Fatalf("nominal without a rating: %v %v", w, missing)
|
||||||
|
}
|
||||||
|
if err := core.SetRating(hpID, 9000, "owner"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if w, _ := core.NominalW(); w != 9000 {
|
||||||
|
t.Fatalf("nominal: %v", w)
|
||||||
|
}
|
||||||
// the control box finds the bridge's LPC and writes a limit
|
// the control box finds the bridge's LPC and writes a limit
|
||||||
var target spineapi.EntityRemoteInterface
|
var target spineapi.EntityRemoteInterface
|
||||||
waitFor(t, "the bridge's LPC at the control box", 60*time.Second, func() bool {
|
waitFor(t, "the bridge's LPC at the control box", 60*time.Second, func() bool {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@@ -27,6 +28,8 @@ import (
|
|||||||
// stdin, events on its stdout (its log on stderr).
|
// stdin, events on its stdout (its log on stderr).
|
||||||
//
|
//
|
||||||
// parent -> child {"cmd": "pair"|"unpair"|"deny", "ski"} | {"cmd": "nominal", "w"} | {"cmd": "start"} (after the pairings)
|
// parent -> child {"cmd": "pair"|"unpair"|"deny", "ski"} | {"cmd": "nominal", "w"} | {"cmd": "start"} (after the pairings)
|
||||||
|
// nominal: the controllable devices' power (core.NominalW); w 0 (or absent): not
|
||||||
|
// known: nothing is declared and LPC is not offered (docs/parameters.md U3)
|
||||||
// child -> parent {"ev": "ski", "ski"} | {"ev": "seen", "ski", "brand", "model", "type", "name"}
|
// child -> parent {"ev": "ski", "ski"} | {"ev": "seen", "ski", "brand", "model", "type", "name"}
|
||||||
// {"ev": "request"|"denied"|"guard"|"written", "ski"} | {"ev": "connected", "ski", "up"}
|
// {"ev": "request"|"denied"|"guard"|"written", "ski"} | {"ev": "connected", "ski", "up"}
|
||||||
// {"ev": "values", "ski", "use_cases", "values"}
|
// {"ev": "values", "ski", "use_cases", "values"}
|
||||||
@@ -54,8 +57,13 @@ type gridLink struct {
|
|||||||
e *EEBUS
|
e *EEBUS
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
stdin *json.Encoder
|
stdin *json.Encoder
|
||||||
|
in io.WriteCloser // the child's stdin: closed, the child ends (and starts again)
|
||||||
paired map[string]bool
|
paired map[string]bool
|
||||||
ready chan struct{}
|
ready chan struct{}
|
||||||
|
// offered: the child offers LPC (a nominal power is known). A control
|
||||||
|
// box reads a device's use cases when it connects: when this changes
|
||||||
|
// the child starts again, so the control box sees it (docs/parameters.md U3).
|
||||||
|
offered bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// startGrid: the child, started again whenever it ends (its pairings sent
|
// startGrid: the child, started again whenever it ends (its pairings sent
|
||||||
@@ -104,12 +112,14 @@ func (g *gridLink) run(first chan error) error {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
nominal, _ := g.e.core.NominalW()
|
||||||
g.mu.Lock()
|
g.mu.Lock()
|
||||||
g.stdin = json.NewEncoder(in)
|
g.stdin, g.in = json.NewEncoder(in), in
|
||||||
for ski := range g.paired {
|
for ski := range g.paired {
|
||||||
_ = g.stdin.Encode(gridMsg{Cmd: "pair", SKI: ski})
|
_ = g.stdin.Encode(gridMsg{Cmd: "pair", SKI: ski})
|
||||||
}
|
}
|
||||||
_ = g.stdin.Encode(gridMsg{Cmd: "nominal", W: g.e.core.NominalW()})
|
g.offered = nominal > 0
|
||||||
|
_ = g.stdin.Encode(gridMsg{Cmd: "nominal", W: nominal})
|
||||||
_ = g.stdin.Encode(gridMsg{Cmd: "start"})
|
_ = g.stdin.Encode(gridMsg{Cmd: "start"})
|
||||||
g.mu.Unlock()
|
g.mu.Unlock()
|
||||||
sc := bufio.NewScanner(out)
|
sc := bufio.NewScanner(out)
|
||||||
@@ -122,7 +132,7 @@ func (g *gridLink) run(first chan error) error {
|
|||||||
g.handle(m, first)
|
g.handle(m, first)
|
||||||
}
|
}
|
||||||
g.mu.Lock()
|
g.mu.Lock()
|
||||||
g.stdin = nil
|
g.stdin, g.in = nil, nil
|
||||||
g.mu.Unlock()
|
g.mu.Unlock()
|
||||||
return cmd.Wait()
|
return cmd.Wait()
|
||||||
}
|
}
|
||||||
@@ -168,6 +178,13 @@ func (g *gridLink) send(m gridMsg) {
|
|||||||
g.paired[strings.ToLower(m.SKI)] = true
|
g.paired[strings.ToLower(m.SKI)] = true
|
||||||
case "unpair":
|
case "unpair":
|
||||||
delete(g.paired, strings.ToLower(m.SKI))
|
delete(g.paired, strings.ToLower(m.SKI))
|
||||||
|
case "nominal":
|
||||||
|
if (m.W > 0) != g.offered && g.in != nil {
|
||||||
|
// LPC offered or withdrawn: the child starts again with it
|
||||||
|
// (run sends the nominal power again), the control box reconnects
|
||||||
|
_ = g.in.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if g.stdin != nil {
|
if g.stdin != nil {
|
||||||
_ = g.stdin.Encode(m)
|
_ = g.stdin.Encode(m)
|
||||||
@@ -223,12 +240,16 @@ func RunGridChild() {
|
|||||||
g.svc = svc
|
g.svc = svc
|
||||||
g.lpc = cslpc.NewLPC(svc.LocalDevice().EntityForType(model.EntityTypeTypeCEM), g.onLPC)
|
g.lpc = cslpc.NewLPC(svc.LocalDevice().EntityForType(model.EntityTypeTypeCEM), g.onLPC)
|
||||||
svc.AddUseCase(g.lpc)
|
svc.AddUseCase(g.lpc)
|
||||||
// §14a's failsafe values until the control box writes its own
|
// No nominal power until the parent says it (the devices' reported or
|
||||||
|
// rated power): until then LPC is not offered (nominal below). The
|
||||||
|
// failsafe values until the control box writes its own: the law's
|
||||||
|
// minimum and the use case's shortest duration (law.go); an inactive
|
||||||
|
// limit at the same minimum, so the control box has one to write.
|
||||||
|
g.lpc.UpdateUseCaseAvailability(false)
|
||||||
for what, err := range map[string]error{
|
for what, err := range map[string]error{
|
||||||
"nominal": g.lpc.SetConsumptionNominalMax(32000),
|
"limit": g.lpc.SetConsumptionLimit(ucapi.LoadLimit{Value: Para14aMinW, IsChangeable: true, IsActive: false}),
|
||||||
"limit": g.lpc.SetConsumptionLimit(ucapi.LoadLimit{Value: 4200, IsChangeable: true, IsActive: false}),
|
"failsafe limit": g.lpc.SetFailsafeConsumptionActivePowerLimit(Para14aMinW, true),
|
||||||
"failsafe limit": g.lpc.SetFailsafeConsumptionActivePowerLimit(4200, true),
|
"failsafe duration": g.lpc.SetFailsafeDurationMinimum(LPCFailsafeDurationMin, true),
|
||||||
"failsafe duration": g.lpc.SetFailsafeDurationMinimum(2*time.Hour, true),
|
|
||||||
} {
|
} {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("LPC: %s: %v", what, err)
|
log.Printf("LPC: %s: %v", what, err)
|
||||||
@@ -270,7 +291,17 @@ func RunGridChild() {
|
|||||||
case "deny":
|
case "deny":
|
||||||
svc.CancelPairingWithSKI(m.SKI)
|
svc.CancelPairingWithSKI(m.SKI)
|
||||||
case "nominal":
|
case "nominal":
|
||||||
_ = g.lpc.SetConsumptionNominalMax(m.W)
|
// known: declared and LPC offered; not known: not offered (never a guess)
|
||||||
|
if m.W > 0 {
|
||||||
|
if err := g.lpc.SetConsumptionNominalMax(m.W); err != nil {
|
||||||
|
log.Printf("LPC: nominal: %v", err)
|
||||||
|
}
|
||||||
|
g.lpc.UpdateUseCaseAvailability(true)
|
||||||
|
log.Printf("LPC: nominal power %.0f W declared, LPC offered", m.W)
|
||||||
|
} else {
|
||||||
|
g.lpc.UpdateUseCaseAvailability(false)
|
||||||
|
log.Print("LPC: nominal power not known: LPC not offered")
|
||||||
|
}
|
||||||
case "start":
|
case "start":
|
||||||
if !started {
|
if !started {
|
||||||
started = true
|
started = true
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
// Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// The values the bridge uses that are not facts of a device (docs/
|
||||||
|
// parameters.md U3, E9: no number standing for a real-world fact lives in
|
||||||
|
// the code unless it is law, a specification's rule or a safe side). One
|
||||||
|
// place: every use refers to these.
|
||||||
|
|
||||||
|
// Para14aMinW: §14a EnWG's minimum power a grid operator must leave a
|
||||||
|
// controllable consumer (Mindestbezugsleistung, 4.2 kW): Bundesnetzagentur,
|
||||||
|
// Festlegung BK6-22-300 of 27.11.2023, Anlage 1 (netzorientierte Steuerung).
|
||||||
|
// Used only as the LPC failsafe limit the bridge holds until the control box
|
||||||
|
// writes its own (the lowest the operator may impose: the safe side); the
|
||||||
|
// control box's own value wins as soon as it sent one. Not applied: the
|
||||||
|
// higher minimum for several devices or large heat pumps behind one energy
|
||||||
|
// manager (Anlage 1's scaling): the control box computes the limit.
|
||||||
|
const Para14aMinW = 4200.0
|
||||||
|
|
||||||
|
// LPCFailsafeDurationMin: the shortest failsafe duration the EEBUS use case
|
||||||
|
// allows (UC TS Limitation of Power Consumption V1.0.0, scenario 2: the
|
||||||
|
// failsafe duration minimum is 2 h to 24 h; eebus-go's cs/lpc
|
||||||
|
// SetFailsafeDurationMinimum refuses anything outside). Held until the
|
||||||
|
// control box writes its own.
|
||||||
|
const LPCFailsafeDurationMin = 2 * time.Hour
|
||||||
|
|
||||||
|
// OrderPhases: a W budget turned into amps for a wallbox whose phase count
|
||||||
|
// is not known: three phases, the most a connection has, so the current
|
||||||
|
// never lets it draw more than the budget (the safe side, as the agent's
|
||||||
|
// evphases.ASSUMED["order"]). Never used to declare a power.
|
||||||
|
const OrderPhases = 3.0
|
||||||
|
|
||||||
|
// IECMinChargeA: the least charging current a wallbox can signal (IEC
|
||||||
|
// 61851-1, control pilot PWM: 6 A); below it a wallbox's share pauses it.
|
||||||
|
// The wallbox's own current_min wins where it reports one.
|
||||||
|
const IECMinChargeA = 6.0
|
||||||
@@ -139,6 +139,9 @@ type Device struct {
|
|||||||
PairedBy string `json:"paired_by,omitempty"`
|
PairedBy string `json:"paired_by,omitempty"`
|
||||||
UseCases []string `json:"use_cases,omitempty"`
|
UseCases []string `json:"use_cases,omitempty"`
|
||||||
ReadOnly bool `json:"read_only,omitempty"` // commands checked and logged, never written (a dry run)
|
ReadOnly bool `json:"read_only,omitempty"` // commands checked and logged, never written (a dry run)
|
||||||
|
// RatedW: the owner's rating of a controllable device's power (W, the
|
||||||
|
// page; from its nameplate) where the device reports none (core.maxW)
|
||||||
|
RatedW float64 `json:"rated_w,omitempty"`
|
||||||
// runtime only
|
// runtime only
|
||||||
State string `json:"-"` // discovered | requesting | connecting | online | offline | denied
|
State string `json:"-"` // discovered | requesting | connecting | online | offline | denied
|
||||||
Values map[string]any `json:"-"`
|
Values map[string]any `json:"-"`
|
||||||
|
|||||||
@@ -46,8 +46,12 @@ var texts = map[string]map[string]string{
|
|||||||
"passon": "Passed on to", "kind_control-box": "Control box", "kind_heat-pump": "Heat pump", "kind_wallbox": "Wallbox",
|
"passon": "Passed on to", "kind_control-box": "Control box", "kind_heat-pump": "Heat pump", "kind_wallbox": "Wallbox",
|
||||||
"kind_inverter": "Inverter", "kind_meter": "Meter", "kind_other": "Device", "untested": "untested on hardware",
|
"kind_inverter": "Inverter", "kind_meter": "Meter", "kind_other": "Device", "untested": "untested on hardware",
|
||||||
"on": "on", "off": "off",
|
"on": "on", "off": "off",
|
||||||
"no_control": "Control: no consent. Cantabile reads these devices and hands the grid operator's limit to its agent, but writes nothing to them (not even the grid operator's limit). Give your consent in the add-on's configuration (Cantabile may control devices).",
|
"no_control": "Control: no consent. Cantabile reads these devices and hands the grid operator's limit to its agent, but writes nothing to them (not even the grid operator's limit). Give your consent in the add-on's configuration (Cantabile may control devices).",
|
||||||
"all_ro": "Read only (a dry run): commands are checked and logged, nothing is written to the devices.",
|
"all_ro": "Read only (a dry run): commands are checked and logged, nothing is written to the devices.",
|
||||||
|
"lpc_declared": "Toward the control box: nominal power {} W declared (LPC offered).",
|
||||||
|
"lpc_refused": "LPC not offered: {}. Nothing is declared to the grid operator's control box until it is known.",
|
||||||
|
"rating": "Rated power (kW)", "rating_none": "not entered",
|
||||||
|
"rating_note": "From the device's nameplate, where it reports no power itself: the bridge declares the devices' power to the grid operator's control box only when it knows it, never a guess.",
|
||||||
},
|
},
|
||||||
"de": {
|
"de": {
|
||||||
"title": "Cantabile EEBUS", "intro": "Wärmepumpen, Wallboxen und die Steuerbox des Netzbetreibers sprechen EEBUS. Koppeln Sie jedes Gerät hier einmal: vergleichen Sie die SKI mit der auf dem Display, dem Etikett oder in der App des Geräts, dann bestätigen.",
|
"title": "Cantabile EEBUS", "intro": "Wärmepumpen, Wallboxen und die Steuerbox des Netzbetreibers sprechen EEBUS. Koppeln Sie jedes Gerät hier einmal: vergleichen Sie die SKI mit der auf dem Display, dem Etikett oder in der App des Geräts, dann bestätigen.",
|
||||||
@@ -66,8 +70,12 @@ var texts = map[string]map[string]string{
|
|||||||
"passon": "Weitergegeben an", "kind_control-box": "Steuerbox", "kind_heat-pump": "Wärmepumpe", "kind_wallbox": "Wallbox",
|
"passon": "Weitergegeben an", "kind_control-box": "Steuerbox", "kind_heat-pump": "Wärmepumpe", "kind_wallbox": "Wallbox",
|
||||||
"kind_inverter": "Wechselrichter", "kind_meter": "Zähler", "kind_other": "Gerät", "untested": "nicht an echter Hardware getestet",
|
"kind_inverter": "Wechselrichter", "kind_meter": "Zähler", "kind_other": "Gerät", "untested": "nicht an echter Hardware getestet",
|
||||||
"on": "an", "off": "aus",
|
"on": "an", "off": "aus",
|
||||||
"no_control": "Steuern: keine Einwilligung. Cantabile liest diese Geräte und gibt das Limit des Netzbetreibers an seinen Agenten weiter, schreibt aber nichts an sie (auch nicht das Limit des Netzbetreibers). Geben Sie Ihre Einwilligung in der Konfiguration des Add-ons (Cantabile darf Geräte steuern).",
|
"no_control": "Steuern: keine Einwilligung. Cantabile liest diese Geräte und gibt das Limit des Netzbetreibers an seinen Agenten weiter, schreibt aber nichts an sie (auch nicht das Limit des Netzbetreibers). Geben Sie Ihre Einwilligung in der Konfiguration des Add-ons (Cantabile darf Geräte steuern).",
|
||||||
"all_ro": "Nur lesen (Probelauf): Befehle werden geprüft und protokolliert, nichts wird an die Geräte geschrieben.",
|
"all_ro": "Nur lesen (Probelauf): Befehle werden geprüft und protokolliert, nichts wird an die Geräte geschrieben.",
|
||||||
|
"lpc_declared": "Gegenüber der Steuerbox: Nennleistung {} W gemeldet (LPC angeboten).",
|
||||||
|
"lpc_refused": "LPC nicht angeboten: {}. Der Steuerbox des Netzbetreibers wird nichts gemeldet, bis sie bekannt ist.",
|
||||||
|
"rating": "Nennleistung (kW)", "rating_none": "nicht eingetragen",
|
||||||
|
"rating_note": "Vom Typenschild des Geräts, wo es selbst keine Leistung meldet: Die Bridge meldet der Steuerbox des Netzbetreibers die Leistung der Geräte nur, wenn sie bekannt ist, nie geschätzt.",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,6 +140,9 @@ type dview struct {
|
|||||||
Status string
|
Status string
|
||||||
Untested bool
|
Untested bool
|
||||||
Applied string
|
Applied string
|
||||||
|
Rated bool // a heat pump or wallbox: the owner may enter its rating
|
||||||
|
RatedKW string // its rating now (kW), "" none
|
||||||
|
Reported bool // the device reports its power itself
|
||||||
}
|
}
|
||||||
|
|
||||||
type pview struct {
|
type pview struct {
|
||||||
@@ -147,6 +158,8 @@ type pview struct {
|
|||||||
Version string
|
Version string
|
||||||
Notice string // no consent to control, or the whole bridge read only
|
Notice string // no consent to control, or the whole bridge read only
|
||||||
AllReadOnly bool
|
AllReadOnly bool
|
||||||
|
LPC string // the nominal power declared toward the control box, or why none (docs/parameters.md U3)
|
||||||
|
LPCRefused bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func fmtValue(s Spec, v any, lang string) string {
|
func fmtValue(s Spec, v any, lang string) string {
|
||||||
@@ -185,6 +198,11 @@ func (p *Page) view(lang string) pview {
|
|||||||
GridSKI: gridSKI(p.core.backend),
|
GridSKI: gridSKI(p.core.backend),
|
||||||
Events: events, Log: p.core.store.Recent(20), Sim: p.sim != nil, Version: version}
|
Events: events, Log: p.core.store.Recent(20), Sim: p.sim != nil, Version: version}
|
||||||
pv.GridText = t[grid.State]
|
pv.GridText = t[grid.State]
|
||||||
|
if w, missing := p.core.NominalW(); w > 0 {
|
||||||
|
pv.LPC = strings.Replace(t["lpc_declared"], "{}", strconv.FormatFloat(w, 'f', 0, 64), 1)
|
||||||
|
} else {
|
||||||
|
pv.LPC, pv.LPCRefused = strings.Replace(t["lpc_refused"], "{}", NominalRefusal(w, missing), 1), true
|
||||||
|
}
|
||||||
pv.AllReadOnly = p.core.opts.ReadOnly
|
pv.AllReadOnly = p.core.opts.ReadOnly
|
||||||
if p.core.opts.NoControl {
|
if p.core.opts.NoControl {
|
||||||
pv.Notice = t["no_control"]
|
pv.Notice = t["no_control"]
|
||||||
@@ -210,6 +228,15 @@ func (p *Page) view(lang string) pview {
|
|||||||
}
|
}
|
||||||
p.core.mu.Lock()
|
p.core.mu.Lock()
|
||||||
dv.Level, dv.Custodian = p.core.Level(&d, "*")
|
dv.Level, dv.Custodian = p.core.Level(&d, "*")
|
||||||
|
if d.Controllable() {
|
||||||
|
dv.Rated = true
|
||||||
|
if d.RatedW > 0 {
|
||||||
|
dv.RatedKW = strconv.FormatFloat(d.RatedW/1000, 'f', -1, 64)
|
||||||
|
}
|
||||||
|
x := d
|
||||||
|
x.RatedW = 0
|
||||||
|
_, dv.Reported = p.core.maxW(&x)
|
||||||
|
}
|
||||||
if pr := p.core.profiles[d.Profile]; pr != nil && pr.Status != "verified" {
|
if pr := p.core.profiles[d.Profile]; pr != nil && pr.Status != "verified" {
|
||||||
dv.Untested = true
|
dv.Untested = true
|
||||||
}
|
}
|
||||||
@@ -295,6 +322,7 @@ var tmpl = template.Must(template.New("page").Funcs(template.FuncMap{
|
|||||||
{{if .Grid.ControlBox}}<div class="row"><span class="big {{if .Grid.Active}}bad{{else}}ok{{end}}">{{.GridText}}</span>
|
{{if .Grid.ControlBox}}<div class="row"><span class="big {{if .Grid.Active}}bad{{else}}ok{{end}}">{{.GridText}}</span>
|
||||||
{{if .Grid.Active}}<span class="big">{{printf "%.0f" .Grid.LimitW}} W</span> <span class="note">{{.Until}}</span>{{end}}</div>
|
{{if .Grid.Active}}<span class="big">{{printf "%.0f" .Grid.LimitW}} W</span> <span class="note">{{.Until}}</span>{{end}}</div>
|
||||||
<div class="note">{{.T.sealed_note}}</div>{{else}}<div class="note">{{.T.no_cb}}</div>{{end}}
|
<div class="note">{{.T.sealed_note}}</div>{{else}}<div class="note">{{.T.no_cb}}</div>{{end}}
|
||||||
|
<div class="note{{if .LPCRefused}} bad{{end}}" id="lpc">{{.LPC}}</div>
|
||||||
{{if .Sim}}<div class="row" style="margin-top:.5rem">
|
{{if .Sim}}<div class="row" style="margin-top:.5rem">
|
||||||
<form method="post"><input type="hidden" name="action" value="sim_limit"><button>{{.T.sim_limit}}</button></form>
|
<form method="post"><input type="hidden" name="action" value="sim_limit"><button>{{.T.sim_limit}}</button></form>
|
||||||
<form method="post"><input type="hidden" name="action" value="sim_end"><button class="off">{{.T.sim_end}}</button></form>
|
<form method="post"><input type="hidden" name="action" value="sim_end"><button class="off">{{.T.sim_end}}</button></form>
|
||||||
@@ -330,6 +358,8 @@ var tmpl = template.Must(template.New("page").Funcs(template.FuncMap{
|
|||||||
{{else}}<form method="post"><input type="hidden" name="action" value="command"><input type="hidden" name="dev" value="{{$d.ID}}"><input type="hidden" name="key" value="{{.Key}}"><input type="number" name="value" min="{{.Min}}" max="{{.Max}}" step="{{.Step}}" required> {{.Unit}} <button>{{$.T.set}}</button></form>{{end}}
|
{{else}}<form method="post"><input type="hidden" name="action" value="command"><input type="hidden" name="dev" value="{{$d.ID}}"><input type="hidden" name="key" value="{{.Key}}"><input type="number" name="value" min="{{.Min}}" max="{{.Max}}" step="{{.Step}}" required> {{.Unit}} <button>{{$.T.set}}</button></form>{{end}}
|
||||||
{{if eq .Level "protected"}}<form method="post" data-confirm="{{tr $.T "confirm_lift" .Name}}"><input type="hidden" name="action" value="protect"><input type="hidden" name="dev" value="{{$d.ID}}"><input type="hidden" name="key" value="{{.Key}}"><input type="hidden" name="level" value="none"><button class="off">{{$.T.lift}}</button></form>
|
{{if eq .Level "protected"}}<form method="post" data-confirm="{{tr $.T "confirm_lift" .Name}}"><input type="hidden" name="action" value="protect"><input type="hidden" name="dev" value="{{$d.ID}}"><input type="hidden" name="key" value="{{.Key}}"><input type="hidden" name="level" value="none"><button class="off">{{$.T.lift}}</button></form>
|
||||||
{{else}}<form method="post"><input type="hidden" name="action" value="protect"><input type="hidden" name="dev" value="{{$d.ID}}"><input type="hidden" name="key" value="{{.Key}}"><input type="hidden" name="level" value="protected"><button class="off">{{$.T.protect}}</button></form>{{end}}{{end}}</td></tr>{{end}}</table></div>{{end}}
|
{{else}}<form method="post"><input type="hidden" name="action" value="protect"><input type="hidden" name="dev" value="{{$d.ID}}"><input type="hidden" name="key" value="{{.Key}}"><input type="hidden" name="level" value="protected"><button class="off">{{$.T.protect}}</button></form>{{end}}{{end}}</td></tr>{{end}}</table></div>{{end}}
|
||||||
|
{{if .Rated}}<div class="card" id="rating"><h3>{{$.T.rating}}</h3><p class="note">{{$.T.rating_note}}</p>
|
||||||
|
<form method="post"><input type="hidden" name="action" value="rating"><input type="hidden" name="dev" value="{{.ID}}"><input type="number" name="kw" min="0" max="1000" step="any" value="{{.RatedKW}}" placeholder="{{$.T.rating_none}}"> kW <button>{{$.T.set}}</button></form></div>{{end}}
|
||||||
{{if eq .Level "sealed"}}<p class="note lock">{{$.T.sealed_note}} ({{$.T.custodian}}: {{.Custodian}})</p>{{else}}
|
{{if eq .Level "sealed"}}<p class="note lock">{{$.T.sealed_note}} ({{$.T.custodian}}: {{.Custodian}})</p>{{else}}
|
||||||
<form method="post" data-confirm="{{tr $.T "confirm_unpair" .Name}}"><input type="hidden" name="action" value="unpair"><input type="hidden" name="dev" value="{{.ID}}"><button class="off">{{$.T.unpair}}</button></form>{{end}}
|
<form method="post" data-confirm="{{tr $.T "confirm_unpair" .Name}}"><input type="hidden" name="action" value="unpair"><input type="hidden" name="dev" value="{{.ID}}"><button class="off">{{$.T.unpair}}</button></form>{{end}}
|
||||||
</section>{{end}}
|
</section>{{end}}
|
||||||
@@ -398,6 +428,14 @@ func (p *Page) post(w http.ResponseWriter, r *http.Request) {
|
|||||||
err = p.core.SetLevel(dev, r.FormValue("key"), r.FormValue("level"), who)
|
err = p.core.SetLevel(dev, r.FormValue("key"), r.FormValue("level"), who)
|
||||||
case "command":
|
case "command":
|
||||||
_, err = p.core.Command(dev, r.FormValue("key"), r.FormValue("value"), who)
|
_, err = p.core.Command(dev, r.FormValue("key"), r.FormValue("value"), who)
|
||||||
|
case "rating":
|
||||||
|
kw := 0.0
|
||||||
|
if v := strings.TrimSpace(r.FormValue("kw")); v != "" {
|
||||||
|
if kw, err = strconv.ParseFloat(strings.Replace(v, ",", ".", 1), 64); err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = p.core.SetRating(dev, kw*1000, who)
|
||||||
case "sim_limit", "sim_end", "sim_hb_off", "sim_hb_on", "sim_plug":
|
case "sim_limit", "sim_end", "sim_hb_off", "sim_hb_on", "sim_plug":
|
||||||
if p.sim == nil {
|
if p.sim == nil {
|
||||||
err = errors.New("not simulating")
|
err = errors.New("not simulating")
|
||||||
@@ -405,7 +443,7 @@ func (p *Page) post(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
switch r.FormValue("action") {
|
switch r.FormValue("action") {
|
||||||
case "sim_limit":
|
case "sim_limit":
|
||||||
p.sim.GridLimit(4200, 15)
|
p.sim.GridLimit(Para14aMinW, 15) // the simulated control box: §14a's minimum
|
||||||
case "sim_end":
|
case "sim_end":
|
||||||
p.sim.GridLimit(0, 0)
|
p.sim.GridLimit(0, 0)
|
||||||
case "sim_hb_off":
|
case "sim_hb_off":
|
||||||
|
|||||||
@@ -92,3 +92,36 @@ func TestPageTextsBothLanguages(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// docs/parameters.md U3: the page says what is declared toward the control
|
||||||
|
// box, or why nothing is, and takes the owner's rating.
|
||||||
|
func TestPageLPCAndRating(t *testing.T) {
|
||||||
|
h := newHarness(t, Options{})
|
||||||
|
p := &Page{core: h.core, peers: []string{"172.30.32.2"}, ha: true}
|
||||||
|
do := func(method string, form url.Values) string {
|
||||||
|
r := httptest.NewRequest(method, "/", strings.NewReader(form.Encode()))
|
||||||
|
r.RemoteAddr = "172.30.32.2:1"
|
||||||
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
r.Header.Set("Accept-Language", "en")
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
p.ServeHTTP(w, r)
|
||||||
|
return w.Body.String()
|
||||||
|
}
|
||||||
|
if b := do("GET", nil); !strings.Contains(b, "LPC not offered: nominal power unknown: no controllable device (heat pump, wallbox) paired.") {
|
||||||
|
t.Fatal("no device: refused, said")
|
||||||
|
}
|
||||||
|
hp := h.pair("hp01", "Vaillant", "aroTHERM plus", "HeatPumpAppliance", "Heat pump")
|
||||||
|
b := do("GET", nil)
|
||||||
|
if !strings.Contains(b, "nominal power unknown: enter the controllable devices' ratings (Heat pump)") ||
|
||||||
|
!strings.Contains(b, `name="action" value="rating"`) {
|
||||||
|
t.Fatal("a heat pump without its power: refused, the rating form")
|
||||||
|
}
|
||||||
|
do("POST", url.Values{"action": {"rating"}, "dev": {hp.ID}, "kw": {"7,5"}})
|
||||||
|
if hp.RatedW != 7500 {
|
||||||
|
t.Fatalf("rated: %v", hp.RatedW)
|
||||||
|
}
|
||||||
|
if b := do("GET", nil); !strings.Contains(b, "Toward the control box: nominal power 7500 W declared (LPC offered).") ||
|
||||||
|
!strings.Contains(b, `value="7.5"`) {
|
||||||
|
t.Fatal("declared")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -54,7 +54,7 @@ const (
|
|||||||
func NewSim() *Sim {
|
func NewSim() *Sim {
|
||||||
return &Sim{paired: map[string]bool{}, now: time.Now, step: 5 * time.Second, devs: []*simDev{
|
return &Sim{paired: map[string]bool{}, now: time.Now, step: 5 * time.Second, devs: []*simDev{
|
||||||
{ski: SimControlBoxSKI, brand: "Simulated", model: "Control box (FNN)", typ: "ElectricitySupplySystem", name: "Control box (simulated)",
|
{ski: SimControlBoxSKI, brand: "Simulated", model: "Control box (FNN)", typ: "ElectricitySupplySystem", name: "Control box (simulated)",
|
||||||
ucs: []string{"LPC-CB", "MGCP"}, heartbeat: true, failsafeW: 4200, failsafeMin: 120},
|
ucs: []string{"LPC-CB", "MGCP"}, heartbeat: true, failsafeW: Para14aMinW, failsafeMin: LPCFailsafeDurationMin.Minutes()},
|
||||||
{ski: SimHeatPumpSKI, brand: "Simulated", model: "Heat pump (aroTHERM-like)", typ: "HeatPumpAppliance", name: "Heat pump (simulated)",
|
{ski: SimHeatPumpSKI, brand: "Simulated", model: "Heat pump (aroTHERM-like)", typ: "HeatPumpAppliance", name: "Heat pump (simulated)",
|
||||||
ucs: []string{"LPC-EG", "MPC"}, limitW: 9000},
|
ucs: []string{"LPC-EG", "MPC"}, limitW: 9000},
|
||||||
{ski: SimWallboxSKI, brand: "Simulated", model: "Wallbox 11 kW", typ: "ChargingStation", name: "Wallbox (simulated)",
|
{ski: SimWallboxSKI, brand: "Simulated", model: "Wallbox 11 kW", typ: "ChargingStation", name: "Wallbox (simulated)",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"id": "fronius-gen24",
|
"id": "fronius-gen24",
|
||||||
"version": "1.0.2",
|
"version": "1.0.3",
|
||||||
"manufacturer": "Fronius",
|
"manufacturer": "Fronius",
|
||||||
"name": "Symo / Primo GEN24 (with Smart Meter and BYD battery)",
|
"name": "Symo / Primo GEN24 (with Smart Meter and BYD battery)",
|
||||||
"models": ["Symo GEN24*", "Primo GEN24*", "Symo*", "Primo*"],
|
"models": ["Symo GEN24*", "Primo GEN24*", "Symo*", "Primo*"],
|
||||||
@@ -13,7 +13,8 @@
|
|||||||
{"name": "rated_inverter_kw", "label": "Inverter power, kW (optional)", "type": "number", "rating": "inverter_kw", "optional": true, "min": 0, "max": 1000},
|
{"name": "rated_inverter_kw", "label": "Inverter power, kW (optional)", "type": "number", "rating": "inverter_kw", "optional": true, "min": 0, "max": 1000},
|
||||||
{"name": "rated_pv_kwp", "label": "Solar panels, kWp (optional)", "type": "number", "rating": "pv_kwp", "optional": true, "min": 0, "max": 1000},
|
{"name": "rated_pv_kwp", "label": "Solar panels, kWp (optional)", "type": "number", "rating": "pv_kwp", "optional": true, "min": 0, "max": 1000},
|
||||||
{"name": "rated_battery_kwh", "label": "Battery capacity, kWh (optional)", "type": "number", "rating": "battery_kwh", "optional": true, "min": 0, "max": 1000},
|
{"name": "rated_battery_kwh", "label": "Battery capacity, kWh (optional)", "type": "number", "rating": "battery_kwh", "optional": true, "min": 0, "max": 1000},
|
||||||
{"name": "rated_battery_kw", "label": "Battery power, kW (optional)", "type": "number", "rating": "battery_kw", "optional": true, "min": 0, "max": 1000}
|
{"name": "rated_battery_kw", "label": "Battery power, kW (optional)", "type": "number", "rating": "battery_kw", "optional": true, "min": 0, "max": 1000},
|
||||||
|
{"name": "rated_battery_efficiency_pct", "label": "Battery round-trip efficiency, % (optional; datasheet)", "type": "number", "rating": "battery_efficiency_pct", "optional": true, "min": 1, "max": 100}
|
||||||
],
|
],
|
||||||
"setup": [
|
"setup": [
|
||||||
{"path": "ha", "domain": "fronius", "fields": ["host"],
|
{"path": "ha", "domain": "fronius", "fields": ["host"],
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"id": "hm8042-hybrid",
|
"id": "hm8042-hybrid",
|
||||||
"version": "0.3.1",
|
"version": "0.3.2",
|
||||||
"manufacturer": "HM",
|
"manufacturer": "HM",
|
||||||
"name": "HM8042 hybrid inverter (EPEVER protocol)",
|
"name": "HM8042 hybrid inverter (EPEVER protocol)",
|
||||||
"models": [
|
"models": [
|
||||||
@@ -71,6 +71,15 @@
|
|||||||
"optional": true,
|
"optional": true,
|
||||||
"min": 0,
|
"min": 0,
|
||||||
"max": 1000
|
"max": 1000
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "rated_battery_efficiency_pct",
|
||||||
|
"label": "Battery round-trip efficiency, % (optional; datasheet)",
|
||||||
|
"type": "number",
|
||||||
|
"rating": "battery_efficiency_pct",
|
||||||
|
"optional": true,
|
||||||
|
"min": 1,
|
||||||
|
"max": 100
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"setup": [
|
"setup": [
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"id": "huawei-sun2000",
|
"id": "huawei-sun2000",
|
||||||
"version": "1.0.2",
|
"version": "1.0.3",
|
||||||
"manufacturer": "Huawei",
|
"manufacturer": "Huawei",
|
||||||
"name": "SUN2000 inverter (with LUNA2000 battery and DTSU666 meter)",
|
"name": "SUN2000 inverter (with LUNA2000 battery and DTSU666 meter)",
|
||||||
"models": ["SUN2000-*KTL-L1", "SUN2000-*KTL-M1", "SUN2000-*KTL-M0", "SUN2000-*K-MAP0", "SUN2000-*K-MB0"],
|
"models": ["SUN2000-*KTL-L1", "SUN2000-*KTL-M1", "SUN2000-*KTL-M0", "SUN2000-*K-MAP0", "SUN2000-*K-MB0"],
|
||||||
@@ -17,7 +17,8 @@
|
|||||||
{"name": "rated_inverter_kw", "label": "Inverter power, kW (optional)", "type": "number", "rating": "inverter_kw", "optional": true, "min": 0, "max": 1000},
|
{"name": "rated_inverter_kw", "label": "Inverter power, kW (optional)", "type": "number", "rating": "inverter_kw", "optional": true, "min": 0, "max": 1000},
|
||||||
{"name": "rated_pv_kwp", "label": "Solar panels, kWp (optional)", "type": "number", "rating": "pv_kwp", "optional": true, "min": 0, "max": 1000},
|
{"name": "rated_pv_kwp", "label": "Solar panels, kWp (optional)", "type": "number", "rating": "pv_kwp", "optional": true, "min": 0, "max": 1000},
|
||||||
{"name": "rated_battery_kwh", "label": "Battery capacity, kWh (optional)", "type": "number", "rating": "battery_kwh", "optional": true, "min": 0, "max": 1000},
|
{"name": "rated_battery_kwh", "label": "Battery capacity, kWh (optional)", "type": "number", "rating": "battery_kwh", "optional": true, "min": 0, "max": 1000},
|
||||||
{"name": "rated_battery_kw", "label": "Battery power, kW (optional)", "type": "number", "rating": "battery_kw", "optional": true, "min": 0, "max": 1000}
|
{"name": "rated_battery_kw", "label": "Battery power, kW (optional)", "type": "number", "rating": "battery_kw", "optional": true, "min": 0, "max": 1000},
|
||||||
|
{"name": "rated_battery_efficiency_pct", "label": "Battery round-trip efficiency, % (optional; datasheet)", "type": "number", "rating": "battery_efficiency_pct", "optional": true, "min": 1, "max": 100}
|
||||||
],
|
],
|
||||||
"setup": [
|
"setup": [
|
||||||
{"path": "ha-integration", "addon": "huawei-solar", "domain": "huawei_solar", "fields": ["host", "port", "unit_id"],
|
{"path": "ha-integration", "addon": "huawei-solar", "domain": "huawei_solar", "fields": ["host", "port", "unit_id"],
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"id": "sma-sunny-boy",
|
"id": "sma-sunny-boy",
|
||||||
"version": "1.0.2",
|
"version": "1.0.3",
|
||||||
"manufacturer": "SMA",
|
"manufacturer": "SMA",
|
||||||
"name": "Sunny Boy / Sunny Tripower (with SMA Energy Meter)",
|
"name": "Sunny Boy / Sunny Tripower (with SMA Energy Meter)",
|
||||||
"models": ["SB*", "STP*", "Sunny Boy*", "Sunny Tripower*"],
|
"models": ["SB*", "STP*", "Sunny Boy*", "Sunny Tripower*"],
|
||||||
@@ -16,7 +16,8 @@
|
|||||||
{"name": "rated_inverter_kw", "label": "Inverter power, kW (optional)", "type": "number", "rating": "inverter_kw", "optional": true, "min": 0, "max": 1000},
|
{"name": "rated_inverter_kw", "label": "Inverter power, kW (optional)", "type": "number", "rating": "inverter_kw", "optional": true, "min": 0, "max": 1000},
|
||||||
{"name": "rated_pv_kwp", "label": "Solar panels, kWp (optional)", "type": "number", "rating": "pv_kwp", "optional": true, "min": 0, "max": 1000},
|
{"name": "rated_pv_kwp", "label": "Solar panels, kWp (optional)", "type": "number", "rating": "pv_kwp", "optional": true, "min": 0, "max": 1000},
|
||||||
{"name": "rated_battery_kwh", "label": "Battery capacity, kWh (optional)", "type": "number", "rating": "battery_kwh", "optional": true, "min": 0, "max": 1000},
|
{"name": "rated_battery_kwh", "label": "Battery capacity, kWh (optional)", "type": "number", "rating": "battery_kwh", "optional": true, "min": 0, "max": 1000},
|
||||||
{"name": "rated_battery_kw", "label": "Battery power, kW (optional)", "type": "number", "rating": "battery_kw", "optional": true, "min": 0, "max": 1000}
|
{"name": "rated_battery_kw", "label": "Battery power, kW (optional)", "type": "number", "rating": "battery_kw", "optional": true, "min": 0, "max": 1000},
|
||||||
|
{"name": "rated_battery_efficiency_pct", "label": "Battery round-trip efficiency, % (optional; datasheet)", "type": "number", "rating": "battery_efficiency_pct", "optional": true, "min": 1, "max": 100}
|
||||||
],
|
],
|
||||||
"setup": [
|
"setup": [
|
||||||
{"path": "ha", "domain": "sma", "fields": ["host", "password"],
|
{"path": "ha", "domain": "sma", "fields": ["host", "password"],
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"id": "solaredge-sunspec",
|
"id": "solaredge-sunspec",
|
||||||
"version": "1.0.2",
|
"version": "1.0.3",
|
||||||
"manufacturer": "SolarEdge",
|
"manufacturer": "SolarEdge",
|
||||||
"name": "SE inverter over Modbus TCP (SunSpec, with SolarEdge meter)",
|
"name": "SE inverter over Modbus TCP (SunSpec, with SolarEdge meter)",
|
||||||
"models": ["SE*"],
|
"models": ["SE*"],
|
||||||
@@ -15,7 +15,8 @@
|
|||||||
{"name": "rated_inverter_kw", "label": "Inverter power, kW (optional)", "type": "number", "rating": "inverter_kw", "optional": true, "min": 0, "max": 1000},
|
{"name": "rated_inverter_kw", "label": "Inverter power, kW (optional)", "type": "number", "rating": "inverter_kw", "optional": true, "min": 0, "max": 1000},
|
||||||
{"name": "rated_pv_kwp", "label": "Solar panels, kWp (optional)", "type": "number", "rating": "pv_kwp", "optional": true, "min": 0, "max": 1000},
|
{"name": "rated_pv_kwp", "label": "Solar panels, kWp (optional)", "type": "number", "rating": "pv_kwp", "optional": true, "min": 0, "max": 1000},
|
||||||
{"name": "rated_battery_kwh", "label": "Battery capacity, kWh (optional)", "type": "number", "rating": "battery_kwh", "optional": true, "min": 0, "max": 1000},
|
{"name": "rated_battery_kwh", "label": "Battery capacity, kWh (optional)", "type": "number", "rating": "battery_kwh", "optional": true, "min": 0, "max": 1000},
|
||||||
{"name": "rated_battery_kw", "label": "Battery power, kW (optional)", "type": "number", "rating": "battery_kw", "optional": true, "min": 0, "max": 1000}
|
{"name": "rated_battery_kw", "label": "Battery power, kW (optional)", "type": "number", "rating": "battery_kw", "optional": true, "min": 0, "max": 1000},
|
||||||
|
{"name": "rated_battery_efficiency_pct", "label": "Battery round-trip efficiency, % (optional; datasheet)", "type": "number", "rating": "battery_efficiency_pct", "optional": true, "min": 1, "max": 100}
|
||||||
],
|
],
|
||||||
"setup": [
|
"setup": [
|
||||||
{"path": "modbus", "fields": ["host", "port", "unit_id"]}
|
{"path": "modbus", "fields": ["host", "port", "unit_id"]}
|
||||||
|
|||||||
@@ -14,7 +14,8 @@
|
|||||||
{"name": "country", "label": "Country (e.g. germany)", "type": "text", "default": "germany"},
|
{"name": "country", "label": "Country (e.g. germany)", "type": "text", "default": "germany"},
|
||||||
{"name": "brand", "label": "Brand (vaillant, saunier_duval, bulex)", "type": "text", "default": "vaillant"},
|
{"name": "brand", "label": "Brand (vaillant, saunier_duval, bulex)", "type": "text", "default": "vaillant"},
|
||||||
{"name": "rated_heatpump_kw", "label": "Heat pump, electric kW (optional)", "type": "number", "rating": "heatpump_kw", "optional": true, "min": 0, "max": 1000},
|
{"name": "rated_heatpump_kw", "label": "Heat pump, electric kW (optional)", "type": "number", "rating": "heatpump_kw", "optional": true, "min": 0, "max": 1000},
|
||||||
{"name": "rated_heat_kw", "label": "Heating power, kW (optional)", "type": "number", "rating": "heat_kw", "optional": true, "min": 0, "max": 1000}
|
{"name": "rated_heat_kw", "label": "Heating power, kW (optional)", "type": "number", "rating": "heat_kw", "optional": true, "min": 0, "max": 1000},
|
||||||
|
{"name": "rated_boiler_efficiency_pct", "label": "Gas boiler efficiency, % of the gas billed (optional; datasheet on Hs)", "type": "number", "rating": "boiler_efficiency_pct", "optional": true, "min": 1, "max": 100}
|
||||||
],
|
],
|
||||||
"setup": [
|
"setup": [
|
||||||
{"path": "ha-integration", "addon": "mypyllant", "domain": "mypyllant", "fields": ["username", "password", "country", "brand"],
|
{"path": "ha-integration", "addon": "mypyllant", "domain": "mypyllant", "fields": ["username", "password", "country", "brand"],
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"id": "victron-gx",
|
"id": "victron-gx",
|
||||||
"version": "1.0.2",
|
"version": "1.0.3",
|
||||||
"manufacturer": "Victron Energy",
|
"manufacturer": "Victron Energy",
|
||||||
"name": "GX device (Cerbo GX, Venus GX) with ESS",
|
"name": "GX device (Cerbo GX, Venus GX) with ESS",
|
||||||
"models": ["Cerbo GX*", "Venus GX*", "Ekrano GX*", "Color Control GX*"],
|
"models": ["Cerbo GX*", "Venus GX*", "Ekrano GX*", "Color Control GX*"],
|
||||||
@@ -13,7 +13,8 @@
|
|||||||
{"name": "port", "label": "Modbus TCP port", "type": "port", "default": 502},
|
{"name": "port", "label": "Modbus TCP port", "type": "port", "default": 502},
|
||||||
{"name": "unit_id", "label": "Modbus unit id (the system: 100)", "type": "int", "default": 100, "min": 0, "max": 247},
|
{"name": "unit_id", "label": "Modbus unit id (the system: 100)", "type": "int", "default": 100, "min": 0, "max": 247},
|
||||||
{"name": "rated_battery_kwh", "label": "Battery capacity, kWh (optional)", "type": "number", "rating": "battery_kwh", "optional": true, "min": 0, "max": 1000},
|
{"name": "rated_battery_kwh", "label": "Battery capacity, kWh (optional)", "type": "number", "rating": "battery_kwh", "optional": true, "min": 0, "max": 1000},
|
||||||
{"name": "rated_battery_kw", "label": "Battery power, kW (optional)", "type": "number", "rating": "battery_kw", "optional": true, "min": 0, "max": 1000}
|
{"name": "rated_battery_kw", "label": "Battery power, kW (optional)", "type": "number", "rating": "battery_kw", "optional": true, "min": 0, "max": 1000},
|
||||||
|
{"name": "rated_battery_efficiency_pct", "label": "Battery round-trip efficiency, % (optional; datasheet)", "type": "number", "rating": "battery_efficiency_pct", "optional": true, "min": 1, "max": 100}
|
||||||
],
|
],
|
||||||
"setup": [
|
"setup": [
|
||||||
{"path": "modbus", "fields": ["host", "port", "unit_id"]}
|
{"path": "modbus", "fields": ["host", "port", "unit_id"]}
|
||||||
|
|||||||
@@ -14,7 +14,8 @@
|
|||||||
{"name": "client_id", "label": "Client id (Viessmann developer portal)", "type": "text"},
|
{"name": "client_id", "label": "Client id (Viessmann developer portal)", "type": "text"},
|
||||||
{"name": "heating_type", "label": "Heating type (auto, heatpump, gas, oil, pellets)", "type": "text", "default": "auto"},
|
{"name": "heating_type", "label": "Heating type (auto, heatpump, gas, oil, pellets)", "type": "text", "default": "auto"},
|
||||||
{"name": "rated_heatpump_kw", "label": "Heat pump, electric kW (optional)", "type": "number", "rating": "heatpump_kw", "optional": true, "min": 0, "max": 1000},
|
{"name": "rated_heatpump_kw", "label": "Heat pump, electric kW (optional)", "type": "number", "rating": "heatpump_kw", "optional": true, "min": 0, "max": 1000},
|
||||||
{"name": "rated_heat_kw", "label": "Heating power, kW (optional)", "type": "number", "rating": "heat_kw", "optional": true, "min": 0, "max": 1000}
|
{"name": "rated_heat_kw", "label": "Heating power, kW (optional)", "type": "number", "rating": "heat_kw", "optional": true, "min": 0, "max": 1000},
|
||||||
|
{"name": "rated_boiler_efficiency_pct", "label": "Gas boiler efficiency, % of the gas billed (optional; datasheet on Hs)", "type": "number", "rating": "boiler_efficiency_pct", "optional": true, "min": 1, "max": 100}
|
||||||
],
|
],
|
||||||
"setup": [
|
"setup": [
|
||||||
{"path": "ha", "domain": "vicare", "fields": ["username", "password", "client_id", "heating_type"],
|
{"path": "ha", "domain": "vicare", "fields": ["username", "password", "client_id", "heating_type"],
|
||||||
|
|||||||
@@ -117,6 +117,17 @@ def test_start_with_the_mosquitto_add_on(box):
|
|||||||
assert "assets: []" in (data / "mapping.yaml").read_text()
|
assert "assets: []" in (data / "mapping.yaml").read_text()
|
||||||
assert env["CANTABILE_PROPOSE"] == "1" # the agent proposes one, consent off
|
assert env["CANTABILE_PROPOSE"] == "1" # the agent proposes one, consent off
|
||||||
assert not (data / "mapping-import.yaml").exists()
|
assert not (data / "mapping-import.yaml").exists()
|
||||||
|
assert "CANTABILE_HA_REMOTE" not in env # remote HA for support: off unless the owner turns it on
|
||||||
|
|
||||||
|
|
||||||
|
def test_support_ha_only_when_the_owner_turns_it_on(box):
|
||||||
|
run, data, etc = box
|
||||||
|
Supervisor.mqtt = None
|
||||||
|
opts = json.loads((data / "options.json").read_text())
|
||||||
|
(data / "options.json").write_text(json.dumps(dict(opts, support_ha=True)))
|
||||||
|
assert run()["env"]["CANTABILE_HA_REMOTE"] == "1"
|
||||||
|
(data / "options.json").write_text(json.dumps(dict(opts, support_ha="yes")))
|
||||||
|
assert "CANTABILE_HA_REMOTE" not in run()["env"]
|
||||||
|
|
||||||
|
|
||||||
def test_start_without_a_broker_and_keeps_the_installers_files(box):
|
def test_start_without_a_broker_and_keeps_the_installers_files(box):
|
||||||
|
|||||||