diff --git a/homeassistant/components/shelly/__init__.py b/homeassistant/components/shelly/__init__.py index 7d640fcbbd46..2d4a71881899 100644 --- a/homeassistant/components/shelly/__init__.py +++ b/homeassistant/components/shelly/__init__.py @@ -42,15 +42,18 @@ from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC from homeassistant.helpers.typing import ConfigType from .const import ( + BLE_SCANNER_FIRMWARE_UNSUPPORTED_ISSUE_ID, BLOCK_EXPECTED_SLEEP_PERIOD, BLOCK_WRONG_SLEEP_PERIOD, CONF_BLE_SCANNER_MODE, CONF_COAP_PORT, CONF_SLEEP_PERIOD, + DEPRECATED_FIRMWARE_ISSUE_ID, DOMAIN, FIRMWARE_UNSUPPORTED_ISSUE_ID, LOGGER, MODELS_WITH_WRONG_SLEEP_PERIOD, + OUTBOUND_WEBSOCKET_INCORRECTLY_ENABLED_ISSUE_ID, PUSH_UPDATE_ISSUE_ID, BLEScannerMode, ) @@ -474,6 +477,13 @@ async def async_unload_entry(hass: HomeAssistant, entry: ShellyConfigEntry) -> N async def async_remove_entry(hass: HomeAssistant, entry: ShellyConfigEntry) -> None: """Remove a config entry.""" + # Issues raised before they were linked to the entry outlive it otherwise + for issue_id in ( + BLE_SCANNER_FIRMWARE_UNSUPPORTED_ISSUE_ID, + DEPRECATED_FIRMWARE_ISSUE_ID, + OUTBOUND_WEBSOCKET_INCORRECTLY_ENABLED_ISSUE_ID, + ): + ir.async_delete_issue(hass, DOMAIN, issue_id.format(unique=entry.unique_id)) if get_device_entry_gen(entry) in RPC_GENERATIONS and ( mac_address := entry.unique_id ): diff --git a/tests/components/shelly/test_repairs.py b/tests/components/shelly/test_repairs.py index e8598e2adfad..61ceca445131 100644 --- a/tests/components/shelly/test_repairs.py +++ b/tests/components/shelly/test_repairs.py @@ -276,6 +276,83 @@ async def test_deprecated_firmware_issue( assert len(issue_registry.issues) == 0 +def _create_unlinked_issue( + hass: HomeAssistant, issue_id: str, translation_key: str +) -> None: + """Create an issue as it was raised before issues were linked to entries.""" + ir.async_create_issue( + hass, + DOMAIN, + issue_id, + is_fixable=True, + is_persistent=True, + severity=ir.IssueSeverity.WARNING, + translation_key=translation_key, + translation_placeholders={ + "device_name": "Test name", + "ip_address": "192.168.1.37", + "firmware": "1.0.0", + "ha_version": "2025.10.0", + }, + ) + + +@pytest.mark.usefixtures("mock_rpc_device") +async def test_deprecated_firmware_issue_linked_on_setup( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test setup links an issue raised before it was linked to the entry.""" + issue_id = DEPRECATED_FIRMWARE_ISSUE_ID.format(unique=MOCK_MAC) + _create_unlinked_issue(hass, issue_id, "deprecated_firmware") + issue_registry.async_ignore(DOMAIN, issue_id, True) + with patch( + "homeassistant.components.shelly.repairs.DEPRECATED_FIRMWARES", + { + MODEL_WALL_DISPLAY: DeprecatedFirmwareInfo( + {"min_firmware": "2.3.0", "ha_version": "2025.10.0"} + ) + }, + ): + entry = await init_integration(hass, 2, model=MODEL_WALL_DISPLAY) + + issue = issue_registry.async_get_issue(DOMAIN, issue_id) + assert issue is not None + assert issue.config_entry_id == entry.entry_id + assert issue.dismissed_version is not None + + +@pytest.mark.parametrize( + ("issue_id_format", "translation_key"), + [ + ( + BLE_SCANNER_FIRMWARE_UNSUPPORTED_ISSUE_ID, + "ble_scanner_firmware_unsupported", + ), + (DEPRECATED_FIRMWARE_ISSUE_ID, "deprecated_firmware"), + ( + OUTBOUND_WEBSOCKET_INCORRECTLY_ENABLED_ISSUE_ID, + "outbound_websocket_incorrectly_enabled", + ), + ], +) +async def test_unlinked_issue_removed_with_entry( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, + issue_id_format: str, + translation_key: str, +) -> None: + """Test removing an entry that never set up deletes its unlinked issue.""" + issue_id = issue_id_format.format(unique=MOCK_MAC) + _create_unlinked_issue(hass, issue_id, translation_key) + entry = await init_integration(hass, 2, skip_setup=True) + + await hass.config_entries.async_remove(entry.entry_id) + await hass.async_block_till_done() + + assert issue_registry.async_get_issue(DOMAIN, issue_id) is None + + async def test_open_wifi_ap_issue( hass: HomeAssistant, hass_client: ClientSessionGenerator,