Fix Met Device Info (#103082)

This commit is contained in:
G Johansson 2023-10-31 08:31:53 +01:00 committed by GitHub
parent 06bbea2e0f
commit 85d49a2920
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 8 deletions

View File

@ -20,6 +20,7 @@ from homeassistant.const import (
)
from homeassistant.core import Event, HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.util import dt as dt_util
@ -68,6 +69,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
await cleanup_old_device(hass)
return True
@ -88,6 +91,15 @@ async def async_update_entry(hass: HomeAssistant, config_entry: ConfigEntry):
await hass.config_entries.async_reload(config_entry.entry_id)
async def cleanup_old_device(hass: HomeAssistant) -> None:
"""Cleanup device without proper device identifier."""
device_reg = dr.async_get(hass)
device = device_reg.async_get_device(identifiers={(DOMAIN,)}) # type: ignore[arg-type]
if device:
_LOGGER.debug("Removing improper device %s", device.name)
device_reg.async_remove_device(device.id)
class CannotConnect(HomeAssistantError):
"""Unable to connect to the web site."""

View File

@ -60,7 +60,7 @@ async def async_setup_entry(
if TYPE_CHECKING:
assert isinstance(name, str)
entities = [MetWeather(coordinator, config_entry.data, False, name, is_metric)]
entities = [MetWeather(coordinator, config_entry, False, name, is_metric)]
# Add hourly entity to legacy config entries
if entity_registry.async_get_entity_id(
@ -69,9 +69,7 @@ async def async_setup_entry(
_calculate_unique_id(config_entry.data, True),
):
name = f"{name} hourly"
entities.append(
MetWeather(coordinator, config_entry.data, True, name, is_metric)
)
entities.append(MetWeather(coordinator, config_entry, True, name, is_metric))
async_add_entities(entities)
@ -114,22 +112,22 @@ class MetWeather(SingleCoordinatorWeatherEntity[MetDataUpdateCoordinator]):
def __init__(
self,
coordinator: MetDataUpdateCoordinator,
config: MappingProxyType[str, Any],
config_entry: ConfigEntry,
hourly: bool,
name: str,
is_metric: bool,
) -> None:
"""Initialise the platform with a data instance and site."""
super().__init__(coordinator)
self._attr_unique_id = _calculate_unique_id(config, hourly)
self._config = config
self._attr_unique_id = _calculate_unique_id(config_entry.data, hourly)
self._config = config_entry.data
self._is_metric = is_metric
self._hourly = hourly
self._attr_entity_registry_enabled_default = not hourly
self._attr_device_info = DeviceInfo(
name="Forecast",
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN,)}, # type: ignore[arg-type]
identifiers={(DOMAIN, config_entry.entry_id)},
manufacturer="Met.no",
model="Forecast",
configuration_url="https://www.met.no/en",

View File

@ -9,6 +9,7 @@ from homeassistant.components.met.const import (
from homeassistant.config import async_process_ha_core_config
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from . import init_integration
@ -48,3 +49,28 @@ async def test_fail_default_home_entry(
"Skip setting up met.no integration; No Home location has been set"
in caplog.text
)
async def test_removing_incorrect_devices(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, mock_weather
) -> None:
"""Test we remove incorrect devices."""
entry = await init_integration(hass)
device_reg = dr.async_get(hass)
device_reg.async_get_or_create(
config_entry_id=entry.entry_id,
name="Forecast_legacy",
entry_type=dr.DeviceEntryType.SERVICE,
identifiers={(DOMAIN,)},
manufacturer="Met.no",
model="Forecast",
configuration_url="https://www.met.no/en",
)
assert await hass.config_entries.async_reload(entry.entry_id)
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert not device_reg.async_get_device(identifiers={(DOMAIN,)})
assert device_reg.async_get_device(identifiers={(DOMAIN, entry.entry_id)})
assert "Removing improper device Forecast_legacy" in caplog.text