Skip to content

Commit 0fb7491

Browse files
committed
Quality
1 parent 21b028c commit 0fb7491

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed

airos/airos8.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,7 @@ async def provmode(self, active: bool = False) -> bool:
398398
_LOGGER.info("Provisioning mode change task was cancelled")
399399
raise
400400

401-
402-
async def warnings(self) -> dict[str, Any]:
401+
async def warnings(self) -> dict[str, Any] | Any:
403402
"""Get warnings."""
404403
if not self.connected:
405404
_LOGGER.error("Not connected, login first")
@@ -420,7 +419,7 @@ async def warnings(self) -> dict[str, Any]:
420419
return json.loads(response_text)
421420
log = f"Unable to fech warning status {response.status} with {response_text}"
422421
_LOGGER.error(log)
423-
return False
422+
raise AirOSDataMissingError from None
424423
except json.JSONDecodeError:
425424
_LOGGER.exception("JSON Decode Error in warning response")
426425
raise AirOSDataMissingError from None
@@ -431,8 +430,7 @@ async def warnings(self) -> dict[str, Any]:
431430
_LOGGER.info("Warning check task was cancelled")
432431
raise
433432

434-
435-
async def update_check(self) -> dict[str, Any]:
433+
async def update_check(self) -> dict[str, Any] | Any:
436434
"""Get warnings."""
437435
if not self.connected:
438436
_LOGGER.error("Not connected, login first")
@@ -455,7 +453,7 @@ async def update_check(self) -> dict[str, Any]:
455453
return json.loads(response_text)
456454
log = f"Unable to fech update status {response.status} with {response_text}"
457455
_LOGGER.error(log)
458-
return False
456+
raise AirOSDataMissingError from None
459457
except json.JSONDecodeError:
460458
_LOGGER.exception("JSON Decode Error in warning response")
461459
raise AirOSDataMissingError from None
@@ -465,4 +463,3 @@ async def update_check(self) -> dict[str, Any]:
465463
except asyncio.CancelledError:
466464
_LOGGER.info("Warning update status task was cancelled")
467465
raise
468-

tests/test_airos8.py

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -260,18 +260,24 @@ async def test_status_missing_required_key_in_json(airos_device: AirOS) -> None:
260260
# Assert that the cause of our exception is the correct type from mashumaro
261261
assert isinstance(excinfo.value.__cause__, MissingField)
262262

263+
263264
# --- Tests for warnings() and update_check() ---
264265
@pytest.mark.asyncio
265266
async def test_warnings_correctly_parses_json() -> None:
266267
"""Test that the warnings() method correctly parses a valid JSON response."""
267268
mock_session = MagicMock()
268-
airos_device = AirOS(host="http://192.168.1.3", username="test", password="test", session=mock_session)
269+
airos_device = AirOS(
270+
host="http://192.168.1.3",
271+
username="test",
272+
password="test",
273+
session=mock_session,
274+
)
269275
airos_device.connected = True
270276

271277
mock_response = MagicMock()
272278
mock_response.__aenter__.return_value = mock_response
273279
mock_response.status = 200
274-
with open("fixtures/warnings.json") as f:
280+
with open("fixtures/warnings.json", encoding="utf-8") as f:
275281
mock_response_data = json.load(f)
276282
mock_response.text = AsyncMock(return_value=json.dumps(mock_response_data))
277283

@@ -285,31 +291,43 @@ async def test_warnings_correctly_parses_json() -> None:
285291
async def test_warnings_raises_exception_on_invalid_json() -> None:
286292
"""Test that warnings() raises an exception on invalid JSON response."""
287293
mock_session = MagicMock()
288-
airos_device = AirOS(host="http://192.168.1.3", username="test", password="test", session=mock_session)
294+
airos_device = AirOS(
295+
host="http://192.168.1.3",
296+
username="test",
297+
password="test",
298+
session=mock_session,
299+
)
289300
airos_device.connected = True
290301

291302
mock_response = MagicMock()
292303
mock_response.__aenter__.return_value = mock_response
293304
mock_response.status = 200
294305
mock_response.text = AsyncMock(return_value="This is not JSON")
295306

