Kodi integration suddenly "can't connect"

I recently upgraded Home Assistant to the latest 2021.12.x version and all of a sudden my once-stable Kodi integration stopped working.

I’ve tried everything from:

  1. Rolling back to an earlier HA version
  2. Removing and re-adding the Kodi integration (both via YAML and via the UI)
  3. Toggling the settings within Kodi (such as re-enabling HTTP access, UPnP, removing the username/password etc)

I can access Kodi as usual from my phone and laptop. And I can see connections to Kodi opening for both Websocket and HTTP ports from the HASS IP.

All the integration reports is “cant_connect” during the configuration flow.

Reading the config flow’s code, it looks like it’s excepting out at CannotConnect - however it does connect in order to:

  1. Open the port to Kodi on the remote instance
  2. Verify my credentials are correct - when incorrect, it tells me

Try this:

  1. Delete the Kodi integration.
  2. Restart Home Assistant
  3. Go to Configuration / Devices & Services and delete any “restored” devices or entities assosciated with Kodi (you may have to turn off filtering, top right of the lists).
  4. Restart.
  5. Try adding the Kodi integration again.

If that does not work you may have to dive into the .storage registry to remove any vestige Kodi entities or devices after deleting and restarting.

Thanks. I gave that a shot but it’s still not getting past the config flow.

There aren’t any vestigial Kodi elements in my .storage/core.* files either. I’m beginning to wonder if it’s a deeper issue with the HA integration not communicating properly with Kodi and vice versa.

It’s working fine for me at the moment (2021.12.7).

EDIT: Ah, though I am still on Kodi v18.9 (Leia).

Maybe something’s up there. Kodi 19 has been awfully unstable with my plugins, too. I’m running:

kodi 19.3-2

I have dug a bit deeper and found that the underlying library used in Home Assistant for Kodi (pykodi) is complaining about a duplicate Content-Length HTTP header:

jsonrpc_base.jsonrpc.TransportError: ("Error calling method 'JSONRPC.Ping': Transport Error", ClientResponseError(RequestInfo(url=URL(''), method='POST', headers=<CIMultiDictProxy('Host': '', 'Content-Type': 'application/json', 'Accept': 'application/json-rpc', 'Accept-Encoding': 'gzip, deflate', 'User-Agent': 'Python/3.9 aiohttp/3.8.1', 'Authorization': 'Basic ******', 'Content-Length': '104')>, real_url=URL('')), (), status=400, message='Duplicate Content-Length'))

Sure enough, there are two Content-Length headers on the JSON-RPC responses:

No idea how to fix this, but it seems to be a Kodi problem. If anyone else stumbles across this problem, see if you can reproduce and report back here:

Hello there!
I’m having the same issue presented here, unable to connect to kodi webserver from HA and having two Content-Lenght HTTP Header on kodi webserver side.
Any advancement on this issue ? I saw on the different GitHub issues that it’s an external one from kodi.

Sorry to hear that @hexxotest - I’ve posted bug reports to both Kodi and the upstream library’s developers:

It’s been over a month and no traction.

I’m not familiar with the languages MHD or Kodi are written in to submit a fix - If more users are getting this issue, it might give the issue enough gravity for a fix from someone who knows what they’re doing.

I could also raise this issue with the aiohttp Python library - technically the exception is being raised by this library, meanwhile other useragents (e.g. Chrome/FF) are handling the duplicate Content-Length headers just fine.

Update: With some gentle prodding, there’s a pull request now with a fix! Let’s hope it makes it for the next Kodi release…

1 Like

Kodi 19.4 has been released with a fix to the headers. I’ve just confirmed it’s working now. Anyone else should try upgrading if they get this issue.

1 Like