Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions homeassistant/components/weather/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Comment thread
rianadon marked this conversation as resolved.
Outdated
if (unit := self.pressure_unit) is not None:
pressure = round(
self.hass.config.units.pressure(
Expand All @@ -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(
Expand All @@ -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(
Expand Down
23 changes: 23 additions & 0 deletions tests/components/weather/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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