From 802f0da493cc3c59f0240ad6d80cc4dd3f561b98 Mon Sep 17 00:00:00 2001 From: Cyrill Raccaud Date: Wed, 17 Jan 2024 19:08:33 +0100 Subject: [PATCH] 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 --- .../swiss_public_transport/config_flow.py | 22 ++++-------- .../test_config_flow.py | 34 ++++++++++--------- 2 files changed, 24 insertions(+), 32 deletions(-) diff --git a/homeassistant/components/swiss_public_transport/config_flow.py b/homeassistant/components/swiss_public_transport/config_flow.py index ceb6f46806d..e864f31cd6c 100644 --- a/homeassistant/components/swiss_public_transport/config_flow.py +++ b/homeassistant/components/swiss_public_transport/config_flow.py @@ -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, diff --git a/tests/components/swiss_public_transport/test_config_flow.py b/tests/components/swiss_public_transport/test_config_flow.py index 55ad51c45c4..5870f6f0555 100644 --- a/tests/components/swiss_public_transport/test_config_flow.py +++ b/tests/components/swiss_public_transport/test_config_flow.py @@ -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)