Skip to content
Merged
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
13 changes: 8 additions & 5 deletions homeassistant/components/derivative/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.components.sensor import PLATFORM_SCHEMA, RestoreSensor, SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT,
Expand All @@ -22,7 +22,6 @@
from homeassistant.helpers import config_validation as cv, entity_registry as er
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_state_change_event
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

from .const import (
Expand Down Expand Up @@ -126,7 +125,7 @@ async def async_setup_platform(
async_add_entities([derivative])


class DerivativeSensor(RestoreEntity, SensorEntity):
class DerivativeSensor(RestoreSensor, SensorEntity):
"""Representation of an derivative sensor."""

_attr_icon = ICON
Expand Down Expand Up @@ -170,9 +169,13 @@ def __init__(
async def async_added_to_hass(self) -> None:
"""Handle entity which will be added."""
await super().async_added_to_hass()
if (state := await self.async_get_last_state()) is not None:
restored_data = await self.async_get_last_sensor_data()
if restored_data:
self._attr_native_unit_of_measurement = (
restored_data.native_unit_of_measurement
)
try:
self._state = Decimal(state.state)
self._state = Decimal(restored_data.native_value) # type: ignore[arg-type]
except SyntaxError as err:
_LOGGER.warning("Could not restore last state: %s", err)

Expand Down