Skip to content

Commit

Permalink
fix: vmoto last warning time
Browse files Browse the repository at this point in the history
  • Loading branch information
drakhart committed Apr 15, 2023
1 parent 9d286f6 commit d783813
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
12 changes: 8 additions & 4 deletions custom_components/super_soco_custom/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ async def _async_update_data(self):
{
DATA_ECU_BATTERY: device_data[DATA_ECU_BATTERY],
DATA_LAST_GPS_TIME: parse_timestamp(
device_data[DATA_LAST_GPS_TIME]
device_data[DATA_LAST_GPS_TIME],
False,
),
DATA_MODEL_NAME: self._user_data[DATA_USER_BIND_DEVICE][
DATA_MODEL_NAME
Expand Down Expand Up @@ -451,10 +452,12 @@ async def _get_last_trip_data(self) -> dict:
1,
),
DATA_LAST_TRIP_BEGIN_TIME: parse_timestamp(
trip[DATA_LAST_TRIP_BEGIN_TIME]
trip[DATA_LAST_TRIP_BEGIN_TIME],
True,
),
DATA_LAST_TRIP_END_TIME: parse_timestamp(
trip[DATA_LAST_TRIP_END_TIME]
trip[DATA_LAST_TRIP_END_TIME],
True,
),
DATA_LAST_TRIP_RIDE_DISTANCE: round(
trip[DATA_LAST_TRIP_MILEAGE], DISTANCE_ROUNDING_DECIMALS
Expand Down Expand Up @@ -544,7 +547,8 @@ async def _get_last_warning_data(self) -> dict:

data = {
DATA_LAST_WARNING_TIME: parse_timestamp(
warning[DATA_CREATE_TIME]
warning[DATA_CREATE_TIME],
True,
),
}
else:
Expand Down
7 changes: 5 additions & 2 deletions custom_components/super_soco_custom/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ def parse_date(date_string: str) -> datetime:
return datetime.strptime(clean_string, "%d/%m/%Y %H:%M%z")


def parse_timestamp(timestampMilliseconds: int) -> datetime:
def parse_timestamp(timestampMilliseconds: int, fix_timezone: bool = False) -> datetime:
timestamp = timestampMilliseconds / MILLISECONDS_IN_A_SECOND
timestamp += UTC_TO_SHANGHAI_HOURS_DIFFERENCE * SECONDS_IN_AN_HOUR

if fix_timezone:
timestamp += UTC_TO_SHANGHAI_HOURS_DIFFERENCE * SECONDS_IN_AN_HOUR

return datetime.fromtimestamp(timestamp, tz=pytz.UTC)
5 changes: 4 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def test_calculate_course():

def test_parse_timestamp():
"""Test parse_timestamp."""
assert parse_timestamp(1681423654000) == datetime.datetime(
assert parse_timestamp(1681423654000, True) == datetime.datetime(
2023, 4, 14, 6, 7, 34, tzinfo=pytz.UTC
)
assert parse_timestamp(1681423654000) == datetime.datetime(
2023, 4, 13, 22, 7, 34, tzinfo=pytz.UTC
)

0 comments on commit d783813

Please sign in to comment.