From fca56993c65cbaed623146018c50771e791fc5f5 Mon Sep 17 00:00:00 2001 From: Christer Vestermark Date: Mon, 10 May 2021 16:44:08 +0200 Subject: [PATCH] Add smhi wind gust speed and thunder probability (#50328) * Added some extra attributes Added the extra attributes wind_gust_speed and thunder_probability that were already implemented in the underlaying library (joysoftware / pypi_smhi). Also for the existing extra attribute cloudiness, it is added if "is not None" instead of just "if self.cloudiness" which would make it False (and therefore not available) if cloudiness = 0. * Trying to solve the style issues Removed white spaces and changed order of list as suggested by the tests. * New try to solve the style issues Removed some more white spaces... * Changed dictionary handling as suggested Changed dictionary handling as suggested by MartinHjelmare. * Updated test Updated test_weather.py to include the new attributes wind_gust_speed and thunder_probability. * Added missing imports Added the missing imports ATTR_SMHI_THUNDER_PROBABILITY, ATTR_SMHI_WIND_GUST_SPEED, * Renaming self.thunder to self.thunder_probability and correcting test valuesfor Renamed the new internal attribute thunder to thunder_probability, same as the exposed attribute for improved consistency. Corrected test values according to smhi.json. * Forgot to change to self.thunder_probability in one place. sorry. --- homeassistant/components/smhi/const.py | 2 ++ homeassistant/components/smhi/weather.py | 32 +++++++++++++++++++++--- tests/components/smhi/test_weather.py | 16 +++++++++++- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/smhi/const.py b/homeassistant/components/smhi/const.py index 7e88f95f691..c2074416295 100644 --- a/homeassistant/components/smhi/const.py +++ b/homeassistant/components/smhi/const.py @@ -2,6 +2,8 @@ from homeassistant.components.weather import DOMAIN as WEATHER_DOMAIN ATTR_SMHI_CLOUDINESS = "cloudiness" +ATTR_SMHI_WIND_GUST_SPEED = "wind_gust_speed" +ATTR_SMHI_THUNDER_PROBABILITY = "thunder_probability" DOMAIN = "smhi" diff --git a/homeassistant/components/smhi/weather.py b/homeassistant/components/smhi/weather.py index 86cdf72e65c..5458abb1786 100644 --- a/homeassistant/components/smhi/weather.py +++ b/homeassistant/components/smhi/weather.py @@ -38,7 +38,12 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers import aiohttp_client from homeassistant.util import Throttle, slugify -from .const import ATTR_SMHI_CLOUDINESS, ENTITY_ID_SENSOR_FORMAT +from .const import ( + ATTR_SMHI_CLOUDINESS, + ATTR_SMHI_THUNDER_PROBABILITY, + ATTR_SMHI_WIND_GUST_SPEED, + ENTITY_ID_SENSOR_FORMAT, +) _LOGGER = logging.getLogger(__name__) @@ -167,6 +172,14 @@ class SmhiWeather(WeatherEntity): return round(self._forecasts[0].wind_speed * 18 / 5) return None + @property + def wind_gust_speed(self) -> float: + """Return the wind gust speed.""" + if self._forecasts is not None: + # Convert from m/s to km/h + return round(self._forecasts[0].wind_gust * 18 / 5) + return None + @property def wind_bearing(self) -> int: """Return the wind bearing.""" @@ -195,6 +208,13 @@ class SmhiWeather(WeatherEntity): return self._forecasts[0].cloudiness return None + @property + def thunder_probability(self) -> int: + """Return the chance of thunder, unit Percent.""" + if self._forecasts is not None: + return self._forecasts[0].thunder + return None + @property def condition(self) -> str: """Return the weather condition.""" @@ -238,5 +258,11 @@ class SmhiWeather(WeatherEntity): @property def extra_state_attributes(self) -> dict: """Return SMHI specific attributes.""" - if self.cloudiness: - return {ATTR_SMHI_CLOUDINESS: self.cloudiness} + extra_attributes = {} + if self.cloudiness is not None: + extra_attributes[ATTR_SMHI_CLOUDINESS] = self.cloudiness + if self.wind_gust_speed is not None: + extra_attributes[ATTR_SMHI_WIND_GUST_SPEED] = self.wind_gust_speed + if self.thunder_probability is not None: + extra_attributes[ATTR_SMHI_THUNDER_PROBABILITY] = self.thunder_probability + return extra_attributes diff --git a/tests/components/smhi/test_weather.py b/tests/components/smhi/test_weather.py index 9170f3a9ed0..10a21f74099 100644 --- a/tests/components/smhi/test_weather.py +++ b/tests/components/smhi/test_weather.py @@ -7,7 +7,11 @@ from unittest.mock import AsyncMock, Mock, patch from smhi.smhi_lib import APIURL_TEMPLATE, SmhiForecastException from homeassistant.components.smhi import weather as weather_smhi -from homeassistant.components.smhi.const import ATTR_SMHI_CLOUDINESS +from homeassistant.components.smhi.const import ( + ATTR_SMHI_CLOUDINESS, + ATTR_SMHI_THUNDER_PROBABILITY, + ATTR_SMHI_WIND_GUST_SPEED, +) from homeassistant.components.weather import ( ATTR_FORECAST_CONDITION, ATTR_FORECAST_PRECIPITATION, @@ -57,6 +61,8 @@ async def test_setup_hass(hass: HomeAssistant, aioclient_mock) -> None: assert state.state == "sunny" assert state.attributes[ATTR_SMHI_CLOUDINESS] == 50 + assert state.attributes[ATTR_SMHI_THUNDER_PROBABILITY] == 33 + assert state.attributes[ATTR_SMHI_WIND_GUST_SPEED] == 17 assert state.attributes[ATTR_WEATHER_ATTRIBUTION].find("SMHI") >= 0 assert state.attributes[ATTR_WEATHER_HUMIDITY] == 55 assert state.attributes[ATTR_WEATHER_PRESSURE] == 1024 @@ -85,10 +91,12 @@ def test_properties_no_data(hass: HomeAssistant) -> None: assert weather.temperature is None assert weather.humidity is None assert weather.wind_speed is None + assert weather.wind_gust_speed is None assert weather.wind_bearing is None assert weather.visibility is None assert weather.pressure is None assert weather.cloudiness is None + assert weather.thunder_probability is None assert weather.condition is None assert weather.forecast is None assert weather.temperature_unit == TEMP_CELSIUS @@ -104,10 +112,12 @@ def test_properties_unknown_symbol() -> None: data.total_precipitation = 1 data.humidity = 5 data.wind_speed = 10 + data.wind_gust_speed = 17 data.wind_direction = 180 data.horizontal_visibility = 6 data.pressure = 1008 data.cloudiness = 52 + data.thunder_probability = 41 data.symbol = 100 # Faulty symbol data.valid_time = datetime(2018, 1, 1, 0, 1, 2) @@ -117,10 +127,12 @@ def test_properties_unknown_symbol() -> None: data2.total_precipitation = 1 data2.humidity = 5 data2.wind_speed = 10 + data2.wind_gust_speed = 17 data2.wind_direction = 180 data2.horizontal_visibility = 6 data2.pressure = 1008 data2.cloudiness = 52 + data2.thunder_probability = 41 data2.symbol = 100 # Faulty symbol data2.valid_time = datetime(2018, 1, 1, 12, 1, 2) @@ -130,10 +142,12 @@ def test_properties_unknown_symbol() -> None: data3.total_precipitation = 1 data3.humidity = 5 data3.wind_speed = 10 + data3.wind_gust_speed = 17 data3.wind_direction = 180 data3.horizontal_visibility = 6 data3.pressure = 1008 data3.cloudiness = 52 + data3.thunder_probability = 41 data3.symbol = 100 # Faulty symbol data3.valid_time = datetime(2018, 1, 2, 12, 1, 2)