core/homeassistant/components/zha/homeassistant_hardware.py
puddly 2e8bc56be4
Keep track of addons and integrations when determining HA radio firmware type (#134598)
* Replace `FirmwareGuess` with `FirmwareInfo` with owner tracking

* Fix up config flow

* Account for OTBR addon existing independent of integration

* Fix remaining unit tests

* Add some tests for ownership

* Unit test `get_zha_firmware_info`

* ZHA `homeassistant_hardware` platform

* OTBR `homeassistant_hardware` platform

* Rework imports

* Fix unit tests

* Add OTBR unit tests

* Add hassfest exemption for `homeassistant_hardware` and `otbr`

* Invert registration to decouple the hardware integration

* Revert "Add hassfest exemption for `homeassistant_hardware` and `otbr`"

This reverts commit c8c6e7044f005239d11fc561cca040a6d89a9b39.

* Fix circular imports

* Fix unit tests

* Address review comments

* Simplify API a little

* Fix `| None` mypy issues

* Remove the `unregister_firmware_info_provider` API

* 100% coverage

* Add `HardwareInfoDispatcher.register_firmware_info_callback`

* Unit test `register_firmware_info_callback` (zha)

* Unit test `register_firmware_info_callback` (otbr)

* Update existing hardware helper tests to use the new function

* Add `async_` prefix to helper function names

* Move OTBR implementation to a separate PR

* Update ZHA diagnostics snapshot

* Switch from `dict.setdefault` to `defaultdict`

* Add some error handling to `iter_firmware_info` and increase test coverage

* Oops
2025-02-06 14:46:07 -05:00

44 lines
1.2 KiB
Python

"""Home Assistant Hardware firmware utilities."""
from __future__ import annotations
from homeassistant.components.homeassistant_hardware.util import (
ApplicationType,
FirmwareInfo,
OwningIntegration,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from .const import DOMAIN
from .helpers import get_zha_gateway
@callback
def get_firmware_info(
hass: HomeAssistant, config_entry: ConfigEntry
) -> FirmwareInfo | None:
"""Return firmware information for the ZHA instance, synchronously."""
# We only support EZSP firmware for now
if config_entry.data.get("radio_type", None) != "ezsp":
return None
if (device := config_entry.data.get("device", {}).get("path")) is None:
return None
try:
gateway = get_zha_gateway(hass)
except ValueError:
firmware_version = None
else:
firmware_version = gateway.state.node_info.version
return FirmwareInfo(
device=device,
firmware_type=ApplicationType.EZSP,
firmware_version=firmware_version,
source=DOMAIN,
owners=[OwningIntegration(config_entry_id=config_entry.entry_id)],
)