SMHI component: Bugfix - calc precipitation (#20745)

* Bugfix - calc precipitation

* Feedback: better way to skip first forecast

* Even less messy way

* lint error
This commit is contained in:
Tomas Hellström 2019-02-05 13:43:04 +01:00 committed by Charles Garwood
parent e581d41ded
commit 208ea6eae4
5 changed files with 26 additions and 25 deletions

View File

@ -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'

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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