washpy.device

 1from dataclasses import dataclass
 2from typing import Final, List
 3
 4import requests
 5
 6
 7@dataclass
 8class Device:
 9    host: Final[str]
10    """hostname or IP address, e.g. `192.168.1.251`"""
11
12    device_id: Final[str]
13    """Device ID, e.g. `000116343328`"""
14
15    url: Final[str]
16    """
17    A device url follows this scheme:
18
19    `https://<host>/Devices/<device_id>`
20
21    e.g. `'https://192.168.1.251/Devices/000116343328'`
22    """
23
24    def __init__(self, host: str, device_id: str) -> None:
25        """
26        host: hostname or IP address
27        device_id: Device ID
28
29        The device url is derived from that parameters.
30        000.177.597.163
31        """
32
33        self.host = host
34        self.device_id = device_id
35
36        self.url = f"https://{self.host}/Devices/{self.device_id}"
37
38
39def get_devices_from_host(
40    host: str, verify_https: bool | str = False, https_timeout: float = 3.0
41) -> List[Device]:
42    """
43    host: hostname or IP address
44
45    verify_https: is passed to the `requests.request` `verify` argument
46
47    https_timeout: timeout for the request, in seconds
48
49
50    Gets the list of all devices that live on the specified host.
51
52    **raises**: `ValueError`, if the request does not return HTTP 200 status code.
53    """
54    url = f"https://{host}/Devices"
55
56    payload = {}
57    headers = {}
58
59    response = requests.request(
60        "GET",
61        url,
62        headers=headers,
63        data=payload,
64        verify=verify_https,
65        timeout=https_timeout,
66    )
67
68    if response.status_code != 200:
69        raise ValueError(f"Unable to GET: got HTTP response {response}")
70
71    return [Device(host, id_str) for id_str in response.json().keys()]
@dataclass
class Device:
 8@dataclass
 9class Device:
10    host: Final[str]
11    """hostname or IP address, e.g. `192.168.1.251`"""
12
13    device_id: Final[str]
14    """Device ID, e.g. `000116343328`"""
15
16    url: Final[str]
17    """
18    A device url follows this scheme:
19
20    `https://<host>/Devices/<device_id>`
21
22    e.g. `'https://192.168.1.251/Devices/000116343328'`
23    """
24
25    def __init__(self, host: str, device_id: str) -> None:
26        """
27        host: hostname or IP address
28        device_id: Device ID
29
30        The device url is derived from that parameters.
31        000.177.597.163
32        """
33
34        self.host = host
35        self.device_id = device_id
36
37        self.url = f"https://{self.host}/Devices/{self.device_id}"
Device(host: str, device_id: str)
25    def __init__(self, host: str, device_id: str) -> None:
26        """
27        host: hostname or IP address
28        device_id: Device ID
29
30        The device url is derived from that parameters.
31        000.177.597.163
32        """
33
34        self.host = host
35        self.device_id = device_id
36
37        self.url = f"https://{self.host}/Devices/{self.device_id}"

host: hostname or IP address device_id: Device ID

The device url is derived from that parameters. 000.177.597.163

host: Final[str]

hostname or IP address, e.g. 192.168.1.251

device_id: Final[str]

Device ID, e.g. 000116343328

url: Final[str]

A device url follows this scheme:

https://<host>/Devices/<device_id>

e.g. 'https://192.168.1.251/Devices/000116343328'

def get_devices_from_host( host: str, verify_https: bool | str = False, https_timeout: float = 3.0) -> List[Device]:
40def get_devices_from_host(
41    host: str, verify_https: bool | str = False, https_timeout: float = 3.0
42) -> List[Device]:
43    """
44    host: hostname or IP address
45
46    verify_https: is passed to the `requests.request` `verify` argument
47
48    https_timeout: timeout for the request, in seconds
49
50
51    Gets the list of all devices that live on the specified host.
52
53    **raises**: `ValueError`, if the request does not return HTTP 200 status code.
54    """
55    url = f"https://{host}/Devices"
56
57    payload = {}
58    headers = {}
59
60    response = requests.request(
61        "GET",
62        url,
63        headers=headers,
64        data=payload,
65        verify=verify_https,
66        timeout=https_timeout,
67    )
68
69    if response.status_code != 200:
70        raise ValueError(f"Unable to GET: got HTTP response {response}")
71
72    return [Device(host, id_str) for id_str in response.json().keys()]

host: hostname or IP address

verify_https: is passed to the requests.request verify argument

https_timeout: timeout for the request, in seconds

Gets the list of all devices that live on the specified host.

raises: ValueError, if the request does not return HTTP 200 status code.