From 8704cb6628fd8f6eac386fe1c48be1b6fa79ccff Mon Sep 17 00:00:00 2001 From: Brett Adams Date: Mon, 8 Jul 2024 10:58:40 +0000 Subject: [PATCH] Adding Restore --- custom_components/teslemetry/sensor.py | 30 +++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/custom_components/teslemetry/sensor.py b/custom_components/teslemetry/sensor.py index 19d7e6b..000aec7 100644 --- a/custom_components/teslemetry/sensor.py +++ b/custom_components/teslemetry/sensor.py @@ -12,6 +12,7 @@ from homeassistant.components.sensor import ( SensorDeviceClass, SensorEntity, + RestoreSensor, SensorEntityDescription, SensorStateClass, ) @@ -43,6 +44,7 @@ TeslemetryVehicleStreamEntity, TeslemetryWallConnectorEntity, ) +from .const import TeslemetryState from .models import TeslemetryEnergyData, TeslemetryVehicleData from .helpers import auto_type, ignore_drop @@ -1095,7 +1097,7 @@ async def async_setup_entry( ) -class TeslemetryVehicleSensorEntity(TeslemetryVehicleEntity, SensorEntity): +class TeslemetryVehicleSensorEntity(TeslemetryVehicleEntity, RestoreSensor): """Base class for Teslemetry vehicle metric sensors.""" entity_description: TeslemetrySensorEntityDescription @@ -1111,6 +1113,14 @@ def __init__( data, description.key, description.timestamp_key, description.streaming_key ) + async def async_added_to_hass(self) -> None: + """Handle entity which will be added.""" + await super().async_added_to_hass() + if self.coordinator.data.get('state') == TeslemetryState.OFFLINE: + + if (sensor_data := await self.async_get_last_sensor_data()) is not None: + self._attr_native_value = sensor_data.native_value + def _async_update_attrs(self) -> None: """Update the attributes of the sensor.""" if self.coordinator.updated_once: @@ -1171,7 +1181,7 @@ def _async_value_from_stream(self, value) -> None: self._attr_native_value = self._get_timestamp(int(value)) -class TeslemetryStreamSensorEntity(TeslemetryVehicleStreamEntity, SensorEntity): +class TeslemetryStreamSensorEntity(TeslemetryVehicleStreamEntity, RestoreSensor): """Base class for Teslemetry vehicle streaming sensors.""" entity_description: TeslemetryStreamSensorEntityDescription @@ -1185,6 +1195,13 @@ def __init__( self.entity_description = description super().__init__(data, description.key) + async def async_added_to_hass(self) -> None: + """Handle entity which will be added.""" + await super().async_added_to_hass() + + if (sensor_data := await self.async_get_last_sensor_data()) is not None: + self._attr_native_value = sensor_data.native_value + def _async_value_from_stream(self, value) -> None: """Update the value of the entity.""" self._attr_available = self.stream.connected @@ -1298,7 +1315,7 @@ def _async_update_attrs(self) -> None: self._attr_available = not self.exactly(None) self._attr_native_value = self._value -class TeslemetryVehicleEventEntity(SensorEntity): +class TeslemetryVehicleEventEntity(RestoreSensor): """Parent class for Teslemetry Vehicle Stream entities.""" _attr_has_entity_name = True @@ -1316,6 +1333,13 @@ def __init__( self._attr_unique_id = f"{data.vin}-event_{key}" self._attr_device_info = data.device + async def async_added_to_hass(self) -> None: + """Handle entity which will be added.""" + await super().async_added_to_hass() + + if (sensor_data := await self.async_get_last_sensor_data()) is not None: + self._attr_native_value = sensor_data.native_value + async def async_added_to_hass(self) -> None: """When entity is added to hass.""" await super().async_added_to_hass()