Home Assistant Unofficial Reference 2024.12.1
network.py
Go to the documentation of this file.
1 """Network helper class for the network integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from homeassistant.core import HomeAssistant, callback
9 from homeassistant.helpers.singleton import singleton
10 from homeassistant.helpers.storage import Store
11 from homeassistant.util.async_ import create_eager_task
12 
13 from .const import (
14  ATTR_CONFIGURED_ADAPTERS,
15  DATA_NETWORK,
16  DEFAULT_CONFIGURED_ADAPTERS,
17  STORAGE_KEY,
18  STORAGE_VERSION,
19 )
20 from .models import Adapter
21 from .util import async_load_adapters, enable_adapters, enable_auto_detected_adapters
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 
26 @singleton(DATA_NETWORK)
27 async def async_get_network(hass: HomeAssistant) -> Network:
28  """Get network singleton."""
29  network = Network(hass)
30  await network.async_setup()
31  network.async_configure()
32 
33  _LOGGER.debug("Adapters: %s", network.adapters)
34  return network
35 
36 
37 class Network:
38  """Network helper class for the network integration."""
39 
40  def __init__(self, hass: HomeAssistant) -> None:
41  """Initialize the Network class."""
42  self._store_store = Store[dict[str, list[str]]](
43  hass, STORAGE_VERSION, STORAGE_KEY, atomic_writes=True
44  )
45  self._data_data: dict[str, list[str]] = {}
46  self.adaptersadapters: list[Adapter] = []
47 
48  @property
49  def configured_adapters(self) -> list[str]:
50  """Return the configured adapters."""
51  return self._data_data.get(ATTR_CONFIGURED_ADAPTERS, DEFAULT_CONFIGURED_ADAPTERS)
52 
53  async def async_setup(self) -> None:
54  """Set up the network config."""
55  storage_load_task = create_eager_task(self.async_loadasync_load())
56  self.adaptersadapters = await async_load_adapters()
57  await storage_load_task
58 
59  @callback
60  def async_configure(self) -> None:
61  """Configure from storage."""
62  if not enable_adapters(self.adaptersadapters, self.configured_adaptersconfigured_adapters):
64 
65  async def async_reconfig(self, config: dict[str, Any]) -> None:
66  """Reconfigure network."""
67  self._data_data[ATTR_CONFIGURED_ADAPTERS] = config[ATTR_CONFIGURED_ADAPTERS]
68  self.async_configureasync_configure()
69  await self._async_save_async_save()
70 
71  async def async_load(self) -> None:
72  """Load config."""
73  if stored := await self._store_store.async_load():
74  self._data_data = stored
75 
76  async def _async_save(self) -> None:
77  """Save preferences."""
78  await self._store_store.async_save(self._data_data)
None async_reconfig(self, dict[str, Any] config)
Definition: network.py:65
None __init__(self, HomeAssistant hass)
Definition: network.py:40
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
Network async_get_network(HomeAssistant hass)
Definition: network.py:27
list[Adapter] async_load_adapters()
Definition: util.py:20
bool enable_adapters(list[Adapter] adapters, list[str] enabled_interfaces)
Definition: util.py:38
None enable_auto_detected_adapters(list[Adapter] adapters)
Definition: util.py:54
None async_save(self, _T data)
Definition: storage.py:424