Home Assistant Unofficial Reference 2024.12.1
helpers.py
Go to the documentation of this file.
1 """Helper functions for the Webmin integration."""
2 
3 from collections.abc import Mapping
4 from typing import Any
5 
6 from webmin_xmlrpc.client import WebminInstance
7 from yarl import URL
8 
9 from homeassistant.const import (
10  CONF_HOST,
11  CONF_PASSWORD,
12  CONF_PORT,
13  CONF_SSL,
14  CONF_USERNAME,
15  CONF_VERIFY_SSL,
16 )
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.aiohttp_client import async_create_clientsession
19 
20 
22  hass: HomeAssistant, options: Mapping[str, Any]
23 ) -> tuple[WebminInstance, URL]:
24  """Retrieve a Webmin instance and the base URL from config options."""
25 
26  base_url = URL.build(
27  scheme="https" if options[CONF_SSL] else "http",
28  user=options[CONF_USERNAME],
29  password=options[CONF_PASSWORD],
30  host=options[CONF_HOST],
31  port=int(options[CONF_PORT]),
32  )
33 
34  return WebminInstance(
36  hass,
37  verify_ssl=options[CONF_VERIFY_SSL],
38  base_url=base_url,
39  )
40  ), base_url
41 
42 
43 def get_sorted_mac_addresses(data: dict[str, Any]) -> list[str]:
44  """Return a sorted list of mac addresses."""
45  return sorted(
46  iface["ether"]
47  for iface in data["active_interfaces"]
48  if "ether" in iface and iface["name"].startswith(("en", "eth", "wl"))
49  )
aiohttp.ClientSession async_create_clientsession()
Definition: coordinator.py:51
tuple[WebminInstance, URL] get_instance_from_options(HomeAssistant hass, Mapping[str, Any] options)
Definition: helpers.py:23
list[str] get_sorted_mac_addresses(dict[str, Any] data)
Definition: helpers.py:43