44
55import asyncio
66import logging
7+ from types import MappingProxyType
78
89import httpx
910import ollama
@@ -100,8 +101,12 @@ async def async_migrate_integration(hass: HomeAssistant) -> None:
100101
101102 for entry in entries :
102103 use_existing = False
104+ # Create subentry with model from entry.data and options from entry.options
105+ subentry_data = entry .options .copy ()
106+ subentry_data [CONF_MODEL ] = entry .data [CONF_MODEL ]
107+
103108 subentry = ConfigSubentry (
104- data = entry . options ,
109+ data = MappingProxyType ( subentry_data ) ,
105110 subentry_type = "conversation" ,
106111 title = entry .title ,
107112 unique_id = None ,
@@ -154,17 +159,19 @@ async def async_migrate_integration(hass: HomeAssistant) -> None:
154159 hass .config_entries .async_update_entry (
155160 entry ,
156161 title = DEFAULT_NAME ,
162+ # Update parent entry to only keep URL, remove model
163+ data = {CONF_URL : entry .data [CONF_URL ]},
157164 options = {},
158- version = 2 ,
159- minor_version = 2 ,
165+ version = 3 ,
166+ minor_version = 1 ,
160167 )
161168
162169
163170async def async_migrate_entry (hass : HomeAssistant , entry : OllamaConfigEntry ) -> bool :
164171 """Migrate entry."""
165172 _LOGGER .debug ("Migrating from version %s:%s" , entry .version , entry .minor_version )
166173
167- if entry .version > 2 :
174+ if entry .version > 3 :
168175 # This means the user has downgraded from a future version
169176 return False
170177
@@ -182,6 +189,25 @@ async def async_migrate_entry(hass: HomeAssistant, entry: OllamaConfigEntry) ->
182189
183190 hass .config_entries .async_update_entry (entry , minor_version = 2 )
184191
192+ if entry .version == 2 and entry .minor_version == 2 :
193+ # Update subentries to include the model
194+ for subentry in entry .subentries .values ():
195+ if subentry .subentry_type == "conversation" :
196+ updated_data = dict (subentry .data )
197+ updated_data [CONF_MODEL ] = entry .data [CONF_MODEL ]
198+
199+ hass .config_entries .async_update_subentry (
200+ entry , subentry , data = MappingProxyType (updated_data )
201+ )
202+
203+ # Update main entry to remove model and bump version
204+ hass .config_entries .async_update_entry (
205+ entry ,
206+ data = {CONF_URL : entry .data [CONF_URL ]},
207+ version = 3 ,
208+ minor_version = 1 ,
209+ )
210+
185211 _LOGGER .debug (
186212 "Migration to version %s:%s successful" , entry .version , entry .minor_version
187213 )
0 commit comments