mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Handle null data in WeatherFlow sensors (#103349)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
91182603d5
commit
b0e04ae690
@ -71,7 +71,8 @@ class WeatherFlowSensorEntityDescription(
|
|||||||
|
|
||||||
def get_native_value(self, device: WeatherFlowDevice) -> datetime | StateType:
|
def get_native_value(self, device: WeatherFlowDevice) -> datetime | StateType:
|
||||||
"""Return the parsed sensor value."""
|
"""Return the parsed sensor value."""
|
||||||
raw_sensor_data = getattr(device, self.key)
|
if (raw_sensor_data := getattr(device, self.key)) is None:
|
||||||
|
return None
|
||||||
return self.raw_data_conv_fn(raw_sensor_data)
|
return self.raw_data_conv_fn(raw_sensor_data)
|
||||||
|
|
||||||
|
|
||||||
@ -371,14 +372,17 @@ class WeatherFlowSensorEntity(SensorEntity):
|
|||||||
return self.device.last_report
|
return self.device.last_report
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
def _async_update_state(self) -> None:
|
||||||
def native_value(self) -> datetime | StateType:
|
"""Update entity state."""
|
||||||
"""Return the state of the sensor."""
|
value = self.entity_description.get_native_value(self.device)
|
||||||
return self.entity_description.get_native_value(self.device)
|
self._attr_available = value is not None
|
||||||
|
self._attr_native_value = value
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Subscribe to events."""
|
"""Subscribe to events."""
|
||||||
|
self._async_update_state()
|
||||||
for event in self.entity_description.event_subscriptions:
|
for event in self.entity_description.event_subscriptions:
|
||||||
self.async_on_remove(
|
self.async_on_remove(
|
||||||
self.device.on(event, lambda _: self.async_write_ha_state())
|
self.device.on(event, lambda _: self._async_update_state())
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user