Add time to charge sensor to Tessie (#108342)

* Add time to charge and type checking

* Revert drive_state_shift_state change

* Use original name

* Use function instead of lambda

* Update homeassistant/components/tessie/sensor.py

Co-authored-by: Jan-Philipp Benecke <github@bnck.me>

* Fix callback

* Avoid having to test None

* Go back to if

* Use minutes instead of hours

---------

Co-authored-by: Jan-Philipp Benecke <github@bnck.me>
This commit is contained in:
Brett Adams 2024-01-23 06:45:08 +10:00 committed by GitHub
parent d75dd0973f
commit e1fd5e83a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 5 deletions

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from datetime import datetime, timedelta
from homeassistant.components.sensor import (
SensorDeviceClass,
@ -24,20 +25,29 @@ from homeassistant.const import (
UnitOfTemperature,
UnitOfTime,
)
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.util import dt as dt_util
from .const import DOMAIN
from .coordinator import TessieStateUpdateCoordinator
from .entity import TessieEntity
@callback
def hours_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 TessieSensorEntityDescription(SensorEntityDescription):
"""Describes Tessie Sensor entity."""
value_fn: Callable[[StateType], StateType] = lambda x: x
value_fn: Callable[[StateType], StateType | datetime] = lambda x: x
DESCRIPTIONS: tuple[TessieSensorEntityDescription, ...] = (
@ -81,6 +91,12 @@ DESCRIPTIONS: tuple[TessieSensorEntityDescription, ...] = (
device_class=SensorDeviceClass.SPEED,
entity_category=EntityCategory.DIAGNOSTIC,
),
TessieSensorEntityDescription(
key="charge_state_minutes_to_full_charge",
device_class=SensorDeviceClass.TIMESTAMP,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=hours_to_datetime,
),
TessieSensorEntityDescription(
key="charge_state_battery_range",
state_class=SensorStateClass.MEASUREMENT,
@ -243,6 +259,6 @@ class TessieSensorEntity(TessieEntity, SensorEntity):
self.entity_description = description
@property
def native_value(self) -> StateType:
def native_value(self) -> StateType | datetime:
"""Return the state of the sensor."""
return self.entity_description.value_fn(self.get())

View File

@ -88,6 +88,9 @@
"charge_state_battery_range": {
"name": "Battery range"
},
"charge_state_minutes_to_full_charge": {
"name": "Time to full charge"
},
"drive_state_speed": {
"name": "Speed"
},

View File

@ -62,7 +62,7 @@
"fast_charger_type": "ACSingleWireCAN",
"ideal_battery_range": 263.68,
"max_range_charge_counter": 0,
"minutes_to_full_charge": 30,
"minutes_to_full_charge": 0,
"not_enough_power_to_heat": null,
"off_peak_charging_enabled": false,
"off_peak_charging_times": "all_week",
@ -77,7 +77,7 @@
"scheduled_departure_time": 1694899800,
"scheduled_departure_time_minutes": 450,
"supercharger_session_trip_planner": false,
"time_to_full_charge": 0.5,
"time_to_full_charge": 0,
"timestamp": 1701139037461,
"trip_charging": false,
"usable_battery_level": 75,