openHASP-custom-component icon indicating copy to clipboard operation
openHASP-custom-component copied to clipboard

Fix blocking open() calls by offloading file I/O to async_add_executo…

Open wjnelson78 opened this issue 10 months ago • 2 comments

…r_job

In __init__.py, direct open() calls were being made on the Home Assistant event loop, leading to “Detected blocking call to open()” warnings and potential performance issues.

To address this:

  • Moved the reading of pages_schema.json out of the constructor and into async_added_to_hass, wrapping the file read with hass.async_add_executor_job().
  • Introduced small synchronous helper functions (_sync_read_json, _sync_read_text, etc.) for file I/O. These are called inside async_add_executor_job(), preventing the main loop from blocking.
  • Updated async_load_page to use the same pattern for JSON or text lines, validating JSON data against the pre-loaded schema.
  • Ensures compliance with Home Assistant’s async guidelines and avoids blocking the event loop with disk I/O.
  • Improves overall responsiveness and prevents related warnings in the HA logs.

wjnelson78 avatar Mar 11 '25 02:03 wjnelson78

FYI - This change for me allows HA to restart much faster than before.

wjnelson78 avatar Mar 11 '25 15:03 wjnelson78

This had already been addressed through an executer task (I have no warnings)

Also too many unrelated changes

dgomes avatar Mar 11 '25 21:03 dgomes