@@ -129,18 +129,28 @@ async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
129129async def async_migrate_entry (hass , config_entry : ConfigEntry ) -> bool :
130130 """Migrate old entry."""
131131 _LOGGER .debug ("Migrating from version %s" , config_entry .version )
132+ data = {** config_entry .data }
133+ options = {** config_entry .options }
134+ unique_id = data [CONF_DEVICE_ID ]
135+
132136 if config_entry .version == 1 :
133- data = {** config_entry .data }
134137 # add the api url. If in option, use it, otherwise use the default one
135138 option_api_url = config_entry .options .get (OPTION_API_URL , None )
136139 api_url = DEFAULT_API_URL if option_api_url is None else option_api_url
137140 data [CONF_API_URL ] = api_url
138- # set unique_id
139- unique_id = data [CONF_DEVICE_ID ]
140- # update the config entry
141141 config_entry .version = 2
142- hass .config_entries .async_update_entry (
143- config_entry , data = data , unique_id = unique_id
144- )
142+
143+ if config_entry .version == 2 :
144+ # if api_url is empty, copy over the one in options
145+ if data [CONF_API_URL ] == "" :
146+ data [CONF_API_URL ] = DEFAULT_API_URL
147+ if OPTION_API_URL in options :
148+ del options [OPTION_API_URL ]
149+ config_entry .version = 3
150+
151+ # update the config entry
152+ hass .config_entries .async_update_entry (
153+ config_entry , data = data , options = options , unique_id = unique_id
154+ )
145155 _LOGGER .info ("Migration to version %s successful" , config_entry .version )
146156 return True
0 commit comments