Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch for swiss_public_transport to unique_id instead of unique_entry #107910

Merged
merged 2 commits into from Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 6 additions & 16 deletions homeassistant/components/swiss_public_transport/config_flow.py
Expand Up @@ -39,12 +39,10 @@ async def async_step_user(
"""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(
Expand All @@ -60,9 +58,6 @@ async def async_step_user(
_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,
Expand All @@ -77,12 +72,10 @@ async def async_step_user(

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(
Expand All @@ -102,9 +95,6 @@ async def async_step_import(self, import_input: dict[str, Any]) -> FlowResult:
)
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,
Expand Down
34 changes: 18 additions & 16 deletions tests/components/swiss_public_transport/test_config_flow.py
Expand Up @@ -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."""
Expand All @@ -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,
Expand All @@ -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 = {
Expand Down Expand Up @@ -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",
Expand All @@ -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)

Expand Down