JeffSteinbok / hass-dreo

Dreo Smart Device Integration for Home Assistant

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

websocket not restarting if it was closed

vitaliy-sk opened this issue · comments

Sometimes (once per 1-2 days) command execution starts failing
seems websocket was closed due to some reason and it's not reconnect
After homeasistant restarts it reconnects

Logger: homeassistant.components.websocket_api.http.connection
Source: custom_components/dreo/pydreo/__init__.py:324 
Integration: Home Assistant WebSocket API (documentation, issues) 
First occurred: 5:17:33 PM (3 occurrences) 
Last logged: 6:25:36 PM

[140474730949904] sent 1000 (OK); then received 1000 (OK)
[140474976374608] sent 1000 (OK); then received 1000 (OK)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 205, in handle_call_service
    await hass.services.async_call(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1910, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1950, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 226, in handle_service
    await service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 811, in entity_service_call
    future.result()  # pop exception if have
    ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1034, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 851, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/components/fan/__init__.py", line 275, in async_turn_on
    await self.hass.async_add_executor_job(
  File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/dreo/fan.py", line 109, in turn_on
    self.device.set_power(True)
  File "/config/custom_components/dreo/pydreo/pydreofan.py", line 102, in set_power
    self._send_command(POWERON_KEY, value)
  File "/config/custom_components/dreo/pydreo/pydreobasedevice.py", line 65, in _send_command
    self._dreo.send_command(self, params)
  File "/config/custom_components/dreo/pydreo/__init__.py", line 324, in send_command
    asyncio.run(self.ws.send(content))
  File "/usr/local/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 635, in send
    await self.ensure_open()
  File "/usr/local/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 935, in ensure_open
    raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedOK: sent 1000 (OK); then received 1000 (OK)

Not surprising. I've been rebooting a lot due to testing.

I don't have reconnect or token refresh logic done. I'll file some work items to remind me.

Try this and let me know?
https://github.com/JeffSteinbok/hass-dreo/releases/tag/v0.2.0b1

Also, you should be able to reload the Integration from within the UX now.
image

@JeffSteinbok working fine couple of days without restart, thanks for the fix

Sometimes I see the temperature chart from the fan is not updating (there are some gaps on the chart) not sure if this is related to closed WebSocket. Anyway, the temperature is wrong (+/- 4-5 degrees from real on my fan), so it's useless

Thanks. I'll integrate it in when I have a chance. Note that sometimes it will run for days even without this fix. Varies.

Full disclosure: I'm pretty busy at work now, so this may wait a bit.