Draft
perf: move image generation off the HA event loop (fixes #17)#27
Conversation
Agent-Logs-Url: https://github.com/Misiu/OpenDisplay_HA_Integration/sessions/4bba66ec-d685-4230-b21c-613b175c6fa9 Co-authored-by: Misiu <1741838+Misiu@users.noreply.github.com>
…adata Agent-Logs-Url: https://github.com/Misiu/OpenDisplay_HA_Integration/sessions/4bba66ec-d685-4230-b21c-613b175c6fa9 Co-authored-by: Misiu <1741838+Misiu@users.noreply.github.com>
…move unused param Agent-Logs-Url: https://github.com/Misiu/OpenDisplay_HA_Integration/sessions/4bba66ec-d685-4230-b21c-613b175c6fa9 Co-authored-by: Misiu <1741838+Misiu@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
Misiu
April 28, 2026 10:12
View session
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the HA event-loop blocking described in OpenDisplay/Home_Assistant_Integration#17.
All PIL drawing operations (text, shapes, QR codes, image downloads/transforms, icon rendering, JPEG encoding) previously ran synchronously on the HA event loop, causing HA to lock up for tens of seconds per image update.
Root causes fixed
hass.async_add_executor_job_mdi_metadata_cache+_mdi_font_cache— loaded once, reused foreverqr.make()) ran on event looprequests.get()was wrapped in extraasync_add_executor_jobinside an already-sync handlerHow it works now
Phase 1 – async (event loop)
_async_prefetch_resources()fetches only data that requires the HA event loop:plotelements (viaprefetch_plot_data)Phase 2 – sync (executor thread)
_generate_sync()runs inhass.async_add_executor_joband handles:draw_downloaded_image(safe in thread)Changes
core.py: Two-phasegenerate_custom_image; new_async_prefetch_resources+_generate_syncmethodsregistry.py: Handler wrapper changed fromasync deftodeftext.py,shapes.py,debug.py:async def→def(no I/O)icons.py: Module-level MDI cache helpers;async def→defmedia.py:async def→def; direct blocking I/O (in thread)visualizations.py: Newasync prefetch_plot_data; all handlersasync def→deftypes.py:DrawingContext.resourcesfield added (with docstring)tests/drawcustom/conftest.py:async_add_executor_jobmock now executes callables so existing 82 tests still pass