Skip to content

Commit

Permalink
Adding Restore
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Jul 8, 2024
1 parent 62f6d74 commit 8704cb6
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions custom_components/teslemetry/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
RestoreSensor,
SensorEntityDescription,
SensorStateClass,
)
Expand Down Expand Up @@ -43,6 +44,7 @@
TeslemetryVehicleStreamEntity,
TeslemetryWallConnectorEntity,
)
from .const import TeslemetryState
from .models import TeslemetryEnergyData, TeslemetryVehicleData
from .helpers import auto_type, ignore_drop

Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 8704cb6

Please sign in to comment.