Skip to content

Commit

Permalink
Add binary sensor restore
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Jul 9, 2024
1 parent fd7bf82 commit c4b9557
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions custom_components/teslemetry/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.restore_state import RestoreEntity

from .const import DOMAIN, TeslemetryState, TeslemetryTimestamp
from .entity import (
Expand Down Expand Up @@ -281,7 +282,7 @@ async def async_setup_entry(
)


class TeslemetryVehicleBinarySensorEntity(TeslemetryVehicleEntity, BinarySensorEntity):
class TeslemetryVehicleBinarySensorEntity(TeslemetryVehicleEntity, BinarySensorEntity, RestoreEntity):
"""Base class for Teslemetry vehicle binary sensors."""

entity_description: TeslemetryBinarySensorEntityDescription
Expand All @@ -297,6 +298,12 @@ 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 (state := await self.async_get_last_state()) is not None:
self._state = state.state == "on"

def _async_update_attrs(self) -> None:
"""Update the attributes of the binary sensor."""

Expand All @@ -317,7 +324,7 @@ def _async_value_from_stream(self, value) -> None:


class TeslemetryStreamBinarySensorEntity(
TeslemetryVehicleStreamEntity, BinarySensorEntity
TeslemetryVehicleStreamEntity, BinarySensorEntity, RestoreEntity
):
"""Base class for Teslemetry vehicle streaming sensors."""

Expand All @@ -332,9 +339,15 @@ 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 (state := await self.async_get_last_state()) is not None:
self._attr_is_on = state.state == "on"

def _async_value_from_stream(self, value) -> None:
"""Update the value of the entity."""
self._attr_native_value = self.entity_description.value_fn(auto_type(value))
self._attr_is_on = self.entity_description.value_fn(auto_type(value))


class TeslemetryEnergyLiveBinarySensorEntity(
Expand Down

0 comments on commit c4b9557

Please sign in to comment.