Skip to content

Commit

Permalink
Add smhi wind gust speed and thunder probability (#50328)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
crallian committed May 10, 2021
1 parent 8e2b3aa commit fca5699
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
2 changes: 2 additions & 0 deletions homeassistant/components/smhi/const.py
Expand Up @@ -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"

Expand Down
32 changes: 29 additions & 3 deletions homeassistant/components/smhi/weather.py
Expand Up @@ -38,7 +38,12 @@
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__)

Expand Down Expand Up @@ -167,6 +172,14 @@ def wind_speed(self) -> float:
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."""
Expand Down Expand Up @@ -195,6 +208,13 @@ def cloudiness(self) -> int:
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."""
Expand Down Expand Up @@ -238,5 +258,11 @@ def forecast(self) -> list:
@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
16 changes: 15 additions & 1 deletion tests/components/smhi/test_weather.py
Expand Up @@ -7,7 +7,11 @@
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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit fca5699

Please sign in to comment.