diff --git a/homeassistant/components/metoffice/const.py b/homeassistant/components/metoffice/const.py index e413b102898..d1f48eb4f2c 100644 --- a/homeassistant/components/metoffice/const.py +++ b/homeassistant/components/metoffice/const.py @@ -24,6 +24,8 @@ DOMAIN = "metoffice" DEFAULT_NAME = "Met Office" ATTRIBUTION = "Data provided by the Met Office" +ATTR_FORECAST_DAYTIME = "daytime" + DEFAULT_SCAN_INTERVAL = timedelta(minutes=15) METOFFICE_COORDINATES = "metoffice_coordinates" diff --git a/homeassistant/components/metoffice/weather.py b/homeassistant/components/metoffice/weather.py index 79363db3667..d25df1d2654 100644 --- a/homeassistant/components/metoffice/weather.py +++ b/homeassistant/components/metoffice/weather.py @@ -15,6 +15,7 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity from . import get_device_info from .const import ( + ATTR_FORECAST_DAYTIME, ATTRIBUTION, CONDITION_CLASSES, DEFAULT_NAME, @@ -46,7 +47,7 @@ async def async_setup_entry( ) -def _build_forecast_data(timestep): +def _build_forecast_data(timestep, use_3hourly): data = {} data[ATTR_FORECAST_TIME] = timestep.date.isoformat() if timestep.weather: @@ -59,6 +60,9 @@ def _build_forecast_data(timestep): data[ATTR_FORECAST_WIND_BEARING] = timestep.wind_direction.value if timestep.wind_speed: data[ATTR_FORECAST_WIND_SPEED] = timestep.wind_speed.value + if not use_3hourly: + # if it's close to noon, mark as Day, otherwise as Night + data[ATTR_FORECAST_DAYTIME] = abs(timestep.date.hour - 12) < 6 return data @@ -82,6 +86,7 @@ class MetOfficeWeather(CoordinatorEntity, WeatherEntity): ) self._attr_name = f"{DEFAULT_NAME} {hass_data[METOFFICE_NAME]} {mode_label}" self._attr_unique_id = hass_data[METOFFICE_COORDINATES] + self._use_3hourly = use_3hourly if not use_3hourly: self._attr_unique_id = f"{self._attr_unique_id}_{MODE_DAILY}" @@ -155,7 +160,7 @@ class MetOfficeWeather(CoordinatorEntity, WeatherEntity): if self.coordinator.data.forecast is None: return None return [ - _build_forecast_data(timestep) + _build_forecast_data(timestep, self._use_3hourly) for timestep in self.coordinator.data.forecast ] diff --git a/tests/components/metoffice/test_weather.py b/tests/components/metoffice/test_weather.py index 158e44ca15b..1970217db5b 100644 --- a/tests/components/metoffice/test_weather.py +++ b/tests/components/metoffice/test_weather.py @@ -181,6 +181,13 @@ async def test_one_weather_site_running(hass, requests_mock, legacy_patchable_ti 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")[7]["daytime"] is True + + # Check that night entry is correctly marked as Night + assert ( + weather.attributes.get("forecast")[6]["datetime"] == "2020-04-29T00:00:00+00:00" + ) + assert weather.attributes.get("forecast")[6]["daytime"] is False @patch( @@ -256,6 +263,7 @@ async def test_two_weather_sites_running(hass, requests_mock, legacy_patchable_t assert weather.attributes.get("forecast")[18]["temperature"] == 9 assert weather.attributes.get("forecast")[18]["wind_speed"] == 4 assert weather.attributes.get("forecast")[18]["wind_bearing"] == "NW" + assert "daytime" not in weather.attributes.get("forecast")[18] # Wavertree daily weather platform expected results weather = hass.states.get("weather.met_office_wavertree_daily") @@ -279,6 +287,7 @@ async def test_two_weather_sites_running(hass, requests_mock, legacy_patchable_t 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")[7]["daytime"] is True # King's Lynn 3-hourly weather platform expected results weather = hass.states.get("weather.met_office_king_s_lynn_3_hourly") @@ -303,6 +312,7 @@ async def test_two_weather_sites_running(hass, requests_mock, legacy_patchable_t assert weather.attributes.get("forecast")[18]["temperature"] == 10 assert weather.attributes.get("forecast")[18]["wind_speed"] == 7 assert weather.attributes.get("forecast")[18]["wind_bearing"] == "SE" + assert "daytime" not in weather.attributes.get("forecast")[18] # King's Lynn daily weather platform expected results weather = hass.states.get("weather.met_office_king_s_lynn_daily") @@ -326,3 +336,4 @@ async def test_two_weather_sites_running(hass, requests_mock, legacy_patchable_t 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")[5]["daytime"] is True