From 7f60d43732a96c660c98fa159c0fdf1a04ddf4c8 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sat, 5 Dec 2026 22:31:48 +0100 Subject: [PATCH] feat: the Supervisor as a read-only container engine container_engines() names the Supervisor (engine supervisor, API ha-supervisor) and open_container_engine("supervisor") returns read access to its API, so netOrk can read Core, the Supervisor, its plugins and the apps as containers (NetOrk/netork#3148). Nothing can be written through it; without a Supervisor there is no engine. --- CHANGELOG.md | 4 +++ README.md | 1 + napalm_homeassistant/driver.py | 50 ++++++++++++++++++++++++++++++++- tests/test_driver.py | 51 ++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ef4b53..f97c89e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,3 +17,7 @@ All notable changes to this driver. The format follows shut down the host. - Discovery: Home Assistant's web page on port 8123. - Type label `Home Assistant OS`, so a driver picker names it. +- Container engine access: `container_engines()` names the Supervisor (engine + `supervisor`, API `ha-supervisor`) and `open_container_engine("supervisor")` returns + read access to its API, through which netOrk reads Core, the Supervisor, its plugins + and the apps as containers (NetOrk/netork#3148). diff --git a/README.md b/README.md index d634292..8657ea5 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ with driver("192.0.2.21", "", "") as device: | `refresh_available_updates` | the Supervisor's `/reload_updates` | | `get_services`, `manage_service` | add-ons by slug: start, stop, restart; enable/disable sets their start at boot | | `get_running_backups` | the Supervisor's backup jobs in progress | +| `container_engines`, `open_container_engine` | the Supervisor as engine `supervisor` (API `ha-supervisor`): read access to its API (`get`), through which netOrk reads Core, the Supervisor, its plugins and the apps as containers; nothing is written through it. None without a Supervisor | | `reboot_host`, `shutdown_host` | the host, through the Supervisor | | Discovery | Home Assistant's web page on port 8123 | diff --git a/napalm_homeassistant/driver.py b/napalm_homeassistant/driver.py index a56fe51..7815560 100644 --- a/napalm_homeassistant/driver.py +++ b/napalm_homeassistant/driver.py @@ -30,9 +30,10 @@ from typing import Any, Protocol from napalm.base.exceptions import ConnectionException from napalm.base.models import FactsDict, InterfaceDict, InterfacesIPDict -from napalm_device_types import FingerprintRule, OSDriver, PortSpec +from napalm_device_types import ContainerEngineUnavailable, FingerprintRule, OSDriver, PortSpec from napalm_device_types.models import ( ApplyUpdatesResultDict, + ContainerEngineDict, HostStatusDict, ServiceDict, UpdateDict, @@ -77,6 +78,10 @@ BOOT_BY_ACTION = {"enable": "auto", "disable": "manual"} #: The order updates run in: add-ons, then what restarts more and more of the host. UPDATE_ORDER = {SUPERVISOR: 1, CORE: 2, OS: 3} CORE_ONLY_MODEL = "Home Assistant (without Supervisor)" +#: The container engine this driver offers: the Supervisor, which runs Core, its plugins +#: and the apps as Docker containers and is the only way to them (NetOrk/netork#3148). +ENGINE = "supervisor" +ENGINE_API = "ha-supervisor" class SupervisorApi(Protocol): @@ -96,6 +101,24 @@ class SupervisorApi(Protocol): def close(self) -> None: ... +class SupervisorEngine: + """Read access to the Supervisor, as a container engine connection (``ha-supervisor``). + + ``get(endpoint)`` is the ``data`` of the Supervisor's answer to a GET of *endpoint*. + There is no way to write through it: what runs on the engine is only read. The + connection is the driver's; closing this leaves it open. + """ + + def __init__(self, get: Callable[[str], Any]) -> None: + self._read = get + + def get(self, endpoint: str) -> Any: + return self._read(endpoint) + + def close(self) -> None: + """Nothing of its own to release.""" + + class HomeAssistantDriver(RunningBackupsMixin, OSDriver): """Home Assistant OS (or a Supervised install) through Home Assistant's WebSocket.""" @@ -238,6 +261,31 @@ class HomeAssistantDriver(RunningBackupsMixin, OSDriver): def get_running_backups(self) -> list[RunningBackupDict]: return parse_running_backups(self._get("/jobs/info")) + # --- container engine ------------------------------------------------------------- + + def container_engines(self) -> list[ContainerEngineDict]: + """ + Returns the container engines the host offers: the Supervisor, which speaks + ``ha-supervisor``; none where Home Assistant runs without one. + + * engine (string) - "supervisor" + * api (string) - "ha-supervisor": the Supervisor's API over Home Assistant's + WebSocket, read through :class:`SupervisorEngine` + """ + try: + self._get("/supervisor/info") + except HomeAssistantError as exc: + if exc.code != UNKNOWN_COMMAND: + raise + return [] + return [{"engine": ENGINE, "api": ENGINE_API}] + + def open_container_engine(self, engine: str) -> SupervisorEngine: + """Read access to *engine*; :class:`ContainerEngineUnavailable` for any other.""" + if engine != ENGINE: + raise ContainerEngineUnavailable(f"no way to reach container engine {engine!r}") + return SupervisorEngine(self._get) + # --- add-ons ---------------------------------------------------------------------- def manage_service(self, name: str, action: str) -> dict[str, Any]: diff --git a/tests/test_driver.py b/tests/test_driver.py index 82f298b..0a159f6 100644 --- a/tests/test_driver.py +++ b/tests/test_driver.py @@ -405,3 +405,54 @@ def test_closing_a_dropped_connection_does_not_raise( driver.close() assert driver.transport is None + + +# --- container engine (NetOrk/netork#3148) --------------------------------------------- + + +def test_the_supervisor_is_the_container_engine(driver: HomeAssistantDriver) -> None: + assert driver.container_engines() == [{"engine": "supervisor", "api": "ha-supervisor"}] + + +def test_without_a_supervisor_there_is_no_engine(fake: FakeSupervisor) -> None: + fake.answers[("get", "/supervisor/info")] = HomeAssistantError( + "unknown_command", "Unknown command." + ) + device = HomeAssistantDriver("192.0.2.21", "", "tok") + device.transport = fake + + assert device.container_engines() == [] + + +def test_a_failed_read_of_the_engines_raises( + driver: HomeAssistantDriver, fake: FakeSupervisor +) -> None: + fake.answers[("get", "/supervisor/info")] = REFUSED + + with pytest.raises(HomeAssistantError): + driver.container_engines() + + +def test_the_engine_connection_only_reads( + driver: HomeAssistantDriver, fake: FakeSupervisor +) -> None: + engine = driver.open_container_engine("supervisor") + + assert engine.get("/core/info") == load("core-info") + assert not hasattr(engine, "post") + assert fake.calls[-1]["method"] == "get" + + +def test_closing_the_engine_leaves_the_drivers_connection_open( + driver: HomeAssistantDriver, fake: FakeSupervisor +) -> None: + driver.open_container_engine("supervisor").close() + + assert fake.closed is False + + +def test_an_unknown_engine_is_refused() -> None: + from napalm_device_types import ContainerEngineUnavailable + + with pytest.raises(ContainerEngineUnavailable): + HomeAssistantDriver("ha", "", "tok").open_container_engine("docker")