From defc23cc7c5144f9653f68f2c3c0b7dc7ec74eb0 Mon Sep 17 00:00:00 2001 From: rikroe <42204099+rikroe@users.noreply.github.com> Date: Wed, 17 May 2023 02:03:01 +0200 Subject: [PATCH] Fix china login for bmw_connected_drive (#93180) * Bump bimmer_connected to 0.13.5 * Fix snapshots after dependency bump * Load gcid from config entry if available * Add tests --------- Co-authored-by: rikroe --- .../components/bmw_connected_drive/config_flow.py | 5 ++++- homeassistant/components/bmw_connected_drive/const.py | 1 + .../components/bmw_connected_drive/coordinator.py | 7 +++++-- .../components/bmw_connected_drive/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/bmw_connected_drive/__init__.py | 3 +++ .../bmw_connected_drive/snapshots/test_diagnostics.ambr | 3 +++ tests/components/bmw_connected_drive/test_config_flow.py | 8 +++++++- 9 files changed, 26 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/bmw_connected_drive/config_flow.py b/homeassistant/components/bmw_connected_drive/config_flow.py index eb58a6c7f13..926706397a6 100644 --- a/homeassistant/components/bmw_connected_drive/config_flow.py +++ b/homeassistant/components/bmw_connected_drive/config_flow.py @@ -16,7 +16,7 @@ from homeassistant.core import callback from homeassistant.data_entry_flow import FlowResult from . import DOMAIN -from .const import CONF_ALLOWED_REGIONS, CONF_READ_ONLY, CONF_REFRESH_TOKEN +from .const import CONF_ALLOWED_REGIONS, CONF_GCID, CONF_READ_ONLY, CONF_REFRESH_TOKEN DATA_SCHEMA = vol.Schema( { @@ -51,6 +51,8 @@ async def validate_input( retval = {"title": f"{data[CONF_USERNAME]}{data.get(CONF_SOURCE, '')}"} if auth.refresh_token: retval[CONF_REFRESH_TOKEN] = auth.refresh_token + if auth.gcid: + retval[CONF_GCID] = auth.gcid return retval @@ -80,6 +82,7 @@ class BMWConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): entry_data = { **user_input, CONF_REFRESH_TOKEN: info.get(CONF_REFRESH_TOKEN), + CONF_GCID: info.get(CONF_GCID), } except CannotConnect: errors["base"] = "cannot_connect" diff --git a/homeassistant/components/bmw_connected_drive/const.py b/homeassistant/components/bmw_connected_drive/const.py index 50634ebdb96..37225fc052f 100644 --- a/homeassistant/components/bmw_connected_drive/const.py +++ b/homeassistant/components/bmw_connected_drive/const.py @@ -11,6 +11,7 @@ CONF_ALLOWED_REGIONS = ["china", "north_america", "rest_of_world"] CONF_READ_ONLY = "read_only" CONF_ACCOUNT = "account" CONF_REFRESH_TOKEN = "refresh_token" +CONF_GCID = "gcid" DATA_HASS_CONFIG = "hass_config" diff --git a/homeassistant/components/bmw_connected_drive/coordinator.py b/homeassistant/components/bmw_connected_drive/coordinator.py index 9c3e15a808b..f6354422312 100644 --- a/homeassistant/components/bmw_connected_drive/coordinator.py +++ b/homeassistant/components/bmw_connected_drive/coordinator.py @@ -15,7 +15,7 @@ from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed -from .const import CONF_READ_ONLY, CONF_REFRESH_TOKEN, DOMAIN +from .const import CONF_GCID, CONF_READ_ONLY, CONF_REFRESH_TOKEN, DOMAIN DEFAULT_SCAN_INTERVAL_SECONDS = 300 SCAN_INTERVAL = timedelta(seconds=DEFAULT_SCAN_INTERVAL_SECONDS) @@ -41,7 +41,10 @@ class BMWDataUpdateCoordinator(DataUpdateCoordinator[None]): self._entry = entry if CONF_REFRESH_TOKEN in entry.data: - self.account.set_refresh_token(entry.data[CONF_REFRESH_TOKEN]) + self.account.set_refresh_token( + refresh_token=entry.data[CONF_REFRESH_TOKEN], + gcid=entry.data.get(CONF_GCID), + ) super().__init__( hass, diff --git a/homeassistant/components/bmw_connected_drive/manifest.json b/homeassistant/components/bmw_connected_drive/manifest.json index afabcbd3df4..c600a1529a9 100644 --- a/homeassistant/components/bmw_connected_drive/manifest.json +++ b/homeassistant/components/bmw_connected_drive/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/bmw_connected_drive", "iot_class": "cloud_polling", "loggers": ["bimmer_connected"], - "requirements": ["bimmer_connected==0.13.3"] + "requirements": ["bimmer_connected==0.13.5"] } diff --git a/requirements_all.txt b/requirements_all.txt index 004b3ef981f..ce8f8ae767a 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -431,7 +431,7 @@ beautifulsoup4==4.11.1 bellows==0.35.5 # homeassistant.components.bmw_connected_drive -bimmer_connected==0.13.3 +bimmer_connected==0.13.5 # homeassistant.components.bizkaibus bizkaibus==0.1.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 07e64b2de3d..fe1e3107c12 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -364,7 +364,7 @@ beautifulsoup4==4.11.1 bellows==0.35.5 # homeassistant.components.bmw_connected_drive -bimmer_connected==0.13.3 +bimmer_connected==0.13.5 # homeassistant.components.bluetooth bleak-retry-connector==3.0.2 diff --git a/tests/components/bmw_connected_drive/__init__.py b/tests/components/bmw_connected_drive/__init__.py index 12957db5cac..b1f1db305b8 100644 --- a/tests/components/bmw_connected_drive/__init__.py +++ b/tests/components/bmw_connected_drive/__init__.py @@ -13,6 +13,7 @@ import respx from homeassistant import config_entries from homeassistant.components.bmw_connected_drive.const import ( + CONF_GCID, CONF_READ_ONLY, CONF_REFRESH_TOKEN, DOMAIN as BMW_DOMAIN, @@ -33,6 +34,7 @@ FIXTURE_USER_INPUT = { CONF_REGION: "rest_of_world", } FIXTURE_REFRESH_TOKEN = "SOME_REFRESH_TOKEN" +FIXTURE_GCID = "SOME_GCID" FIXTURE_CONFIG_ENTRY = { "entry_id": "1", @@ -43,6 +45,7 @@ FIXTURE_CONFIG_ENTRY = { CONF_PASSWORD: FIXTURE_USER_INPUT[CONF_PASSWORD], CONF_REGION: FIXTURE_USER_INPUT[CONF_REGION], CONF_REFRESH_TOKEN: FIXTURE_REFRESH_TOKEN, + CONF_GCID: FIXTURE_GCID, }, "options": {CONF_READ_ONLY: False}, "source": config_entries.SOURCE_USER, diff --git a/tests/components/bmw_connected_drive/snapshots/test_diagnostics.ambr b/tests/components/bmw_connected_drive/snapshots/test_diagnostics.ambr index 7ee3f625911..f5966afb32e 100644 --- a/tests/components/bmw_connected_drive/snapshots/test_diagnostics.ambr +++ b/tests/components/bmw_connected_drive/snapshots/test_diagnostics.ambr @@ -2357,6 +2357,7 @@ }), ]), 'info': dict({ + 'gcid': 'SOME_GCID', 'password': '**REDACTED**', 'refresh_token': '**REDACTED**', 'region': 'rest_of_world', @@ -3860,6 +3861,7 @@ }), ]), 'info': dict({ + 'gcid': 'SOME_GCID', 'password': '**REDACTED**', 'refresh_token': '**REDACTED**', 'region': 'rest_of_world', @@ -4692,6 +4694,7 @@ }), ]), 'info': dict({ + 'gcid': 'SOME_GCID', 'password': '**REDACTED**', 'refresh_token': '**REDACTED**', 'region': 'rest_of_world', diff --git a/tests/components/bmw_connected_drive/test_config_flow.py b/tests/components/bmw_connected_drive/test_config_flow.py index e471a61d027..3540df851e9 100644 --- a/tests/components/bmw_connected_drive/test_config_flow.py +++ b/tests/components/bmw_connected_drive/test_config_flow.py @@ -15,7 +15,12 @@ from homeassistant.components.bmw_connected_drive.const import ( from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant -from . import FIXTURE_CONFIG_ENTRY, FIXTURE_REFRESH_TOKEN, FIXTURE_USER_INPUT +from . import ( + FIXTURE_CONFIG_ENTRY, + FIXTURE_GCID, + FIXTURE_REFRESH_TOKEN, + FIXTURE_USER_INPUT, +) from tests.common import MockConfigEntry @@ -26,6 +31,7 @@ FIXTURE_IMPORT_ENTRY = {**FIXTURE_USER_INPUT, CONF_REFRESH_TOKEN: None} def login_sideeffect(self: MyBMWAuthentication): """Mock logging in and setting a refresh token.""" self.refresh_token = FIXTURE_REFRESH_TOKEN + self.gcid = FIXTURE_GCID async def test_show_form(hass: HomeAssistant) -> None: