Skip to content

Commit

Permalink
More sensor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Jan 31, 2024
1 parent b9edcd5 commit 604cbb0
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions custom_components/teslemetry/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections.abc import Callable
from dataclasses import dataclass
from datetime import datetime, timedelta
from typing import cast

from homeassistant.components.sensor import (
SensorDeviceClass,
Expand Down Expand Up @@ -40,14 +41,6 @@
from .models import TeslemetryEnergyData, TeslemetryVehicleData


@callback
def minutes_to_datetime(value: StateType) -> datetime | None:
"""Convert relative hours into absolute datetime."""
if isinstance(value, int | float) and value > 0:
return dt_util.now() + timedelta(minutes=value)
return None


@dataclass(frozen=True, kw_only=True)
class TeslemetrySensorEntityDescription(SensorEntityDescription):
"""Describes Teslemetry Sensor entity."""
Expand Down Expand Up @@ -102,7 +95,7 @@ class TeslemetrySensorEntityDescription(SensorEntityDescription):
key="charge_state_minutes_to_full_charge",
device_class=SensorDeviceClass.TIMESTAMP,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=minutes_to_datetime,
value_fn=lambda value: dt_util.now() + timedelta(minutes=cast(float,value)),
),
TeslemetrySensorEntityDescription(
key="charge_state_battery_range",
Expand All @@ -129,7 +122,7 @@ class TeslemetrySensorEntityDescription(SensorEntityDescription):
icon="mdi:car-shift-pattern",
options=["p", "d", "r", "n"],
device_class=SensorDeviceClass.ENUM,
value_fn=lambda x: x.lower() if isinstance(x, str) else x,
value_fn=lambda x: cast(str,x).lower(),
),
TeslemetrySensorEntityDescription(
key="vehicle_state_odometer",
Expand Down Expand Up @@ -403,6 +396,11 @@ def native_value(self) -> StateType | datetime:
"""Return the state of the sensor."""
return self.entity_description.value_fn(self.get())

@property
def available(self) -> bool:
"""Return if sensor is available."""
return super().available and self.entity_description.available_fn(self.get())


class TeslemetryEnergySensorEntity(TeslemetryEnergyEntity, SensorEntity):
"""Base class for Teslemetry energy site metric sensors."""
Expand All @@ -423,6 +421,11 @@ def native_value(self) -> StateType:
"""Return the state of the sensor."""
return self.get()

@property
def available(self) -> bool:
"""Return if sensor is available."""
return super().available and self.entity_description.available_fn(self.get())


class TeslemetryWallConnectorSensorEntity(TeslemetryWallConnectorEntity, SensorEntity):
"""Base class for Teslemetry energy site metric sensors."""
Expand All @@ -447,3 +450,8 @@ def __init__(
def native_value(self) -> StateType:
"""Return the state of the sensor."""
return self._value

@property
def available(self) -> bool:
"""Return if sensor is available."""
return super().available and self.entity_description.available_fn(self._value)

0 comments on commit 604cbb0

Please sign in to comment.