1
1
"""https://github.com/andreasbrett/thewatchman§"""
2
+
2
3
from datetime import timedelta
3
4
import logging
4
5
import os
@@ -133,18 +134,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
133
134
await add_event_handlers (hass )
134
135
if hass .is_running :
135
136
# integration reloaded or options changed via UI
136
- parse_config (hass , reason = "changes in watchman configuration" )
137
+ await parse_config (hass , reason = "changes in watchman configuration" )
137
138
await coordinator .async_config_entry_first_refresh ()
138
139
else :
139
140
# first run, home assistant is loading
140
141
# parse_config will be scheduled once HA is fully loaded
141
142
_LOGGER .info ("Watchman started [%s]" , VERSION )
142
143
143
-
144
- # resources = hass.data["lovelace"]["resources"]
145
- # await resources.async_get_info()
146
- # for itm in resources.async_items():
147
- # _LOGGER.debug(itm)
144
+ # resources = hass.data["lovelace"]["resources"]
145
+ # await resources.async_get_info()
146
+ # for itm in resources.async_items():
147
+ # _LOGGER.debug(itm)
148
148
149
149
return True
150
150
@@ -210,7 +210,7 @@ async def async_handle_report(call):
210
210
await async_notification (hass , "Watchman error" , message , error = True )
211
211
212
212
if call .data .get (CONF_PARSE_CONFIG , False ):
213
- parse_config (hass , reason = "service call" )
213
+ await parse_config (hass , reason = "service call" )
214
214
215
215
if send_notification :
216
216
chunk_size = call .data .get (CONF_CHUNK_SIZE , config .get (CONF_CHUNK_SIZE ))
@@ -270,7 +270,7 @@ async def async_delayed_refresh_states(timedate): # pylint: disable=unused-argu
270
270
await coordinator .async_refresh ()
271
271
272
272
async def async_on_home_assistant_started (event ): # pylint: disable=unused-argument
273
- parse_config (hass , reason = "HA restart" )
273
+ await parse_config (hass , reason = "HA restart" )
274
274
startup_delay = get_config (hass , CONF_STARTUP_DELAY , 0 )
275
275
await async_schedule_refresh_states (hass , startup_delay )
276
276
@@ -283,12 +283,12 @@ async def async_on_configuration_changed(event):
283
283
"reload_core_config" ,
284
284
"reload" ,
285
285
]:
286
- parse_config (hass , reason = "configuration changes" )
286
+ await parse_config (hass , reason = "configuration changes" )
287
287
coordinator = hass .data [DOMAIN ]["coordinator" ]
288
288
await coordinator .async_refresh ()
289
289
290
290
elif typ in [EVENT_AUTOMATION_RELOADED , EVENT_SCENE_RELOADED ]:
291
- parse_config (hass , reason = "configuration changes" )
291
+ await parse_config (hass , reason = "configuration changes" )
292
292
coordinator = hass .data [DOMAIN ]["coordinator" ]
293
293
await coordinator .async_refresh ()
294
294
@@ -340,14 +340,14 @@ def state_or_missing(state_id):
340
340
hass .data [DOMAIN ]["cancel_handlers" ] = hdlr
341
341
342
342
343
- def parse_config (hass : HomeAssistant , reason = None ):
343
+ async def parse_config (hass : HomeAssistant , reason = None ):
344
344
"""parse home assistant configuration files"""
345
345
assert hass .data .get (DOMAIN_DATA )
346
346
start_time = time .time ()
347
347
included_folders = get_included_folders (hass )
348
348
ignored_files = hass .data [DOMAIN_DATA ].get (CONF_IGNORED_FILES , None )
349
349
350
- entity_list , service_list , files_parsed , files_ignored = parse (
350
+ entity_list , service_list , files_parsed , files_ignored = await parse (
351
351
hass , included_folders , ignored_files , hass .config .config_dir
352
352
)
353
353
hass .data [DOMAIN ]["entity_list" ] = entity_list
@@ -387,11 +387,16 @@ async def async_report_to_file(hass, path, test_mode):
387
387
"""save report to a file"""
388
388
coordinator = hass .data [DOMAIN ]["coordinator" ]
389
389
await coordinator .async_refresh ()
390
- report_chunks = report (hass , table_renderer , chunk_size = 0 , test_mode = test_mode )
391
- # OSError exception is handled in async_handle_report
392
- with open (path , "w" , encoding = "utf-8" ) as report_file :
393
- for chunk in report_chunks :
394
- report_file .write (chunk )
390
+ report_chunks = await report (
391
+ hass , table_renderer , chunk_size = 0 , test_mode = test_mode
392
+ )
393
+
394
+ def write (path ):
395
+ with open (path , "w" , encoding = "utf-8" ) as report_file :
396
+ for chunk in report_chunks :
397
+ report_file .write (chunk )
398
+
399
+ await hass .async_add_executor_job (write , path )
395
400
396
401
397
402
async def async_report_to_notification (hass , service_str , service_data , chunk_size ):
@@ -422,7 +427,7 @@ async def async_report_to_notification(hass, service_str, service_data, chunk_si
422
427
423
428
coordinator = hass .data [DOMAIN ]["coordinator" ]
424
429
await coordinator .async_refresh ()
425
- report_chunks = report (hass , text_renderer , chunk_size )
430
+ report_chunks = await report (hass , text_renderer , chunk_size )
426
431
for chunk in report_chunks :
427
432
data ["message" ] = chunk
428
433
# blocking=True ensures execution order
0 commit comments