Skip to content

Commit 7d29542

Browse files
Copilotliudger
andcommitted
Fix linting issues for BSB-LAN 5.x compatibility code
Co-authored-by: liudger <4112111+liudger@users.noreply.github.com>
1 parent 539a99a commit 7d29542

2 files changed

Lines changed: 31 additions & 25 deletions

File tree

src/bsblan/bsblan.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import asyncio
66
import logging
77
from dataclasses import dataclass, field
8-
from typing import TYPE_CHECKING, Any, Literal, Mapping, cast
8+
from typing import TYPE_CHECKING, Any, Literal, cast
9+
10+
if TYPE_CHECKING:
11+
from collections.abc import Mapping
912

1013
import aiohttp
1114
from aiohttp.hdrs import METH_POST
@@ -309,7 +312,7 @@ async def _request(
309312
headers=headers,
310313
) as response:
311314
response.raise_for_status()
312-
response_data = cast(dict[str, Any], await response.json())
315+
response_data = cast("dict[str, Any]", await response.json())
313316
return self._process_response(response_data, base_path)
314317
except asyncio.TimeoutError as e:
315318
raise BSBLANConnectionError(BSBLANConnectionError.message_timeout) from e
@@ -318,35 +321,38 @@ async def _request(
318321
except ValueError as e:
319322
raise BSBLANError(str(e)) from e
320323

321-
def _process_response(self, response_data: dict[str, Any], base_path: str) -> dict[str, Any]:
324+
def _process_response(
325+
self, response_data: dict[str, Any], base_path: str
326+
) -> dict[str, Any]:
322327
"""Process response data based on firmware version.
323-
328+
324329
BSB-LAN 5.0+ includes additional 'payload' field in /JQ responses
325330
that needs to be handled for compatibility.
326-
331+
327332
Args:
328333
response_data: Raw response data from BSB-LAN
329334
base_path: The API endpoint that was called
330-
335+
331336
Returns:
332337
Processed response data compatible with existing code
338+
333339
"""
334340
# For non-JQ endpoints, return response as-is
335341
if base_path != "/JQ":
336342
return response_data
337-
343+
338344
# Check if we have a firmware version to determine processing
339345
if not self._firmware_version:
340346
return response_data
341-
347+
342348
# For BSB-LAN 5.0+, remove 'payload' field if present as it's for debugging
343349
version = pkg_version.parse(self._firmware_version)
344-
if version >= pkg_version.parse("5.0.0"):
350+
if version >= pkg_version.parse("5.0.0") and "payload" in response_data:
345351
# Remove payload field if present - it's added for debugging in 5.0+
346-
if "payload" in response_data:
347-
processed_data = {k: v for k, v in response_data.items() if k != "payload"}
348-
return processed_data
349-
352+
return {
353+
k: v for k, v in response_data.items() if k != "payload"
354+
}
355+
350356
return response_data
351357

352358
def _build_url(self, base_path: str) -> URL:

tests/test_version_errors.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,31 +120,31 @@ async def test_set_api_version_v5_early() -> None:
120120
assert bsblan._api_version == "v3" # BSB-LAN 5.x uses v3 API with extensions
121121

122122

123-
@pytest.mark.asyncio
123+
@pytest.mark.asyncio
124124
async def test_process_response_v5_payload_removal() -> None:
125125
"""Test that BSB-LAN 5.x payload field is removed from responses."""
126126
config = BSBLANConfig(host="example.com")
127127
bsblan = BSBLAN(config)
128-
128+
129129
# Set firmware version to 5.0.16
130130
bsblan._firmware_version = "5.0.16"
131-
131+
132132
# Mock response with payload field (as added in BSB-LAN 5.0+)
133133
response_with_payload = {
134134
"8700": {"value": "20.5", "unit": "°C"},
135-
"8740": {"value": "21.0", "unit": "°C"},
135+
"8740": {"value": "21.0", "unit": "°C"},
136136
"payload": "debug_payload_data_here"
137137
}
138-
138+
139139
# Process the response
140140
processed = bsblan._process_response(response_with_payload, "/JQ")
141-
141+
142142
# Payload should be removed
143143
expected = {
144144
"8700": {"value": "20.5", "unit": "°C"},
145145
"8740": {"value": "21.0", "unit": "°C"}
146146
}
147-
147+
148148
assert processed == expected
149149
assert "payload" not in processed
150150

@@ -154,20 +154,20 @@ async def test_process_response_non_jq_endpoint() -> None:
154154
"""Test that non-JQ endpoints are not processed for payload removal."""
155155
config = BSBLANConfig(host="example.com")
156156
bsblan = BSBLAN(config)
157-
157+
158158
# Set firmware version to 5.0.16
159159
bsblan._firmware_version = "5.0.16"
160-
160+
161161
# Mock response with payload field
162162
response_with_payload = {
163163
"name": "BSB-LAN",
164164
"version": "5.0.16",
165165
"payload": "should_remain_for_non_jq"
166166
}
167-
167+
168168
# Process the response for non-JQ endpoint
169169
processed = bsblan._process_response(response_with_payload, "/JI")
170-
170+
171171
# Payload should remain for non-JQ endpoints
172172
assert processed == response_with_payload
173173
assert "payload" in processed
@@ -198,6 +198,6 @@ async def test_unsupported_version_still_fails() -> None:
198198

199199
# Test that version 2.0.0 still fails (gap between v1 and v3)
200200
bsblan._firmware_version = "2.0.0"
201-
201+
202202
with pytest.raises(BSBLANVersionError):
203203
bsblan._set_api_version()

0 commit comments

Comments
 (0)