mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 01:37:08 +00:00
Fix time to full charge in Teslemetry (#137996)
* Fix streaming full charge * ruff
This commit is contained in:
parent
4e759e59a4
commit
220bd5a27f
@ -7,7 +7,7 @@ from dataclasses import dataclass
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from propcache.api import cached_property
|
||||
from teslemetry_stream import Signal
|
||||
from teslemetry_stream import Signal, TeslemetryStreamVehicle
|
||||
from teslemetry_stream.const import ShiftState
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
@ -50,6 +50,7 @@ from .models import TeslemetryEnergyData, TeslemetryVehicleData
|
||||
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
||||
CHARGE_STATES = {
|
||||
"Starting": "starting",
|
||||
"Charging": "charging",
|
||||
@ -350,21 +351,26 @@ class TeslemetryTimeEntityDescription(SensorEntityDescription):
|
||||
"""Describes Teslemetry Sensor entity."""
|
||||
|
||||
variance: int
|
||||
streaming_key: Signal
|
||||
streaming_listener: Callable[
|
||||
[TeslemetryStreamVehicle, Callable[[float | None], None]],
|
||||
Callable[[], None],
|
||||
]
|
||||
streaming_firmware: str = "2024.26"
|
||||
streaming_value_fn: Callable[[float], float] = lambda x: x
|
||||
|
||||
|
||||
VEHICLE_TIME_DESCRIPTIONS: tuple[TeslemetryTimeEntityDescription, ...] = (
|
||||
TeslemetryTimeEntityDescription(
|
||||
key="charge_state_minutes_to_full_charge",
|
||||
streaming_key=Signal.TIME_TO_FULL_CHARGE,
|
||||
streaming_value_fn=lambda x: x * 60,
|
||||
streaming_listener=lambda x, y: x.listen_TimeToFullCharge(y),
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
variance=4,
|
||||
),
|
||||
TeslemetryTimeEntityDescription(
|
||||
key="drive_state_active_route_minutes_to_arrival",
|
||||
streaming_key=Signal.MINUTES_TO_ARRIVAL,
|
||||
streaming_listener=lambda x, y: x.listen_MinutesToArrival(y),
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
variance=1,
|
||||
),
|
||||
@ -667,18 +673,22 @@ class TeslemetryStreamTimeSensorEntity(TeslemetryVehicleStreamEntity, SensorEnti
|
||||
"""Initialize the sensor."""
|
||||
self.entity_description = description
|
||||
self._get_timestamp = ignore_variance(
|
||||
func=lambda value: dt_util.now() + timedelta(minutes=value),
|
||||
func=lambda value: dt_util.now()
|
||||
+ timedelta(minutes=description.streaming_value_fn(value)),
|
||||
ignored_variance=timedelta(minutes=description.variance),
|
||||
)
|
||||
assert description.streaming_key
|
||||
super().__init__(data, description.key, description.streaming_key)
|
||||
super().__init__(data, description.key)
|
||||
|
||||
@cached_property
|
||||
def available(self) -> bool:
|
||||
"""Return True if entity is available."""
|
||||
return self.stream.connected
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""When entity is added to hass."""
|
||||
await super().async_added_to_hass()
|
||||
self.async_on_remove(
|
||||
self.entity_description.streaming_listener(
|
||||
self.vehicle.stream_vehicle, self._value_callback
|
||||
)
|
||||
)
|
||||
|
||||
def _async_value_from_stream(self, value) -> None:
|
||||
def _value_callback(self, value: float | None) -> None:
|
||||
"""Update the value of the entity."""
|
||||
if value is None:
|
||||
self._attr_native_value = None
|
||||
|
@ -72,7 +72,7 @@ async def test_sensors_streaming(
|
||||
Signal.AC_CHARGING_ENERGY_IN: 10,
|
||||
Signal.AC_CHARGING_POWER: 2,
|
||||
Signal.CHARGING_CABLE_TYPE: None,
|
||||
Signal.TIME_TO_FULL_CHARGE: 10,
|
||||
Signal.TIME_TO_FULL_CHARGE: 0.166666667,
|
||||
Signal.MINUTES_TO_ARRIVAL: None,
|
||||
},
|
||||
"createdAt": "2024-10-04T10:45:17.537Z",
|
||||
|
Loading…
x
Reference in New Issue
Block a user