29
29
CONF_ALLOW_ALL_IMPORTS ,
30
30
CONF_HASS_IS_GLOBAL ,
31
31
CONFIG_ENTRY ,
32
+ CONFIG_ENTRY_OLD ,
32
33
DOMAIN ,
33
34
FOLDER ,
34
35
LOGGER_PATH ,
@@ -100,6 +101,24 @@ async def update_yaml_config(hass, config_entry):
100
101
if config != config_entry .data :
101
102
await hass .config_entries .flow .async_init (DOMAIN , context = {"source" : SOURCE_IMPORT }, data = config )
102
103
104
+ #
105
+ # if hass_is_global or allow_all_imports have changed, we need to reload all scripts
106
+ # since they affect all scripts
107
+ #
108
+ config_save = {
109
+ param : config_entry .data .get (param , False ) for param in {CONF_HASS_IS_GLOBAL , CONF_ALLOW_ALL_IMPORTS }
110
+ }
111
+ if DOMAIN not in hass .data :
112
+ hass .data .setdefault (DOMAIN , {})
113
+ if CONFIG_ENTRY_OLD in hass .data [DOMAIN ]:
114
+ old_entry = hass .data [DOMAIN ][CONFIG_ENTRY_OLD ]
115
+ hass .data [DOMAIN ][CONFIG_ENTRY_OLD ] = config_save
116
+ for param in {CONF_HASS_IS_GLOBAL , CONF_ALLOW_ALL_IMPORTS }:
117
+ if old_entry .get (param , False ) != config_entry .data .get (param , False ):
118
+ return True
119
+ hass .data [DOMAIN ][CONFIG_ENTRY_OLD ] = config_save
120
+ return False
121
+
103
122
104
123
def start_global_contexts (global_ctx_only = None ):
105
124
"""Start all the file and apps global contexts."""
@@ -192,7 +211,7 @@ def check_event(event, do_reload):
192
211
watchdog_q = asyncio .Queue (0 )
193
212
observer = watchdog .observers .Observer ()
194
213
if observer is not None :
195
- # don't run watchdog when we are testing (it patches to None)
214
+ # don't run watchdog when we are testing (Observer() patches to None)
196
215
hass .data [DOMAIN ][WATCHDOG_OBSERVER ] = observer
197
216
hass .data [DOMAIN ][WATCHDOG_TASK ] = Function .create_task (task_watchdog (watchdog_q ))
198
217
@@ -201,11 +220,13 @@ def check_event(event, do_reload):
201
220
202
221
async def async_setup_entry (hass , config_entry ):
203
222
"""Initialize the pyscript config entry."""
223
+ global_ctx_only = None
204
224
if Function .hass :
205
225
#
206
226
# reload yaml if this isn't the first time (ie, on reload)
207
227
#
208
- await update_yaml_config (hass , config_entry )
228
+ if await update_yaml_config (hass , config_entry ):
229
+ global_ctx_only = "*"
209
230
210
231
Function .init (hass )
211
232
Event .init (hass )
@@ -227,19 +248,20 @@ async def async_setup_entry(hass, config_entry):
227
248
State .set_pyscript_config (config_entry .data )
228
249
229
250
await install_requirements (hass , config_entry , pyscript_folder )
230
- await load_scripts (hass , config_entry .data )
251
+ await load_scripts (hass , config_entry .data , global_ctx_only = global_ctx_only )
231
252
232
253
async def reload_scripts_handler (call ):
233
254
"""Handle reload service calls."""
234
255
_LOGGER .debug ("reload: yaml, reloading scripts, and restarting" )
235
256
236
- await update_yaml_config (hass , config_entry )
257
+ global_ctx_only = call .data .get ("global_ctx" , None ) if call else None
258
+
259
+ if await update_yaml_config (hass , config_entry ):
260
+ global_ctx_only = "*"
237
261
State .set_pyscript_config (config_entry .data )
238
262
239
263
await State .get_service_params ()
240
264
241
- global_ctx_only = call .data .get ("global_ctx" , None ) if call else None
242
-
243
265
await install_requirements (hass , config_entry , pyscript_folder )
244
266
await load_scripts (hass , config_entry .data , global_ctx_only = global_ctx_only )
245
267
@@ -333,14 +355,13 @@ async def hass_stop(event):
333
355
334
356
async def async_unload_entry (hass , config_entry ):
335
357
"""Unload a config entry."""
336
- # Unload scripts
358
+ _LOGGER . info ( "Unloading all scripts" )
337
359
await unload_scripts ()
338
360
339
- # Unsubscribe from listeners
340
361
for unsub_listener in hass .data [DOMAIN ][UNSUB_LISTENERS ]:
341
362
unsub_listener ()
363
+ hass .data [DOMAIN ][UNSUB_LISTENERS ] = []
342
364
343
- hass .data .pop (DOMAIN )
344
365
return True
345
366
346
367
0 commit comments