fix: adhere to CONTRIBUTING.md — ruff clean + all tests passing
- 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 <noreply@openclaw.ai>
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -7,7 +7,6 @@ from unittest.mock import patch
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Fixtures
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user