Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Open Thread Border Router integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 import aiohttp
8 import python_otbr_api
9 
10 from homeassistant.components.thread import async_add_dataset
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import ConfigEntryNotReady, HomeAssistantError
14 from homeassistant.helpers import config_validation as cv, issue_registry as ir
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
16 from homeassistant.helpers.typing import ConfigType
17 
18 from . import websocket_api
19 from .const import DOMAIN
20 from .util import (
21  GetBorderAgentIdNotSupported,
22  OTBRData,
23  update_issues,
24  update_unique_id,
25 )
26 
27 _LOGGER = logging.getLogger(__name__)
28 
29 CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
30 
31 type OTBRConfigEntry = ConfigEntry[OTBRData]
32 
33 
34 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
35  """Set up the Open Thread Border Router component."""
36  websocket_api.async_setup(hass)
37  return True
38 
39 
40 async def async_setup_entry(hass: HomeAssistant, entry: OTBRConfigEntry) -> bool:
41  """Set up an Open Thread Border Router config entry."""
42  api = python_otbr_api.OTBR(entry.data["url"], async_get_clientsession(hass), 10)
43 
44  otbrdata = OTBRData(entry.data["url"], api, entry.entry_id)
45  try:
46  border_agent_id = await otbrdata.get_border_agent_id()
47  dataset_tlvs = await otbrdata.get_active_dataset_tlvs()
48  extended_address = await otbrdata.get_extended_address()
49  except GetBorderAgentIdNotSupported:
50  ir.async_create_issue(
51  hass,
52  DOMAIN,
53  f"get_get_border_agent_id_unsupported_{otbrdata.entry_id}",
54  is_fixable=False,
55  is_persistent=False,
56  severity=ir.IssueSeverity.WARNING,
57  translation_key="get_get_border_agent_id_unsupported",
58  )
59  return False
60  except (
61  HomeAssistantError,
62  aiohttp.ClientError,
63  TimeoutError,
64  ) as err:
65  raise ConfigEntryNotReady("Unable to connect") from err
66  await update_unique_id(hass, entry, border_agent_id)
67  if dataset_tlvs:
68  await update_issues(hass, otbrdata, dataset_tlvs)
69  await async_add_dataset(
70  hass,
71  DOMAIN,
72  dataset_tlvs.hex(),
73  preferred_border_agent_id=border_agent_id.hex(),
74  preferred_extended_address=extended_address.hex(),
75  )
76 
77  entry.async_on_unload(entry.add_update_listener(async_reload_entry))
78  entry.runtime_data = otbrdata
79 
80  return True
81 
82 
83 async def async_unload_entry(hass: HomeAssistant, entry: OTBRConfigEntry) -> bool:
84  """Unload a config entry."""
85  return True
86 
87 
88 async def async_reload_entry(hass: HomeAssistant, entry: OTBRConfigEntry) -> None:
89  """Handle an options update."""
90  await hass.config_entries.async_reload(entry.entry_id)
dict[str, str]|None update_unique_id(er.RegistryEntry entity_entry, str unique_id)
Definition: __init__.py:168
None update_issues(HomeAssistant hass, OTBRData otbrdata, bytes dataset_tlvs)
Definition: util.py:270
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:34
None async_reload_entry(HomeAssistant hass, OTBRConfigEntry entry)
Definition: __init__.py:88
bool async_unload_entry(HomeAssistant hass, OTBRConfigEntry entry)
Definition: __init__.py:83
bool async_setup_entry(HomeAssistant hass, OTBRConfigEntry entry)
Definition: __init__.py:40
None async_add_dataset(HomeAssistant hass, str source, str tlv, *str|None preferred_border_agent_id=None, str|None preferred_extended_address=None)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)