11"""The generic_thermostat component."""
22
3+ import logging
4+
35from homeassistant .config_entries import ConfigEntry
46from homeassistant .core import Event , HomeAssistant
57from homeassistant .helpers import entity_registry as er
810 async_remove_stale_devices_links_keep_entity_device ,
911)
1012from homeassistant .helpers .event import async_track_entity_registry_updated_event
11- from homeassistant .helpers .helper_integration import async_handle_source_entity_changes
13+ from homeassistant .helpers .helper_integration import (
14+ async_handle_source_entity_changes ,
15+ async_remove_helper_config_entry_from_source_device ,
16+ )
1217
1318from .const import CONF_HEATER , CONF_SENSOR , PLATFORMS
1419
20+ _LOGGER = logging .getLogger (__name__ )
21+
1522
1623async def async_setup_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
1724 """Set up from a config entry."""
1825
26+ # This can be removed in HA Core 2026.2
1927 async_remove_stale_devices_links_keep_entity_device (
2028 hass ,
2129 entry .entry_id ,
@@ -28,23 +36,19 @@ def set_humidifier_entity_id_or_uuid(source_entity_id: str) -> None:
2836 options = {** entry .options , CONF_HEATER : source_entity_id },
2937 )
3038
31- async def source_entity_removed () -> None :
32- # The source entity has been removed, we need to clean the device links.
33- async_remove_stale_devices_links_keep_entity_device (hass , entry .entry_id , None )
34-
3539 entry .async_on_unload (
3640 # We use async_handle_source_entity_changes to track changes to the heater, but
3741 # not the temperature sensor because the generic_hygrostat adds itself to the
3842 # heater's device.
3943 async_handle_source_entity_changes (
4044 hass ,
45+ add_helper_config_entry_to_device = False ,
4146 helper_config_entry_id = entry .entry_id ,
4247 set_source_entity_id_or_uuid = set_humidifier_entity_id_or_uuid ,
4348 source_device_id = async_entity_id_to_device_id (
4449 hass , entry .options [CONF_HEATER ]
4550 ),
4651 source_entity_id_or_uuid = entry .options [CONF_HEATER ],
47- source_entity_removed = source_entity_removed ,
4852 )
4953 )
5054
@@ -75,6 +79,40 @@ async def async_sensor_updated(
7579 return True
7680
7781
82+ async def async_migrate_entry (hass : HomeAssistant , config_entry : ConfigEntry ) -> bool :
83+ """Migrate old entry."""
84+ _LOGGER .debug (
85+ "Migrating from version %s.%s" , config_entry .version , config_entry .minor_version
86+ )
87+
88+ if config_entry .version > 1 :
89+ # This means the user has downgraded from a future version
90+ return False
91+ if config_entry .version == 1 :
92+ options = {** config_entry .options }
93+ if config_entry .minor_version < 2 :
94+ # Remove the generic_thermostat config entry from the source device
95+ if source_device_id := async_entity_id_to_device_id (
96+ hass , options [CONF_HEATER ]
97+ ):
98+ async_remove_helper_config_entry_from_source_device (
99+ hass ,
100+ helper_config_entry_id = config_entry .entry_id ,
101+ source_device_id = source_device_id ,
102+ )
103+ hass .config_entries .async_update_entry (
104+ config_entry , options = options , minor_version = 2
105+ )
106+
107+ _LOGGER .debug (
108+ "Migration to version %s.%s successful" ,
109+ config_entry .version ,
110+ config_entry .minor_version ,
111+ )
112+
113+ return True
114+
115+
78116async def config_entry_update_listener (hass : HomeAssistant , entry : ConfigEntry ) -> None :
79117 """Update listener, called when the config entry options are changed."""
80118 await hass .config_entries .async_reload (entry .entry_id )
0 commit comments