Skip to content

Commit 5ffd4e2

Browse files
committed
Merge branch 'develop'
2 parents 5bdc79d + ee64d26 commit 5ffd4e2

12 files changed

Lines changed: 336 additions & 25 deletions

File tree

‎.github/workflows/update_lock.yml‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Update lock file
2+
3+
on:
4+
schedule:
5+
- cron: '0 6 1 * *'
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: write
10+
pull-requests: write
11+
12+
jobs:
13+
update-lock:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v7
19+
with:
20+
ref: develop
21+
22+
- name: Set up Python
23+
uses: actions/setup-python@v7
24+
with:
25+
python-version: "3.12"
26+
27+
- name: Install Poetry
28+
uses: snok/install-poetry@v1
29+
30+
- name: Update lock file
31+
run: poetry update --lock --no-interaction
32+
33+
# PRs opened with GITHUB_TOKEN do not trigger CI, so validate here before opening one
34+
- name: Install dependencies
35+
run: poetry install --no-root --no-interaction
36+
37+
- name: Type check
38+
run: poetry run mypy
39+
40+
- name: Run tests
41+
run: poetry run pytest tests
42+
43+
- name: Open pull request
44+
uses: peter-evans/create-pull-request@v7
45+
with:
46+
base: develop
47+
branch: chore/update-lock
48+
commit-message: "chore: update lock file"
49+
title: "chore: update lock file"
50+
body: "Monthly `poetry update`. Review the lock file diff and wait for CI."
51+
add-paths: poetry.lock
52+
delete-branch: true

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Supported functionality (partial list, check the code for more)
2727
* get charging status
2828
* lock vehicle
2929
* unlock vehicle
30+
* unlock the tailgate (releases the latch), or open and close a power tailgate
3031
* door windows: close, ventilate (open a few centimetres) or fully open
3132
* start rear window heating
3233
* stop rear window heating

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "saic_ismart_client_ng"
3-
version = "0.14.0"
3+
version = "0.15.0"
44
description = "SAIC next gen client library (MG iSMART)"
55
authors = [
66
{ name = "Giovanni Condello", email = "saic-python-client@nanomad.net" },

‎src/saic_ismart_client_ng/api/message/schema.py‎

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,54 @@ class MessageEntity:
3232

3333
@property
3434
def message_time(self) -> datetime.datetime:
35-
if self.messageTime:
36-
for date_format in MESSAGE_DATE_TIME_FORMATS:
37-
try:
38-
return datetime.datetime.strptime(self.messageTime, date_format)
39-
except ValueError:
40-
pass
41-
LOGGER.error(
42-
"Could not parse messageTime '%s'. This is a bug. Please file a ticket",
43-
self.messageTime,
44-
)
35+
"""The message time, or the current local time if there isn't one.
36+
37+
Falling back to now() means a message with no time looks brand new.
38+
Prefer ``message_time_or_none`` or ``create_time_utc`` when that
39+
matters (e.g. deciding whether a message is old).
40+
"""
41+
parsed = self.message_time_or_none
42+
if parsed is not None:
43+
return parsed
4544
return datetime.datetime.now()
4645

46+
@property
47+
def message_time_or_none(self) -> datetime.datetime | None:
48+
"""``messageTime`` parsed, or None if it is missing or unreadable.
49+
50+
The result is naive: SAIC sends it in the server's local time with no
51+
timezone (the EU servers use CET/CEST), so it can't safely be compared
52+
with UTC. Use ``create_time_utc`` for that.
53+
"""
54+
if not self.messageTime:
55+
return None
56+
for date_format in MESSAGE_DATE_TIME_FORMATS:
57+
try:
58+
return datetime.datetime.strptime(self.messageTime, date_format)
59+
except ValueError:
60+
pass
61+
LOGGER.error(
62+
"Could not parse messageTime '%s'. This is a bug. Please file a ticket",
63+
self.messageTime,
64+
)
65+
return None
66+
67+
@property
68+
def create_time_utc(self) -> datetime.datetime | None:
69+
"""``createTime`` (Unix milliseconds) as a UTC datetime, or None.
70+
71+
None when the message has no createTime (common on EU accounts) or
72+
it can't be read. Unlike ``messageTime`` this has no timezone doubt.
73+
"""
74+
if self.createTime is None:
75+
return None
76+
try:
77+
return datetime.datetime.fromtimestamp(
78+
self.createTime / 1000.0, tz=datetime.UTC
79+
)
80+
except (TypeError, OSError, OverflowError, ValueError):
81+
return None
82+
4783
@property
4884
def read_status(self) -> str:
4985
if self.readStatus is None:

‎src/saic_ismart_client_ng/api/vehicle/locks/__init__.py‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from __future__ import annotations
22

33
from saic_ismart_client_ng.api.vehicle import SaicVehicleApi
4-
from saic_ismart_client_ng.api.vehicle.locks.schema import VehicleLockId
4+
from saic_ismart_client_ng.api.vehicle.locks.schema import (
5+
TailgateAction,
6+
VehicleLockId,
7+
)
58
from saic_ismart_client_ng.api.vehicle.schema import (
69
RvcParams,
710
RvcParamsId,
@@ -12,7 +15,7 @@
1215
from saic_ismart_client_ng.crypto_utils import sha256_hex_digest
1316
from saic_ismart_client_ng.exceptions import SaicApiException
1417

15-
__all__ = ["VehicleLockId"]
18+
__all__ = ["TailgateAction", "VehicleLockId"]
1619

1720

1821
class SaicVehicleLocksApi(SaicVehicleApi):
@@ -25,10 +28,38 @@ async def unlock_vehicle(self, vin: str) -> VehicleControlResp:
2528
)
2629

2730
async def open_tailgate(self, vin: str) -> VehicleControlResp:
31+
"""Unlock the tailgate (lock 2), which releases its latch.
32+
33+
The iSmart app calls this "Unlock Tailgate". On a car with a power
34+
tailgate, ``control_tailgate`` opens and closes it instead.
35+
"""
2836
return await self.control_vehicle_locks(
2937
vin, should_lock=False, lock_id=VehicleLockId.TAILGATE
3038
)
3139

40+
async def control_tailgate(
41+
self, vin: str, *, action: TailgateAction
42+
) -> VehicleControlResp:
43+
"""Open or close a power tailgate.
44+
45+
Matches what the iSmart app sends (decrypted MG S6 EV traffic, UK):
46+
request type 9 with a single parameter, 31 = 1 (open) or 2 (close),
47+
and no end-of-parameters marker. On that car both were accepted, the
48+
tailgate moved, and ``BasicVehicleStatus.bootStatus`` then read 1
49+
(open) and 0 (closed). Only the S6 EV has been checked; cars without a
50+
power tailgate are expected to refuse it.
51+
"""
52+
command = VehicleControlReq(
53+
rvc_req_type=RvcReqType.TAILGATE,
54+
rvc_params=[
55+
RvcParams(
56+
RvcParamsId.TAILGATE, action.value.to_bytes(1, byteorder="big")
57+
)
58+
],
59+
vin=sha256_hex_digest(vin),
60+
)
61+
return await self.send_vehicle_control_command(command, vin)
62+
3263
async def control_vehicle_locks(
3364
self,
3465
vin: str,

‎src/saic_ismart_client_ng/api/vehicle/locks/schema.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,13 @@
66
class VehicleLockId(Enum):
77
TAILGATE = 2
88
DOORS = 3
9+
10+
11+
class TailgateAction(Enum):
12+
"""Open or close a power tailgate.
13+
14+
The values are what the iSmart app sends (decrypted MG S6 EV traffic, UK).
15+
"""
16+
17+
OPEN = 1
18+
CLOSE = 2

‎src/saic_ismart_client_ng/api/vehicle/schema.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,17 @@ class RvcParamsId(Enum):
167167
UNK_6 = 6
168168
LOCK_ID = 7
169169
WINDOW_SUNROOF = 8
170+
# The door windows follow the status model (driverWindow, passengerWindow,
171+
# rearLeftWindow, rearRightWindow). The front pair is named by role, the
172+
# rear pair by side. The WINDOW_2/3/4 names are the original ones, kept as
173+
# aliases.
170174
WINDOW_DRIVER = 9
171-
WINDOW_2 = 10
172-
WINDOW_3 = 11
173-
WINDOW_4 = 12
175+
WINDOW_PASSENGER = 10
176+
WINDOW_REAR_LEFT = 11
177+
WINDOW_REAR_RIGHT = 12
178+
WINDOW_2 = 10 # noqa: PIE796 - alias of WINDOW_PASSENGER
179+
WINDOW_3 = 11 # noqa: PIE796 - alias of WINDOW_REAR_LEFT
180+
WINDOW_4 = 12 # noqa: PIE796 - alias of WINDOW_REAR_RIGHT
174181
WINDOW_OPEN_CLOSE = 13
175182
# Heated seats are addressed by PHYSICAL side, not driver/passenger:
176183
# 17 is the front-left seat and 18 the front-right on both left- and
@@ -188,6 +195,7 @@ class RvcParamsId(Enum):
188195
HEATED_STEERING_WHEEL = 24
189196
HEATED_SEAT_REAR_LEFT = 25
190197
HEATED_SEAT_REAR_RIGHT = 26
198+
TAILGATE = 31
191199
PARAMS_MAX = 0xFF
192200

193201

@@ -211,6 +219,7 @@ class RvcReqType(Enum):
211219
CLIMATE = "6"
212220
AIR_CLEAN = "7"
213221
HEATED_STEERING_WHEEL = "8"
222+
TAILGATE = "9"
214223
ENGINE_CONTROL = "17"
215224
REMOTE_REFRESH = "18"
216225
REMOTE_IMMOBILIZER = "19"

‎src/saic_ismart_client_ng/api/vehicle/windows/__init__.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ async def control_windows(
3838
for i in [
3939
VehicleWindowId.SUNROOF,
4040
VehicleWindowId.DRIVER,
41-
VehicleWindowId.WINDOW_2,
42-
VehicleWindowId.WINDOW_3,
43-
VehicleWindowId.WINDOW_4,
41+
VehicleWindowId.PASSENGER,
42+
VehicleWindowId.REAR_LEFT,
43+
VehicleWindowId.REAR_RIGHT,
4444
]:
4545
if i.value.value in requested_windows:
4646
rcv_params.append(RvcParams(i.value, b"\x01"))
@@ -78,9 +78,9 @@ async def control_door_windows(
7878
rvc_params = [
7979
RvcParams(RvcParamsId.WINDOW_SUNROOF, b"\x00"),
8080
RvcParams(RvcParamsId.WINDOW_DRIVER, b"\x01"),
81-
RvcParams(RvcParamsId.WINDOW_2, b"\x01"),
82-
RvcParams(RvcParamsId.WINDOW_3, b"\x01"),
83-
RvcParams(RvcParamsId.WINDOW_4, b"\x01"),
81+
RvcParams(RvcParamsId.WINDOW_PASSENGER, b"\x01"),
82+
RvcParams(RvcParamsId.WINDOW_REAR_LEFT, b"\x01"),
83+
RvcParams(RvcParamsId.WINDOW_REAR_RIGHT, b"\x01"),
8484
RvcParams(RvcParamsId.WINDOW_OPEN_CLOSE, action.value.to_bytes(1, "big")),
8585
]
8686
request = VehicleControlReq(

‎src/saic_ismart_client_ng/api/vehicle/windows/schema.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
class VehicleWindowId(Enum):
99
SUNROOF = RvcParamsId.WINDOW_SUNROOF
1010
DRIVER = RvcParamsId.WINDOW_DRIVER
11-
WINDOW_2 = RvcParamsId.WINDOW_2
12-
WINDOW_3 = RvcParamsId.WINDOW_3
13-
WINDOW_4 = RvcParamsId.WINDOW_4
11+
PASSENGER = RvcParamsId.WINDOW_PASSENGER
12+
REAR_LEFT = RvcParamsId.WINDOW_REAR_LEFT
13+
REAR_RIGHT = RvcParamsId.WINDOW_REAR_RIGHT
14+
# The original names, kept as aliases.
15+
WINDOW_2 = RvcParamsId.WINDOW_2 # alias of PASSENGER
16+
WINDOW_3 = RvcParamsId.WINDOW_3 # alias of REAR_LEFT
17+
WINDOW_4 = RvcParamsId.WINDOW_4 # alias of REAR_RIGHT
1418

1519

1620
class DoorWindowsAction(Enum):

‎tests/test_door_window_names.py‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""Door windows are named after the status model, the old names are aliases.
2+
3+
The parameters sent to the car don't change.
4+
"""
5+
6+
from __future__ import annotations
7+
8+
import base64
9+
from typing import Any
10+
import unittest
11+
12+
from saic_ismart_client_ng import SaicApi
13+
from saic_ismart_client_ng.api.vehicle import RvcParamsId
14+
from saic_ismart_client_ng.api.vehicle.windows import VehicleWindowId
15+
from saic_ismart_client_ng.model import SaicApiConfiguration
16+
17+
18+
class TestDoorWindowNames(unittest.IsolatedAsyncioTestCase):
19+
def test_new_names_keep_the_param_ids(self) -> None:
20+
assert RvcParamsId.WINDOW_DRIVER.value == 9
21+
assert RvcParamsId.WINDOW_PASSENGER.value == 10
22+
assert RvcParamsId.WINDOW_REAR_LEFT.value == 11
23+
assert RvcParamsId.WINDOW_REAR_RIGHT.value == 12
24+
25+
def test_old_names_still_work(self) -> None:
26+
# Looked up by name: mypy doesn't know enum aliases share a member.
27+
assert RvcParamsId["WINDOW_2"] is RvcParamsId.WINDOW_PASSENGER
28+
assert RvcParamsId["WINDOW_3"] is RvcParamsId.WINDOW_REAR_LEFT
29+
assert RvcParamsId["WINDOW_4"] is RvcParamsId.WINDOW_REAR_RIGHT
30+
assert VehicleWindowId["WINDOW_2"] is VehicleWindowId.PASSENGER
31+
assert VehicleWindowId["WINDOW_3"] is VehicleWindowId.REAR_LEFT
32+
assert VehicleWindowId["WINDOW_4"] is VehicleWindowId.REAR_RIGHT
33+
34+
async def test_control_windows_command_unchanged(self) -> None:
35+
api = SaicApi(SaicApiConfiguration(username="user", password="pass")) # noqa: S106
36+
captured: dict[str, Any] = {}
37+
38+
async def capture(body: Any, _vin: str) -> None:
39+
captured["body"] = body
40+
41+
api.send_vehicle_control_command = capture # type: ignore[method-assign,assignment]
42+
await api.control_windows(
43+
"OFFLINE0000000000",
44+
should_open=False,
45+
windows=[VehicleWindowId.PASSENGER],
46+
)
47+
await api.close()
48+
params = [
49+
(p.paramId, base64.b64decode(p.paramValue))
50+
for p in captured["body"].rvcParams
51+
]
52+
assert params == [
53+
(8, b"\x00"),
54+
(9, b"\x00"),
55+
(10, b"\x01"),
56+
(11, b"\x00"),
57+
(12, b"\x00"),
58+
(13, b"\x00"),
59+
]

0 commit comments

Comments
 (0)