mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 04:07:08 +00:00
Fix NWS error with no observation (#92997)
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
parent
b9798e4f4f
commit
d0460d5424
@ -195,9 +195,12 @@ class NWSSensor(CoordinatorEntity[NwsDataUpdateCoordinator], SensorEntity):
|
|||||||
@property
|
@property
|
||||||
def native_value(self) -> float | None:
|
def native_value(self) -> float | None:
|
||||||
"""Return the state."""
|
"""Return the state."""
|
||||||
value = self._nws.observation.get(self.entity_description.key)
|
if (
|
||||||
if value is None:
|
not (observation := self._nws.observation)
|
||||||
|
or (value := observation.get(self.entity_description.key)) is None
|
||||||
|
):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Set alias to unit property -> prevent unnecessary hasattr calls
|
# Set alias to unit property -> prevent unnecessary hasattr calls
|
||||||
unit_of_measurement = self.native_unit_of_measurement
|
unit_of_measurement = self.native_unit_of_measurement
|
||||||
if unit_of_measurement == UnitOfSpeed.MILES_PER_HOUR:
|
if unit_of_measurement == UnitOfSpeed.MILES_PER_HOUR:
|
||||||
|
@ -70,10 +70,13 @@ async def test_imperial_metric(
|
|||||||
assert state.attributes.get(ATTR_ATTRIBUTION) == ATTRIBUTION
|
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."""
|
"""Test with no values."""
|
||||||
instance = mock_simple_nws.return_value
|
instance = mock_simple_nws.return_value
|
||||||
instance.observation = NONE_OBSERVATION
|
instance.observation = values
|
||||||
|
|
||||||
registry = er.async_get(hass)
|
registry = er.async_get(hass)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user