66
77from __future__ import annotations
88
9+ import voluptuous as vol
910from awesomeversion .awesomeversion import AwesomeVersion
1011from homeassistant .config_entries import ConfigEntry
1112from homeassistant .const import __version__ as HA_VERSION # noqa: N812
12- from homeassistant .core import HomeAssistant
13+ from homeassistant .core import HomeAssistant , callback
1314from homeassistant .helpers import config_validation as cv
15+ from homeassistant .helpers import entity_registry as er
16+ from homeassistant .helpers .helper_integration import (
17+ async_handle_source_entity_changes ,
18+ async_remove_helper_config_entry_from_source_device ,
19+ )
1420from homeassistant .helpers .typing import ConfigType
1521
1622from .const import (
23+ CONF_ENTITY_ID ,
1724 DOMAIN ,
1825 LOGGER ,
1926 MIN_HA_VERSION ,
2330CONFIG_SCHEMA = cv .config_entry_only_config_schema (DOMAIN )
2431
2532
33+ @callback
34+ def async_get_source_entity_device_id (
35+ hass : HomeAssistant , entity_id : str
36+ ) -> str | None :
37+ """Get the entity device id."""
38+ registry = er .async_get (hass )
39+
40+ if not (source_entity := registry .async_get (entity_id )):
41+ return None
42+
43+ return source_entity .device_id
44+
45+
2646async def async_setup (
2747 hass : HomeAssistant , # pylint: disable=unused-argument
2848 config : ConfigType , # pylint: disable=unused-argument
@@ -43,10 +63,46 @@ async def async_setup(
4363
4464async def async_setup_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
4565 """Set up Min/Max from a config entry."""
46- await hass .config_entries .async_forward_entry_setups (entry , PLATFORMS )
4766
67+ entity_registry = er .async_get (hass )
68+ try :
69+ entity_id = er .async_validate_entity_id (
70+ entity_registry , entry .options [CONF_ENTITY_ID ]
71+ )
72+ except vol .Invalid :
73+ # The entity is identified by an unknown entity registry ID
74+ LOGGER .error (
75+ "Failed to setup periodic_min_max for unknown entity %s" ,
76+ entry .options [CONF_ENTITY_ID ],
77+ )
78+ return False
79+
80+ def set_source_entity_id_or_uuid (source_entity_id : str ) -> None :
81+ hass .config_entries .async_update_entry (
82+ entry ,
83+ options = {** entry .options , CONF_ENTITY_ID : source_entity_id },
84+ )
85+
86+ async def source_entity_removed () -> None :
87+ # The source entity has been removed, we remove the config entry because
88+ # periodic_min_max will not work without the source entity.
89+ await hass .config_entries .async_remove (entry .entry_id )
90+
91+ entry .async_on_unload (
92+ async_handle_source_entity_changes (
93+ hass ,
94+ add_helper_config_entry_to_device = False ,
95+ helper_config_entry_id = entry .entry_id ,
96+ set_source_entity_id_or_uuid = set_source_entity_id_or_uuid ,
97+ source_device_id = async_get_source_entity_device_id (hass , entity_id ),
98+ source_entity_id_or_uuid = entry .options [CONF_ENTITY_ID ],
99+ source_entity_removed = source_entity_removed ,
100+ )
101+ )
48102 entry .async_on_unload (entry .add_update_listener (config_entry_update_listener ))
49103
104+ await hass .config_entries .async_forward_entry_setups (entry , PLATFORMS )
105+
50106 return True
51107
52108
@@ -58,3 +114,37 @@ async def config_entry_update_listener(hass: HomeAssistant, entry: ConfigEntry)
58114async def async_unload_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
59115 """Unload a config entry."""
60116 return await hass .config_entries .async_unload_platforms (entry , PLATFORMS )
117+
118+
119+ async def async_migrate_entry (hass : HomeAssistant , config_entry : ConfigEntry ) -> bool :
120+ """Migrate old entry."""
121+ LOGGER .debug (
122+ "Migrating from version %s.%s" , config_entry .version , config_entry .minor_version
123+ )
124+
125+ if config_entry .version > 1 :
126+ # This means the user has downgraded from a future version
127+ return False
128+ if config_entry .version == 1 :
129+ options = {** config_entry .options }
130+ if config_entry .minor_version < 2 :
131+ # Remove the periodic min/max config entry from the source device
132+ if source_device_id := async_get_source_entity_device_id (
133+ hass , options [CONF_ENTITY_ID ]
134+ ):
135+ async_remove_helper_config_entry_from_source_device (
136+ hass ,
137+ helper_config_entry_id = config_entry .entry_id ,
138+ source_device_id = source_device_id ,
139+ )
140+ hass .config_entries .async_update_entry (
141+ config_entry , options = options , minor_version = 2
142+ )
143+
144+ LOGGER .debug (
145+ "Migration to version %s.%s successful" ,
146+ config_entry .version ,
147+ config_entry .minor_version ,
148+ )
149+
150+ return True
0 commit comments