11"""The generic_hygrostat component."""
22
3+ import logging
4+
35import voluptuous as vol
46
57from homeassistant .components .humidifier import HumidifierDeviceClass
1618 async_remove_stale_devices_links_keep_entity_device ,
1719)
1820from homeassistant .helpers .event import async_track_entity_registry_updated_event
19- from homeassistant .helpers .helper_integration import async_handle_source_entity_changes
21+ from homeassistant .helpers .helper_integration import (
22+ async_handle_source_entity_changes ,
23+ async_remove_helper_config_entry_from_source_device ,
24+ )
2025from homeassistant .helpers .typing import ConfigType
2126
2227DOMAIN = "generic_hygrostat"
7075 extra = vol .ALLOW_EXTRA ,
7176)
7277
78+ _LOGGER = logging .getLogger (__name__ )
79+
7380
7481async def async_setup (hass : HomeAssistant , config : ConfigType ) -> bool :
7582 """Set up the Generic Hygrostat component."""
@@ -89,6 +96,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
8996async def async_setup_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
9097 """Set up from a config entry."""
9198
99+ # This can be removed in HA Core 2026.2
92100 async_remove_stale_devices_links_keep_entity_device (
93101 hass ,
94102 entry .entry_id ,
@@ -101,23 +109,19 @@ def set_humidifier_entity_id_or_uuid(source_entity_id: str) -> None:
101109 options = {** entry .options , CONF_HUMIDIFIER : source_entity_id },
102110 )
103111
104- async def source_entity_removed () -> None :
105- # The source entity has been removed, we need to clean the device links.
106- async_remove_stale_devices_links_keep_entity_device (hass , entry .entry_id , None )
107-
108112 entry .async_on_unload (
109113 # We use async_handle_source_entity_changes to track changes to the humidifer,
110114 # but not the humidity sensor because the generic_hygrostat adds itself to the
111115 # humidifier's device.
112116 async_handle_source_entity_changes (
113117 hass ,
118+ add_helper_config_entry_to_device = False ,
114119 helper_config_entry_id = entry .entry_id ,
115120 set_source_entity_id_or_uuid = set_humidifier_entity_id_or_uuid ,
116121 source_device_id = async_entity_id_to_device_id (
117122 hass , entry .options [CONF_HUMIDIFIER ]
118123 ),
119124 source_entity_id_or_uuid = entry .options [CONF_HUMIDIFIER ],
120- source_entity_removed = source_entity_removed ,
121125 )
122126 )
123127
@@ -148,6 +152,40 @@ async def async_sensor_updated(
148152 return True
149153
150154
155+ async def async_migrate_entry (hass : HomeAssistant , config_entry : ConfigEntry ) -> bool :
156+ """Migrate old entry."""
157+ _LOGGER .debug (
158+ "Migrating from version %s.%s" , config_entry .version , config_entry .minor_version
159+ )
160+
161+ if config_entry .version > 1 :
162+ # This means the user has downgraded from a future version
163+ return False
164+ if config_entry .version == 1 :
165+ options = {** config_entry .options }
166+ if config_entry .minor_version < 2 :
167+ # Remove the generic_hygrostat config entry from the source device
168+ if source_device_id := async_entity_id_to_device_id (
169+ hass , options [CONF_HUMIDIFIER ]
170+ ):
171+ async_remove_helper_config_entry_from_source_device (
172+ hass ,
173+ helper_config_entry_id = config_entry .entry_id ,
174+ source_device_id = source_device_id ,
175+ )
176+ hass .config_entries .async_update_entry (
177+ config_entry , options = options , minor_version = 2
178+ )
179+
180+ _LOGGER .debug (
181+ "Migration to version %s.%s successful" ,
182+ config_entry .version ,
183+ config_entry .minor_version ,
184+ )
185+
186+ return True
187+
188+
151189async def config_entry_update_listener (hass : HomeAssistant , entry : ConfigEntry ) -> None :
152190 """Update listener, called when the config entry options are changed."""
153191 await hass .config_entries .async_reload (entry .entry_id )
0 commit comments