File: helpers.py

package info (click to toggle)
python-airos 0.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 704 kB
  • sloc: python: 2,967; sh: 19; makefile: 3
file content (58 lines) | stat: -rw-r--r-- 1,611 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Ubiquiti AirOS firmware helpers."""

import logging
from typing import TypedDict

import aiohttp

from .airos8 import AirOS8
from .exceptions import (
    AirOSConnectionAuthenticationError,
    AirOSConnectionSetupError,
    AirOSDataMissingError,
    AirOSDeviceConnectionError,
    AirOSKeyDataMissingError,
)

_LOGGER = logging.getLogger(__name__)


class DetectDeviceData(TypedDict):
    """Container for device data."""

    fw_major: int
    mac: str
    hostname: str


async def async_get_firmware_data(
    host: str,
    username: str,
    password: str,
    session: aiohttp.ClientSession,
    use_ssl: bool = True,
) -> DetectDeviceData:
    """Connect to a device and return the major firmware version."""
    detect_device: AirOS8 = AirOS8(host, username, password, session, use_ssl)

    try:
        await detect_device.login()
        device_data = await detect_device.raw_status()
    except (
        AirOSConnectionSetupError,
        AirOSDeviceConnectionError,
    ):
        _LOGGER.exception("Error connecting to device at %s", host)
        raise
    except (AirOSConnectionAuthenticationError, AirOSDataMissingError):
        _LOGGER.exception("Authentication error connecting to device at %s", host)
        raise
    except AirOSKeyDataMissingError:
        _LOGGER.exception("Key data missing from device at %s", host)
        raise

    return {
        "fw_major": AirOS8.get_fw_major(device_data.get("host", {}).get("fwversion")),
        "hostname": device_data.get("host", {}).get("hostname"),
        "mac": AirOS8.get_mac(device_data.get("interfaces", {}))["mac"],
    }