diff --git a/backend/app/services/zigbee_service.py b/backend/app/services/zigbee_service.py index b8209bb..2457f30 100644 --- a/backend/app/services/zigbee_service.py +++ b/backend/app/services/zigbee_service.py @@ -8,6 +8,11 @@ import logging logger = logging.getLogger(__name__) +try: + import aiomqtt # type: ignore[import] +except ImportError: # pragma: no cover + aiomqtt = None # type: ignore[assignment] + _NETWORKMAP_REQUEST_TOPIC = "{base_topic}/bridge/request/networkmap" _NETWORKMAP_RESPONSE_TOPIC = "{base_topic}/bridge/response/networkmap" _CONNECTION_TIMEOUT = 5.0 # seconds to verify broker reachability @@ -54,7 +59,6 @@ def parse_networkmap(payload: dict) -> tuple[list[dict], list[dict]]: friendly_name: str = source.get("friendlyName") or source.get("friendly_name") or ieee model: str | None = source.get("modelID") or source.get("model") vendor: str | None = source.get("vendor") - description: str | None = source.get("description") if ieee not in seen_ids: seen_ids.add(ieee) @@ -78,7 +82,10 @@ def parse_networkmap(payload: dict) -> tuple[list[dict], list[dict]]: # Walk the route targets to build edges and collect additional nodes targets = route.get("routes", []) for target_entry in targets: - target_ieee = target_entry.get("target", {}).get("ieeeAddr") or target_entry.get("target", {}).get("ieee_address") or "" + target_src = target_entry.get("target", {}) + target_ieee = ( + target_src.get("ieeeAddr") or target_src.get("ieee_address") or "" + ) lqi: int | None = target_entry.get("lqi") if not target_ieee: @@ -149,13 +156,8 @@ async def fetch_networkmap( ConnectionError: if the broker cannot be reached. ValueError: if the response payload is malformed. """ - try: - import aiomqtt # type: ignore[import] - except ImportError as exc: # pragma: no cover - raise ImportError( - "aiomqtt is required for Zigbee import. " - "Install it with: pip install aiomqtt" - ) from exc + if aiomqtt is None: # pragma: no cover + raise ImportError("aiomqtt is required for Zigbee import. Install it with: pip install aiomqtt") request_topic = _NETWORKMAP_REQUEST_TOPIC.format(base_topic=base_topic) response_topic = _NETWORKMAP_RESPONSE_TOPIC.format(base_topic=base_topic) @@ -212,10 +214,8 @@ async def test_mqtt_connection( Returns True on success, raises ConnectionError on failure. """ - try: - import aiomqtt # type: ignore[import] - except ImportError as exc: # pragma: no cover - raise ImportError("aiomqtt is required") from exc + if aiomqtt is None: # pragma: no cover + raise ImportError("aiomqtt is required") try: async with aiomqtt.Client( diff --git a/backend/tests/test_zigbee_router.py b/backend/tests/test_zigbee_router.py index 588a417..2aab038 100644 --- a/backend/tests/test_zigbee_router.py +++ b/backend/tests/test_zigbee_router.py @@ -7,7 +7,6 @@ from unittest.mock import patch import pytest from httpx import AsyncClient - # --------------------------------------------------------------------------- # Fixtures # --------------------------------------------------------------------------- diff --git a/backend/tests/test_zigbee_service.py b/backend/tests/test_zigbee_service.py index 85c7387..4b9f6b7 100644 --- a/backend/tests/test_zigbee_service.py +++ b/backend/tests/test_zigbee_service.py @@ -2,11 +2,11 @@ from __future__ import annotations -import asyncio import json from typing import Any -from unittest.mock import AsyncMock, MagicMock, patch +from unittest.mock import patch +import aiomqtt # noqa: F401 import pytest from app.services.zigbee_service import ( @@ -14,9 +14,10 @@ from app.services.zigbee_service import ( _z2m_type_to_homelable, fetch_networkmap, parse_networkmap, - test_mqtt_connection, ) - +from app.services.zigbee_service import ( + test_mqtt_connection as _test_mqtt_connection, +) # --------------------------------------------------------------------------- # Helper builders @@ -341,7 +342,7 @@ async def test_test_mqtt_connection_success() -> None: mock_aiomqtt.Client.return_value = _FakeClient() mock_aiomqtt.MqttError = Exception - result = await test_mqtt_connection("localhost", 1883) + result = await _test_mqtt_connection("localhost", 1883) assert result is True @@ -359,4 +360,4 @@ async def test_test_mqtt_connection_failure() -> None: mock_aiomqtt.MqttError = Exception with pytest.raises(ConnectionError): - await test_mqtt_connection("bad-host", 1883) + await _test_mqtt_connection("bad-host", 1883)