Home Assistant Unofficial Reference 2024.12.1
utils.py
Go to the documentation of this file.
1 """Utilities for the Huawei LTE integration."""
2 
3 from __future__ import annotations
4 
5 from contextlib import suppress
6 import re
7 from urllib.parse import urlparse
8 import warnings
9 
10 from huawei_lte_api.Session import GetResponseType
11 import requests
12 from urllib3.exceptions import InsecureRequestWarning
13 
14 from homeassistant.helpers.device_registry import format_mac
15 
16 
18  device_info: GetResponseType, wlan_settings: GetResponseType
19 ) -> list[str]:
20  """Get list of device MAC addresses.
21 
22  :param device_info: the device.information structure for the device
23  :param wlan_settings: the wlan.multi_basic_settings structure for the device
24  """
25  macs = [
26  device_info.get(x)
27  for x in ("MacAddress1", "MacAddress2", "WifiMacAddrWl0", "WifiMacAddrWl1")
28  ]
29  # Assume not supported when exception is thrown
30  with suppress(Exception):
31  macs.extend(x.get("WifiMac") for x in wlan_settings["Ssids"]["Ssid"])
32 
33  return sorted({format_mac(str(x)) for x in macs if x})
34 
35 
36 def non_verifying_requests_session(url: str) -> requests.Session:
37  """Get requests.Session that does not verify HTTPS, filter warnings about it."""
38  parsed_url = urlparse(url)
39  assert parsed_url.hostname
40  requests_session = requests.Session()
41  requests_session.verify = False
42  warnings.filterwarnings(
43  "ignore",
44  message=rf"^.*\b{re.escape(parsed_url.hostname)}\b",
45  category=InsecureRequestWarning,
46  module=r"^urllib3\.connectionpool$",
47  )
48  return requests_session
requests.Session non_verifying_requests_session(str url)
Definition: utils.py:36
list[str] get_device_macs(GetResponseType device_info, GetResponseType wlan_settings)
Definition: utils.py:19