Fix blocking httpx calls inside the event loop#68
Open
emanuelstrom wants to merge 1 commit into
Open
Conversation
The integration calls synchronous httpx HTTPS requests from within async contexts (async_setup_entry, the config flow's authenticate / get_facility_id, and the fetch_facilities service). Each sync HTTPS call triggers ssl.SSLContext.load_verify_locations on first use, which Home Assistant flags with "Detected blocking call to load_verify_locations inside the event loop" warnings. This wraps the three remaining sync-in-async call sites in hass.async_add_executor_job so the work runs in a thread instead of blocking the loop. The sync update() polling path in sensor.py is unaffected because those methods are already executed on an executor thread by Home Assistant. Fixes linsvensson#59 Fixes linsvensson#66
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.
Fixes #59
Fixes #66
What
Three remaining call sites called sync
httpx.get()/httpx.post()from within async contexts:__init__.py: async_setup_entry—api.check_auth()andapi.get_facility_id()config_flow.py: Greenelyhub.authenticate / .get_facility_id(declaredasyncbut bodies were sync)services.py: async_fetch_facilities—api.check_auth()andapi.get_facility_ids()Each sync HTTPS request triggers
ssl.SSLContext.load_verify_locationson first use, which Home Assistant now warns about:How
Wrapped each call in
hass.async_add_executor_job(...)so the blocking work runs on a thread.Greenelyhubnow receiveshassto use the executor.The sync
update()polling path insensor.pyis unaffected — those methods are already executed on an executor thread by Home Assistant's polling system, so they were never the source of the warnings.Testing