bimmer_connected.api

The bimmer_connected.api module contains helper functions to communicate with the BMW APIs.

bimmer_connected.api.authentication

Authentication management for BMW APIs.

class bimmer_connected.api.authentication.MyBMWAuthentication(username: str, password: str, region: Regions, access_token: str | None = None, expires_at: datetime | None = None, refresh_token: str | None = None, gcid: str | None = None, verify: str | bool | SSLContext = True)[source]

Authentication and Retry Handler for MyBMW API.

async async_auth_flow(request: Request) AsyncGenerator[Request, Response][source]

Execute the authentication flow asynchronously.

By default, this defers to .auth_flow(). You should override this method when the authentication scheme does I/O and/or uses concurrency primitives.

async login() None[source]

Get a valid OAuth token.

property login_lock: Lock

Make sure that there is a lock in the current event loop.

sync_auth_flow(request: Request) Generator[Request, Response, None][source]

Execute the authentication flow synchronously.

By default, this defers to .auth_flow(). You should override this method when the authentication scheme does I/O and/or uses concurrency primitives.

class bimmer_connected.api.authentication.MyBMWLoginClient(*args, **kwargs)[source]

Async HTTP client based on httpx.AsyncClient with automated OAuth token refresh.

class bimmer_connected.api.authentication.MyBMWLoginRetry[source]

httpx.Auth used as workaround to retry & sleep on 429 Too Many Requests.

async async_auth_flow(request: Request) AsyncGenerator[Request, Response][source]

Execute the authentication flow asynchronously.

By default, this defers to .auth_flow(). You should override this method when the authentication scheme does I/O and/or uses concurrency primitives.

sync_auth_flow(request: Request) Generator[Request, Response, None][source]

Execute the authentication flow synchronously.

By default, this defers to .auth_flow(). You should override this method when the authentication scheme does I/O and/or uses concurrency primitives.

bimmer_connected.api.authentication.get_retry_wait_time(response: Response) int[source]

Get the wait time for the next retry from the response and multiply by 2.

bimmer_connected.api.client

Generic API management.

class bimmer_connected.api.client.MyBMWClient(config: MyBMWClientConfiguration, *args, brand: CarBrands | None = None, **kwargs)[source]

Async HTTP client based on httpx.AsyncClient with automated OAuth token refresh.

generate_default_header(brand: CarBrands | None = None) Dict[str, str][source]

Generate a header for HTTP requests to the server.

class bimmer_connected.api.client.MyBMWClientConfiguration(authentication: MyBMWAuthentication, log_responses: bool | None = False, observer_position: GPSPosition | None = None, verify: str | bool | SSLContext = True)[source]

Stores global settings for MyBMWClient.

authentication: MyBMWAuthentication
log_responses: bool | None = False
observer_position: GPSPosition | None = None
set_log_responses(log_responses: bool) None[source]

Set if responses are logged and clear response store.

verify: str | bool | SSLContext = True

bimmer_connected.api.regions

Get the right url for the different countries.

bimmer_connected.api.regions.get_app_version(region: Regions) str[source]

Get the app version & build number for the region.

bimmer_connected.api.regions.get_ocp_apim_key(region: Regions) str[source]

Get the authorization for OAuth settings.

bimmer_connected.api.regions.get_region_from_name(name: str) Regions[source]

Get a region for a string.

This function is not case-sensitive.

bimmer_connected.api.regions.get_server_url(region: Regions) str[source]

Get the url of the server for the region.

bimmer_connected.api.regions.get_user_agent(region: Regions) str[source]

Get the Dart user agent for the region.

bimmer_connected.api.regions.valid_regions() List[str][source]

Get list of valid regions as strings.

bimmer_connected.api.utils

Utils for bimmer_connected.api.

bimmer_connected.api.utils.anonymize_data(json_data: List | Dict) List | Dict[source]

Replace parts of the logfiles containing personal information.

bimmer_connected.api.utils.anonymize_response(response: Response) AnonymizedResponse[source]

Anonymize a responses URL and content.

bimmer_connected.api.utils.anonymize_vin(match: Match)[source]

Anonymize VINs but keep assignment.

bimmer_connected.api.utils.create_s256_code_challenge(code_verifier: str) str[source]

Create S256 code_challenge with the given code_verifier.

bimmer_connected.api.utils.generate_cn_nonce(username: str) str[source]

Generate a x-login-nonce string.

bimmer_connected.api.utils.generate_random_base64_string(size: int) str[source]

Generate a random base64 string with size.

bimmer_connected.api.utils.generate_token(length: int = 30, chars: str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~') str[source]

Generate a random token with given length and characters.

bimmer_connected.api.utils.get_capture_position(base64_background_img: str) str[source]

Get the position of the capture in the background image.

bimmer_connected.api.utils.get_correlation_id() Dict[str, str][source]

Generate corrlation headers.

async bimmer_connected.api.utils.handle_httpstatuserror(ex: HTTPStatusError, module: str = 'API', log_handler: Logger | None = None, dont_raise: bool = False) None[source]

Try to extract information from response and re-raise Exception.

bimmer_connected.api.utils.try_import_pillow_image()[source]

Try to import PIL.Image and return if successful.

We only need to load PIL if we are in China, so we try to avoid a general dependency on Pillow for all users. Installing Pillow on Raspberry Pi (ARMv7) is painful.