Fix elkm1 service calls running in the executor (#110655)

fixes
```
  File "/usr/src/homeassistant/homeassistant/components/elkm1/__init__.py", line 416, in _set_time_service
    _getelk(service).panel.set_time(dt_util.now())
  File "/usr/local/lib/python3.11/site-packages/elkm1_lib/panel.py", line 55, in set_time
    self._connection.send(rw_encode(datetime))
  File "/usr/local/lib/python3.11/site-packages/elkm1_lib/connection.py", line 152, in send
    self._send(QueuedWrite(msg.message, msg.response_command), priority_send)
  File "/usr/local/lib/python3.11/site-packages/elkm1_lib/connection.py", line 148, in _send
    self._check_write_queue.set()
  File "/usr/local/lib/python3.11/asyncio/locks.py", line 192, in set
    fut.set_result(True)
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 763, in call_soon
    self._check_thread()
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 800, in _check_thread
    raise RuntimeError(
RuntimeError: Non-thread-safe operation invoked on an event loop other than the current one
```
This commit is contained in:
J. Nick Koston 2024-02-16 05:27:13 -06:00 committed by GitHub
parent 66f189ef26
commit da55ef7901
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,7 +10,7 @@ from types import MappingProxyType
from typing import Any
from elkm1_lib.elements import Element
from elkm1_lib.elk import Elk
from elkm1_lib.elk import Elk, Panel
from elkm1_lib.util import parse_url
import voluptuous as vol
@ -398,22 +398,30 @@ async def async_wait_for_elk_to_sync(
return success
@callback
def _async_get_elk_panel(hass: HomeAssistant, service: ServiceCall) -> Panel:
"""Get the ElkM1 panel from a service call."""
prefix = service.data["prefix"]
elk = _find_elk_by_prefix(hass, prefix)
if elk is None:
raise HomeAssistantError(f"No ElkM1 with prefix '{prefix}' found")
return elk.panel
def _create_elk_services(hass: HomeAssistant) -> None:
def _getelk(service: ServiceCall) -> Elk:
prefix = service.data["prefix"]
elk = _find_elk_by_prefix(hass, prefix)
if elk is None:
raise HomeAssistantError(f"No ElkM1 with prefix '{prefix}' found")
return elk
"""Create ElkM1 services."""
@callback
def _speak_word_service(service: ServiceCall) -> None:
_getelk(service).panel.speak_word(service.data["number"])
_async_get_elk_panel(hass, service).speak_word(service.data["number"])
@callback
def _speak_phrase_service(service: ServiceCall) -> None:
_getelk(service).panel.speak_phrase(service.data["number"])
_async_get_elk_panel(hass, service).speak_phrase(service.data["number"])
@callback
def _set_time_service(service: ServiceCall) -> None:
_getelk(service).panel.set_time(dt_util.now())
_async_get_elk_panel(hass, service).set_time(dt_util.now())
hass.services.async_register(
DOMAIN, "speak_word", _speak_word_service, SPEAK_SERVICE_SCHEMA