22
33from __future__ import annotations
44
5+ import logging
6+
57from homeassistant .config_entries import ConfigEntry
68from homeassistant .const import Platform
79from homeassistant .core import HomeAssistant
810from homeassistant .helpers .device import (
911 async_entity_id_to_device_id ,
1012 async_remove_stale_devices_links_keep_entity_device ,
1113)
12- from homeassistant .helpers .helper_integration import async_handle_source_entity_changes
14+ from homeassistant .helpers .helper_integration import (
15+ async_handle_source_entity_changes ,
16+ async_remove_helper_config_entry_from_source_device ,
17+ )
1318
1419from .const import CONF_SOURCE_SENSOR
1520
21+ _LOGGER = logging .getLogger (__name__ )
22+
1623
1724async def async_setup_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
1825 """Set up Integration from a config entry."""
1926
27+ # This can be removed in HA Core 2026.2
2028 async_remove_stale_devices_links_keep_entity_device (
2129 hass ,
2230 entry .entry_id ,
@@ -29,20 +37,16 @@ def set_source_entity_id_or_uuid(source_entity_id: str) -> None:
2937 options = {** entry .options , CONF_SOURCE_SENSOR : source_entity_id },
3038 )
3139
32- async def source_entity_removed () -> None :
33- # The source entity has been removed, we need to clean the device links.
34- async_remove_stale_devices_links_keep_entity_device (hass , entry .entry_id , None )
35-
3640 entry .async_on_unload (
3741 async_handle_source_entity_changes (
3842 hass ,
43+ add_helper_config_entry_to_device = False ,
3944 helper_config_entry_id = entry .entry_id ,
4045 set_source_entity_id_or_uuid = set_source_entity_id_or_uuid ,
4146 source_device_id = async_entity_id_to_device_id (
4247 hass , entry .options [CONF_SOURCE_SENSOR ]
4348 ),
4449 source_entity_id_or_uuid = entry .options [CONF_SOURCE_SENSOR ],
45- source_entity_removed = source_entity_removed ,
4650 )
4751 )
4852
@@ -51,6 +55,40 @@ async def source_entity_removed() -> None:
5155 return True
5256
5357
58+ async def async_migrate_entry (hass : HomeAssistant , config_entry : ConfigEntry ) -> bool :
59+ """Migrate old entry."""
60+ _LOGGER .debug (
61+ "Migrating from version %s.%s" , config_entry .version , config_entry .minor_version
62+ )
63+
64+ if config_entry .version > 1 :
65+ # This means the user has downgraded from a future version
66+ return False
67+ if config_entry .version == 1 :
68+ options = {** config_entry .options }
69+ if config_entry .minor_version < 2 :
70+ # Remove the integration config entry from the source device
71+ if source_device_id := async_entity_id_to_device_id (
72+ hass , options [CONF_SOURCE_SENSOR ]
73+ ):
74+ async_remove_helper_config_entry_from_source_device (
75+ hass ,
76+ helper_config_entry_id = config_entry .entry_id ,
77+ source_device_id = source_device_id ,
78+ )
79+ hass .config_entries .async_update_entry (
80+ config_entry , options = options , minor_version = 2
81+ )
82+
83+ _LOGGER .debug (
84+ "Migration to version %s.%s successful" ,
85+ config_entry .version ,
86+ config_entry .minor_version ,
87+ )
88+
89+ return True
90+
91+
5492async def config_entry_update_listener (hass : HomeAssistant , entry : ConfigEntry ) -> None :
5593 """Update listener, called when the config entry options are changed."""
5694 # Remove device link for entry, the source device may have changed.
0 commit comments