Fix meaters not being added after a reload (#147614)

This commit is contained in:
Joost Lekkerkerker 2025-06-26 19:52:29 +02:00 committed by Franck Nijhof
parent 26521f8cc0
commit c0ec987b07
No known key found for this signature in database
GPG Key ID: AB33ADACE7101952
3 changed files with 52 additions and 4 deletions

View File

@ -25,4 +25,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: MeaterConfigEntry) -> bo
async def async_unload_entry(hass: HomeAssistant, entry: MeaterConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[MEATER_DATA] = (
hass.data[MEATER_DATA] - entry.runtime_data.found_probes
)
return unload_ok

View File

@ -44,6 +44,7 @@ class MeaterCoordinator(DataUpdateCoordinator[dict[str, MeaterProbe]]):
)
session = async_get_clientsession(hass)
self.client = MeaterApi(session)
self.found_probes: set[str] = set()
async def _async_setup(self) -> None:
"""Set up the Meater Coordinator."""
@ -73,5 +74,6 @@ class MeaterCoordinator(DataUpdateCoordinator[dict[str, MeaterProbe]]):
raise UpdateFailed(
"Too many requests have been made to the API, rate limiting is in place"
) from err
return {device.id: device for device in devices}
res = {device.id: device for device in devices}
self.found_probes.update(set(res.keys()))
return res

View File

@ -5,8 +5,10 @@ from unittest.mock import AsyncMock
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.meater.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers import device_registry as dr, entity_registry as er
from . import setup_integration
from .const import PROBE_ID
@ -26,3 +28,43 @@ async def test_device_info(
device_entry = device_registry.async_get_device(identifiers={(DOMAIN, PROBE_ID)})
assert device_entry is not None
assert device_entry == snapshot
async def test_load_unload(
hass: HomeAssistant,
mock_meater_client: AsyncMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test unload of Meater integration."""
await setup_integration(hass, mock_config_entry)
assert mock_config_entry.state is ConfigEntryState.LOADED
assert (
len(
er.async_entries_for_config_entry(
entity_registry, mock_config_entry.entry_id
)
)
== 8
)
assert (
hass.states.get("sensor.meater_probe_40a72384_ambient_temperature").state
!= STATE_UNAVAILABLE
)
assert await hass.config_entries.async_reload(mock_config_entry.entry_id)
assert mock_config_entry.state is ConfigEntryState.LOADED
assert (
len(
er.async_entries_for_config_entry(
entity_registry, mock_config_entry.entry_id
)
)
== 8
)
assert (
hass.states.get("sensor.meater_probe_40a72384_ambient_temperature").state
!= STATE_UNAVAILABLE
)