Add-on: kodi2home

I’m back again, migrating my install to HAOS once more.
I forget how much I appreciate and rely on this addon until it don’t have it!
It’s worked perfectly since your assistance setting up as a standalone docker. I’ll admit my knowledge has got a little rusty since.
I’m trying to install as addon with a fresh setup on latest homeassistant OS, and after adding the repo and attempting to install I get the following error:

Failed to install add-on

The command ‘/bin/ash -o pipefail -c apk add --update --no-cache python3 py3-pip && ln -sf python3 /usr/bin/python && python3 -m ensurepip’ returned a non-zero code: 1

Any ideas @DJJo14 ?!

Thanks!

I have watchdog not enabled. But I have noticed that the addon sometimes is stopped. But I have not found a reason for that. When it occurs again i try to find the error.

Looks like a problem with building the container… But at my side it is working, with docker it should be the same. after checking the logs i found the same error. I went googling and i found something, parcial related. So I created a new version. So:

Version 0.3.2

  • Docker build fix?

What kind of system architecture are you running from? x86 (pc) or aarch64 (raspberry pi)

1 Like

It is an Odroid N2+, same type I believe as the HA Blue hardware, which is arm64 I think.

Version 0.3.2 appears to work - or at least the installation completes successfully now, I just need to configure it all.
Thanks so much!

I too am experiencing the issue the addon fails to launch after restart of HA, both with and without watchdog service enabled. I think a similar error.
But it always works with manual start.
Perhaps something to do with the timing of addon launch? If the network is not yet up could cause failure. I’m no expert.

I do seem to have found a workaround, perhaps @NoFace you’d be interested.
I have the Portainer add-on installed, and if I use that to change the restart policy for the kodi2home container to “always”, it successfully launches after rebooting twice now.
I think it still throws the same error on first launch, but subsequently launches ok.
All logs below if it can be of use.
As ever thanks @DJJo14 for the fantastic add-on.

With watchdog enabled:

