LightwaveSmartHome / homeassistant-lightwave-smart

Lightwave custom component for Home Assistant. Requires a Link Plus hub (Smart Series / generation 2), but will control both generation 1 ("Connect Series") and generation 2 ("Smart Series") devices.
https://www.lightwaverf.com
10 stars 2 forks source link

Error Service Call- lightwave_smart.reconnect #11

Open RGarrett93 opened 6 months ago

RGarrett93 commented 6 months ago

lightwave_smart.reconnect service does not work and throws an error.

I'm finding that after a small duration the integration no longer registers multi-touch buttons unless HA is rebooted (force restart the Lightwave integration). It would be handy to call the service request lightwave_smart.reconnect.

This could easily be automated for when a state change occurs but no corresponding multi-touch is detected, which would then call the service lightwave_smart.reconnect

Screenshot_20240429_034341_Home Assistant.jpg

Logger: homeassistant.components.websocket_api.http.connection Source: custom_components/lightwave_smart/init.py:45 integration: Home Assistant WebSocket API (documentation, issues) First occurred: 03:46:14 (1 occurrences) Last logged: 03:46:14

[546704016576] Error handling message: Unknown error (unknown_error) Ryan from 192.168.86.25 (Mozilla/5.0 (Linux; Android 13; SM-N986B Build/TP1A.220624.014; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/123.0.6312.120 Mobile Safari/537.36 Home Assistant/2024.4.3-12648 (Android 13; SM-N986B))
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 27, in _handle_async_response
    await func(hass, connection, msg)
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 794, in handle_execute_script
    script_result = await script_obj.async_run(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1728, in async_run
    return await asyncio.shield(create_eager_task(run.async_run()))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 457, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 509, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 539, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 507, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 742, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 705, in _async_run_long_action
    return await long_task
           ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2543, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2580, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/lightwave_smart/__init__.py", line 45, in service_handle_reconnect
    if link._websocket is not None:
       ^^^^^^^^^^^^^^^
AttributeError: 'LWLink2' object has no attribute '_websocket'

Logger: homeassistant.components.websocket_api.http.connection Source: custom_components/lightwave_smart/init.py:45 integration: Home Assistant WebSocket API (documentation, issues) First occurred: 03:46:14 (1 occurrences) Last logged: 03:46:14

[546704016576] Error handling message: Unknown error (unknown_error) 
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 27, in _handle_async_response
    await func(hass, connection, msg)
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 794, in handle_execute_script
    script_result = await script_obj.async_run(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1728, in async_run
    return await asyncio.shield(create_eager_task(run.async_run()))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 457, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 509, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 539, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 507, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 742, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 705, in _async_run_long_action
    return await long_task
           ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2543, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2580, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/lightwave_smart/__init__.py", line 45, in service_handle_reconnect
    if link._websocket is not None:
       ^^^^^^^^^^^^^^^
AttributeError: 'LWLink2' object has no attribute '_websocket'
RGarrett93 commented 1 day ago

In the lightwave_smart Home Assistant integration, the service_handle_reconnect function currently relies on directly accessing a nonexistent _websocket attribute from the LWLink2 object. This leads to an AttributeError when the lightwave_smart.reconnect service is called.

async def service_handle_reconnect(call):
    _LOGGER.debug("Received service call reconnect")
    for entry_id in hass.data[DOMAIN]:
        link = hass.data[DOMAIN][entry_id][LIGHTWAVE_LINK2]
        try:
            # Close the existing WebSocket connection if it exists
            if link._ws and link._ws._websocket is not None:
                _LOGGER.debug("Closing existing WebSocket connection...")
                await link._ws._websocket.close()
            else:
                _LOGGER.debug("No WebSocket connection to close.")
        except Exception as e:
            _LOGGER.error("Error closing WebSocket: %s", e)

Changing __init__.py with the above resolves the AttributeError and now disconnects the WebSocket connection when action: lightwave_smart.reconnect is performed.

Using a modified version of an automation found here , when an event is not received from Lightwave i.e multi-press event no longer registering, this action: lightwave_smart.reconnect is called automatically. The WebSocket is then reconnected and events can be received again in Home Assistant.