Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Met Device Info #103082

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions homeassistant/components/met/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)
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
Expand Down Expand Up @@ -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


Expand All @@ -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."""

Expand Down
14 changes: 6 additions & 8 deletions homeassistant/components/met/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)

Expand Down Expand Up @@ -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)},
edenhaus marked this conversation as resolved.
Show resolved Hide resolved
manufacturer="Met.no",
model="Forecast",
configuration_url="https://www.met.no/en",
Expand Down
26 changes: 26 additions & 0 deletions tests/components/met/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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

Expand Down Expand Up @@ -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