s6-rc: info: service s6rc-oneshot-runner: starting
s6-rc: info: service s6rc-oneshot-runner successfully started
s6-rc: info: service fix-attrs: starting
s6-rc: info: service fix-attrs successfully started
s6-rc: info: service legacy-cont-init: starting
s6-rc: info: service legacy-cont-init successfully started
s6-rc: info: service legacy-services: starting
s6-rc: info: service legacy-services successfully started
2024-03-04 22:35:19,420 kodi settings: 10.0.1.11, 8080 9090, kodi, *
2024-03-04 22:35:19,422 Home assistant settings: ws://supervisor/core/api/websocket,  False, *
2024-03-04 22:35:19,466 Kodi is connected {'name': 'Kodi', 'version': {'major': 21, 'minor': 0, 'revision': '87d2d6f84799224f5fe63a6bba3e973e84e18fef', 'tag': 'beta', 'tagversion': '2'}}
Traceback (most recent call last):
  File "/kodi2home.py", line 178, in main
    loop.run_until_complete( tasks )
  File "/usr/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/kodi2home.py", line 112, in run_recv_home
    await self.connect_to_home()
  File "/kodi2home.py", line 66, in connect_to_home
    self.websocket = await websockets.connect(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/websockets/legacy/client.py", line 647, in __await_impl_timeout__
    return await self.__await_impl__()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/websockets/legacy/client.py", line 654, in __await_impl__
    await protocol.handshake(
  File "/usr/lib/python3.11/site-packages/websockets/legacy/client.py", line 325, in handshake
    raise InvalidStatusCode(status_code, response_headers)
websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 502

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/kodi2home.py", line 185, in <module>
    main()
  File "/kodi2home.py", line 180, in main
    loop = asyncio.get_running_loop()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: no running event loop
s6-rc: info: service legacy-services: stopping
s6-rc: info: service legacy-services successfully stopped
s6-rc: info: service legacy-cont-init: stopping
s6-rc: info: service legacy-cont-init successfully stopped
s6-rc: info: service fix-attrs: stopping
s6-rc: info: service fix-attrs successfully stopped
s6-rc: info: service s6rc-oneshot-runner: stopping
s6-rc: info: service s6rc-oneshot-runner successfully stopped

With no watchdog but “start on boot” enabled:

s6-rc: info: service s6rc-oneshot-runner: starting
s6-rc: info: service s6rc-oneshot-runner successfully started
s6-rc: info: service fix-attrs: starting
s6-rc: info: service fix-attrs successfully started
s6-rc: info: service legacy-cont-init: starting
s6-rc: info: service legacy-cont-init successfully started
s6-rc: info: service legacy-services: starting
s6-rc: info: service legacy-services successfully started
2024-03-04 22:42:53,834 kodi settings: 10.0.1.11, 8080 9090, kodi, *
2024-03-04 22:42:53,836 Home assistant settings: ws://supervisor/core/api/websocket,  False, *
2024-03-04 22:42:53,876 Kodi is connected {'name': 'Kodi', 'version': {'major': 21, 'minor': 0, 'revision': '87d2d6f84799224f5fe63a6bba3e973e84e18fef', 'tag': 'beta', 'tagversion': '2'}}
2024-03-04 22:42:53,888 Home Assistant: {"type":"auth_required","ha_version":"2024.2.5"}
2024-03-04 22:42:53,891 Home Assistant: {"type":"auth_ok","ha_version":"2024.2.5"}
2024-03-04 22:42:53,891 connected to home
Traceback (most recent call last):
  File "/usr/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 963, in transfer_data
    message = await self.read_message()
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 1033, in read_message
    frame = await self.read_data_frame(max_size=self.max_size)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 1108, in read_data_frame
    frame = await self.read_frame(max_size)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 1165, in read_frame
    frame = await Frame.read(
            ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/websockets/legacy/framing.py", line 68, in read
    data = await reader(2)
           ^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/streams.py", line 730, in readexactly
    raise exceptions.IncompleteReadError(incomplete, n)
asyncio.exceptions.IncompleteReadError: 0 bytes read on a total of 2 expected bytes

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/kodi2home.py", line 178, in main
    loop.run_until_complete( tasks )
  File "/usr/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/kodi2home.py", line 117, in run_recv_home
    home_data = await self.websocket.recv()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 568, in recv
    await self.ensure_open()
  File "/usr/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 939, in ensure_open
    raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedError: no close frame received or sent

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/kodi2home.py", line 185, in <module>
    main()
  File "/kodi2home.py", line 180, in main
    loop = asyncio.get_running_loop()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: no running event loop
s6-rc: info: service legacy-services: stopping
s6-rc: info: service legacy-services successfully stopped
s6-rc: info: service legacy-cont-init: stopping
s6-rc: info: service legacy-cont-init successfully stopped
s6-rc: info: service fix-attrs: stopping
s6-rc: info: service fix-attrs successfully stopped
s6-rc: info: service s6rc-oneshot-runner: stopping
s6-rc: info: service s6rc-oneshot-runner successfully stopped

With restart policy modified via portainer:

2024-03-04 22:52:25,556 Kodi is connected {'name': 'Kodi', 'version': {'major': 21, 'minor': 0, 'revision': '87d2d6f84799224f5fe63a6bba3e973e84e18fef', 'tag': 'beta', 'tagversion': '2'}}
Traceback (most recent call last):
  File "/kodi2home.py", line 178, in main
    loop.run_until_complete( tasks )
  File "/usr/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/kodi2home.py", line 112, in run_recv_home
    await self.connect_to_home()
  File "/kodi2home.py", line 66, in connect_to_home
    self.websocket = await websockets.connect(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/websockets/legacy/client.py", line 647, in __await_impl_timeout__
    return await self.__await_impl__()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/websockets/legacy/client.py", line 654, in __await_impl__
    await protocol.handshake(
  File "/usr/lib/python3.11/site-packages/websockets/legacy/client.py", line 325, in handshake
    raise InvalidStatusCode(status_code, response_headers)
websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 502

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/kodi2home.py", line 185, in <module>
    main()
  File "/kodi2home.py", line 180, in main
    loop = asyncio.get_running_loop()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: no running event loop
s6-rc: info: service legacy-services: stopping
s6-rc: info: service legacy-services successfully stopped
s6-rc: info: service legacy-cont-init: stopping
s6-rc: info: service legacy-cont-init successfully stopped
s6-rc: info: service fix-attrs: stopping
s6-rc: info: service fix-attrs successfully stopped
s6-rc: info: service s6rc-oneshot-runner: stopping
s6-rc: info: service s6rc-oneshot-runner successfully stopped
s6-rc: info: service s6rc-oneshot-runner: starting
s6-rc: info: service s6rc-oneshot-runner successfully started
s6-rc: info: service fix-attrs: starting
s6-rc: info: service fix-attrs successfully started
s6-rc: info: service legacy-cont-init: starting
s6-rc: info: service legacy-cont-init successfully started
s6-rc: info: service legacy-services: starting
s6-rc: info: service legacy-services successfully started
2024-03-04 22:52:31,551 kodi settings: 10.0.1.11, 8080 9090, kodi, *
2024-03-04 22:52:31,554 Home assistant settings: ws://supervisor/core/api/websocket,  False, *
2024-03-04 22:52:31,594 Kodi is connected {'name': 'Kodi', 'version': {'major': 21, 'minor': 0, 'revision': '87d2d6f84799224f5fe63a6bba3e973e84e18fef', 'tag': 'beta', 'tagversion': '2'}}
Traceback (most recent call last):
  File "/kodi2home.py", line 178, in main
    loop.run_until_complete( tasks )
  File "/usr/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/kodi2home.py", line 112, in run_recv_home
    await self.connect_to_home()
  File "/kodi2home.py", line 66, in connect_to_home
    self.websocket = await websockets.connect(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/websockets/legacy/client.py", line 647, in __await_impl_timeout__
    return await self.__await_impl__()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/websockets/legacy/client.py", line 654, in __await_impl__
    await protocol.handshake(
  File "/usr/lib/python3.11/site-packages/websockets/legacy/client.py", line 325, in handshake
    raise InvalidStatusCode(status_code, response_headers)
websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 502

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/kodi2home.py", line 185, in <module>
    main()
  File "/kodi2home.py", line 180, in main
    loop = asyncio.get_running_loop()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: no running event loop
s6-rc: info: service legacy-services: stopping
s6-rc: info: service legacy-services successfully stopped
s6-rc: info: service legacy-cont-init: stopping
s6-rc: info: service legacy-cont-init successfully stopped
s6-rc: info: service fix-attrs: stopping
s6-rc: info: service fix-attrs successfully stopped
s6-rc: info: service s6rc-oneshot-runner: stopping
s6-rc: info: service s6rc-oneshot-runner successfully stopped
s6-rc: info: service s6rc-oneshot-runner: starting
s6-rc: info: service s6rc-oneshot-runner successfully started
s6-rc: info: service fix-attrs: starting
s6-rc: info: service fix-attrs successfully started
s6-rc: info: service legacy-cont-init: starting
s6-rc: info: service legacy-cont-init successfully started
s6-rc: info: service legacy-services: starting
s6-rc: info: service legacy-services successfully started
2024-03-04 22:52:39,281 kodi settings: 10.0.1.11, 8080 9090, kodi, *
2024-03-04 22:52:39,283 Home assistant settings: ws://supervisor/core/api/websocket,  False, *
2024-03-04 22:52:39,328 Kodi is connected {'name': 'Kodi', 'version': {'major': 21, 'minor': 0, 'revision': '87d2d6f84799224f5fe63a6bba3e973e84e18fef', 'tag': 'beta', 'tagversion': '2'}}
2024-03-04 22:52:39,354 Home Assistant: {"type":"auth_required","ha_version":"2024.2.5"}
2024-03-04 22:52:39,356 Home Assistant: {"type":"auth_ok","ha_version":"2024.2.5"}
2024-03-04 22:52:39,357 connected to home
1 Like

Manually changing restart policy via portainer throws alternative errors as it conflicts with the HA supervisor.

I’ve had time to look into this some more:
I believe what is happening here is kodi2home is attempting to start the websocket connection with homeassistant before homeassistant core is fully up and running. The resulting errors cause the watchdog service to attempt to restart kodi2home multiple times until it hits a rate limit and gives up.

supervisor.exceptions.AddonsJobError: Rate limit exceeded, more than 10 calls in 0:30:00

Restarting Home Assistant from dev tools or from updates restarts only Home Assistant. All other containers & addons continue to run. So kodi2home is losing its connection while HA is restarting and throwing these errors.

If you restart Home Assistant by rebooting the system(settings->system->power->advanced options) or by power cycling, all containers are started correctly and this error for kodi2home does not occur.

@DJJo14 I attempted some changes to your code, to try handle the errors I was observing - the first is the error posted above, and a second error that was logged after I fixed the first:

in run_recv_home
    home_data = await self.websocket.recv()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 568, in recv
    await self.ensure_open()
  File "/usr/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 939, in ensure_open
    raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedError: no close frame received or sent

I have opened a pull request on github so you can review these code changes. I am not sure if it is the best way to handle this but it seems to be working for me, hopefully it is of some help.

Perhaps the simpler way in the meantime is to have an automation handle the startup of kodi2home:

trigger:
  - platform: homeassistant
    event: start
condition: []
action:
  - service: hassio.addon_start
    metadata: {}
    data:
      addon: c43b38df_kodi2home
mode: single

This triggers after the homeassistant startup event and does not encounter any errors at this point.

@killthewiseone Thanks for your work, I will use your work. I will maybe modify it a bit.
What you did is reopen the socket when the connection closes, it seems to be correct, but i do not know if the authentication token is still vallit when home assistant restart.

The bug was known for me for a while, but i almost never restart home assistant. And i have not have not have the time to fix it.
Your automation trick will definitely work, maybe check if the add-on is already started and than restart. lets use this as a workaround for now. and i will figure out if i have other options.

I think i fixed it, please let met know, if you still have trouble.
Tanks for using kodi2home

0.3.3

  • Fix restart home assistant. Tanks to @goodlucknow
1 Like

Have just installed here and seems to be working well even after a restart.
If I encounter any errors in use I’ll report back.
Thanks @DJJo14 !