From f662e7e3cf7763745165e8dd2ec77c38ea7d78ec Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 20 Mar 2024 22:33:36 -1000 Subject: [PATCH] Only calculate native value once per update in systemmonitor (#113921) --- homeassistant/components/systemmonitor/sensor.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/systemmonitor/sensor.py b/homeassistant/components/systemmonitor/sensor.py index a8b0d6bd146..b2dacc27327 100644 --- a/homeassistant/components/systemmonitor/sensor.py +++ b/homeassistant/components/systemmonitor/sensor.py @@ -776,6 +776,7 @@ class SystemMonitorSensor(CoordinatorEntity[SystemMonitorCoordinator], SensorEnt self.argument = argument self.value: int | None = None self.update_time: float | None = None + self._attr_native_value = self.entity_description.value_fn(self) async def async_added_to_hass(self) -> None: """When added to hass.""" @@ -791,16 +792,19 @@ class SystemMonitorSensor(CoordinatorEntity[SystemMonitorCoordinator], SensorEnt ].remove(self.entity_id) return await super().async_will_remove_from_hass() - @property - def native_value(self) -> StateType | datetime: - """Return the state.""" - return self.entity_description.value_fn(self) + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + # Set the native value here so we can use it in available property + # without having to recalculate it + self._attr_native_value = self.entity_description.value_fn(self) + super()._handle_coordinator_update() @property def available(self) -> bool: """Return if entity is available.""" if self.entity_description.none_is_unavailable: - return bool( + return ( self.coordinator.last_update_success is True and self.native_value is not None )