# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved. """The HA add-on's Devices page (devpage.py): HA's devices with the profile that fits, what each would give, "Use with Cantabile" writing the mapping fragment (consent off, protection defaults), the proposal yielding to it, one writer per entity, consent not given through HA, protection, heating or cooling, removing, owner only. HA is a fake; the profiles are the real ones.""" import asyncio import json import pathlib import urllib.parse import pytest import yaml from cantabile_edge import i18n, propose from cantabile_edge.devpage import App, Devices, SupportSessions from cantabile_edge.joinpage import Fleet from cantabile_edge.mapping import Mapping PROFILES = propose.load_profiles(str(pathlib.Path(__file__).resolve().parents[2] / "profiles")) INV, AC, LAMP = "dev-inverter", "dev-ac", "dev-lamp" def ent(eid, dev, uid, platform): return {"entity_id": eid, "device_id": dev, "unique_id": uid, "platform": platform} def st(eid, state, **attrs): return {"entity_id": eid, "state": state, "attributes": attrs} class FakeHA: def __init__(self, admins=("u-admin",)): self.admins = set(admins) self.reg = { "areas": [{"area_id": "living", "name": "Living room"}], "devices": [ {"id": INV, "name": "Inverter", "manufacturer": "Huawei", "model": "SUN2000-10KTL", "config_entries": ["e1"]}, {"id": AC, "name": "Living room A/C", "manufacturer": "Espressif", "model": "esp32", "area_id": "living", "config_entries": ["e2"]}, {"id": LAMP, "name": "Lamp", "manufacturer": "IKEA", "model": "TRADFRI", "config_entries": ["e3"]}], "entities": [ent("sensor.inverter_input_power", INV, "x_input_power", "huawei_solar"), ent("sensor.inverter_yield", INV, "x_accumulated_yield_energy", "huawei_solar"), ent("sensor.battery_soc", INV, "x_storage_state_of_capacity", "huawei_solar"), ent("climate.living_ac", AC, "ac1", "esphome"), ent("light.lamp", LAMP, "l1", "zha")]} self.states = [st("sensor.inverter_input_power", "3200", device_class="power", unit_of_measurement="W", friendly_name="Inverter input power"), st("sensor.inverter_yield", "12000", device_class="energy", unit_of_measurement="kWh", state_class="total_increasing", friendly_name="Inverter yield"), st("sensor.battery_soc", "55", device_class="battery", unit_of_measurement="%", friendly_name="Battery state of capacity"), st("climate.living_ac", "heat", hvac_modes=["off", "heat", "cool"], hvac_action="idle", current_temperature=21.5, temperature=22, min_temp=16, max_temp=30, target_temp_step=0.5), st("light.lamp", "on")] async def registries(self): return json.loads(json.dumps(self.reg)) async def send(self, type_, **kw): assert type_ == "get_states" return json.loads(json.dumps(self.states)) async def is_admin(self, user_id): return user_id in self.admins class Site: """An add-on's /data: the mapping, its fragments, the state directory.""" def __init__(self, tmp, mapping=None): self.tmp, self.ha, self.changes, self.restarts = tmp, FakeHA(), 0, 0 self.mapping = tmp / "mapping.yaml" self.state = tmp / "state" self.state.mkdir() if mapping is None: self.mapping.write_text("version: 1\nassets: []\n") self.propose() # what the add-on proposes at install else: self.mapping.write_text(yaml.safe_dump(mapping)) self.dev = Devices(str(self.mapping), str(self.state), PROFILES, ha=lambda: self.ha, changed=self.changed) self.app = App(Fleet(str(self.state), site_id="ab" * 16), self.dev, location=lambda: None) def propose(self): return propose.ensure(str(self.mapping), str(self.state), self.ha.states, self.ha.reg, PROFILES) async def changed(self, restart=False): self.changes += 1 self.restarts += restart # the agent starts again (agent.restart) self.propose() # as agent.devices_changed def give_consent(self, share=True, control=False): """The site's consent, as addon-run writes it from the configuration.""" (self.state / "consent.json").write_text(json.dumps({"share": share, "control": control, "by": "ha-addon-config"})) def frag(self, dev_id): return json.loads((self.tmp / "mapping.d" / f"{dev_id}.yaml").read_text()) def devices(self): return json.loads((self.state / "devices.json").read_text()) def req(self, method="GET", query="", form=None, hdr=None, peer="127.0.0.1"): url = urllib.parse.urlsplit("/" + (f"?{query}" if query else "")) data = urllib.parse.urlencode(form or {}).encode() return asyncio.run(self.app.respond(method, url, hdr or {}, data, peer))[1] def post(self, **form): return self.req("POST", "p=devices", form) def test_page_shows_ha_devices_with_their_profile_and_what_they_would_give(tmp_path): s = Site(tmp_path) out = s.req() assert "