Fix NWS error with no observation (#92997)
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>pull/93234/head
parent
b9798e4f4f
commit
d0460d5424
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue