diff --git a/homeassistant/components/otbr/homeassistant_hardware.py b/homeassistant/components/otbr/homeassistant_hardware.py new file mode 100644 index 00000000000..206e5bbcdf7 --- /dev/null +++ b/homeassistant/components/otbr/homeassistant_hardware.py @@ -0,0 +1,54 @@ +"""Home Assistant Hardware firmware utilities.""" + +from __future__ import annotations + +import voluptuous as vol +from yarl import URL + +from homeassistant.components.hassio import valid_addon +from homeassistant.components.homeassistant_hardware.util import ( + ApplicationType, + FirmwareInfo, + OwningAddon, + OwningIntegration, +) +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers.hassio import is_hassio + +from . import OTBRConfigEntry +from .const import DOMAIN + + +async def get_firmware_info( + hass: HomeAssistant, config_entry: OTBRConfigEntry +) -> FirmwareInfo | None: + """Return firmware information for the OpenThread Border Router.""" + device = config_entry.data["device"] + if device is None: + return None + + owners: list[OwningIntegration | OwningAddon] = [ + OwningIntegration(config_entry_id=config_entry.entry_id) + ] + + if is_hassio(hass) and (host := URL(config_entry.data["url"]).host) is not None: + try: + valid_addon(host) + except vol.Invalid: + pass + else: + owners.append(OwningAddon(slug=host)) + + try: + firmware_version = await config_entry.runtime_data.get_coprocessor_version() + except HomeAssistantError: + firmware_version = None + + return FirmwareInfo( + device=device, + firmware_type=ApplicationType.SPINEL, + firmware_version=firmware_version, + source=DOMAIN, + owners=owners, + ) diff --git a/homeassistant/components/otbr/util.py b/homeassistant/components/otbr/util.py index 351e23c7736..30e456e11a8 100644 --- a/homeassistant/components/otbr/util.py +++ b/homeassistant/components/otbr/util.py @@ -163,6 +163,11 @@ class OTBRData: """Get extended address (EUI-64).""" return await self.api.get_extended_address() + @_handle_otbr_error + async def get_coprocessor_version(self) -> str: + """Get coprocessor firmware version.""" + return await self.api.get_coprocessor_version() + async def get_allowed_channel(hass: HomeAssistant, otbr_url: str) -> int | None: """Return the allowed channel, or None if there's no restriction."""