pyscript icon indicating copy to clipboard operation
pyscript copied to clipboard

@service crashs homeassistant

Open littleyoda opened this issue 1 year ago • 6 comments

After updating HA to the latest version (2023.8.3) from 2023.7, HA did not start. I had to disable all pyscripts to get it to run at all. After a lot of testing, it turns out that the @service statement is the problem.

The following statement runed from jupyter, leads to HA being unstable. HA seems to run, but no longer accepts new browser connections. I've to restart HA.

@service("halloworldtest.run")
def myTestTrigger():
    print("Hallo World")
2023-08-20 19:33:50.097 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [140134801655744] Error handling message: Unknown error (unknown_error) <NAME REMOVED> from <IP REMOVED> (Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 26, in _handle_async_response
    await func(hass, connection, msg)
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 417, in handle_get_services
    payload = await _async_get_all_descriptions_json(hass)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 398, in _async_get_all_descriptions_json
    descriptions = await async_get_all_descriptions(hass)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 612, in async_get_all_descriptions
    translations = await translation.async_get_translations(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/translation.py", line 318, in async_get_translations
    cached = await cache.async_fetch(language, category, components)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/translation.py", line 209, in async_fetch
    await self._async_load(language, components_to_load)
  File "/usr/src/homeassistant/homeassistant/helpers/translation.py", line 230, in _async_load
    raise int_or_exc
homeassistant.loader.IntegrationNotFound: Integration 'halloworldtest' not found.

littleyoda avatar Aug 20 '23 17:08 littleyoda

I tried 2023.8.3 with your service example and it works fine for me. I'm not sure why it is crashing in your case.

craigbarratt avatar Aug 23 '23 05:08 craigbarratt

Same problem for me. HA 2023.8.3, core install on venv - Python 3.11.4 Exact same error as @littleyoda

EDIT: Seems the error only occurs when assigning a name to the service definition.

@service  # <- WORKS, assigned service name is pyscript.sunlight_set
@service(f"sunlight.set") # <- ERROR,  homeassistant.loader.IntegrationNotFound: Integration 'sunlight' not found.
def sunlight_set (newval:str):
    input_select.select_option(entity_id="input_select.sunlight", option=newval)

Kaptensanders avatar Aug 23 '23 18:08 Kaptensanders

can confirm the workaround from Kaptensanders. @service works @service("xxyz"), however, does not works.

@Kaptensanders thanks for the workaround

littleyoda avatar Aug 23 '23 18:08 littleyoda

I've confirmed that this code works in 2023.7.3, but fails in 2023.8.3 when there is a service defined in pyscript that doesn't have a corresponding integration of the same name.

from homeassistant.helpers.service import async_get_all_descriptions

descriptions = async_get_all_descriptions(hass)

From the error message, it fails trying to load translations. Is anyone familiar with any recent HASS service description/translation changes?

craigbarratt avatar Aug 24 '23 01:08 craigbarratt

The issue is this code homeassistant/helpers/service.py got added in in 2023.8.0. HASS now loads all translations for all services. When an arbitrary service name isn't part of an associated integration, the loading of translations fails. That causes async_get_all_descriptions() to fail.

I can't see a work around for this. Perhaps it's a breaking change, and you can only create services in pyscript that are part of an existing integration (ie, top-level name)?

craigbarratt avatar Aug 24 '23 23:08 craigbarratt

Breaking change maybe but an ok one. When you think about it it's is maybe bad practice to refer to non-existing integrations. Not surprising that could lead to problems with HA.

@service("pyscript.service_name") # works fine

Kaptensanders avatar Aug 25 '23 07:08 Kaptensanders