From cc9c0100024e25b7759e60ab216992cd78e29649 Mon Sep 17 00:00:00 2001 From: pranjal-joshi Date: Mon, 4 May 2026 14:12:38 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20adhere=20to=20CONTRIBUTING.md=20?= =?UTF-8?q?=E2=80=94=20ruff=20clean=20+=20all=20tests=20passing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Move aiomqtt to module-level import (enables proper patch() in tests) - Remove unused variable (description) in zigbee_service — ruff F841 - Split long line (132 chars) to fit 120 char limit — ruff E501 - Fix import sort order in test files — ruff I001 - Remove unused imports (asyncio, AsyncMock, MagicMock) — ruff F401 - Rename test_mqtt_connection import alias to _test_mqtt_connection to avoid pytest fixture name collision (ERROR at setup) - All 33 backend tests now pass (21 service + 12 router) - TypeScript typecheck: 0 errors Co-authored-by: CyberKeys --- backend/app/services/zigbee_service.py | 26 +++++++++++++------------- backend/tests/test_zigbee_router.py | 1 - backend/tests/test_zigbee_service.py | 13 +++++++------ 3 files changed, 20 insertions(+), 20 deletions(-) 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)