Switch for swiss_public_transport to unique_id instead of unique_entry (#107910)

* use unique_id instead of unique_entry

* move entry mock out of patch context
pull/108252/head^2
Cyrill Raccaud 2024-01-17 19:08:33 +01:00 committed by GitHub
parent 3d410a1d6e
commit 802f0da493
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 32 deletions

View File

@ -39,12 +39,10 @@ class SwissPublicTransportConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Async user step to set up the connection."""
errors: dict[str, str] = {}
if user_input is not None:
self._async_abort_entries_match(
{
CONF_START: user_input[CONF_START],
CONF_DESTINATION: user_input[CONF_DESTINATION],
}
await self.async_set_unique_id(
f"{user_input[CONF_START]} {user_input[CONF_DESTINATION]}"
)
self._abort_if_unique_id_configured()
session = async_get_clientsession(self.hass)
opendata = OpendataTransport(
@ -60,9 +58,6 @@ class SwissPublicTransportConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
_LOGGER.exception("Unknown error")
errors["base"] = "unknown"
else:
await self.async_set_unique_id(
f"{user_input[CONF_START]} {user_input[CONF_DESTINATION]}"
)
return self.async_create_entry(
title=f"{user_input[CONF_START]} {user_input[CONF_DESTINATION]}",
data=user_input,
@ -77,12 +72,10 @@ class SwissPublicTransportConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
async def async_step_import(self, import_input: dict[str, Any]) -> FlowResult:
"""Async import step to set up the connection."""
self._async_abort_entries_match(
{
CONF_START: import_input[CONF_START],
CONF_DESTINATION: import_input[CONF_DESTINATION],
}
await self.async_set_unique_id(
f"{import_input[CONF_START]} {import_input[CONF_DESTINATION]}"
)
self._abort_if_unique_id_configured()
session = async_get_clientsession(self.hass)
opendata = OpendataTransport(
@ -102,9 +95,6 @@ class SwissPublicTransportConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
)
return self.async_abort(reason="unknown")
await self.async_set_unique_id(
f"{import_input[CONF_START]} {import_input[CONF_DESTINATION]}"
)
return self.async_create_entry(
title=import_input[CONF_NAME],
data=import_input,

View File

@ -65,7 +65,7 @@ async def test_flow_user_init_data_success(hass: HomeAssistant) -> None:
(IndexError(), "unknown"),
],
)
async def test_flow_user_init_data_unknown_error_and_recover(
async def test_flow_user_init_data_error_and_recover(
hass: HomeAssistant, raise_error, text_error
) -> None:
"""Test unknown errors."""
@ -88,9 +88,6 @@ async def test_flow_user_init_data_unknown_error_and_recover(
# Recover
mock_OpendataTransport.side_effect = None
mock_OpendataTransport.return_value = True
result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN, context={"source": "user"}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input=MOCK_DATA_STEP,
@ -108,20 +105,26 @@ async def test_flow_user_init_data_already_configured(hass: HomeAssistant) -> No
entry = MockConfigEntry(
domain=config_flow.DOMAIN,
data=MOCK_DATA_STEP,
unique_id=f"{MOCK_DATA_STEP[CONF_START]} {MOCK_DATA_STEP[CONF_DESTINATION]}",
)
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN, context={"source": "user"}
)
with patch(
"homeassistant.components.swiss_public_transport.config_flow.OpendataTransport.async_get_data",
autospec=True,
return_value=True,
):
result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN, context={"source": "user"}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input=MOCK_DATA_STEP,
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input=MOCK_DATA_STEP,
)
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "already_configured"
MOCK_DATA_IMPORT = {
@ -161,9 +164,7 @@ async def test_import(
(IndexError(), "unknown"),
],
)
async def test_import_cannot_connect_error(
hass: HomeAssistant, raise_error, text_error
) -> None:
async def test_import_error(hass: HomeAssistant, raise_error, text_error) -> None:
"""Test import flow cannot_connect error."""
with patch(
"homeassistant.components.swiss_public_transport.config_flow.OpendataTransport.async_get_data",
@ -187,6 +188,7 @@ async def test_import_already_configured(hass: HomeAssistant) -> None:
entry = MockConfigEntry(
domain=config_flow.DOMAIN,
data=MOCK_DATA_IMPORT,
unique_id=f"{MOCK_DATA_IMPORT[CONF_START]} {MOCK_DATA_IMPORT[CONF_DESTINATION]}",
)
entry.add_to_hass(hass)