feat: the host ports a started add-on maps, as its listening sockets
driver / lint (pull_request) Successful in 17s
driver / standard (pull_request) Successful in 24s
driver / lowest (pull_request) Successful in 36s
driver / test (3.13) (pull_request) Successful in 53s
driver / types (pull_request) Successful in 1m10s
driver / test (3.12) (pull_request) Successful in 1m22s
driver / build (pull_request) Successful in 27s
driver / security (pull_request) Successful in 1m19s
CI / driver (pull_request) Successful in 2m20s
driver / publish (pull_request) Skipped

netOrk names a service's port from the sockets its unit holds. An add-on's
container ports are published by Docker, so no socket names it: a service
on an add-on (Zigbee2MQTT's frontend, netork#3147) got no port.

get_listening_sockets now reads /addons/<slug>/info of each started add-on
and reports every host port it maps, with the slug as the socket's unit.
A port mapped to null is off; an add-on on the host network applies no
mapping and names none. Core, the Supervisor and the OS are not told, so
the reading is not attributed.
This commit is contained in:
Christian Manivong committed 2026-12-05 17:47:09 +01:00
1 parent a0a3dd9554
commit 36bfd6d6c1
9 files changed
+171 -4

No files matched your search

+4
View File
@@ -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.
- Listening sockets: the host ports each started add-on maps, with its slug as the
socket's unit, so a service on an add-on (Zigbee2MQTT's frontend) has its port.
Add-ons on the host network, Core and the OS's own sockets are not told; the
reading says so (`attributed` is false).
+1
View File
@@ -52,6 +52,7 @@ with driver("192.0.2.21", "", "<long-lived access token>") 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 |
| `get_listening_sockets` | the host ports each started add-on maps, the add-on's slug as unit; not attributed: add-ons on the host network, Core and the OS are not told |
| `reboot_host`, `shutdown_host` | the host, through the Supervisor |
| Discovery | Home Assistant's web page on port 8123 |
+14
View File
@@ -34,6 +34,7 @@ from napalm_device_types import FingerprintRule, OSDriver, PortSpec
from napalm_device_types.models import (
ApplyUpdatesResultDict,
HostStatusDict,
ListeningSocketsDict,
ServiceDict,
UpdateDict,
)
@@ -45,10 +46,12 @@ from napalm_homeassistant.parsers import (
parse_host_status,
parse_interfaces,
parse_interfaces_ip,
parse_listening_sockets,
parse_running_backups,
parse_services,
parse_updates,
)
from napalm_homeassistant.parsers.services import STARTED
from napalm_homeassistant.parsers.system import VENDOR
from napalm_homeassistant.parsers.updates import CORE, OS, SUPERVISOR
from napalm_homeassistant.transport import (
@@ -235,6 +238,17 @@ class HomeAssistantDriver(RunningBackupsMixin, OSDriver):
}
return parse_services(addons, boots)
def get_listening_sockets(self) -> ListeningSocketsDict:
"""The host ports each started add-on maps; see ``parse_listening_sockets``."""
addons = self._get("/addons").get("addons", [])
return parse_listening_sockets(
[
self._get(f"/addons/{addon['slug']}/info")
for addon in addons
if addon.get("state") == STARTED
]
)
def get_running_backups(self) -> list[RunningBackupDict]:
return parse_running_backups(self._get("/jobs/info"))
+2 -1
View File
@@ -2,7 +2,7 @@
from napalm_homeassistant.parsers.jobs import parse_running_backups
from napalm_homeassistant.parsers.network import parse_interfaces, parse_interfaces_ip
from napalm_homeassistant.parsers.services import parse_services
from napalm_homeassistant.parsers.services import parse_listening_sockets, parse_services
from napalm_homeassistant.parsers.system import (
board_model,
parse_facts,
@@ -17,6 +17,7 @@ __all__ = [
"parse_host_status",
"parse_interfaces",
"parse_interfaces_ip",
"parse_listening_sockets",
"parse_running_backups",
"parse_services",
"parse_updates",
+43 -1
View File
@@ -1,12 +1,16 @@
"""Add-ons as services: ``/addons`` and each add-on's ``boot`` setting."""
"""Add-ons as services: ``/addons`` and each add-on's ``boot`` setting; the host
ports a started add-on maps, as its listening sockets."""
from __future__ import annotations
import re
from typing import Any
from napalm_device_types.models import (
SERVICE_RESULT_EXIT_CODE,
SERVICE_RESULT_SUCCESS,
ListeningSocketDict,
ListeningSocketsDict,
ServiceDict,
)
@@ -19,6 +23,8 @@ RESULTS = {
"error": SERVICE_RESULT_EXIT_CODE,
}
BOOT_AUTO = "auto"
#: A key of an add-on's ``network``: its container port and protocol, ``"8099/tcp"``.
CONTAINER_PORT = re.compile(r"\d+/(tcp|udp)")
def parse_services(addons: dict[str, Any], boots: dict[str, str]) -> list[ServiceDict]:
@@ -33,3 +39,39 @@ def parse_services(addons: dict[str, Any], boots: dict[str, str]) -> list[Servic
}
for addon in addons.get("addons", [])
]
def parse_listening_sockets(infos: list[dict[str, Any]]) -> ListeningSocketsDict:
"""The host ports each started add-on maps, from its ``/addons/<slug>/info``.
Docker publishes each on every address of the host; the add-on's slug is the
socket's unit, as it is the service's name. A port mapped to ``null`` is off.
An add-on on the host network applies no mapping, and Core, the Supervisor and
the OS's own sockets are not told at all: the reading is not attributed.
"""
return {
"attributed": False,
"sockets": [
socket
for info in infos
if info.get("state") == STARTED and not info.get("host_network")
for socket in _mapped(str(info["slug"]), info.get("network") or {})
],
}
def _mapped(slug: str, network: dict[str, Any]) -> list[ListeningSocketDict]:
return [
{
"proto": key.split("/", 1)[1],
"address": "*",
"port": int(host_port),
"interface": None,
"process": None, # the Supervisor names no process
"pid": None,
"unit": slug,
"container_id": None, # an add-on's container is not on the host network
}
for key, host_port in network.items()
if CONTAINER_PORT.fullmatch(key) and isinstance(host_port, int) and host_port > 0
]
+1 -1
View File
@@ -22,7 +22,7 @@ def load(name: str) -> Any:
def addon_info(slug: str) -> dict[str, Any]:
"""The part of ``/addons/<slug>/info`` the driver reads."""
return {"slug": slug, "boot": load("addon-boot")[slug]}
return {"slug": slug, "boot": load("addon-boot")[slug], **load("addon-network")[slug]}
def device_answers() -> dict[tuple[str, str], Any]:
@@ -0,0 +1,22 @@
{
"core_mosquitto": {
"state": "started",
"host_network": false,
"network": {"1883/tcp": 1883, "1884/tcp": null, "8883/tcp": 8883, "8884/tcp": 8884}
},
"core_samba": {
"state": "started",
"host_network": true,
"network": {"139/tcp": 139, "445/tcp": 445}
},
"a0d7b954_vscode": {
"state": "stopped",
"host_network": false,
"network": {"1337/tcp": null}
},
"a0d7b954_ssh": {
"state": "error",
"host_network": false,
"network": {"22/tcp": 22}
}
}
+11
View File
@@ -191,6 +191,17 @@ def test_get_services_reads_each_add_ons_boot_setting(
} in fake.calls
def test_get_listening_sockets_asks_only_the_started_add_ons(
driver: HomeAssistantDriver, fake: FakeSupervisor
) -> None:
reading = driver.get_listening_sockets()
assert reading["attributed"] is False
assert {s["unit"] for s in reading["sockets"]} == {"core_mosquitto"}
asked = [c["endpoint"] for c in fake.calls if c["endpoint"].startswith("/addons/")]
assert asked == ["/addons/core_mosquitto/info", "/addons/core_samba/info"]
def test_get_running_backups(driver: HomeAssistantDriver) -> None:
assert [b["kind"] for b in driver.get_running_backups()] == ["backup"]
+73 -1
View File
@@ -8,12 +8,13 @@ from napalm_homeassistant.parsers import (
parse_host_status,
parse_interfaces,
parse_interfaces_ip,
parse_listening_sockets,
parse_running_backups,
parse_services,
parse_updates,
uptime_seconds,
)
from tests.conftest import load
from tests.conftest import addon_info, load
GIB = 1024**3
@@ -272,6 +273,77 @@ def test_an_add_on_without_its_boot_setting_is_not_enabled() -> None:
assert [s["enabled"] for s in services] == [False, False, False, False]
# --- listening sockets -------------------------------------------------------------------
def _infos() -> list[dict]:
return [addon_info(slug) for slug in load("addon-boot")]
def _socket(proto: str, port: int, unit: str) -> dict:
return {
"proto": proto,
"address": "*",
"port": port,
"interface": None,
"process": None,
"pid": None,
"unit": unit,
"container_id": None,
}
def test_a_started_add_on_listens_on_the_host_ports_it_maps() -> None:
assert parse_listening_sockets(_infos()) == {
"attributed": False,
"sockets": [
_socket("tcp", 1883, "core_mosquitto"),
_socket("tcp", 8883, "core_mosquitto"),
_socket("tcp", 8884, "core_mosquitto"),
],
}
def test_a_port_mapped_to_nothing_is_not_listening() -> None:
"""``null``: the user turned the port off in the add-on's network settings."""
ports = [s["port"] for s in parse_listening_sockets(_infos())["sockets"]]
assert 1884 not in ports
def test_an_add_on_that_is_not_started_listens_on_nothing() -> None:
units = {s["unit"] for s in parse_listening_sockets(_infos())["sockets"]}
assert "a0d7b954_ssh" not in units and "a0d7b954_vscode" not in units
def test_an_add_on_on_the_host_network_names_no_socket() -> None:
"""Its mapping is not applied: what it listens on, the Supervisor does not say."""
units = {s["unit"] for s in parse_listening_sockets(_infos())["sockets"]}
assert "core_samba" not in units
def test_a_udp_port_and_a_host_port_other_than_the_containers() -> None:
info = {
"slug": "core_matter_server",
"state": "started",
"host_network": False,
"network": {"5580/tcp": 15580, "5353/udp": 5353, "bogus": 1, "80/sctp": 80},
}
assert parse_listening_sockets([info])["sockets"] == [
_socket("tcp", 15580, "core_matter_server"),
_socket("udp", 5353, "core_matter_server"),
]
def test_without_network_settings_an_add_on_listens_on_nothing() -> None:
info = {"slug": "x", "state": "started", "host_network": False, "network": None}
assert parse_listening_sockets([info]) == {"attributed": False, "sockets": []}
# --- running backups ---------------------------------------------------------------------