Skip to content

Commit 626ddf3

Browse files
committed
Improve logic and naming
1 parent f9cabdf commit 626ddf3

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

airos/airos8.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,22 +191,34 @@ async def login(self) -> bool:
191191
_LOGGER.exception("Error during login")
192192
raise DeviceConnectionError from err
193193

194-
def custom_data(
194+
def derived_data(
195195
self, response: dict[str, Any] | None = None
196196
) -> dict[str, Any] | None:
197-
"""Add custom data to the device response."""
197+
"""Add derived data to the device response."""
198198
addresses = {}
199199
interface_order = ["br0", "eth0", "ath0"]
200-
for interface in response.get("interfaces", []):
200+
interfaces = response.get("interfaces", [])
201+
202+
# No interfaces, no mac, no usability
203+
if not interfaces:
204+
raise KeyDataMissingError from None
205+
206+
for interface in interfaces:
201207
if interface["enabled"]: # Only consider if enabled
202208
addresses[interface["ifname"]] = interface["hwaddr"]
203209

204210
for interface in interface_order:
205-
response["custom"] = {
211+
response["derived"] = {
206212
"mac": addresses[interface],
207213
"mac_interface": interface,
208214
}
209215
return response
216+
217+
# Fallback take fist alternate interface found
218+
response["derived"] = {
219+
"mac": interfaces[0]["hwaddr"],
220+
"mac_interface": interfaces[0]["ifname"],
221+
}
210222
return response
211223

212224
async def status(self) -> AirOSData:
@@ -230,7 +242,7 @@ async def status(self) -> AirOSData:
230242
response_text = await response.text()
231243
response_json = json.loads(response_text)
232244
try:
233-
adjusted_json = self.custom_data(response_json)
245+
adjusted_json = self.derived_data(response_json)
234246
airos_data = AirOSData.from_dict(adjusted_json)
235247
except (MissingField, InvalidFieldValue) as err:
236248
_LOGGER.exception("Failed to deserialize AirOS data")

airos/data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ class GPSMain:
428428

429429

430430
@dataclass
431-
class Custom:
431+
class Derived:
432432
"""Contain custom data generated by this module."""
433433

434434
mac: str # Base device MAC address (i.e. eth0)
@@ -451,4 +451,4 @@ class AirOS8Data(DataClassDictMixin):
451451
ntpclient: Any
452452
unms: UnmsStatus
453453
gps: GPSMain
454-
custom: Custom
454+
derived: Derived

0 commit comments

Comments
 (0)