Debian Codegraph @

airos/helpers.py

raw
1 """Ubiquiti AirOS firmware helpers."""
2
3 import logging
4 from typing import TypedDict
5
6 import aiohttp
7
8 from .airos8 import AirOS8
9 from .exceptions import (
10 AirOSConnectionAuthenticationError,
11 AirOSConnectionSetupError,
12 AirOSDataMissingError,
13 AirOSDeviceConnectionError,
14 AirOSKeyDataMissingError,
15 AirOSTLSCompatibilityError,
16 )
17
18 _LOGGER = logging.getLogger(__name__)
19
20
21 class DetectDeviceData(TypedDict):
22 """Container for device data."""
23
24 fw_major: int
25 mac: str
26 hostname: str
27
28
29 async def async_get_firmware_data(
30 host: str,
31 username: str,
32 password: str,
33 session: aiohttp.ClientSession,
34 use_ssl: bool = True,
35 ) -> DetectDeviceData:
36 """Connect to a device and return the major firmware version."""
37 detect_device: AirOS8 = AirOS8(host, username, password, session, use_ssl)
38
39 try:
40 await detect_device.login()
41 device_data = await detect_device.raw_status()
42 except AirOSTLSCompatibilityError:
43 _LOGGER.exception("TLS compatibility error during API call to %s", host)
44 raise
45 except (
46 AirOSConnectionSetupError,
47 AirOSDeviceConnectionError,
48 ):
49 _LOGGER.exception("Error connecting to device at %s", host)
50 raise
51 except AirOSConnectionAuthenticationError, AirOSDataMissingError:
52 _LOGGER.exception("Authentication error connecting to device at %s", host)
53 raise
54 except AirOSKeyDataMissingError:
55 _LOGGER.exception("Key data missing from device at %s", host)
56 raise
57
58 return {
59 "fw_major": AirOS8.get_fw_major(device_data.get("host", {}).get("fwversion")),
60 "hostname": device_data.get("host", {}).get("hostname"),
61 "mac": AirOS8.get_mac(device_data.get("interfaces", {}))["mac"],
62 }