Skip to content

Commit d9d4cc9

Browse files
jpbedeedenhaus
andauthored
Use a config entry migration instead of migrating in async_setup in Ping (#155403)
Co-authored-by: Robert Resch <robert@resch.dev>
1 parent 67baa2c commit d9d4cc9

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

homeassistant/components/ping/__init__.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@
2323
DATA_PRIVILEGED_KEY: HassKey[bool | None] = HassKey(DOMAIN)
2424

2525

26+
async def async_migrate_entry(hass: HomeAssistant, entry: PingConfigEntry) -> bool:
27+
"""Migrate old config entries."""
28+
if entry.version == 1 and entry.minor_version == 1:
29+
_LOGGER.debug("Migrating to minor version 2")
30+
31+
# Migrate device registry identifiers from homeassistant domain to ping domain
32+
registry = dr.async_get(hass)
33+
if (
34+
device := registry.async_get_device(
35+
identifiers={(HOMEASSISTANT_DOMAIN, entry.entry_id)}
36+
)
37+
) is not None and entry.entry_id in device.config_entries:
38+
registry.async_update_device(
39+
device_id=device.id,
40+
new_identifiers={(DOMAIN, entry.entry_id)},
41+
)
42+
43+
hass.config_entries.async_update_entry(entry, minor_version=2)
44+
45+
return True
46+
47+
2648
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
2749
"""Set up the ping integration."""
2850
hass.data[DATA_PRIVILEGED_KEY] = await _can_use_icmp_lib_with_privilege()
@@ -32,19 +54,6 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
3254

3355
async def async_setup_entry(hass: HomeAssistant, entry: PingConfigEntry) -> bool:
3456
"""Set up Ping (ICMP) from a config entry."""
35-
36-
# Migrate device registry identifiers from homeassistant domain to ping domain
37-
registry = dr.async_get(hass)
38-
if (
39-
device := registry.async_get_device(
40-
identifiers={(HOMEASSISTANT_DOMAIN, entry.entry_id)}
41-
)
42-
) is not None and entry.entry_id in device.config_entries:
43-
registry.async_update_device(
44-
device_id=device.id,
45-
new_identifiers={(DOMAIN, entry.entry_id)},
46-
)
47-
4857
privileged = hass.data[DATA_PRIVILEGED_KEY]
4958

5059
host: str = entry.options[CONF_HOST]

homeassistant/components/ping/config_flow.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class PingConfigFlow(ConfigFlow, domain=DOMAIN):
3737
"""Handle a config flow for Ping."""
3838

3939
VERSION = 1
40+
MINOR_VERSION = 2
4041

4142
async def async_step_user(
4243
self, user_input: dict[str, Any] | None = None

tests/components/ping/test_init.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ async def test_device_migration(
3333
identifiers={(HOMEASSISTANT_DOMAIN, config_entry.entry_id)},
3434
)
3535

36+
assert config_entry.minor_version == 1
37+
3638
await hass.config_entries.async_setup(config_entry.entry_id)
3739
await hass.async_block_till_done()
3840

@@ -44,3 +46,4 @@ async def test_device_migration(
4446
assert device is not None
4547
assert device.id == old_device.id
4648
assert device.config_entries == {config_entry.entry_id}
49+
assert config_entry.minor_version == 2

0 commit comments

Comments
 (0)