diff --git a/homeassistant/components/zha/radio_manager.py b/homeassistant/components/zha/radio_manager.py index 3733a978ccc6..d2486613fcbe 100644 --- a/homeassistant/components/zha/radio_manager.py +++ b/homeassistant/components/zha/radio_manager.py @@ -39,6 +39,7 @@ from .const import ( CONF_RADIO_TYPE, CONF_ZIGPY, DEFAULT_DATABASE_NAME, + DOMAIN, EZSP_OVERWRITE_EUI64, ) from .helpers import get_zha_data @@ -293,9 +294,15 @@ class ZhaRadioManager: repairs.async_delete_blocking_issues(self.hass) return ProbeResult.RADIO_TYPE_DETECTED + # Disabled entries are skipped since their linked issues are hidden + entries = self.hass.config_entries.async_entries( + DOMAIN, include_ignore=False, include_disabled=False + ) + config_entry_id = entries[0].entry_id if len(entries) == 1 else None + with suppress(repairs.wrong_silabs_firmware.AlreadyRunningEZSP): if await repairs.wrong_silabs_firmware.warn_on_wrong_silabs_firmware( - self.hass, self.device_path + self.hass, self.device_path, config_entry_id ): return ProbeResult.WRONG_FIRMWARE_INSTALLED diff --git a/homeassistant/components/zha/repairs/wrong_silabs_firmware.py b/homeassistant/components/zha/repairs/wrong_silabs_firmware.py index da7413ff353a..ee26a90fd633 100644 --- a/homeassistant/components/zha/repairs/wrong_silabs_firmware.py +++ b/homeassistant/components/zha/repairs/wrong_silabs_firmware.py @@ -99,6 +99,14 @@ async def warn_on_wrong_silabs_firmware( # ZHA uses this firmware through the Ziggurat radio type return False + # The entry may have been removed or disabled while probing, issues linked to + # disabled entries are hidden + if config_entry_id is not None and ( + (entry := hass.config_entries.async_get_entry(config_entry_id)) is None + or entry.disabled_by is not None + ): + config_entry_id = None + hardware_type = _detect_radio_hardware(hass, device) ir.async_create_issue( hass, diff --git a/tests/components/zha/test_repairs.py b/tests/components/zha/test_repairs.py index 0215f9c177d7..20af901f4c0a 100644 --- a/tests/components/zha/test_repairs.py +++ b/tests/components/zha/test_repairs.py @@ -1,6 +1,8 @@ """Test ZHA repairs.""" +from collections.abc import Awaitable, Callable from http import HTTPStatus +from typing import Any from unittest.mock import Mock, call, patch import pytest @@ -16,6 +18,7 @@ from homeassistant.components.homeassistant_sky_connect.const import ( # pylint ) from homeassistant.components.repairs import DOMAIN as REPAIRS_DOMAIN from homeassistant.components.zha.const import DOMAIN +from homeassistant.components.zha.radio_manager import ProbeResult, ZhaRadioManager from homeassistant.components.zha.repairs.network_settings_inconsistent import ( ISSUE_INCONSISTENT_NETWORK_SETTINGS, ) @@ -25,7 +28,7 @@ from homeassistant.components.zha.repairs.wrong_silabs_firmware import ( _detect_radio_hardware, warn_on_wrong_silabs_firmware, ) -from homeassistant.config_entries import ConfigEntryState +from homeassistant.config_entries import ConfigEntryDisabler, ConfigEntryState from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import issue_registry as ir @@ -244,6 +247,118 @@ async def test_multipan_firmware_retry_on_probe_ezsp( assert issue is None +async def _detect_wrong_firmware( + hass: HomeAssistant, + probe_side_effect: Callable[..., Awaitable[ApplicationType]] | None = None, +) -> None: + """Run radio type detection as a flow would, probing multi-PAN firmware.""" + radio_manager = ZhaRadioManager() + radio_manager.hass = hass + radio_manager.device_path = SKYCONNECT_DEVICE + + with ( + patch("homeassistant.components.zha.radio_manager.AUTOPROBE_RADIOS", ()), + patch( + "homeassistant.components.zha.repairs.wrong_silabs_firmware.probe_silabs_firmware_type", + return_value=ApplicationType.CPC, + side_effect=probe_side_effect, + ), + patch( + "homeassistant.components.zha.repairs.wrong_silabs_firmware._detect_radio_hardware", + return_value=HardwareType.SKYCONNECT, + ), + ): + assert ( + await radio_manager.detect_radio_type() + == ProbeResult.WRONG_FIRMWARE_INSTALLED + ) + + +async def test_wrong_firmware_flow_issue_linked_to_entry( + hass: HomeAssistant, config_entry: MockConfigEntry, issue_registry: ir.IssueRegistry +) -> None: + """Test the flow's wrong firmware issue is linked to and removed with the entry.""" + config_entry.add_to_hass(hass) + + await _detect_wrong_firmware(hass) + + issue = issue_registry.async_get_issue( + DOMAIN, ISSUE_WRONG_SILABS_FIRMWARE_INSTALLED + ) + assert issue is not None + assert issue.config_entry_id == config_entry.entry_id + + await hass.config_entries.async_remove(config_entry.entry_id) + + assert ( + issue_registry.async_get_issue(DOMAIN, ISSUE_WRONG_SILABS_FIRMWARE_INSTALLED) + is None + ) + + +@pytest.mark.parametrize( + "change_entry", + [ + pytest.param( + lambda hass, entry: hass.config_entries.async_remove(entry.entry_id), + id="removed", + ), + pytest.param( + lambda hass, entry: hass.config_entries.async_set_disabled_by( + entry.entry_id, ConfigEntryDisabler.USER + ), + id="disabled", + ), + ], +) +async def test_wrong_firmware_flow_entry_changed_while_probing( + hass: HomeAssistant, + config_entry: MockConfigEntry, + issue_registry: ir.IssueRegistry, + change_entry: Callable[[HomeAssistant, MockConfigEntry], Awaitable[Any]], +) -> None: + """Test the flow's issue is unlinked if the entry is removed or disabled.""" + config_entry.add_to_hass(hass) + + async def change_entry_and_probe(*args: Any, **kwargs: Any) -> ApplicationType: + await change_entry(hass, config_entry) + return ApplicationType.CPC + + await _detect_wrong_firmware(hass, change_entry_and_probe) + + issue = issue_registry.async_get_issue( + DOMAIN, ISSUE_WRONG_SILABS_FIRMWARE_INSTALLED + ) + assert issue is not None + assert issue.config_entry_id is None + assert not issue_registry.async_is_hidden(issue) + + +@pytest.mark.parametrize( + "disabled_by", + [ + pytest.param([], id="no_entry"), + pytest.param([ConfigEntryDisabler.USER], id="disabled_entry"), + ], +) +async def test_wrong_firmware_flow_issue_unlinked( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, + disabled_by: list[ConfigEntryDisabler], +) -> None: + """Test the flow's wrong firmware issue is unlinked without an enabled entry.""" + for disabler in disabled_by: + MockConfigEntry(domain=DOMAIN, disabled_by=disabler).add_to_hass(hass) + + await _detect_wrong_firmware(hass) + + issue = issue_registry.async_get_issue( + DOMAIN, ISSUE_WRONG_SILABS_FIRMWARE_INSTALLED + ) + assert issue is not None + assert issue.config_entry_id is None + + async def test_no_warn_on_ziggurat_firmware( hass: HomeAssistant, issue_registry: ir.IssueRegistry ) -> None: