From 457425b63517e414d19fe3b72065b8d8d1c184d4 Mon Sep 17 00:00:00 2001 From: Ryan Adolf Date: Fri, 10 Dec 2021 21:44:53 -0800 Subject: [PATCH 1/3] Gracefully handle None values in forecast --- homeassistant/components/weather/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/weather/__init__.py b/homeassistant/components/weather/__init__.py index b9fa7e2ae3914..eaf3c2d6f2731 100644 --- a/homeassistant/components/weather/__init__.py +++ b/homeassistant/components/weather/__init__.py @@ -262,7 +262,7 @@ def state_attributes(self): self.temperature_unit, self.precision, ) - if ATTR_FORECAST_PRESSURE in forecast_entry: + if forecast_entry.get(ATTR_FORECAST_PRESSURE) is not None: if (unit := self.pressure_unit) is not None: pressure = round( self.hass.config.units.pressure( @@ -271,7 +271,7 @@ def state_attributes(self): ROUNDING_PRECISION, ) forecast_entry[ATTR_FORECAST_PRESSURE] = pressure - if ATTR_FORECAST_WIND_SPEED in forecast_entry: + if forecast_entry.get(ATTR_FORECAST_WIND_SPEED) is not None: if (unit := self.wind_speed_unit) is not None: wind_speed = round( self.hass.config.units.wind_speed( @@ -280,7 +280,7 @@ def state_attributes(self): ROUNDING_PRECISION, ) forecast_entry[ATTR_FORECAST_WIND_SPEED] = wind_speed - if ATTR_FORECAST_PRECIPITATION in forecast_entry: + if forecast_entry.get(ATTR_FORECAST_PRECIPITATION) is not None: if (unit := self.precipitation_unit) is not None: precipitation = round( self.hass.config.units.accumulated_precipitation( From fb3d3098b6bc95e37e9beab3dc3eb42038b0c9f2 Mon Sep 17 00:00:00 2001 From: Ryan Adolf Date: Tue, 14 Dec 2021 12:41:37 -0800 Subject: [PATCH 2/3] Add tests --- tests/components/weather/test_init.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/components/weather/test_init.py b/tests/components/weather/test_init.py index 4125e94749ada..9849a6abe1886 100644 --- a/tests/components/weather/test_init.py +++ b/tests/components/weather/test_init.py @@ -168,3 +168,26 @@ async def test_precipitation_conversion( native_value, native_unit, unit_system.accumulated_precipitation_unit ) assert float(forecast[ATTR_FORECAST_PRECIPITATION]) == approx(expected, rel=1e-2) + + +async def test_none_forecast( + hass, + enable_custom_integrations, +): + """Test that conversion with None values succeeds.""" + entity0 = await create_entity( + hass, + pressure=None, + pressure_unit=PRESSURE_INHG, + wind_speed=None, + wind_speed_unit=SPEED_METERS_PER_SECOND, + precipitation=None, + precipitation_unit=LENGTH_MILLIMETERS, + ) + + state = hass.states.get(entity0.entity_id) + forecast = state.attributes[ATTR_FORECAST][0] + + assert forecast[ATTR_FORECAST_PRESSURE] is None + assert forecast[ATTR_FORECAST_WIND_SPEED] is None + assert forecast[ATTR_FORECAST_PRECIPITATION] is None From 2e38788898cb525c299e0789a43e4d7cba9bd1a8 Mon Sep 17 00:00:00 2001 From: Ryan Adolf Date: Thu, 16 Dec 2021 12:26:04 -0800 Subject: [PATCH 3/3] Assign pressures and wind speeds so they can be reused --- homeassistant/components/weather/__init__.py | 22 +++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/weather/__init__.py b/homeassistant/components/weather/__init__.py index eaf3c2d6f2731..0a562a40f64eb 100644 --- a/homeassistant/components/weather/__init__.py +++ b/homeassistant/components/weather/__init__.py @@ -262,29 +262,31 @@ def state_attributes(self): self.temperature_unit, self.precision, ) - if forecast_entry.get(ATTR_FORECAST_PRESSURE) is not None: + if ( + native_pressure := forecast_entry.get(ATTR_FORECAST_PRESSURE) + ) is not None: if (unit := self.pressure_unit) is not None: pressure = round( - self.hass.config.units.pressure( - forecast_entry[ATTR_FORECAST_PRESSURE], unit - ), + self.hass.config.units.pressure(native_pressure, unit), ROUNDING_PRECISION, ) forecast_entry[ATTR_FORECAST_PRESSURE] = pressure - if forecast_entry.get(ATTR_FORECAST_WIND_SPEED) is not None: + if ( + native_wind_speed := forecast_entry.get(ATTR_FORECAST_WIND_SPEED) + ) is not None: if (unit := self.wind_speed_unit) is not None: wind_speed = round( - self.hass.config.units.wind_speed( - forecast_entry[ATTR_FORECAST_WIND_SPEED], unit - ), + self.hass.config.units.wind_speed(native_wind_speed, unit), ROUNDING_PRECISION, ) forecast_entry[ATTR_FORECAST_WIND_SPEED] = wind_speed - if forecast_entry.get(ATTR_FORECAST_PRECIPITATION) is not None: + if ( + native_precip := forecast_entry.get(ATTR_FORECAST_PRECIPITATION) + ) is not None: if (unit := self.precipitation_unit) is not None: precipitation = round( self.hass.config.units.accumulated_precipitation( - forecast_entry[ATTR_FORECAST_PRECIPITATION], unit + native_precip, unit ), ROUNDING_PRECISION, )