Fix NWS error with no observation (#92997)

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
pull/93234/head
MatthewFlamm 2023-05-17 16:00:13 -04:00 committed by GitHub
parent b9798e4f4f
commit d0460d5424
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 4 deletions

View File

@ -195,9 +195,12 @@ class NWSSensor(CoordinatorEntity[NwsDataUpdateCoordinator], SensorEntity):
@property
def native_value(self) -> float | None:
"""Return the state."""
value = self._nws.observation.get(self.entity_description.key)
if value is None:
if (
not (observation := self._nws.observation)
or (value := observation.get(self.entity_description.key)) is None
):
return None
# Set alias to unit property -> prevent unnecessary hasattr calls
unit_of_measurement = self.native_unit_of_measurement
if unit_of_measurement == UnitOfSpeed.MILES_PER_HOUR:

View File

@ -70,10 +70,13 @@ async def test_imperial_metric(
assert state.attributes.get(ATTR_ATTRIBUTION) == ATTRIBUTION
async def test_none_values(hass: HomeAssistant, mock_simple_nws, no_weather) -> None:
@pytest.mark.parametrize("values", [NONE_OBSERVATION, None])
async def test_none_values(
hass: HomeAssistant, mock_simple_nws, no_weather, values
) -> None:
"""Test with no values."""
instance = mock_simple_nws.return_value
instance.observation = NONE_OBSERVATION
instance.observation = values
registry = er.async_get(hass)