mirror of
https://github.com/home-assistant/core.git
synced 2026-02-15 07:36:16 +00:00
* Add Thread integration * Add get/set operational dataset as TLVS * Add create operational dataset * Add set thread state * Adjust after rebase * Improve HTTP status handling * Improve test coverage * Change domains from thread to otbr * Setup otbr from a config entry * Add files * Store URL in config entry data * Make sure manifest is not sorted * Remove useless async * Call the JSON parser more * Don't raise exceptions without messages * Remove stuff which will be needed in the future * Remove more future stuff * Use API library * Bump library to 1.0.1
60 lines
1.6 KiB
Python
60 lines
1.6 KiB
Python
"""The Open Thread Border Router integration."""
|
|
from __future__ import annotations
|
|
|
|
import dataclasses
|
|
|
|
import python_otbr_api
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class OTBRData:
|
|
"""Container for OTBR data."""
|
|
|
|
url: str
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Set up an Open Thread Border Router config entry."""
|
|
|
|
hass.data[DOMAIN] = OTBRData(entry.data["url"])
|
|
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Unload a config entry."""
|
|
hass.data.pop(DOMAIN)
|
|
return True
|
|
|
|
|
|
def _async_get_thread_rest_service_url(hass) -> str:
|
|
"""Return Thread REST API URL."""
|
|
otbr_data: OTBRData | None = hass.data.get(DOMAIN)
|
|
if not otbr_data:
|
|
raise HomeAssistantError("otbr not setup")
|
|
|
|
return otbr_data.url
|
|
|
|
|
|
async def async_get_active_dataset_tlvs(hass: HomeAssistant) -> bytes | None:
|
|
"""Get current active operational dataset in TLVS format, or None.
|
|
|
|
Returns None if there is no active operational dataset.
|
|
Raises if the http status is 400 or higher or if the response is invalid.
|
|
"""
|
|
|
|
api = python_otbr_api.OTBR(
|
|
_async_get_thread_rest_service_url(hass), async_get_clientsession(hass), 10
|
|
)
|
|
try:
|
|
return await api.get_active_dataset_tlvs()
|
|
except python_otbr_api.OTBRError as exc:
|
|
raise HomeAssistantError("Failed to call OTBR API") from exc
|