Metadata-Version: 2.4
Name: mg-saic-client
Version: 0.10.1
Summary: SAIC next gen client library (MG iSMART)
License-File: LICENSE
License-File: LICENSES/Apache-2.0-home-assistant-mazda.txt
License-File: LICENSES/BSD-3-Clause-chromium.txt
License-File: NOTICE
Author: James Townsend
Requires-Python: >=3.11
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Provides-Extra: push
Requires-Dist: dacite (>=1.8.1,<2.0.0)
Requires-Dist: httpx (>=0.27.0,<0.29.0)
Requires-Dist: protobuf (>=6.30.2,<8.0.0) ; extra == "push"
Requires-Dist: pycryptodome (>=3.20.0,<4.0.0)
Requires-Dist: tenacity (>=9.0.0,<10.0.0)
Project-URL: Homepage, https://github.com/townsmcp/saic-python-client-ng
Project-URL: Issues, https://github.com/townsmcp/saic-python-client-ng/issues
Project-URL: Source, https://github.com/townsmcp/saic-python-client-ng
Description-Content-Type: text/markdown

# SAIC Python client library

## About

A Python package for interacting with the MG iSmart API.

MG iSmart is the connectivity system in your MG car (MG5 MG4, ZS, MGS5, MGS6 ...).

Supported functionality (partial list, check the code for more)

* login
* set alarm switches
* get message list
* get vehicle status
* get cached vehicle status (SAIC's stored copy; does not wake the car)
* get charging status
* lock vehicle
* unlock vehicle
* start rear window heating
* stop rear window heating
* remote climate
* heated seats: each seat on its own, front and rear, plus rear seat status
* heated steering wheel
* door windows: close, ventilate or fully open


## Push notifications

The optional `saic_ismart_client_ng.push` package receives SAIC's real-time
notifications (vehicle started, charging finished, ...) instead of polling the
message queue. Install it with `pip install "mg-saic-client[push]"`.

It registers as an Android device with Google, keeps a connection to Google's
push servers open, and binds that registration to your SAIC session at login:

```python
from saic_ismart_client_ng import SaicApi
from saic_ismart_client_ng.model import SaicApiConfiguration
from saic_ismart_client_ng.push import PushMessage, SaicPushClient


async def on_push(message: PushMessage) -> None:
    if message.is_vehicle_start:
        print("Car started:", message.vin, message.sent_at)


push = SaicPushClient(on_message=on_push)
credentials = await push.start()  # registers on first use; keep `credentials` to reuse

config = SaicApiConfiguration("user@example.com", "password", push_token=credentials.token)
api = SaicApi(config)
await api.login()  # SAIC now sends pushes for this account to `push`
```

Things worth knowing:

* SAIC allows one session per account, and push follows the most recent login.
  Logging in anywhere else (including the phone app) moves pushes there. Use a
  separate account for the integration.
* A registration that has been in use for several days can stop receiving
  vehicle pushes while still looking healthy. `SaicPushClient.renew()` swaps in
  a fresh identity; log in again with the new `push_token` afterwards.
* Every message is acknowledged to Google, and `persistent_ids` can be saved
  between runs so a restart does not see old messages again. Google keeps
  undelivered messages for up to 28 days, so check `PushMessage.age_seconds()`
  before acting on one.
* A connected client does not prove pushes are arriving. Keep polling as a
  slow fallback.
* If Google refuses the login several times in a row, `credentials_rejected`
  becomes true (and `on_credentials_rejected` fires once). Call `renew()` and
  log in to SAIC again with the new token. Plain network failures do not count.

### Notification switches

The notifications in the app (Vehicle Fault, Vehicle Start, ...) are switches on
the vehicle. SAIC seems to start sending some messages, finished charging among
them, only when a switch changes from off to on, not when it is already on.
`rearm_alarm_switches` does what unticking and re-ticking in the app does:

```python
results = await api.rearm_alarm_switches(vin, pause=10)  # Fault and Start by default
for result in results:
    print(result.alarm_type, result.off_confirmed, result.on_confirmed)
```

Each type is sent as its own request, with a read-back after each change. The
switch is always turned back on, even if something fails in between.
`set_alarm_switches(types, vin, enabled=False)` turns types off directly.

## Errors

Every error is a `SaicApiException` (or a subclass). Besides the message text, it carries SAIC's reply as separate values, so you don't need to parse the text:

* `return_code`: SAIC's numeric code (see `SaicReturnCode`), or `None`
* `saic_message`: the message on its own
* checks for the common cases: `is_vehicle_unreachable` (code 4), `is_request_rejected` (code 8), `is_vehicle_not_locked`, `is_logged_out` and `is_unexpected_failure`

## Prerequisites

You have an iSmart account (can be created in the iSmart app)

## Credit

This is a maintained fork of [SAIC-iSmart-API/saic-python-client-ng](https://github.com/SAIC-iSmart-API/saic-python-client-ng) by Giovanni Condello, used by the [mg-saic-ha](https://github.com/townsmcp/mg-saic-ha) Home Assistant integration. Licensed under MIT; see [LICENSE](LICENSE).

