mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
BMW: Add reconfiguration flow (#127726)
* BMW: Add reconfiguration flow * Implement requested changes -------- Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Abort if unique_id changes, small adjustments --------- Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
parent
c3bf1dde7e
commit
54c4fb5f56
@ -13,6 +13,7 @@ import voluptuous as vol
|
|||||||
|
|
||||||
from homeassistant.config_entries import (
|
from homeassistant.config_entries import (
|
||||||
SOURCE_REAUTH,
|
SOURCE_REAUTH,
|
||||||
|
SOURCE_RECONFIGURE,
|
||||||
ConfigEntry,
|
ConfigEntry,
|
||||||
ConfigFlow,
|
ConfigFlow,
|
||||||
ConfigFlowResult,
|
ConfigFlowResult,
|
||||||
@ -20,6 +21,7 @@ from homeassistant.config_entries import (
|
|||||||
)
|
)
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_REGION, CONF_SOURCE, CONF_USERNAME
|
from homeassistant.const import CONF_PASSWORD, CONF_REGION, CONF_SOURCE, CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
from homeassistant.data_entry_flow import AbortFlow
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers.selector import SelectSelector, SelectSelectorConfig
|
from homeassistant.helpers.selector import SelectSelector, SelectSelectorConfig
|
||||||
|
|
||||||
@ -72,7 +74,8 @@ class BMWConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
|
|
||||||
_reauth_entry: ConfigEntry
|
_existing_entry_data: Mapping[str, Any] | None = None
|
||||||
|
_existing_entry_unique_id: str | None = None
|
||||||
|
|
||||||
async def async_step_user(
|
async def async_step_user(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
@ -83,9 +86,14 @@ class BMWConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
unique_id = f"{user_input[CONF_REGION]}-{user_input[CONF_USERNAME]}"
|
unique_id = f"{user_input[CONF_REGION]}-{user_input[CONF_USERNAME]}"
|
||||||
|
|
||||||
if self.source != SOURCE_REAUTH:
|
if self.source not in {SOURCE_REAUTH, SOURCE_RECONFIGURE}:
|
||||||
await self.async_set_unique_id(unique_id)
|
await self.async_set_unique_id(unique_id)
|
||||||
self._abort_if_unique_id_configured()
|
self._abort_if_unique_id_configured()
|
||||||
|
elif (
|
||||||
|
self.source in {SOURCE_REAUTH, SOURCE_RECONFIGURE}
|
||||||
|
and unique_id != self._existing_entry_unique_id
|
||||||
|
):
|
||||||
|
raise AbortFlow("account_mismatch")
|
||||||
|
|
||||||
info = None
|
info = None
|
||||||
try:
|
try:
|
||||||
@ -102,23 +110,22 @@ class BMWConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
if info:
|
if info:
|
||||||
if self.source == SOURCE_REAUTH:
|
if self.source == SOURCE_REAUTH:
|
||||||
self.hass.config_entries.async_update_entry(
|
return self.async_update_reload_and_abort(
|
||||||
self._reauth_entry, data=entry_data
|
self._get_reauth_entry(), data=entry_data
|
||||||
)
|
)
|
||||||
self.hass.async_create_task(
|
if self.source == SOURCE_RECONFIGURE:
|
||||||
self.hass.config_entries.async_reload(
|
return self.async_update_reload_and_abort(
|
||||||
self._reauth_entry.entry_id
|
self._get_reconfigure_entry(),
|
||||||
)
|
data=entry_data,
|
||||||
)
|
)
|
||||||
return self.async_abort(reason="reauth_successful")
|
|
||||||
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=info["title"],
|
title=info["title"],
|
||||||
data=entry_data,
|
data=entry_data,
|
||||||
)
|
)
|
||||||
|
|
||||||
schema = self.add_suggested_values_to_schema(
|
schema = self.add_suggested_values_to_schema(
|
||||||
DATA_SCHEMA, self._reauth_entry.data if self.source == SOURCE_REAUTH else {}
|
DATA_SCHEMA,
|
||||||
|
self._existing_entry_data,
|
||||||
)
|
)
|
||||||
|
|
||||||
return self.async_show_form(step_id="user", data_schema=schema, errors=errors)
|
return self.async_show_form(step_id="user", data_schema=schema, errors=errors)
|
||||||
@ -127,7 +134,17 @@ class BMWConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
self, entry_data: Mapping[str, Any]
|
self, entry_data: Mapping[str, Any]
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Handle configuration by re-auth."""
|
"""Handle configuration by re-auth."""
|
||||||
self._reauth_entry = self._get_reauth_entry()
|
self._existing_entry_data = entry_data
|
||||||
|
self._existing_entry_unique_id = self._get_reauth_entry().unique_id
|
||||||
|
return await self.async_step_user()
|
||||||
|
|
||||||
|
async def async_step_reconfigure(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> ConfigFlowResult:
|
||||||
|
"""Handle a reconfiguration flow initialized by the user."""
|
||||||
|
reconfigure_entry = self._get_reconfigure_entry()
|
||||||
|
self._existing_entry_data = reconfigure_entry.data
|
||||||
|
self._existing_entry_unique_id = reconfigure_entry.unique_id
|
||||||
return await self.async_step_user()
|
return await self.async_step_user()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -15,7 +15,9 @@
|
|||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
|
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
|
||||||
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
|
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
|
||||||
|
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]",
|
||||||
|
"account_mismatch": "Username and region are not allowed to change"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"options": {
|
"options": {
|
||||||
|
@ -40,7 +40,7 @@ FIXTURE_CONFIG_ENTRY = {
|
|||||||
},
|
},
|
||||||
"options": {CONF_READ_ONLY: False},
|
"options": {CONF_READ_ONLY: False},
|
||||||
"source": config_entries.SOURCE_USER,
|
"source": config_entries.SOURCE_USER,
|
||||||
"unique_id": f"{FIXTURE_USER_INPUT[CONF_REGION]}-{FIXTURE_USER_INPUT[CONF_REGION]}",
|
"unique_id": f"{FIXTURE_USER_INPUT[CONF_REGION]}-{FIXTURE_USER_INPUT[CONF_USERNAME]}",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ from homeassistant.components.bmw_connected_drive.const import (
|
|||||||
CONF_READ_ONLY,
|
CONF_READ_ONLY,
|
||||||
CONF_REFRESH_TOKEN,
|
CONF_REFRESH_TOKEN,
|
||||||
)
|
)
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import CONF_PASSWORD, CONF_REGION, CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
|
|
||||||
@ -193,6 +193,14 @@ async def test_reauth(hass: HomeAssistant) -> None:
|
|||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
|
|
||||||
|
suggested_values = {
|
||||||
|
key: key.description.get("suggested_value")
|
||||||
|
for key in result["data_schema"].schema
|
||||||
|
}
|
||||||
|
assert suggested_values[CONF_USERNAME] == FIXTURE_USER_INPUT[CONF_USERNAME]
|
||||||
|
assert suggested_values[CONF_PASSWORD] == wrong_password
|
||||||
|
assert suggested_values[CONF_REGION] == FIXTURE_USER_INPUT[CONF_REGION]
|
||||||
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"], FIXTURE_USER_INPUT
|
result["flow_id"], FIXTURE_USER_INPUT
|
||||||
)
|
)
|
||||||
@ -203,3 +211,103 @@ async def test_reauth(hass: HomeAssistant) -> None:
|
|||||||
assert config_entry.data == FIXTURE_COMPLETE_ENTRY
|
assert config_entry.data == FIXTURE_COMPLETE_ENTRY
|
||||||
|
|
||||||
assert len(mock_setup_entry.mock_calls) == 2
|
assert len(mock_setup_entry.mock_calls) == 2
|
||||||
|
|
||||||
|
|
||||||
|
async def test_reauth_unique_id_abort(hass: HomeAssistant) -> None:
|
||||||
|
"""Test aborting the reauth form if unique_id changes."""
|
||||||
|
with patch(
|
||||||
|
"bimmer_connected.api.authentication.MyBMWAuthentication.login",
|
||||||
|
side_effect=login_sideeffect,
|
||||||
|
autospec=True,
|
||||||
|
):
|
||||||
|
wrong_password = "wrong"
|
||||||
|
|
||||||
|
config_entry_with_wrong_password = deepcopy(FIXTURE_CONFIG_ENTRY)
|
||||||
|
config_entry_with_wrong_password["data"][CONF_PASSWORD] = wrong_password
|
||||||
|
|
||||||
|
config_entry = MockConfigEntry(**config_entry_with_wrong_password)
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert config_entry.data == config_entry_with_wrong_password["data"]
|
||||||
|
|
||||||
|
result = await config_entry.start_reauth_flow(hass)
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "user"
|
||||||
|
assert result["errors"] == {}
|
||||||
|
|
||||||
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"], {**FIXTURE_USER_INPUT, CONF_REGION: "north_america"}
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert result2["type"] is FlowResultType.ABORT
|
||||||
|
assert result2["reason"] == "account_mismatch"
|
||||||
|
assert config_entry.data == config_entry_with_wrong_password["data"]
|
||||||
|
|
||||||
|
|
||||||
|
async def test_reconfigure(hass: HomeAssistant) -> None:
|
||||||
|
"""Test the reconfiguration form."""
|
||||||
|
with patch(
|
||||||
|
"bimmer_connected.api.authentication.MyBMWAuthentication.login",
|
||||||
|
side_effect=login_sideeffect,
|
||||||
|
autospec=True,
|
||||||
|
):
|
||||||
|
config_entry = MockConfigEntry(**FIXTURE_CONFIG_ENTRY)
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
result = await config_entry.start_reconfigure_flow(hass)
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "user"
|
||||||
|
assert result["errors"] == {}
|
||||||
|
|
||||||
|
suggested_values = {
|
||||||
|
key: key.description.get("suggested_value")
|
||||||
|
for key in result["data_schema"].schema
|
||||||
|
}
|
||||||
|
assert suggested_values[CONF_USERNAME] == FIXTURE_USER_INPUT[CONF_USERNAME]
|
||||||
|
assert suggested_values[CONF_PASSWORD] == FIXTURE_USER_INPUT[CONF_PASSWORD]
|
||||||
|
assert suggested_values[CONF_REGION] == FIXTURE_USER_INPUT[CONF_REGION]
|
||||||
|
|
||||||
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"], FIXTURE_USER_INPUT
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert result2["type"] is FlowResultType.ABORT
|
||||||
|
assert result2["reason"] == "reconfigure_successful"
|
||||||
|
assert config_entry.data == FIXTURE_COMPLETE_ENTRY
|
||||||
|
|
||||||
|
|
||||||
|
async def test_reconfigure_unique_id_abort(hass: HomeAssistant) -> None:
|
||||||
|
"""Test aborting the reconfiguration form if unique_id changes."""
|
||||||
|
with patch(
|
||||||
|
"bimmer_connected.api.authentication.MyBMWAuthentication.login",
|
||||||
|
side_effect=login_sideeffect,
|
||||||
|
autospec=True,
|
||||||
|
):
|
||||||
|
config_entry = MockConfigEntry(**FIXTURE_CONFIG_ENTRY)
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
result = await config_entry.start_reconfigure_flow(hass)
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "user"
|
||||||
|
assert result["errors"] == {}
|
||||||
|
|
||||||
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
{**FIXTURE_USER_INPUT, CONF_USERNAME: "somebody@email.com"},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert result2["type"] is FlowResultType.ABORT
|
||||||
|
assert result2["reason"] == "account_mismatch"
|
||||||
|
assert config_entry.data == FIXTURE_COMPLETE_ENTRY
|
||||||
|
Loading…
x
Reference in New Issue
Block a user