async_get_hass wrong thread error
I've started receiving the following error when running one of my scripts that uses the call_service function - seemingly since updating to HA 2024.4.4 a few days ago (no change to the PSP script):
Logger: homeassistant.components.script.light_store Source: helpers/script.py:1933 integration: Script (documentation, issues) First occurred: April 26, 2024 at 07:05:30 (7 occurrences) Last logged: 13:37:06
Light Store: Error executing script. Error for call_service at pos 1: async_get_hass called from the wrong thread
Hello,
I'm a same error, when calls async_create_task
Error executing script: Detected that custom integration 'python_script' calls async_create_task from a thread at custom_components/python_script/__init__.py, line 81: exec(code). Please report it to the author of the 'python_script' custom integration. Traceback (most recent call last): File "/config/custom_components/python_script/__init__.py", line 81, in execute_script exec(code) File "/config/config_mcp/scripts_python/actif/enedis_recuperation_donnees.py", line 117, in <module> hass.async_create_task(hass.services.async_call('logbook', 'log', {"name":"XXXX", "message":"yyyyyy", "entity_id":"sensor.zzzz"})) File "/usr/src/homeassistant/homeassistant/core.py", line 816, in async_create_task self.verify_event_loop_thread("async_create_task") File "/usr/src/homeassistant/homeassistant/core.py", line 440, in verify_event_loop_thread frame.report( File "/usr/src/homeassistant/homeassistant/helpers/frame.py", line 162, in report _report_integration(what, integration_frame, level, error_if_integration) File "/usr/src/homeassistant/homeassistant/helpers/frame.py", line 203, in _report_integration raise RuntimeError( RuntimeError: Detected that custom integration 'python_script' calls async_create_task from a thread at custom_components/python_script/__init__.py, line 81: exec(code). Please report it to the author of the 'python_script' custom integration.
I had something similar. Got it working by switching from hass.services.call to hass.services.async_call. Looks like a known change in core: https://developers.home-assistant.io/blog/2023/06/14/service-calls?_highlight=service&_highlight=call
Edit: my bad, was testing incorrectly.
Did manage to get this working though, with the following parameters:
x = hass.services.call('todo', 'get_items', {
"entity_id": "todo.alexa"
}, blocking=True, return_response=True)
Hello @daniel-simpson
Thanks for this post, i have rempace "hass.services.async_call" to "hass.services.call", and this working !
I tried adding blocking=True, return_response=True to my hass.services.call call and unfortunately still get the error.