cantabile_edge, cantabile_modbus, cantabile_eebus 0.1.629: full source

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

No files matched your search

+20 -14
View File
@@ -111,20 +111,22 @@ class Site:
def test_page_shows_ha_devices_with_their_profile_and_what_they_would_give(tmp_path):
s = Site(tmp_path)
out = s.req()
assert "<nav>" in out and "?p=building" in out and "Devices</h2>" in out
assert "<nav>" in out and "?p=building" in out and "Devices<details" in out
assert out.count("Use with Cantabile</button>") == 2 # the inverter and the A/C
assert "Cantabile would read: pv: power" in out and "battery: soc" in out
assert "Cantabile would read: solar power" in out and "battery charge" in out
assert "It could control: heating, cooling." in out and "generic profile" in out
assert "room: living room.temp" in out
assert "room temperature (Living room), setpoint, heating/cooling state" in out # words, never asset ids
assert "room: " not in out and "Devices in Home Assistant you can add" in out
assert "Proposed by Cantabile (not checked yet)" in out # the install's proposal
assert "1 more in Home Assistant without a profile" in out and "Lamp" in out
assert "Used with Cantabile" not in out
assert "in use with Cantabile" not in out
def test_use_with_cantabile_writes_the_fragment_consent_off(tmp_path):
s = Site(tmp_path)
out = s.post(do="use", ha_device=AC, profile="ha-thermostat", name="Living room A/C")
assert "is used with Cantabile now" in out and s.changes == 1
assert "Reading and control" in out and "‹ Devices" in out # its own page
dev = next(iter(s.devices().values()))
assert dev["ha_device"] == AC and dev["profile"] == "ha-thermostat" and not dev["read"] and not dev["control"]
frag = s.frag(dev["id"])
@@ -140,11 +142,14 @@ def test_use_with_cantabile_writes_the_fragment_consent_off(tmp_path):
assert {t for t, e in prot.items() if e["level"] == "protected"} == {f"control:{c['id']}" for c in frag["controls"]}
specs = json.loads((s.state / "devices-specs.json").read_text())["devices"]
assert specs[dev["id"]]["profile"] == "ha-thermostat" and specs[dev["id"]]["ha_device"] == AC
# the page: used, reading off, no second offer for it
# the list: in use, reading off, in words, no second offer for it
out = s.req()
assert "Used with Cantabile" in out and "first give your consent in the add-on&#x27;s configuration" in out
assert 'name="read"' not in out
assert "1 device in use with Cantabile" in out and "Added · reading off · control off" in out
assert f'href="?p=devices&amp;d={dev["id"]}"' in out and "21.5 °C, set to 22 °C, idle" in out
assert out.count("Use with Cantabile</button>") == 1
out = s.req(query=f"p=devices&d={dev['id']}")
assert "first give your consent in the add-on&#x27;s configuration" in out and 'name="read"' not in out
assert '<span class="badge quiet">untested</span>' in out
assert "This device is used with Cantabile already." in s.post(do="use", ha_device=AC, profile="ha-thermostat")
assert len(s.devices()) == 1
# the agent loads it: values and both controls, nothing consented
@@ -182,10 +187,10 @@ def test_a_hand_written_mapping_wins_and_one_writer_per_entity(tmp_path):
"controls": [{"id": "heating", "kind": "heating", "consent": False, "entities": {"climate": "climate.living_ac"}}]}
s = Site(tmp_path, mapping=hand)
out = s.req()
assert "In your own mapping: room: living.temp. Those stay as they are." in out
assert "In your own mapping: room temperature (Living). Those stay as they are." in out
# the climate entity is the hand mapping's (read and written there):
# nothing is left for the thermostat's fragment, and the page says why
assert "Read by your own mapping already, so left out here: room: living room.temp" in out
assert "Read by your own mapping already, so left out here: room temperature (Living room)" in out
assert "Another control writes to this device already, so Cantabile leaves out: heating, cooling." in out
out = s.post(do="use", ha_device=AC, profile="ha-thermostat")
assert "or your mapping has them all already" in out and not (tmp_path / "state" / "devices.json").exists()
@@ -207,12 +212,12 @@ def test_reading_and_control_need_the_site_consent(tmp_path):
assert not any(a["consent"] for a in s.frag(dev_id)["assets"]) and s.restarts == 0
# with it: the switches, as on the Hub's Devices page
s.give_consent(share=True, control=False)
out = s.req()
out = s.req(query=f"p=devices&d={dev_id}")
assert 'name="read"' in out and 'name="control"' in out
assert "Cantabile may control it (heating, cooling), within your limits" in out
assert "Control also needs your consent to control devices" in out
out = s.post(do="consent", id=dev_id, read="1")
assert "Cantabile starts again" in out and s.restarts == 1
assert "Cantabile starts again" in out and s.restarts == 1 and "In use · reading on · control off" in out
assert s.devices()[dev_id]["read"] and not s.devices()[dev_id]["control"]
frag = s.frag(dev_id)
assert all(a["consent"] for a in frag["assets"]) and not any(c["consent"] for c in frag["controls"])
@@ -232,7 +237,7 @@ def test_protection_and_heating_or_cooling(tmp_path):
s.post(do="use", ha_device=AC, profile="ha-thermostat")
dev_id = next(iter(s.devices()))
heat = next(c["id"] for c in s.frag(dev_id)["controls"] if c["kind"] == "heating")
out = s.req()
out = s.req(query=f"p=devices&d={dev_id}")
assert "Allow switching off" in out
out = s.req("POST", "p=devices", {"do": "protect", "id": dev_id, "target": f"control:{heat}", "level": "none"},
hdr={"x-remote-user-display-name": "Ana"})
@@ -242,7 +247,7 @@ def test_protection_and_heating_or_cooling(tmp_path):
# a seal from the cloud: shown locked, nothing to lift, not removable
(s.state / "acl.json").write_text(json.dumps({"v": 1, "kind": "acl", "site": "ab" * 16, "entries": [
{"target": f"device:{dev_id}", "level": "sealed", "custodian": "grid_operator", "at": 1}]}))
out = s.req()
out = s.req(query=f"p=devices&d={dev_id}")
assert "Sealed" in out and "only the grid operator can change it" in out
assert "only they can change it" in s.post(do="protect", id=dev_id, target=f"device:{dev_id}", level="none")
assert "Sealed: only the grid operator can remove it." in s.post(do="remove", id=dev_id)
@@ -291,7 +296,8 @@ def test_german(tmp_path):
return s.req()
import contextvars
out = contextvars.copy_context().run(run)
assert '<html lang="de">' in out and "Geräte</h2>" in out and "Mit Cantabile nutzen" in out
assert '<html lang="de">' in out and "Geräte<details" in out and "Mit Cantabile nutzen" in out
assert "Raumtemperatur (Living room)" in out and "Geräte in Home Assistant, die Sie hinzufügen können" in out
assert "Cantabile würde auslesen" in out and "Steuern könnte es: Heizen, Kühlen." in out
assert "Use with Cantabile" not in out