core: Error with Twitch integration when updating

The problem

Hello, I have successfully connected the new Twitch integration on 2023.10 and at the time of integration, all my followed channels were retrieved with the correct status (streaming/offline). However, since yesterday, they haven’t updated. Checking the logs I have the following:

Logger: homeassistant.helpers.entity
Source: helpers/entity.py:732
First occurred: 03:57:31 (123858 occurrences)
Last logged: 20:20:13

    Update for sensor.bengineering fails
    Update for sensor.annacramling fails
    Update for sensor.hueywj fails
    Update for sensor.amazonian fails
    Update for sensor.benjamin_wheeler fails

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 732, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 991, in async_device_update
    await self.async_update()
  File "/usr/src/homeassistant/homeassistant/components/twitch/sensor.py", line 133, in async_update
    followers = (await self._client.get_channel_followers(self._channel.id)).total
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'ChannelFollowersResult' object has no attribute 'total'

After enabling debug logging I also found this (it repeats for each account I follow):

2023-10-05 00:02:05.647 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.lafistreams fails
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 1155, in _create_direct_connection
    hosts = await asyncio.shield(host_resolved)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 874, in _resolve_host
    addrs = await self._resolver.resolve(host, port, family=self._family)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/resolver.py", line 33, in resolve
    infos = await self._loop.getaddrinfo(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 867, in getaddrinfo
    return await self.run_in_executor(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/socket.py", line 962, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno -3] Try again

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

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 732, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 991, in async_device_update
    await self.async_update()
  File "/usr/src/homeassistant/homeassistant/components/twitch/sensor.py", line 139, in async_update
    await self._async_add_user_attributes()
  File "/usr/src/homeassistant/homeassistant/components/twitch/sensor.py", line 163, in _async_add_user_attributes
    sub = await self._client.check_user_subscription(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/twitchAPI/twitch.py", line 1645, in check_user_subscription
    return await self._build_result('GET', 'subscriptions/user', param, AuthType.EITHER, [AuthScope.USER_READ_SUBSCRIPTIONS], UserSubscription)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/twitchAPI/twitch.py", line 516, in _build_result
    response = await self._api_request(method, session, _url, auth_type, auth_scope, data=body_data)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/twitchAPI/twitch.py", line 441, in _api_request
    req = await session.request(method, url, headers=headers, json=data)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 536, in _request
    conn = await self._connector.connect(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 540, in connect
    proto = await self._create_connection(req, traces, timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 901, in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 1169, in _create_direct_connection
    raise ClientConnectorError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host api.twitch.tv:443 ssl:default [Try again]

What version of Home Assistant Core has the issue?

core-2023.10.0

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Twitch

Link to integration documentation on our website

https://www.home-assistant.io/integrations/twitch/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

About this issue

  • Original URL
  • State: open
  • Created 9 months ago
  • Reactions: 3
  • Comments: 24 (6 by maintainers)

Most upvoted comments

I get baffled how some simple integrations go into the HA Core and are broken for months (or “since forever” as can be seen by the amount of integration bugs abandoned here) and some other very complex integrations and almost flawless end up in HACS as “secondary citizens”, such as LG devices.

At the start, I thought naively there would be a “promotion process” of some sort to move in-dev integrations from HACS into the Core once they’re considered useful and stable… but now I see it’s just a matter of where the person is comfortable coding and allowed to do PRs (or leaving bugs).

I know this is open source and all, not to shoot the ~messenger~ developer, Joost is definitely busy with school, life happens, etc… Been there a bunch of times. But hey, this bug was posted shortly after a blog mention about an official integration and no one pulled it out, reverted the changes, or fixed it in three months? There’s a completely broken integration in HA. But I guess it’s so rarely used (125 installations so far) it’s not worth thinking this reduces the overall quality perception of HA.

Yes, we use a bearer token. But we’re doing more requests for one channel. I will try to optimize this and split out the current sensor into more sensors before the next release as I think this is the only way to proper fix this.

If this is throttling related, a more robust approach may be to implement a “push” model using webhooks: https://dev.twitch.tv/docs/eventsub/

Chiming in: I disabled polling on the integration and set up an automation to poll 2 channels every 5 minutes. It still failed. Strangely enough, it starts working again after a reload, as if it got stuck on something that’s not rate limiting. (Is it not refreshing auth tokens properly or something? …)

Going to try scheduling an automated reload of the integration to see what that does…

Update: Regularly reloading the integration, combined with the more limited polling, indeed appears to make it work.