Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Data update coordinator for the Webmin integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import ATTR_CONNECTIONS, ATTR_IDENTIFIERS, CONF_HOST
9 from homeassistant.core import HomeAssistant
11  CONNECTION_NETWORK_MAC,
12  DeviceInfo,
13  format_mac,
14 )
15 from homeassistant.helpers.entity_component import DEFAULT_SCAN_INTERVAL
16 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
17 
18 from .const import DOMAIN, LOGGER
19 from .helpers import get_instance_from_options, get_sorted_mac_addresses
20 
21 
23  """The Webmin data update coordinator."""
24 
25  mac_address: str
26  unique_id: str
27 
28  def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
29  """Initialize the Webmin data update coordinator."""
30 
31  super().__init__(
32  hass, logger=LOGGER, name=DOMAIN, update_interval=DEFAULT_SCAN_INTERVAL
33  )
34 
35  self.instance, base_url = get_instance_from_options(hass, config_entry.options)
36 
37  self.device_infodevice_info = DeviceInfo(
38  configuration_url=base_url,
39  name=config_entry.options[CONF_HOST],
40  )
41 
42  async def async_setup(self) -> None:
43  """Provide needed data to the device info."""
44  mac_addresses = get_sorted_mac_addresses(self.datadata)
45  if len(mac_addresses) > 0:
46  self.mac_addressmac_address = mac_addresses[0]
47  self.unique_idunique_id = self.mac_addressmac_address
48  self.device_infodevice_info[ATTR_CONNECTIONS] = {
49  (CONNECTION_NETWORK_MAC, format_mac(mac_address))
50  for mac_address in mac_addresses
51  }
52  self.device_infodevice_info[ATTR_IDENTIFIERS] = {
53  (DOMAIN, format_mac(mac_address)) for mac_address in mac_addresses
54  }
55  else:
56  assert self.config_entryconfig_entry
57  self.unique_idunique_id = self.config_entryconfig_entry.entry_id
58 
59  async def _async_update_data(self) -> dict[str, Any]:
60  data = await self.instance.update()
61  data["disk_fs"] = {item["dir"]: item for item in data["disk_fs"]}
62  return data
None __init__(self, HomeAssistant hass, ConfigEntry config_entry)
Definition: coordinator.py:28
IssData update(pyiss.ISS iss)
Definition: __init__.py:33
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