core/tests/components/zha/test_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

121 lines
3.6 KiB
Python

"""Test Home Assistant Hardware platform for ZHA."""
from unittest.mock import MagicMock, patch
import pytest
from zigpy.application import ControllerApplication
from homeassistant.components.homeassistant_hardware.helpers import (
async_register_firmware_info_callback,
)
from homeassistant.components.homeassistant_hardware.util import (
ApplicationType,
FirmwareInfo,
OwningIntegration,
)
from homeassistant.components.zha.homeassistant_hardware import get_firmware_info
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
async def test_get_firmware_info_normal(hass: HomeAssistant) -> None:
"""Test `get_firmware_info`."""
zha = MockConfigEntry(
domain="zha",
unique_id="some_unique_id",
data={
"device": {
"path": "/dev/ttyUSB1",
"baudrate": 115200,
"flow_control": None,
},
"radio_type": "ezsp",
},
version=4,
)
zha.add_to_hass(hass)
zha.mock_state(hass, ConfigEntryState.LOADED)
# With ZHA running
with patch(
"homeassistant.components.zha.homeassistant_hardware.get_zha_gateway"
) as mock_get_zha_gateway:
mock_get_zha_gateway.return_value.state.node_info.version = "1.2.3.4"
fw_info_running = get_firmware_info(hass, zha)
assert fw_info_running == FirmwareInfo(
device="/dev/ttyUSB1",
firmware_type=ApplicationType.EZSP,
firmware_version="1.2.3.4",
source="zha",
owners=[OwningIntegration(config_entry_id=zha.entry_id)],
)
assert await fw_info_running.is_running(hass) is True
# With ZHA not running
zha.mock_state(hass, ConfigEntryState.NOT_LOADED)
fw_info_not_running = get_firmware_info(hass, zha)
assert fw_info_not_running == FirmwareInfo(
device="/dev/ttyUSB1",
firmware_type=ApplicationType.EZSP,
firmware_version=None,
source="zha",
owners=[OwningIntegration(config_entry_id=zha.entry_id)],
)
assert await fw_info_not_running.is_running(hass) is False
@pytest.mark.parametrize(
"data",
[
# Missing data
{},
# Bad radio type
{"device": {"path": "/dev/ttyUSB1"}, "radio_type": "znp"},
],
)
async def test_get_firmware_info_errors(
hass: HomeAssistant, data: dict[str, str | int | None]
) -> None:
"""Test `get_firmware_info` with config entry data format errors."""
zha = MockConfigEntry(
domain="zha",
unique_id="some_unique_id",
data=data,
version=4,
)
zha.add_to_hass(hass)
assert (get_firmware_info(hass, zha)) is None
async def test_hardware_firmware_info_provider_notification(
hass: HomeAssistant,
config_entry: MockConfigEntry,
mock_zigpy_connect: ControllerApplication,
) -> None:
"""Test that the ZHA gateway provides hardware and firmware information."""
config_entry.add_to_hass(hass)
await async_setup_component(hass, "homeassistant_hardware", {})
callback = MagicMock()
async_register_firmware_info_callback(hass, "/dev/ttyUSB0", callback)
await hass.config_entries.async_setup(config_entry.entry_id)
callback.assert_called_once_with(
FirmwareInfo(
device="/dev/ttyUSB0",
firmware_type=ApplicationType.EZSP,
firmware_version="7.1.4.0 build 389",
source="zha",
owners=[OwningIntegration(config_entry_id=config_entry.entry_id)],
)
)