Skip to content

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

fix(zigbee): carry LQI through the inventory approve path

Pouzor865102278899
diff --git a/backend/app/api/routes/scan.py b/backend/app/api/routes/scan.py
index e6b5c84..a563816 100644
--- a/backend/app/api/routes/scan.py
+++ b/backend/app/api/routes/scan.py
@@ -784,7 +784,7 @@ async def bulk_approve_devices(
ref = entry.pop("_ref")
entry["existing_node_id"] = ref.id if isinstance(ref, Node) else ref
- all_edges: list[dict[str, str]] = []
+ all_edges: list[dict[str, Any]] = []
for device in approved_devices:
all_edges.extend(
await _resolve_pending_links_for_ieee(db, device.ieee_address, default_design_id)
@@ -992,7 +992,7 @@ async def _is_proxmox_cluster_member(db: AsyncSession, ieee: str | None) -> bool
async def _resolve_pending_links_for_ieee(
db: AsyncSession, ieee: str | None, design_id: str | None
-) -> list[dict[str, str]]:
+) -> list[dict[str, Any]]:
"""Materialize edges for any device_inventory_links involving ``ieee`` on the
canvas identified by ``design_id``.
@@ -1050,7 +1050,7 @@ async def _resolve_pending_links_for_ieee(
)
existing_pairs = {(e.source, e.target) for e in existing_q.scalars().all()}
- created: list[dict[str, str]] = []
+ created: list[dict[str, Any]] = []
for link in links:
other_ieee = (
link.target_ieee if link.source_ieee == ieee else link.source_ieee
@@ -1094,6 +1094,9 @@ async def _resolve_pending_links_for_ieee(
source_handle=src_handle,
target_handle=tgt_handle,
design_id=edge_design_id,
+ # Measured link quality, carried from the import. A mesh link has no
+ # node-property fallback, so losing it here loses it for good.
+ lqi=link.lqi,
)
db.add(edge)
await db.flush()
@@ -1107,6 +1110,7 @@ async def _resolve_pending_links_for_ieee(
"type": edge_type,
"source_handle": src_handle,
"target_handle": tgt_handle,
+ "lqi": link.lqi,
})
return created
diff --git a/backend/tests/scan/test_scan_properties.py b/backend/tests/scan/test_scan_properties.py
index 234f6c3..3068324 100644
--- a/backend/tests/scan/test_scan_properties.py
+++ b/backend/tests/scan/test_scan_properties.py
@@ -189,6 +189,128 @@ async def test_approve_zigbee_creates_edge_when_other_endpoint_is_node(
assert edges[0].type == "iot"
+async def test_approve_zigbee_mesh_link_carries_lqi_onto_edge(
+ client: AsyncClient, headers, db_session
+):
+ """A mesh link has no node-property fallback, so the LQI measured at import
+ must reach the Edge — and the response — or it is lost for good."""
+ from sqlalchemy import select
+
+ from app.db.models import Edge, InventoryDeviceLink
+
+ coord_device = InventoryDevice(
+ ieee_address="0xCOORD",
+ friendly_name="Coordinator",
+ suggested_type="zigbee_coordinator",
+ status="approved",
+ )
+ db_session.add(coord_device)
+ await db_session.flush()
+ coord = Node(label="Coordinator", type="zigbee_coordinator", device_id=coord_device.id)
+ db_session.add(coord)
+
+ pending = InventoryDevice(
+ ieee_address="0xR1",
+ friendly_name="router_1",
+ suggested_type="zigbee_router",
+ status="pending",
+ discovery_source="zigbee",
+ )
+ db_session.add(pending)
+ db_session.add(
+ InventoryDeviceLink(
+ source_ieee="0xCOORD",
+ target_ieee="0xR1",
+ discovery_source="zigbee_mesh",
+ lqi=132,
+ )
+ )
+ await db_session.commit()
+
+ res = await client.post(
+ f"/api/v1/scan/pending/{pending.id}/approve",
+ json={
+ "label": "router_1",
+ "type": "zigbee_router",
+ "ip": None,
+ "status": "unknown",
+ "services": [],
+ },
+ headers=headers,
+ )
+ assert res.status_code == 200
+ data = res.json()
+ assert data["edges_created"] == 1
+ assert data["edges"][0]["type"] == "zigbee_mesh"
+ assert data["edges"][0]["lqi"] == 132
+
+ edges = (await db_session.execute(select(Edge))).scalars().all()
+ assert len(edges) == 1
+ assert edges[0].type == "zigbee_mesh"
+ # Sideways, like the cluster edges — it reads apart from the tree.
+ assert edges[0].source_handle == "right"
+ assert edges[0].target_handle == "left"
+ assert edges[0].lqi == 132
+
+
+async def test_approve_zigbee_tree_link_carries_lqi_onto_edge(
+ client: AsyncClient, headers, db_session
+):
+ """Same for a tree link, and lqi=0 (a real, worst-case reading) survives."""
+ from sqlalchemy import select
+
+ from app.db.models import Edge, InventoryDeviceLink
+
+ coord_device = InventoryDevice(
+ ieee_address="0xCOORD",
+ friendly_name="Coordinator",
+ suggested_type="zigbee_coordinator",
+ status="approved",
+ )
+ db_session.add(coord_device)
+ await db_session.flush()
+ db_session.add(
+ Node(label="Coordinator", type="zigbee_coordinator", device_id=coord_device.id)
+ )
+ pending = InventoryDevice(
+ ieee_address="0xR1",
+ friendly_name="router_1",
+ suggested_type="zigbee_router",
+ status="pending",
+ discovery_source="zigbee",
+ )
+ db_session.add(pending)
+ db_session.add(
+ InventoryDeviceLink(
+ source_ieee="0xCOORD",
+ target_ieee="0xR1",
+ discovery_source="zigbee",
+ lqi=0,
+ )
+ )
+ await db_session.commit()
+
+ res = await client.post(
+ f"/api/v1/scan/pending/{pending.id}/approve",
+ json={
+ "label": "router_1",
+ "type": "zigbee_router",
+ "ip": None,
+ "status": "unknown",
+ "services": [],
+ },
+ headers=headers,
+ )
+ assert res.status_code == 200
+ assert res.json()["edges"][0]["lqi"] == 0
+
+ edges = (await db_session.execute(select(Edge))).scalars().all()
+ assert edges[0].type == "iot"
+ assert edges[0].lqi == 0
+
+
@pytest.mark.asyncio
async def test_approve_zigbee_skips_duplicate_edge(
client: AsyncClient, headers, db_session
diff --git a/frontend/src/api/client.ts b/frontend/src/api/client.ts
index b892fa9..65dff2d 100644
--- a/frontend/src/api/client.ts
+++ b/frontend/src/api/client.ts
@@ -198,7 +198,7 @@ export const scanApi = {
approved: boolean
node_id: string
edges_created: number
- edges: { id: string; source: string; target: string; type?: string; source_handle?: string | null; target_handle?: string | null }[]
+ edges: { id: string; source: string; target: string; type?: string; source_handle?: string | null; target_handle?: string | null; lqi?: number | null }[]
}>(`/scan/pending/${id}/approve`, nodeData),
hide: (id: string) => api.post(`/scan/pending/${id}/hide`),
ignore: (id: string) => api.post(`/scan/pending/${id}/ignore`),
@@ -208,7 +208,7 @@ export const scanApi = {
node_ids: string[]
device_ids: string[]
edges_created: number
- edges: { id: string; source: string; target: string; type?: string; source_handle?: string | null; target_handle?: string | null }[]
+ edges: { id: string; source: string; target: string; type?: string; source_handle?: string | null; target_handle?: string | null; lqi?: number | null }[]
skipped: number
skipped_devices: SkippedDevice[]
}>('/scan/pending/bulk-approve', { device_ids: ids, design_id: designId ?? undefined }),
diff --git a/frontend/src/utils/__tests__/autoEdges.test.ts b/frontend/src/utils/__tests__/autoEdges.test.ts
index 4f4c8f6..5397cca 100644
--- a/frontend/src/utils/__tests__/autoEdges.test.ts
+++ b/frontend/src/utils/__tests__/autoEdges.test.ts
@@ -55,6 +55,22 @@ describe('applyAutoEdges', () => {
expect(res.nodes).toBe(nodes)
})
+ it('carries the imported lqi onto the edge data (0 included)', () => {
+ const res = applyAutoEdges([node('a'), node('b')], [], [
+ { id: 'e1', source: 'a', target: 'b', type: 'zigbee_mesh', source_handle: 'right', target_handle: 'left', lqi: 132 },
+ { id: 'e2', source: 'a', target: 'b', type: 'iot', lqi: 0 },
+ ])
+ expect(res.edges[0].data).toMatchObject({ type: 'zigbee_mesh', lqi: 132 })
+ expect(res.edges[1].data).toMatchObject({ type: 'iot', lqi: 0 })
+ })
+
+ it('omits lqi when the server sent none', () => {
+ const res = applyAutoEdges([node('a'), node('b')], [], [
+ { id: 'e1', source: 'a', target: 'b', type: 'iot', lqi: null },
+ ])
+ expect(res.edges[0].data).toEqual({ type: 'iot' })
+ })
+
it('appends to existing edges rather than replacing them', () => {
const existing = [{ id: 'x', source: 'a', target: 'b' }] as Edge<EdgeData>[]
const res = applyAutoEdges([node('a'), node('b')], existing, [
diff --git a/frontend/src/utils/autoEdges.ts b/frontend/src/utils/autoEdges.ts
index bd9c941..5378469 100644
--- a/frontend/src/utils/autoEdges.ts
+++ b/frontend/src/utils/autoEdges.ts
@@ -18,6 +18,8 @@ export interface AutoEdge {
type?: string
source_handle?: string | null
target_handle?: string | null
+ /** Measured link quality, carried from the import (mesh links have no other home). */
+ lqi?: number | null
}
/** Side a handle id sits on ('left-t' → 'left', 'bottom-2' → 'bottom'). */
@@ -59,7 +61,7 @@ export function applyAutoEdges(
sourceHandle,
targetHandle,
type,
- data: { type } as EdgeData,
+ data: (e.lqi == null ? { type } : { type, lqi: e.lqi }) as EdgeData,
}
})