From 208ea6eae471d32aa2862576a7a3f10e990ef3d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20Hellstr=C3=B6m?= Date: Tue, 5 Feb 2019 13:43:04 +0100 Subject: [PATCH] SMHI component: Bugfix - calc precipitation (#20745) * Bugfix - calc precipitation * Feedback: better way to skip first forecast * Even less messy way * lint error --- homeassistant/components/smhi/__init__.py | 2 +- homeassistant/components/smhi/weather.py | 25 +++++++++++------------ requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/smhi/test_weather.py | 20 ++++++++++-------- 5 files changed, 26 insertions(+), 25 deletions(-) diff --git a/homeassistant/components/smhi/__init__.py b/homeassistant/components/smhi/__init__.py index 0ca3bac3e35..ba7ccc2f682 100644 --- a/homeassistant/components/smhi/__init__.py +++ b/homeassistant/components/smhi/__init__.py @@ -11,7 +11,7 @@ from homeassistant.core import Config, HomeAssistant from .config_flow import smhi_locations # noqa: F401 from .const import DOMAIN # noqa: F401 -REQUIREMENTS = ['smhi-pkg==1.0.5'] +REQUIREMENTS = ['smhi-pkg==1.0.8'] DEFAULT_NAME = 'smhi' diff --git a/homeassistant/components/smhi/weather.py b/homeassistant/components/smhi/weather.py index 94873b03bd6..79fd1166a4b 100644 --- a/homeassistant/components/smhi/weather.py +++ b/homeassistant/components/smhi/weather.py @@ -22,7 +22,7 @@ from homeassistant.const import ( CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME, TEMP_CELSIUS) from homeassistant.core import HomeAssistant from homeassistant.helpers import aiohttp_client -from homeassistant.util import Throttle, dt, slugify +from homeassistant.util import Throttle, slugify DEPENDENCIES = ['smhi'] @@ -208,25 +208,24 @@ class SmhiWeather(WeatherEntity): @property def forecast(self) -> List: """Return the forecast.""" - if self._forecasts is None: + if self._forecasts is None or len(self._forecasts) < 2: return None data = [] - for forecast in self._forecasts: + + for forecast in self._forecasts[1:]: condition = next(( k for k, v in CONDITION_CLASSES.items() if forecast.symbol in v), None) - # Only get mid day forecasts - if forecast.valid_time.hour == 12: - data.append({ - ATTR_FORECAST_TIME: dt.as_local(forecast.valid_time), - ATTR_FORECAST_TEMP: forecast.temperature_max, - ATTR_FORECAST_TEMP_LOW: forecast.temperature_min, - ATTR_FORECAST_PRECIPITATION: - round(forecast.mean_precipitation*24), - ATTR_FORECAST_CONDITION: condition, - }) + data.append({ + ATTR_FORECAST_TIME: forecast.valid_time.isoformat(), + ATTR_FORECAST_TEMP: forecast.temperature_max, + ATTR_FORECAST_TEMP_LOW: forecast.temperature_min, + ATTR_FORECAST_PRECIPITATION: + round(forecast.total_precipitation), + ATTR_FORECAST_CONDITION: condition, + }) return data diff --git a/requirements_all.txt b/requirements_all.txt index 68a7bc3e931..35019f74408 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1552,7 +1552,7 @@ smappy==0.2.16 # smbus-cffi==0.5.1 # homeassistant.components.smhi -smhi-pkg==1.0.5 +smhi-pkg==1.0.8 # homeassistant.components.media_player.snapcast snapcast==2.0.9 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index cb6d000dca8..dd455ef88fd 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -269,7 +269,7 @@ simplisafe-python==3.1.14 sleepyq==0.6 # homeassistant.components.smhi -smhi-pkg==1.0.5 +smhi-pkg==1.0.8 # homeassistant.components.climate.honeywell somecomfort==0.5.2 diff --git a/tests/components/smhi/test_weather.py b/tests/components/smhi/test_weather.py index aaf22ffce65..f0acd231ebe 100644 --- a/tests/components/smhi/test_weather.py +++ b/tests/components/smhi/test_weather.py @@ -1,7 +1,7 @@ """Test for the smhi weather entity.""" import asyncio import logging -from datetime import datetime, timezone +from datetime import datetime from unittest.mock import Mock, patch from homeassistant.components.weather import ( @@ -61,12 +61,11 @@ async def test_setup_hass(hass: HomeAssistant, aioclient_mock) -> None: assert state.attributes[ATTR_WEATHER_WIND_SPEED] == 7 assert state.attributes[ATTR_WEATHER_WIND_BEARING] == 134 _LOGGER.error(state.attributes) - assert len(state.attributes['forecast']) == 1 + assert len(state.attributes['forecast']) == 4 - forecast = state.attributes['forecast'][0] - assert forecast[ATTR_FORECAST_TIME] == datetime(2018, 9, 2, 12, 0, - tzinfo=timezone.utc) - assert forecast[ATTR_FORECAST_TEMP] == 20 + forecast = state.attributes['forecast'][1] + assert forecast[ATTR_FORECAST_TIME] == '2018-09-02T12:00:00' + assert forecast[ATTR_FORECAST_TEMP] == 21 assert forecast[ATTR_FORECAST_TEMP_LOW] == 6 assert forecast[ATTR_FORECAST_PRECIPITATION] == 0 assert forecast[ATTR_FORECAST_CONDITION] == 'partlycloudy' @@ -102,7 +101,8 @@ def test_properties_unknown_symbol() -> None: hass = Mock() data = Mock() data.temperature = 5 - data.mean_precipitation = 1 + data.mean_precipitation = 0.5 + data.total_precipitation = 1 data.humidity = 5 data.wind_speed = 10 data.wind_direction = 180 @@ -114,7 +114,8 @@ def test_properties_unknown_symbol() -> None: data2 = Mock() data2.temperature = 5 - data2.mean_precipitation = 1 + data2.mean_precipitation = 0.5 + data2.total_precipitation = 1 data2.humidity = 5 data2.wind_speed = 10 data2.wind_direction = 180 @@ -126,7 +127,8 @@ def test_properties_unknown_symbol() -> None: data3 = Mock() data3.temperature = 5 - data3.mean_precipitation = 1 + data3.mean_precipitation = 0.5 + data3.total_precipitation = 1 data3.humidity = 5 data3.wind_speed = 10 data3.wind_direction = 180