Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions custom_components/prix_carburant/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def __init__(
self.fuel = fuel

self._last_update = None
self._last_value: float | None = None
self._attr_unique_id = "_".join([DOMAIN, str(self.station_id), self.fuel])
if self.station_info[ATTR_NAME] != "undefined":
station_name = self.station_info[ATTR_NAME]
Expand Down Expand Up @@ -168,6 +169,12 @@ def __init__(
),
}

async def async_added_to_hass(self) -> None:
"""Restore last state on startup."""
await super().async_added_to_hass()
if (last_state := await self.async_get_last_sensor_data()) is not None:
self._last_value = last_state.native_value

@property
def native_value(self) -> float | None:
"""Return the current price."""
Expand All @@ -193,5 +200,6 @@ def native_value(self) -> float | None:
err,
)
if fuel.get(ATTR_PRICE) is not None:
return round(float(fuel[ATTR_PRICE]), 3)
return None
self._last_value = round(float(fuel[ATTR_PRICE]), 3)
return self._last_value
return self._last_value
Loading