Skip to content

Commit

Permalink
Create a single entity for new met_eireann config entries (home-assis…
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored Aug 15, 2023
1 parent caeb20f commit 3cf86d5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
43 changes: 27 additions & 16 deletions homeassistant/components/met_eireann/weather.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Support for Met Éireann weather service."""
import logging
from typing import cast
from types import MappingProxyType
from typing import Any, cast

from homeassistant.components.weather import (
ATTR_FORECAST_CONDITION,
ATTR_FORECAST_TIME,
DOMAIN as WEATHER_DOMAIN,
Forecast,
WeatherEntity,
WeatherEntityFeature,
Expand All @@ -20,6 +22,7 @@
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
Expand Down Expand Up @@ -50,12 +53,28 @@ async def async_setup_entry(
) -> None:
"""Add a weather entity from a config_entry."""
coordinator = hass.data[DOMAIN][config_entry.entry_id]
async_add_entities(
[
MetEireannWeather(coordinator, config_entry.data, False),
MetEireannWeather(coordinator, config_entry.data, True),
]
)
entity_registry = er.async_get(hass)

entities = [MetEireannWeather(coordinator, config_entry.data, False)]

# Add hourly entity to legacy config entries
if entity_registry.async_get_entity_id(
WEATHER_DOMAIN,
DOMAIN,
_calculate_unique_id(config_entry.data, True),
):
entities.append(MetEireannWeather(coordinator, config_entry.data, True))

async_add_entities(entities)


def _calculate_unique_id(config: MappingProxyType[str, Any], hourly: bool) -> str:
"""Calculate unique ID."""
name_appendix = ""
if hourly:
name_appendix = "-hourly"

return f"{config[CONF_LATITUDE]}-{config[CONF_LONGITUDE]}{name_appendix}"


class MetEireannWeather(
Expand All @@ -75,6 +94,7 @@ class MetEireannWeather(
def __init__(self, coordinator, config, hourly):
"""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._hourly = hourly

Expand All @@ -87,15 +107,6 @@ def _handle_coordinator_update(self) -> None:
self.hass, self.async_update_listeners(("daily", "hourly"))
)

@property
def unique_id(self):
"""Return unique ID."""
name_appendix = ""
if self._hourly:
name_appendix = "-hourly"

return f"{self._config[CONF_LATITUDE]}-{self._config[CONF_LONGITUDE]}{name_appendix}"

@property
def name(self):
"""Return the name of the sensor."""
Expand Down
26 changes: 26 additions & 0 deletions tests/components/met_eireann/test_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er

from tests.common import MockConfigEntry
from tests.typing import WebSocketGenerator
Expand All @@ -31,6 +32,31 @@ async def setup_config_entry(hass: HomeAssistant) -> ConfigEntry:
return mock_data


async def test_new_config_entry(hass: HomeAssistant, mock_weather) -> None:
"""Test the expected entities are created."""
registry = er.async_get(hass)
await setup_config_entry(hass)
assert len(hass.states.async_entity_ids("weather")) == 1

entry = hass.config_entries.async_entries()[0]
assert len(er.async_entries_for_config_entry(registry, entry.entry_id)) == 1


async def test_legacy_config_entry(hass: HomeAssistant, mock_weather) -> None:
"""Test the expected entities are created."""
registry = er.async_get(hass)
registry.async_get_or_create(
WEATHER_DOMAIN,
DOMAIN,
"10-20-hourly",
)
await setup_config_entry(hass)
assert len(hass.states.async_entity_ids("weather")) == 2

entry = hass.config_entries.async_entries()[0]
assert len(er.async_entries_for_config_entry(registry, entry.entry_id)) == 2


async def test_weather(hass: HomeAssistant, mock_weather) -> None:
"""Test weather entity."""
await setup_config_entry(hass)
Expand Down

0 comments on commit 3cf86d5

Please sign in to comment.