PythonScriptsPro icon indicating copy to clipboard operation
PythonScriptsPro copied to clipboard

async_get_hass wrong thread error

Open jmot205 opened this issue 1 year ago • 5 comments

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

jmot205 avatar Apr 28 '24 05:04 jmot205

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.

mcpfr avatar May 09 '24 10:05 mcpfr

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

daniel-simpson avatar May 14 '24 05:05 daniel-simpson

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)

daniel-simpson avatar May 14 '24 05:05 daniel-simpson

Hello @daniel-simpson

Thanks for this post, i have rempace "hass.services.async_call" to "hass.services.call", and this working !

mcpfr avatar May 14 '24 09:05 mcpfr

I tried adding blocking=True, return_response=True to my hass.services.call call and unfortunately still get the error.

jmot205 avatar May 15 '24 12:05 jmot205