296-
with (patch.object(airos_device.session, "get", return_value=mock_response),
297-
pytest.raises(airos.exceptions.AirOSDataMissingError)):
298-
await airos_device.warnings()
307+
with (
308+
patch.object(airos_device.session, "get", return_value=mock_response),
309+
pytest.raises(airos.exceptions.AirOSDataMissingError),
310+
):
311+
await airos_device.warnings()
299312

300313

301314
@pytest.mark.asyncio
302315
async def test_update_check_correctly_parses_json() -> None:
303316
"""Test that update_check() method correctly parses a valid JSON response."""
304317
mock_session = MagicMock()
305-
airos_device = AirOS(host="http://192.168.1.3", username="test", password="test", session=mock_session)
318+
airos_device = AirOS(
319+
host="http://192.168.1.3",
320+
username="test",
321+
password="test",
322+
session=mock_session,
323+
)
306324
airos_device.connected = True
307325
airos_device.current_csrf_token = "mock-csrf-token"
308326

309327
mock_response = MagicMock()
310328
mock_response.__aenter__.return_value = mock_response
311329
mock_response.status = 200
312-
with open("fixtures/update_check_available.json") as f:
330+
with open("fixtures/update_check_available.json", encoding="utf-8") as f:
313331
mock_response_data = json.load(f)
314332
mock_response.text = AsyncMock(return_value=json.dumps(mock_response_data))
315333

@@ -323,7 +341,12 @@ async def test_update_check_correctly_parses_json() -> None:
323341
async def test_update_check_raises_exception_on_invalid_json() -> None:
324342
"""Test that update_check() raises an exception on invalid JSON response."""
325343
mock_session = MagicMock()
326-
airos_device = AirOS(host="http://192.168.1.3", username="test", password="test", session=mock_session)
344+
airos_device = AirOS(
345+
host="http://192.168.1.3",
346+
username="test",
347+
password="test",
348+
session=mock_session,
349+
)
327350
airos_device.connected = True
328351
airos_device.current_csrf_token = "mock-csrf-token"
329352

@@ -332,16 +355,23 @@ async def test_update_check_raises_exception_on_invalid_json() -> None:
332355
mock_response.status = 200
333356
mock_response.text = AsyncMock(return_value="This is not JSON")
334357

335-
with (patch.object(airos_device.session, "post", return_value=mock_response),
336-
pytest.raises(airos.exceptions.AirOSDataMissingError)):
337-
await airos_device.update_check()
358+
with (
359+
patch.object(airos_device.session, "post", return_value=mock_response),
360+
pytest.raises(airos.exceptions.AirOSDataMissingError),
361+
):
362+
await airos_device.update_check()
338363

339364

340365
@pytest.mark.asyncio
341366
async def test_warnings_when_not_connected() -> None:
342367
"""Test calling warnings() before a successful login."""
343368
mock_session = MagicMock()
344-
airos_device = AirOS(host="http://192.168.1.3", username="test", password="test", session=mock_session)
369+
airos_device = AirOS(
370+
host="http://192.168.1.3",
371+
username="test",
372+
password="test",
373+
session=mock_session,
374+
)
345375
airos_device.connected = False # Explicitly set connected state to false
346376

347377
with pytest.raises(airos.exceptions.AirOSDeviceConnectionError):
@@ -352,8 +382,13 @@ async def test_warnings_when_not_connected() -> None:
352382
async def test_update_check_when_not_connected() -> None:
353383
"""Test calling update_check() before a successful login."""
354384
mock_session = MagicMock()
355-
airos_device = AirOS(host="http://192.168.1.3", username="test", password="test", session=mock_session)
356-
airos_device.connected = False # Explicitly set connected state to false
385+
airos_device = AirOS(
386+
host="http://192.168.1.3",
387+
username="test",
388+
password="test",
389+
session=mock_session,
390+
)
391+
airos_device.connected = False # Explicitly set connected state to false
357392

358393
with pytest.raises(airos.exceptions.AirOSDeviceConnectionError):
359394
await airos_device.update_check()

0 commit comments

Comments
 (0)