Skip to content

Commit f571942

Browse files
authored
Merge pull request #23 from CoMPaTech/testing
Bump json to mashumaro
2 parents 45d51d3 + e4ad2ca commit f571942

File tree

8 files changed

+493
-16
lines changed

8 files changed

+493
-16
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ async def test_airos():
2222
# Fetch status (large dict, including connected stations)
2323
result = await device.status()
2424
print(f"Result: {result}")
25+
print(f"Result: {result.wireless.mode}")
2526
# Reconnect 'other side'
2627
result = await device.stakick("01:23:45:67:89:AB")
2728
print(f"Result: {result}")

airos/airos8.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from urllib.parse import urlparse
88

99
import aiohttp
10+
from mashumaro.exceptions import InvalidFieldValue, MissingField
1011

12+
from .airos8data import AirOSData
1113
from .exceptions import (
1214
ConnectionAuthenticationError,
1315
ConnectionSetupError,
@@ -185,7 +187,7 @@ async def login(self) -> bool:
185187
logger.exception("Error during login")
186188
raise DeviceConnectionError from err
187189

188-
async def status(self) -> dict:
190+
async def status(self, return_json: bool = False) -> dict | AirOSData:
189191
"""Retrieve status from the device."""
190192
if not self.connected:
191193
logger.error("Not connected, login first")
@@ -205,15 +207,22 @@ async def status(self) -> dict:
205207
try:
206208
response_text = await response.text()
207209
response_json = json.loads(response_text)
208-
if (
209-
"host" not in response_json
210-
or "device_id" not in response_json["host"]
211-
):
212-
logger.error(
213-
"Source data missing 'host' or 'device_id' keys"
214-
)
215-
raise KeyDataMissingError from None
216-
return response_json
210+
try:
211+
airos_data = AirOSData.from_dict(response_json)
212+
except (MissingField, InvalidFieldValue) as err:
213+
logger.exception("Failed to deserialize AirOS data")
214+
raise KeyDataMissingError from err
215+
216+
# Show new enums detected
217+
if airos_data.warnings:
218+
for field_name, messages in airos_data.warnings.items():
219+
for msg in messages:
220+
log = f"AirOS data warning for field '{field_name}': {msg}"
221+
logger.warning(log)
222+
223+
if return_json:
224+
return response_json
225+
return airos_data
217226
except json.JSONDecodeError:
218227
logger.exception(
219228
"JSON Decode Error in authenticated status response"

0 commit comments

Comments
 (0)