mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Avoid duplicate entity names in proximity (#109413)
* avoid duplicate config entry title * consecutive range 2..10 * use existing logic
This commit is contained in:
parent
6e5a085413
commit
17f1aa644b
@ -18,6 +18,7 @@ from homeassistant.helpers.selector import (
|
|||||||
NumberSelector,
|
NumberSelector,
|
||||||
NumberSelectorConfig,
|
NumberSelectorConfig,
|
||||||
)
|
)
|
||||||
|
from homeassistant.util import slugify
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_IGNORED_ZONES,
|
CONF_IGNORED_ZONES,
|
||||||
@ -89,11 +90,19 @@ class ProximityConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
self._async_abort_entries_match(user_input)
|
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(
|
slugified_existing_entry_titles = [
|
||||||
title=cast(State, zone).name, data=user_input
|
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(
|
return self.async_show_form(
|
||||||
step_id="user",
|
step_id="user",
|
||||||
|
@ -185,3 +185,67 @@ async def test_abort_duplicated_entry(hass: HomeAssistant) -> None:
|
|||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
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()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user