Skip to content

Commit e44ae32

Browse files
committed
Add datetime fields
1 parent 73dcb92 commit e44ae32

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

teslemetry_stream/vehicle.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import asyncio
44
import logging
5+
from datetime import datetime, timezone
56
from typing import TYPE_CHECKING, Callable
67

78
from .const import (
@@ -28,6 +29,7 @@
2829
HvacPowerState,
2930
HvilStatus,
3031
LaneAssistLevel,
32+
MediaStatus,
3133
PowershareState,
3234
PowershareStopReasonStatus,
3335
PowershareTypeStatus,
@@ -39,8 +41,8 @@
3941
SunroofInstalledState,
4042
TemperatureUnit,
4143
TeslaLocation,
44+
TurnSignalState,
4245
WindowState,
43-
MediaStatus
4446
)
4547

4648
if TYPE_CHECKING:
@@ -1521,11 +1523,11 @@ def listen_ScheduledChargingPending(self, callback: Callable[[bool | None], None
15211523
{"vin":self.vin, "data": {Signal.SCHEDULED_CHARGING_PENDING: None}}
15221524
)
15231525

1524-
def listen_ScheduledChargingStartTime(self, callback: Callable[[str | None], None]) -> Callable[[],None]:
1526+
def listen_ScheduledChargingStartTime(self, callback: Callable[[datetime | None], None]) -> Callable[[],None]:
15251527
"""Listen for Scheduled Charging Start Time."""
15261528
self._enable_field(Signal.SCHEDULED_CHARGING_START_TIME)
15271529
return self.stream.async_add_listener(
1528-
lambda x: callback(x['data'][Signal.SCHEDULED_CHARGING_START_TIME]),
1530+
make_datetime(Signal.SCHEDULED_CHARGING_START_TIME, callback),
15291531
{"vin":self.vin, "data": {Signal.SCHEDULED_CHARGING_START_TIME: None}}
15301532
)
15311533

@@ -1745,35 +1747,35 @@ def listen_TpmsHardWarnings(self, callback: Callable[[int | None], None]) -> Cal
17451747
{"vin":self.vin, "data": {Signal.TPMS_HARD_WARNINGS: None}}
17461748
)
17471749

1748-
def listen_TpmsLastSeenPressureTimeFl(self, callback: Callable[[str | None], None]) -> Callable[[],None]:
1750+
def listen_TpmsLastSeenPressureTimeFl(self, callback: Callable[[datetime | None], None]) -> Callable[[],None]:
17491751
"""Listen for TPMS Last Seen Pressure Time Front Left."""
17501752
self._enable_field(Signal.TPMS_LAST_SEEN_PRESSURE_TIME_FL)
17511753
return self.stream.async_add_listener(
1752-
lambda x: callback(x['data'][Signal.TPMS_LAST_SEEN_PRESSURE_TIME_FL]),
1754+
make_datetime(Signal.TPMS_LAST_SEEN_PRESSURE_TIME_FL, callback),
17531755
{"vin":self.vin, "data": {Signal.TPMS_LAST_SEEN_PRESSURE_TIME_FL: None}}
17541756
)
17551757

1756-
def listen_TpmsLastSeenPressureTimeFr(self, callback: Callable[[str | None], None]) -> Callable[[],None]:
1758+
def listen_TpmsLastSeenPressureTimeFr(self, callback: Callable[[datetime | None], None]) -> Callable[[],None]:
17571759
"""Listen for TPMS Last Seen Pressure Time Front Right."""
17581760
self._enable_field(Signal.TPMS_LAST_SEEN_PRESSURE_TIME_FR)
17591761
return self.stream.async_add_listener(
1760-
lambda x: callback(x['data'][Signal.TPMS_LAST_SEEN_PRESSURE_TIME_FR]),
1762+
make_datetime(Signal.TPMS_LAST_SEEN_PRESSURE_TIME_FR, callback),
17611763
{"vin":self.vin, "data": {Signal.TPMS_LAST_SEEN_PRESSURE_TIME_FR: None}}
17621764
)
17631765

1764-
def listen_TpmsLastSeenPressureTimeRl(self, callback: Callable[[str | None], None]) -> Callable[[],None]:
1766+
def listen_TpmsLastSeenPressureTimeRl(self, callback: Callable[[datetime | None], None]) -> Callable[[],None]:
17651767
"""Listen for TPMS Last Seen Pressure Time Rear Left."""
17661768
self._enable_field(Signal.TPMS_LAST_SEEN_PRESSURE_TIME_RL)
17671769
return self.stream.async_add_listener(
1768-
lambda x: callback(x['data'][Signal.TPMS_LAST_SEEN_PRESSURE_TIME_RL]),
1770+
make_datetime(Signal.TPMS_LAST_SEEN_PRESSURE_TIME_RL, callback),
17691771
{"vin":self.vin, "data": {Signal.TPMS_LAST_SEEN_PRESSURE_TIME_RL: None}}
17701772
)
17711773

1772-
def listen_TpmsLastSeenPressureTimeRr(self, callback: Callable[[str | None], None]) -> Callable[[],None]:
1774+
def listen_TpmsLastSeenPressureTimeRr(self, callback: Callable[[datetime | None], None]) -> Callable[[],None]:
17731775
"""Listen for TPMS Last Seen Pressure Time Rear Right."""
17741776
self._enable_field(Signal.TPMS_LAST_SEEN_PRESSURE_TIME_RR)
17751777
return self.stream.async_add_listener(
1776-
lambda x: callback(x['data'][Signal.TPMS_LAST_SEEN_PRESSURE_TIME_RR]),
1778+
make_datetime(Signal.TPMS_LAST_SEEN_PRESSURE_TIME_RR, callback),
17771779
{"vin":self.vin, "data": {Signal.TPMS_LAST_SEEN_PRESSURE_TIME_RR: None}}
17781780
)
17791781

@@ -2067,6 +2069,18 @@ def typer(event: dict):
20672069
callback(None)
20682070
return typer
20692071

2072+
def make_datetime(signal: Signal, callback: Callable[[datetime | None], None]) -> Callable[[dict], None]:
2073+
"""Listener factory"""
2074+
def typer(event: dict):
2075+
data = event["data"][signal]
2076+
if isinstance(data, int):
2077+
try:
2078+
data = datetime.fromtimestamp(data, tz=timezone.utc)
2079+
except ValueError:
2080+
data = None
2081+
callback(data)
2082+
return typer
2083+
20702084
def merge(source, destination):
20712085
for key, value in source.items():
20722086
if isinstance(value, dict):

0 commit comments

Comments
 (0)