From d9d4cc90046aabd8d331e25979e6f4c1f1e820d2 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Wed, 29 Oct 2025 11:19:23 +0100 Subject: [PATCH] Use a config entry migration instead of migrating in async_setup in Ping (#155403) Co-authored-by: Robert Resch --- homeassistant/components/ping/__init__.py | 35 ++++++++++++-------- homeassistant/components/ping/config_flow.py | 1 + tests/components/ping/test_init.py | 3 ++ 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/ping/__init__.py b/homeassistant/components/ping/__init__.py index f2a8105577a..1383e4c035a 100644 --- a/homeassistant/components/ping/__init__.py +++ b/homeassistant/components/ping/__init__.py @@ -23,6 +23,28 @@ PLATFORMS = [Platform.BINARY_SENSOR, Platform.DEVICE_TRACKER, Platform.SENSOR] DATA_PRIVILEGED_KEY: HassKey[bool | None] = HassKey(DOMAIN) +async def async_migrate_entry(hass: HomeAssistant, entry: PingConfigEntry) -> bool: + """Migrate old config entries.""" + if entry.version == 1 and entry.minor_version == 1: + _LOGGER.debug("Migrating to minor version 2") + + # Migrate device registry identifiers from homeassistant domain to ping domain + registry = dr.async_get(hass) + if ( + device := registry.async_get_device( + identifiers={(HOMEASSISTANT_DOMAIN, entry.entry_id)} + ) + ) is not None and entry.entry_id in device.config_entries: + registry.async_update_device( + device_id=device.id, + new_identifiers={(DOMAIN, entry.entry_id)}, + ) + + hass.config_entries.async_update_entry(entry, minor_version=2) + + return True + + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the ping integration.""" 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: async def async_setup_entry(hass: HomeAssistant, entry: PingConfigEntry) -> bool: """Set up Ping (ICMP) from a config entry.""" - - # Migrate device registry identifiers from homeassistant domain to ping domain - registry = dr.async_get(hass) - if ( - device := registry.async_get_device( - identifiers={(HOMEASSISTANT_DOMAIN, entry.entry_id)} - ) - ) is not None and entry.entry_id in device.config_entries: - registry.async_update_device( - device_id=device.id, - new_identifiers={(DOMAIN, entry.entry_id)}, - ) - privileged = hass.data[DATA_PRIVILEGED_KEY] host: str = entry.options[CONF_HOST] diff --git a/homeassistant/components/ping/config_flow.py b/homeassistant/components/ping/config_flow.py index d66f4beb8e5..b496d6ac4b5 100644 --- a/homeassistant/components/ping/config_flow.py +++ b/homeassistant/components/ping/config_flow.py @@ -37,6 +37,7 @@ class PingConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a config flow for Ping.""" VERSION = 1 + MINOR_VERSION = 2 async def async_step_user( self, user_input: dict[str, Any] | None = None diff --git a/tests/components/ping/test_init.py b/tests/components/ping/test_init.py index 596d06ae9e7..2e34cbba3b8 100644 --- a/tests/components/ping/test_init.py +++ b/tests/components/ping/test_init.py @@ -33,6 +33,8 @@ async def test_device_migration( identifiers={(HOMEASSISTANT_DOMAIN, config_entry.entry_id)}, ) + assert config_entry.minor_version == 1 + await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() @@ -44,3 +46,4 @@ async def test_device_migration( assert device is not None assert device.id == old_device.id assert device.config_entries == {config_entry.entry_id} + assert config_entry.minor_version == 2