diff --git a/homeassistant/components/proximity/config_flow.py b/homeassistant/components/proximity/config_flow.py index 231a50c6c00..f3306bebf39 100644 --- a/homeassistant/components/proximity/config_flow.py +++ b/homeassistant/components/proximity/config_flow.py @@ -18,6 +18,7 @@ from homeassistant.helpers.selector import ( NumberSelector, NumberSelectorConfig, ) +from homeassistant.util import slugify from .const import ( CONF_IGNORED_ZONES, @@ -89,11 +90,19 @@ class ProximityConfigFlow(ConfigFlow, domain=DOMAIN): if user_input is not None: self._async_abort_entries_match(user_input) - zone = self.hass.states.get(user_input[CONF_ZONE]) + title = cast(State, self.hass.states.get(user_input[CONF_ZONE])).name - return self.async_create_entry( - title=cast(State, zone).name, data=user_input - ) + slugified_existing_entry_titles = [ + slugify(e.title) for e in self._async_current_entries() + ] + + possible_title = title + tries = 1 + while slugify(possible_title) in slugified_existing_entry_titles: + tries += 1 + possible_title = f"{title} {tries}" + + return self.async_create_entry(title=possible_title, data=user_input) return self.async_show_form( step_id="user", diff --git a/tests/components/proximity/test_config_flow.py b/tests/components/proximity/test_config_flow.py index 92b924be1ce..3c94e941227 100644 --- a/tests/components/proximity/test_config_flow.py +++ b/tests/components/proximity/test_config_flow.py @@ -185,3 +185,67 @@ async def test_abort_duplicated_entry(hass: HomeAssistant) -> None: assert result["reason"] == "already_configured" await hass.async_block_till_done() + + +async def test_avoid_duplicated_title(hass: HomeAssistant) -> None: + """Test if we avoid duplicate titles.""" + MockConfigEntry( + domain=DOMAIN, + title="home", + data={ + CONF_ZONE: "zone.home", + CONF_TRACKED_ENTITIES: ["device_tracker.test1"], + CONF_IGNORED_ZONES: ["zone.work"], + CONF_TOLERANCE: 10, + }, + unique_id=f"{DOMAIN}_home", + ).add_to_hass(hass) + + MockConfigEntry( + domain=DOMAIN, + title="home 3", + data={ + CONF_ZONE: "zone.home", + CONF_TRACKED_ENTITIES: ["device_tracker.test2"], + CONF_IGNORED_ZONES: ["zone.work"], + CONF_TOLERANCE: 10, + }, + unique_id=f"{DOMAIN}_home", + ).add_to_hass(hass) + + with patch( + "homeassistant.components.proximity.async_setup_entry", return_value=True + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_ZONE: "zone.home", + CONF_TRACKED_ENTITIES: ["device_tracker.test3"], + CONF_IGNORED_ZONES: [], + CONF_TOLERANCE: 10, + }, + ) + assert result["type"] == FlowResultType.CREATE_ENTRY + assert result["title"] == "home 2" + + await hass.async_block_till_done() + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_ZONE: "zone.home", + CONF_TRACKED_ENTITIES: ["device_tracker.test4"], + CONF_IGNORED_ZONES: [], + CONF_TOLERANCE: 10, + }, + ) + assert result["type"] == FlowResultType.CREATE_ENTRY + assert result["title"] == "home 4" + + await hass.async_block_till_done()