diff --git a/homeassistant/components/tado/repairs.py b/homeassistant/components/tado/repairs.py index 90e20c615f26..2a99f1766add 100644 --- a/homeassistant/components/tado/repairs.py +++ b/homeassistant/components/tado/repairs.py @@ -9,25 +9,31 @@ from .const import ( DOMAIN, WATER_HEATER_FALLBACK_REPAIR, ) +from .coordinator import TadoConfigEntry def manage_water_heater_fallback_issue( hass: HomeAssistant, - water_heater_names: list[str], + entry: TadoConfigEntry, + has_water_heaters: bool, integration_overlay_fallback: str | None, ) -> None: """Notify users about water heater respecting fallback setting.""" - if integration_overlay_fallback in ( + # The fallback is an entry option shared by all hot water zones + issue_id = f"{WATER_HEATER_FALLBACK_REPAIR}_{entry.entry_id}" + if has_water_heaters and integration_overlay_fallback in ( CONST_OVERLAY_TADO_DEFAULT, CONST_OVERLAY_MANUAL, ): - for water_heater_name in water_heater_names: - ir.async_create_issue( - hass=hass, - domain=DOMAIN, - issue_id=f"{WATER_HEATER_FALLBACK_REPAIR}_{water_heater_name}", - is_fixable=False, - is_persistent=False, - severity=ir.IssueSeverity.WARNING, - translation_key=WATER_HEATER_FALLBACK_REPAIR, - ) + ir.async_create_issue( + hass=hass, + domain=DOMAIN, + issue_id=issue_id, + config_entry_id=entry.entry_id, + is_fixable=False, + is_persistent=False, + severity=ir.IssueSeverity.WARNING, + translation_key=WATER_HEATER_FALLBACK_REPAIR, + ) + else: + ir.async_delete_issue(hass, DOMAIN, issue_id) diff --git a/homeassistant/components/tado/water_heater.py b/homeassistant/components/tado/water_heater.py index 27d1f31defad..34e9c077ee81 100644 --- a/homeassistant/components/tado/water_heater.py +++ b/homeassistant/components/tado/water_heater.py @@ -60,7 +60,8 @@ async def async_setup_entry( manage_water_heater_fallback_issue( hass=hass, - water_heater_names=[e.zone_name for e in entities], + entry=entry, + has_water_heaters=bool(entities), integration_overlay_fallback=coordinator.fallback, ) diff --git a/tests/components/tado/test_repairs.py b/tests/components/tado/test_repairs.py index 9b7a010e3592..7717af9c94fb 100644 --- a/tests/components/tado/test_repairs.py +++ b/tests/components/tado/test_repairs.py @@ -1,6 +1,7 @@ """Repair tests.""" import pytest +from pytest_homeassistant.common import MockConfigEntry from homeassistant.components.tado.const import ( CONST_OVERLAY_MANUAL, @@ -14,31 +15,34 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers import issue_registry as ir -class MockWaterHeater: - """Mock Water heater entity.""" - - def __init__(self, zone_name) -> None: - """Init mock entity class.""" - self.zone_name = zone_name +def _issue_id(entry: MockConfigEntry) -> str: + """Return the water heater fallback issue id of an entry.""" + return f"{WATER_HEATER_FALLBACK_REPAIR}_{entry.entry_id}" +@pytest.mark.parametrize( + ("has_water_heaters", "integration_overlay_fallback"), + [ + pytest.param(True, CONST_OVERLAY_TADO_MODE, id="tado_mode"), + pytest.param(False, CONST_OVERLAY_TADO_DEFAULT, id="no_water_heaters"), + ], +) async def test_manage_water_heater_fallback_issue_not_created( hass: HomeAssistant, issue_registry: ir.IssueRegistry, + mock_config_entry: MockConfigEntry, + has_water_heaters: bool, + integration_overlay_fallback: str, ) -> None: """Test water heater fallback issue is not needed.""" - zone_name = "Hot Water" - expected_issue_id = f"{WATER_HEATER_FALLBACK_REPAIR}_{zone_name}" - water_heater_names = [zone_name] + mock_config_entry.add_to_hass(hass) manage_water_heater_fallback_issue( - water_heater_names=water_heater_names, - integration_overlay_fallback=CONST_OVERLAY_TADO_MODE, hass=hass, + entry=mock_config_entry, + has_water_heaters=has_water_heaters, + integration_overlay_fallback=integration_overlay_fallback, ) - assert ( - issue_registry.async_get_issue(issue_id=expected_issue_id, domain=DOMAIN) - is None - ) + assert not issue_registry.issues @pytest.mark.parametrize( @@ -47,18 +51,83 @@ async def test_manage_water_heater_fallback_issue_not_created( async def test_manage_water_heater_fallback_issue_created( hass: HomeAssistant, issue_registry: ir.IssueRegistry, + mock_config_entry: MockConfigEntry, integration_overlay_fallback: str, ) -> None: """Test water heater fallback issue created cases.""" - zone_name = "Hot Water" - expected_issue_id = f"{WATER_HEATER_FALLBACK_REPAIR}_{zone_name}" - water_heater_names = [zone_name] + mock_config_entry.add_to_hass(hass) manage_water_heater_fallback_issue( - water_heater_names=water_heater_names, - integration_overlay_fallback=integration_overlay_fallback, hass=hass, + entry=mock_config_entry, + has_water_heaters=True, + integration_overlay_fallback=integration_overlay_fallback, ) - assert ( - issue_registry.async_get_issue(issue_id=expected_issue_id, domain=DOMAIN) - is not None + assert len(issue_registry.issues) == 1 + issue = issue_registry.async_get_issue(DOMAIN, _issue_id(mock_config_entry)) + assert issue is not None + assert issue.config_entry_id == mock_config_entry.entry_id + + +@pytest.mark.parametrize( + ("has_water_heaters", "integration_overlay_fallback"), + [ + pytest.param(True, CONST_OVERLAY_TADO_MODE, id="tado_mode"), + pytest.param(False, CONST_OVERLAY_TADO_DEFAULT, id="no_water_heaters"), + ], +) +async def test_manage_water_heater_fallback_issue_deleted( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, + mock_config_entry: MockConfigEntry, + has_water_heaters: bool, + integration_overlay_fallback: str, +) -> None: + """Test the issue is deleted once it no longer applies.""" + mock_config_entry.add_to_hass(hass) + manage_water_heater_fallback_issue( + hass=hass, + entry=mock_config_entry, + has_water_heaters=True, + integration_overlay_fallback=CONST_OVERLAY_TADO_DEFAULT, ) + assert issue_registry.async_get_issue(DOMAIN, _issue_id(mock_config_entry)) + + manage_water_heater_fallback_issue( + hass=hass, + entry=mock_config_entry, + has_water_heaters=has_water_heaters, + integration_overlay_fallback=integration_overlay_fallback, + ) + assert not issue_registry.issues + + +async def test_manage_water_heater_fallback_issue_per_entry( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test each config entry gets and clears its own issue.""" + entry_1 = MockConfigEntry(domain=DOMAIN, unique_id="1") + entry_2 = MockConfigEntry(domain=DOMAIN, unique_id="2") + entry_1.add_to_hass(hass) + entry_2.add_to_hass(hass) + for entry in (entry_1, entry_2): + manage_water_heater_fallback_issue( + hass=hass, + entry=entry, + has_water_heaters=True, + integration_overlay_fallback=CONST_OVERLAY_TADO_DEFAULT, + ) + assert len(issue_registry.issues) == 2 + for entry in (entry_1, entry_2): + issue = issue_registry.async_get_issue(DOMAIN, _issue_id(entry)) + assert issue is not None + assert issue.config_entry_id == entry.entry_id + + manage_water_heater_fallback_issue( + hass=hass, + entry=entry_1, + has_water_heaters=True, + integration_overlay_fallback=CONST_OVERLAY_TADO_MODE, + ) + assert issue_registry.async_get_issue(DOMAIN, _issue_id(entry_1)) is None + assert issue_registry.async_get_issue(DOMAIN, _issue_id(entry_2)) is not None