From 66b02ecff038672df3aa5b53a408c79e26236bae Mon Sep 17 00:00:00 2001 From: Gordon Allott Date: Mon, 20 Jun 2022 19:27:39 +0100 Subject: [PATCH] Ensure metoffice daily are returned once daily (#72440) * ensure metoffice daily are returned once daily * Fixes metoffice tests for MODE_DAILY --- homeassistant/components/metoffice/helpers.py | 4 ++ tests/components/metoffice/test_weather.py | 45 ++++++++++--------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/metoffice/helpers.py b/homeassistant/components/metoffice/helpers.py index 31ac4c141a9..00d5e73501d 100644 --- a/homeassistant/components/metoffice/helpers.py +++ b/homeassistant/components/metoffice/helpers.py @@ -7,6 +7,7 @@ import datapoint from homeassistant.helpers.update_coordinator import UpdateFailed from homeassistant.util.dt import utcnow +from .const import MODE_3HOURLY from .data import MetOfficeData _LOGGER = logging.getLogger(__name__) @@ -39,6 +40,9 @@ def fetch_data(connection: datapoint.Manager, site, mode) -> MetOfficeData: for day in forecast.days for timestep in day.timesteps if timestep.date > time_now + and ( + mode == MODE_3HOURLY or timestep.date.hour > 6 + ) # ensures only one result per day in MODE_DAILY ], site, ) diff --git a/tests/components/metoffice/test_weather.py b/tests/components/metoffice/test_weather.py index fb00203661b..bf279ff3cf7 100644 --- a/tests/components/metoffice/test_weather.py +++ b/tests/components/metoffice/test_weather.py @@ -163,16 +163,17 @@ async def test_one_weather_site_running(hass, requests_mock): assert weather.attributes.get("humidity") == 50 # Also has Forecasts added - again, just pick out 1 entry to check - assert len(weather.attributes.get("forecast")) == 8 + # ensures that daily filters out multiple results per day + assert len(weather.attributes.get("forecast")) == 4 assert ( - weather.attributes.get("forecast")[7]["datetime"] == "2020-04-29T12:00:00+00:00" + weather.attributes.get("forecast")[3]["datetime"] == "2020-04-29T12:00:00+00:00" ) - assert weather.attributes.get("forecast")[7]["condition"] == "rainy" - assert weather.attributes.get("forecast")[7]["precipitation_probability"] == 59 - assert weather.attributes.get("forecast")[7]["temperature"] == 13 - assert weather.attributes.get("forecast")[7]["wind_speed"] == 13 - assert weather.attributes.get("forecast")[7]["wind_bearing"] == "SE" + assert weather.attributes.get("forecast")[3]["condition"] == "rainy" + assert weather.attributes.get("forecast")[3]["precipitation_probability"] == 59 + assert weather.attributes.get("forecast")[3]["temperature"] == 13 + assert weather.attributes.get("forecast")[3]["wind_speed"] == 13 + assert weather.attributes.get("forecast")[3]["wind_bearing"] == "SE" @freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.timezone.utc)) @@ -258,16 +259,17 @@ async def test_two_weather_sites_running(hass, requests_mock): assert weather.attributes.get("humidity") == 50 # Also has Forecasts added - again, just pick out 1 entry to check - assert len(weather.attributes.get("forecast")) == 8 + # ensures that daily filters out multiple results per day + assert len(weather.attributes.get("forecast")) == 4 assert ( - weather.attributes.get("forecast")[7]["datetime"] == "2020-04-29T12:00:00+00:00" + weather.attributes.get("forecast")[3]["datetime"] == "2020-04-29T12:00:00+00:00" ) - assert weather.attributes.get("forecast")[7]["condition"] == "rainy" - assert weather.attributes.get("forecast")[7]["precipitation_probability"] == 59 - assert weather.attributes.get("forecast")[7]["temperature"] == 13 - assert weather.attributes.get("forecast")[7]["wind_speed"] == 13 - assert weather.attributes.get("forecast")[7]["wind_bearing"] == "SE" + assert weather.attributes.get("forecast")[3]["condition"] == "rainy" + assert weather.attributes.get("forecast")[3]["precipitation_probability"] == 59 + assert weather.attributes.get("forecast")[3]["temperature"] == 13 + assert weather.attributes.get("forecast")[3]["wind_speed"] == 13 + 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") @@ -305,13 +307,14 @@ async def test_two_weather_sites_running(hass, requests_mock): assert weather.attributes.get("humidity") == 75 # All should have Forecast added - again, just picking out 1 entry to check - assert len(weather.attributes.get("forecast")) == 8 + # ensures daily filters out multiple results per day + assert len(weather.attributes.get("forecast")) == 4 assert ( - weather.attributes.get("forecast")[5]["datetime"] == "2020-04-28T12:00:00+00:00" + weather.attributes.get("forecast")[2]["datetime"] == "2020-04-28T12:00:00+00:00" ) - assert weather.attributes.get("forecast")[5]["condition"] == "cloudy" - assert weather.attributes.get("forecast")[5]["precipitation_probability"] == 14 - assert weather.attributes.get("forecast")[5]["temperature"] == 11 - assert weather.attributes.get("forecast")[5]["wind_speed"] == 7 - assert weather.attributes.get("forecast")[5]["wind_bearing"] == "ESE" + assert weather.attributes.get("forecast")[2]["condition"] == "cloudy" + assert weather.attributes.get("forecast")[2]["precipitation_probability"] == 14 + assert weather.attributes.get("forecast")[2]["temperature"] == 11 + assert weather.attributes.get("forecast")[2]["wind_speed"] == 7 + assert weather.attributes.get("forecast")[2]["wind_bearing"] == "ESE"