Skip to content

Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring

fix(zigbee,zwave): record the import in a device's sources

Pouzor5d4a99b17a9e
diff --git a/backend/app/api/routes/zigbee.py b/backend/app/api/routes/zigbee.py
index 47d975d..b1fe700 100644
--- a/backend/app/api/routes/zigbee.py
+++ b/backend/app/api/routes/zigbee.py
@@ -29,6 +29,7 @@ from app.schemas.zigbee import (
ZigbeeTestConnectionRequest,
ZigbeeTestConnectionResponse,
)
+from app.services.discovery_sources import add_source
from app.services.import_jobs import create_job, fail_job, finish_job, get_job
from app.services.inventory_sync import attach_device_ids
from app.services.node_dedupe import dedupe_nodes_by_device
@@ -315,6 +316,7 @@ async def _persist_pending_import(
properties=props,
status="pending",
discovery_source="zigbee",
+ discovery_sources=["zigbee"],
)
)
pending_created += 1
@@ -327,6 +329,10 @@ async def _persist_pending_import(
if n.get("lqi") is not None:
pending.lqi = n.get("lqi")
pending.properties = merge_zigbee_properties(list(pending.properties or []), props)
+ # Record the import among the row's sources: the UI files a device
+ # by that list, so a row a canvas save listed as ["canvas"] only
+ # would otherwise stay out of the zigbee filter for good.
+ pending.discovery_sources = add_source(pending.discovery_sources, "zigbee")
if pending.status == "approved" and not await _is_drawn(db, pending.id):
# Approved earlier but no canvas draws it any more — the node was
# deleted. Revive the row to "pending" so it reappears in the list
diff --git a/backend/app/api/routes/zwave.py b/backend/app/api/routes/zwave.py
index cbc2b7c..c23a5cb 100644
--- a/backend/app/api/routes/zwave.py
+++ b/backend/app/api/routes/zwave.py
@@ -27,6 +27,7 @@ from app.schemas.zwave import (
ZwaveTestConnectionRequest,
ZwaveTestConnectionResponse,
)
+from app.services.discovery_sources import add_source
from app.services.inventory_sync import attach_device_ids
from app.services.node_dedupe import dedupe_nodes_by_device
from app.services.zwave_service import (
@@ -247,6 +248,7 @@ async def _persist_pending_import(
properties=props,
status="pending",
discovery_source="zwave",
+ discovery_sources=["zwave"],
)
)
pending_created += 1
@@ -257,6 +259,10 @@ async def _persist_pending_import(
pending.model = n.get("model") or pending.model
pending.vendor = n.get("vendor") or pending.vendor
pending.properties = merge_zwave_properties(list(pending.properties or []), props)
+ # Record the import among the row's sources: the UI files a device
+ # by that list, so a row a canvas save listed as ["canvas"] only
+ # would otherwise stay out of the zwave filter for good.
+ pending.discovery_sources = add_source(pending.discovery_sources, "zwave")
if pending.status == "approved" and not await _is_drawn(db, pending.id):
# Approved earlier but no canvas draws it any more (the node was
# deleted) — revive to "pending" so it reappears in the list.
diff --git a/backend/tests/test_zigbee_router.py b/backend/tests/test_zigbee_router.py
index af8d5b9..fc9688e 100644
--- a/backend/tests/test_zigbee_router.py
+++ b/backend/tests/test_zigbee_router.py
@@ -1292,3 +1292,26 @@ async def test_second_card_is_linked_after_the_canvas_was_saved(
assert (second["edges"][0]["source"], second["edges"][0]["target"]) == (
coord["node_id"], second["node_id"],
)
+
+
+async def test_persist_pending_import_records_its_source(db_session) -> None:
+ """A row the import creates lists it among its sources, and a re-import puts
+ it back on a row a canvas save left as ["canvas"] alone (#532) — the UI files
+ a device by that list."""
+ from sqlalchemy import select
+
+ from app.api.routes.zigbee import _persist_pending_import
+ from app.db.models import InventoryDevice
+
+ await _persist_pending_import(db_session, _PENDING_NODES, _PENDING_EDGES)
+ rows = (await db_session.execute(select(InventoryDevice))).scalars().all()
+ assert rows and all(r.discovery_sources == ["zigbee"] for r in rows)
+
+ for row in rows:
+ row.discovery_sources = ["canvas"]
+ await db_session.commit()
+ await _persist_pending_import(db_session, _PENDING_NODES, _PENDING_EDGES)
+
+ rows = (await db_session.execute(select(InventoryDevice))).scalars().all()
+ assert all(r.discovery_sources == ["canvas", "zigbee"] for r in rows)
diff --git a/backend/tests/test_zwave_router.py b/backend/tests/test_zwave_router.py
index 961adbe..96712cc 100644
--- a/backend/tests/test_zwave_router.py
+++ b/backend/tests/test_zwave_router.py
@@ -677,3 +677,26 @@ async def test_import_reuses_the_existing_inventory_row(
n for n in res.json()["nodes"] if n["ieee_address"] == "zwave-0xh-1"
)
assert coordinator["device_id"] == existing_id
+
+
+async def test_persist_pending_import_records_its_source(db_session) -> None:
+ """A row the import creates lists it among its sources, and a re-import puts
+ it back on a row a canvas save left as ["canvas"] alone (#532) — the UI files
+ a device by that list."""
+ from sqlalchemy import select
+
+ from app.api.routes.zwave import _persist_pending_import
+ from app.db.models import InventoryDevice
+
+ await _persist_pending_import(db_session, _PENDING_NODES, _PENDING_EDGES)
+ rows = (await db_session.execute(select(InventoryDevice))).scalars().all()
+ assert rows and all(r.discovery_sources == ["zwave"] for r in rows)
+
+ for row in rows:
+ row.discovery_sources = ["canvas"]
+ await db_session.commit()
+ await _persist_pending_import(db_session, _PENDING_NODES, _PENDING_EDGES)
+
+ rows = (await db_session.execute(select(InventoryDevice))).scalars().all()
+ assert all(r.discovery_sources == ["canvas", "zwave"] for r in rows)