Remove hourly weather entity from metoffice (#112452)

This commit is contained in:
G Johansson 2024-03-27 11:06:31 +01:00 committed by GitHub
parent 69356300f0
commit b190cdceaf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 85 additions and 2172 deletions

View File

@ -45,31 +45,24 @@ async def async_setup_entry(
entity_registry = er.async_get(hass)
hass_data = hass.data[DOMAIN][entry.entry_id]
entities = [
MetOfficeWeather(
hass_data[METOFFICE_DAILY_COORDINATOR],
hass_data[METOFFICE_HOURLY_COORDINATOR],
hass_data,
False,
)
]
# Add hourly entity to legacy config entries
if entity_registry.async_get_entity_id(
# Remove hourly entity from legacy config entries
if entity_id := entity_registry.async_get_entity_id(
WEATHER_DOMAIN,
DOMAIN,
_calculate_unique_id(hass_data[METOFFICE_COORDINATES], True),
):
entities.append(
entity_registry.async_remove(entity_id)
async_add_entities(
[
MetOfficeWeather(
hass_data[METOFFICE_DAILY_COORDINATOR],
hass_data[METOFFICE_HOURLY_COORDINATOR],
hass_data,
True,
)
)
async_add_entities(entities, False)
],
False,
)
def _build_forecast_data(timestep: Timestep) -> Forecast:
@ -119,14 +112,9 @@ class MetOfficeWeather(
coordinator_daily: TimestampDataUpdateCoordinator[MetOfficeData],
coordinator_hourly: TimestampDataUpdateCoordinator[MetOfficeData],
hass_data: dict[str, Any],
use_3hourly: bool,
) -> None:
"""Initialise the platform with a data instance."""
self._hourly = use_3hourly
if use_3hourly:
observation_coordinator = coordinator_hourly
else:
observation_coordinator = coordinator_daily
observation_coordinator = coordinator_daily
super().__init__(
observation_coordinator,
daily_coordinator=coordinator_daily,
@ -136,9 +124,9 @@ class MetOfficeWeather(
self._attr_device_info = get_device_info(
coordinates=hass_data[METOFFICE_COORDINATES], name=hass_data[METOFFICE_NAME]
)
self._attr_name = "3-Hourly" if use_3hourly else "Daily"
self._attr_name = "Daily"
self._attr_unique_id = _calculate_unique_id(
hass_data[METOFFICE_COORDINATES], use_3hourly
hass_data[METOFFICE_COORDINATES], False
)
@property

File diff suppressed because it is too large Load Diff

View File

@ -109,14 +109,6 @@ async def test_site_cannot_update(
) -> None:
"""Test we handle cannot connect error."""
# Pre-create the hourly entity
entity_registry.async_get_or_create(
WEATHER_DOMAIN,
DOMAIN,
"53.38374_-2.90929",
suggested_object_id="met_office_wavertree_3_hourly",
)
entry = MockConfigEntry(
domain=DOMAIN,
data=METOFFICE_CONFIG_WAVERTREE,
@ -125,9 +117,6 @@ async def test_site_cannot_update(
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
weather = hass.states.get("weather.met_office_wavertree_3_hourly")
assert weather
weather = hass.states.get("weather.met_office_wavertree_daily")
assert weather
@ -138,9 +127,6 @@ async def test_site_cannot_update(
async_fire_time_changed(hass, future_time)
await hass.async_block_till_done()
weather = hass.states.get("weather.met_office_wavertree_3_hourly")
assert weather.state == STATE_UNAVAILABLE
weather = hass.states.get("weather.met_office_wavertree_daily")
assert weather.state == STATE_UNAVAILABLE
@ -154,14 +140,6 @@ async def test_one_weather_site_running(
) -> None:
"""Test the Met Office weather platform."""
# Pre-create the hourly entity
entity_registry.async_get_or_create(
WEATHER_DOMAIN,
DOMAIN,
"53.38374_-2.90929",
suggested_object_id="met_office_wavertree_3_hourly",
)
entry = MockConfigEntry(
domain=DOMAIN,
data=METOFFICE_CONFIG_WAVERTREE,
@ -175,30 +153,6 @@ async def test_one_weather_site_running(
device_wavertree = dev_reg.async_get_device(identifiers=DEVICE_KEY_WAVERTREE)
assert device_wavertree.name == "Met Office Wavertree"
# Wavertree 3-hourly weather platform expected results
weather = hass.states.get("weather.met_office_wavertree_3_hourly")
assert weather
assert weather.state == "sunny"
assert weather.attributes.get("temperature") == 17
assert weather.attributes.get("wind_speed") == 14.48
assert weather.attributes.get("wind_speed_unit") == "km/h"
assert weather.attributes.get("wind_bearing") == "SSE"
assert weather.attributes.get("humidity") == 50
# Forecasts added - just pick out 1 entry to check
assert len(weather.attributes.get("forecast")) == 35
assert (
weather.attributes.get("forecast")[26]["datetime"]
== "2020-04-28T21:00:00+00:00"
)
assert weather.attributes.get("forecast")[26]["condition"] == "cloudy"
assert weather.attributes.get("forecast")[26]["precipitation_probability"] == 9
assert weather.attributes.get("forecast")[26]["temperature"] == 10
assert weather.attributes.get("forecast")[26]["wind_speed"] == 6.44
assert weather.attributes.get("forecast")[26]["wind_bearing"] == "NNE"
# Wavertree daily weather platform expected results
weather = hass.states.get("weather.met_office_wavertree_daily")
assert weather
@ -232,20 +186,6 @@ async def test_two_weather_sites_running(
) -> None:
"""Test we handle two different weather sites both running."""
# Pre-create the hourly entities
entity_registry.async_get_or_create(
WEATHER_DOMAIN,
DOMAIN,
"53.38374_-2.90929",
suggested_object_id="met_office_wavertree_3_hourly",
)
entity_registry.async_get_or_create(
WEATHER_DOMAIN,
DOMAIN,
"52.75556_0.44231",
suggested_object_id="met_office_king_s_lynn_3_hourly",
)
# all metoffice test data encapsulated in here
mock_json = json.loads(load_fixture("metoffice.json", "metoffice"))
kingslynn_hourly = json.dumps(mock_json["kingslynn_hourly"])
@ -279,30 +219,6 @@ async def test_two_weather_sites_running(
device_wavertree = dev_reg.async_get_device(identifiers=DEVICE_KEY_WAVERTREE)
assert device_wavertree.name == "Met Office Wavertree"
# Wavertree 3-hourly weather platform expected results
weather = hass.states.get("weather.met_office_wavertree_3_hourly")
assert weather
assert weather.state == "sunny"
assert weather.attributes.get("temperature") == 17
assert weather.attributes.get("wind_speed") == 14.48
assert weather.attributes.get("wind_speed_unit") == "km/h"
assert weather.attributes.get("wind_bearing") == "SSE"
assert weather.attributes.get("humidity") == 50
# Forecasts added - just pick out 1 entry to check
assert len(weather.attributes.get("forecast")) == 35
assert (
weather.attributes.get("forecast")[18]["datetime"]
== "2020-04-27T21:00:00+00:00"
)
assert weather.attributes.get("forecast")[18]["condition"] == "clear-night"
assert weather.attributes.get("forecast")[18]["precipitation_probability"] == 1
assert weather.attributes.get("forecast")[18]["temperature"] == 9
assert weather.attributes.get("forecast")[18]["wind_speed"] == 6.44
assert weather.attributes.get("forecast")[18]["wind_bearing"] == "NW"
# Wavertree daily weather platform expected results
weather = hass.states.get("weather.met_office_wavertree_daily")
assert weather
@ -327,30 +243,6 @@ async def test_two_weather_sites_running(
assert weather.attributes.get("forecast")[3]["wind_speed"] == 20.92
assert weather.attributes.get("forecast")[3]["wind_bearing"] == "SE"
# King's Lynn 3-hourly weather platform expected results
weather = hass.states.get("weather.met_office_king_s_lynn_3_hourly")
assert weather
assert weather.state == "sunny"
assert weather.attributes.get("temperature") == 14
assert weather.attributes.get("wind_speed") == 3.22
assert weather.attributes.get("wind_speed_unit") == "km/h"
assert weather.attributes.get("wind_bearing") == "E"
assert weather.attributes.get("humidity") == 60
# Also has Forecast added - just pick out 1 entry to check
assert len(weather.attributes.get("forecast")) == 35
assert (
weather.attributes.get("forecast")[18]["datetime"]
== "2020-04-27T21:00:00+00:00"
)
assert weather.attributes.get("forecast")[18]["condition"] == "cloudy"
assert weather.attributes.get("forecast")[18]["precipitation_probability"] == 9
assert weather.attributes.get("forecast")[18]["temperature"] == 10
assert weather.attributes.get("forecast")[18]["wind_speed"] == 11.27
assert weather.attributes.get("forecast")[18]["wind_bearing"] == "SE"
# King's Lynn daily weather platform expected results
weather = hass.states.get("weather.met_office_king_s_lynn_daily")
assert weather
@ -395,33 +287,6 @@ async def test_new_config_entry(
assert len(er.async_entries_for_config_entry(entity_registry, entry.entry_id)) == 1
@pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC))
async def test_legacy_config_entry(
hass: HomeAssistant, entity_registry: er.EntityRegistry, no_sensor, wavertree_data
) -> None:
"""Test the expected entities are created."""
# Pre-create the hourly entity
entity_registry.async_get_or_create(
WEATHER_DOMAIN,
DOMAIN,
"53.38374_-2.90929",
suggested_object_id="met_office_wavertree_3_hourly",
)
entry = MockConfigEntry(
domain=DOMAIN,
data=METOFFICE_CONFIG_WAVERTREE,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids("weather")) == 2
entry = hass.config_entries.async_entries()[0]
assert len(er.async_entries_for_config_entry(entity_registry, entry.entry_id)) == 2
@pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC))
@pytest.mark.parametrize(
("service"),
@ -513,27 +378,11 @@ async def test_forecast_service(
assert response == snapshot
@pytest.mark.parametrize(
"entity_id",
[
"weather.met_office_wavertree_3_hourly",
"weather.met_office_wavertree_daily",
],
)
@pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC))
async def test_forecast_subscription(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
entity_registry: er.EntityRegistry,
freezer: FrozenDateTimeFactory,
snapshot: SnapshotAssertion,
no_sensor,
wavertree_data: dict[str, _Matcher],
entity_id: str,
async def test_legacy_config_entry_is_removed(
hass: HomeAssistant, entity_registry: er.EntityRegistry, no_sensor, wavertree_data
) -> None:
"""Test multiple forecast."""
client = await hass_ws_client(hass)
"""Test the expected entities are created."""
# Pre-create the hourly entity
entity_registry.async_get_or_create(
WEATHER_DOMAIN,
@ -551,47 +400,75 @@ async def test_forecast_subscription(
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
for forecast_type in ("daily", "hourly"):
await client.send_json_auto_id(
{
"type": "weather/subscribe_forecast",
"forecast_type": forecast_type,
"entity_id": entity_id,
}
)
msg = await client.receive_json()
assert msg["success"]
assert msg["result"] is None
subscription_id = msg["id"]
assert len(hass.states.async_entity_ids("weather")) == 1
entry = hass.config_entries.async_entries()[0]
assert len(er.async_entries_for_config_entry(entity_registry, entry.entry_id)) == 1
msg = await client.receive_json()
assert msg["id"] == subscription_id
assert msg["type"] == "event"
forecast1 = msg["event"]["forecast"]
assert forecast1 != []
assert forecast1 == snapshot
@pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC))
@pytest.mark.parametrize("forecast_type", ["daily", "hourly"])
async def test_forecast_subscription(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
entity_registry: er.EntityRegistry,
freezer: FrozenDateTimeFactory,
snapshot: SnapshotAssertion,
no_sensor,
wavertree_data: dict[str, _Matcher],
forecast_type: str,
) -> None:
"""Test multiple forecast."""
client = await hass_ws_client(hass)
freezer.tick(DEFAULT_SCAN_INTERVAL + timedelta(seconds=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
msg = await client.receive_json()
entry = MockConfigEntry(
domain=DOMAIN,
data=METOFFICE_CONFIG_WAVERTREE,
)
entry.add_to_hass(hass)
assert msg["id"] == subscription_id
assert msg["type"] == "event"
forecast2 = msg["event"]["forecast"]
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert forecast2 != []
assert forecast2 == snapshot
await client.send_json_auto_id(
{
"type": "weather/subscribe_forecast",
"forecast_type": forecast_type,
"entity_id": "weather.met_office_wavertree_daily",
}
)
msg = await client.receive_json()
assert msg["success"]
assert msg["result"] is None
subscription_id = msg["id"]
await client.send_json_auto_id(
{
"type": "unsubscribe_events",
"subscription": subscription_id,
}
)
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
msg = await client.receive_json()
assert msg["success"]
msg = await client.receive_json()
assert msg["id"] == subscription_id
assert msg["type"] == "event"
forecast1 = msg["event"]["forecast"]
assert forecast1 != []
assert forecast1 == snapshot
freezer.tick(DEFAULT_SCAN_INTERVAL + timedelta(seconds=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
msg = await client.receive_json()
assert msg["id"] == subscription_id
assert msg["type"] == "event"
forecast2 = msg["event"]["forecast"]
assert forecast2 != []
assert forecast2 == snapshot
await client.send_json_auto_id(
{
"type": "unsubscribe_events",
"subscription": subscription_id,
}
)
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
msg = await client.receive_json()
assert msg["success"]