mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +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 datetime import datetime, timedelta
|
||||||
|
|
||||||
from propcache.api import cached_property
|
from propcache.api import cached_property
|
||||||
from teslemetry_stream import Signal
|
from teslemetry_stream import Signal, TeslemetryStreamVehicle
|
||||||
from teslemetry_stream.const import ShiftState
|
from teslemetry_stream.const import ShiftState
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
@ -50,6 +50,7 @@ from .models import TeslemetryEnergyData, TeslemetryVehicleData
|
|||||||
|
|
||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 0
|
||||||
|
|
||||||
|
|
||||||
CHARGE_STATES = {
|
CHARGE_STATES = {
|
||||||
"Starting": "starting",
|
"Starting": "starting",
|
||||||
"Charging": "charging",
|
"Charging": "charging",
|
||||||
@ -350,21 +351,26 @@ class TeslemetryTimeEntityDescription(SensorEntityDescription):
|
|||||||
"""Describes Teslemetry Sensor entity."""
|
"""Describes Teslemetry Sensor entity."""
|
||||||
|
|
||||||
variance: int
|
variance: int
|
||||||
streaming_key: Signal
|
streaming_listener: Callable[
|
||||||
|
[TeslemetryStreamVehicle, Callable[[float | None], None]],
|
||||||
|
Callable[[], None],
|
||||||
|
]
|
||||||
streaming_firmware: str = "2024.26"
|
streaming_firmware: str = "2024.26"
|
||||||
|
streaming_value_fn: Callable[[float], float] = lambda x: x
|
||||||
|
|
||||||
|
|
||||||
VEHICLE_TIME_DESCRIPTIONS: tuple[TeslemetryTimeEntityDescription, ...] = (
|
VEHICLE_TIME_DESCRIPTIONS: tuple[TeslemetryTimeEntityDescription, ...] = (
|
||||||
TeslemetryTimeEntityDescription(
|
TeslemetryTimeEntityDescription(
|
||||||
key="charge_state_minutes_to_full_charge",
|
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,
|
device_class=SensorDeviceClass.TIMESTAMP,
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
variance=4,
|
variance=4,
|
||||||
),
|
),
|
||||||
TeslemetryTimeEntityDescription(
|
TeslemetryTimeEntityDescription(
|
||||||
key="drive_state_active_route_minutes_to_arrival",
|
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,
|
device_class=SensorDeviceClass.TIMESTAMP,
|
||||||
variance=1,
|
variance=1,
|
||||||
),
|
),
|
||||||
@ -667,18 +673,22 @@ class TeslemetryStreamTimeSensorEntity(TeslemetryVehicleStreamEntity, SensorEnti
|
|||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
self._get_timestamp = ignore_variance(
|
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),
|
ignored_variance=timedelta(minutes=description.variance),
|
||||||
)
|
)
|
||||||
assert description.streaming_key
|
super().__init__(data, description.key)
|
||||||
super().__init__(data, description.key, description.streaming_key)
|
|
||||||
|
|
||||||
@cached_property
|
async def async_added_to_hass(self) -> None:
|
||||||
def available(self) -> bool:
|
"""When entity is added to hass."""
|
||||||
"""Return True if entity is available."""
|
await super().async_added_to_hass()
|
||||||
return self.stream.connected
|
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."""
|
"""Update the value of the entity."""
|
||||||
if value is None:
|
if value is None:
|
||||||
self._attr_native_value = None
|
self._attr_native_value = None
|
||||||
|
@ -72,7 +72,7 @@ async def test_sensors_streaming(
|
|||||||
Signal.AC_CHARGING_ENERGY_IN: 10,
|
Signal.AC_CHARGING_ENERGY_IN: 10,
|
||||||
Signal.AC_CHARGING_POWER: 2,
|
Signal.AC_CHARGING_POWER: 2,
|
||||||
Signal.CHARGING_CABLE_TYPE: None,
|
Signal.CHARGING_CABLE_TYPE: None,
|
||||||
Signal.TIME_TO_FULL_CHARGE: 10,
|
Signal.TIME_TO_FULL_CHARGE: 0.166666667,
|
||||||
Signal.MINUTES_TO_ARRIVAL: None,
|
Signal.MINUTES_TO_ARRIVAL: None,
|
||||||
},
|
},
|
||||||
"createdAt": "2024-10-04T10:45:17.537Z",
|
"createdAt": "2024-10-04T10:45:17.537Z",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user