Reuse title of deleted enphase_envoy config entry if present (#133611)

This commit is contained in:
Arie Catsman 2024-12-21 15:49:24 +01:00 committed by GitHub
parent ef31413a59
commit cc134c820b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 88 additions and 4 deletions

View File

@ -141,9 +141,13 @@ class EnphaseConfigFlow(ConfigFlow, domain=DOMAIN):
and entry.data[CONF_HOST] == self.ip_address
):
_LOGGER.debug(
"Zeroconf update envoy with this ip and blank serial in unique_id",
"Zeroconf update envoy with this ip and blank unique_id",
)
title = f"{ENVOY} {serial}" if entry.title == ENVOY else ENVOY
# Found an entry with blank unique_id (prior deleted) with same ip
# If the title is still default shorthand 'Envoy' then append serial
# to differentiate multiple Envoy. Don't change the title if any other
# title is still present in the old entry.
title = f"{ENVOY} {serial}" if entry.title == ENVOY else entry.title
return self.async_update_reload_and_abort(
entry, title=title, unique_id=serial, reason="already_configured"
)

View File

@ -18,8 +18,6 @@ rules:
config-flow:
status: todo
comment: |
- async_step_zeroconf -> a config entry title is considered userland,
so if someone renamed their entry, it will be reverted back with the code at L146.
- async_step_reaut L160: I believe that the unique is already set when starting a reauth flow
- The config flow is missing data descriptions for the other fields
dependency-transparency: done

View File

@ -631,6 +631,88 @@ async def test_zero_conf_old_blank_entry(
assert entry.title == "Envoy 1234"
async def test_zero_conf_old_blank_entry_standard_title(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_envoy: AsyncMock,
) -> None:
"""Test re-using old blank entry was Envoy as title."""
entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_HOST: "1.1.1.1",
CONF_USERNAME: "",
CONF_PASSWORD: "",
CONF_NAME: "unknown",
},
unique_id=None,
title="Envoy",
)
entry.add_to_hass(hass)
# test if shorthand title Envoy gets serial appended
hass.config_entries.async_update_entry(entry, title="Envoy")
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_ZEROCONF},
data=zeroconf.ZeroconfServiceInfo(
ip_address=ip_address("1.1.1.1"),
ip_addresses=[ip_address("1.1.1.1"), ip_address("1.1.1.2")],
hostname="mock_hostname",
name="mock_name",
port=None,
properties={"serialnum": "1234", "protovers": "7.1.2"},
type="mock_type",
),
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert entry.data[CONF_HOST] == "1.1.1.1"
assert entry.unique_id == "1234"
assert entry.title == "Envoy 1234"
async def test_zero_conf_old_blank_entry_user_title(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_envoy: AsyncMock,
) -> None:
"""Test re-using old blank entry with user title."""
entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_HOST: "1.1.1.1",
CONF_USERNAME: "",
CONF_PASSWORD: "",
CONF_NAME: "unknown",
},
unique_id=None,
title="Envoy",
)
entry.add_to_hass(hass)
# set user title on entry
hass.config_entries.async_update_entry(entry, title="Envoy Backyard")
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_ZEROCONF},
data=zeroconf.ZeroconfServiceInfo(
ip_address=ip_address("1.1.1.1"),
ip_addresses=[ip_address("1.1.1.1"), ip_address("1.1.1.2")],
hostname="mock_hostname",
name="mock_name",
port=None,
properties={"serialnum": "1234", "protovers": "7.1.2"},
type="mock_type",
),
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert entry.data[CONF_HOST] == "1.1.1.1"
assert entry.unique_id == "1234"
assert entry.title == "Envoy Backyard"
async def test_reauth(
hass: HomeAssistant,
config_entry: MockConfigEntry,