pyTwitchAPI
pyTwitchAPI copied to clipboard
Home Assistant issue with get_channel_followers
Home Assistant is using pyTwitchAPI for the core integration, but it has been broken for a while. I tracked it down to the call to get_channel_followers
stops returning the total
field after a while.
I added a simple ppprint
call, and this is returned at startup.
'_data': {'auth_s': [],
'auth_t': <AuthType.EITHER: 3>,
'body': None,
'in_data': False,
'iter_field': 'data',
'method': 'GET',
'param': {'after': None,
'broadcaster_id': '653186452',
'first': None,
'user_id': None},
'req': <bound method Twitch._api_request of <twitchAPI.twitch.Twitch object at 0xffff2b0ccf50>>,
'split': False,
'url': 'https://api.twitch.tv/helix/channels/followers'},
'data': [],
'total': 158}```
After some time, this is what is returned:
```{'_AsyncIterTwitchObject__idx': 0,
'_data': {'auth_s': [],
'auth_t': <AuthType.EITHER: 3>,
'body': None,
'in_data': False,
'iter_field': 'data',
'method': 'GET',
'param': {'after': None,
'broadcaster_id': '561888476',
'first': None,
'user_id': None},
'req': <bound method Twitch._api_request of <twitchAPI.twitch.Twitch object at 0xffff5bc43f90>>,
'split': False,
'url': 'https://api.twitch.tv/helix/channels/followers'}}```
Restarting Home Assistant resolves the issue. I am thinking it might maybe be some rate limit, or expiring token?
Some more details, since this stops working after some time I would expect authentication to be the main issue.
HA is handling the OAuth handshake itself, since it needs to keep track of the current token, so using client.auto_refresh_auth = False
.
Problem is that the call doesn't fail, but just return without the total
field present. I would have expected some kind of authentication error exception.
hm that should still raise an exception, thanks for the further investigation!
It gets more strange. I changed the code to actually refresh the token when it expired and I still get the same error. I get this at startup.
2024-03-09 00:23:40.582 DEBUG (MainThread) [homeassistant.components.twitch.sensor] Token: {'access_token': 'whr8pbvgcy61jupn0harp5aqd02eh3', 'expires_in': 15759, 'refresh_token': 'f56blnott4u856pwlay1sxrvxbsdlu9vg5v7mn4olhf0cnkz0d', 'scope': ['user:read:follows', 'user:read:subscriptions'], 'token_type': 'bearer', 'expires_at': 1709954343.5376744}
Couple of hours later the log shows the above problem, with this auth, so it did indeed do a token refresh.
2024-03-09 00:29:38.069 DEBUG (MainThread) [homeassistant.components.twitch.sensor] Token: {'access_token': 'whr8pbvgcy61jupn0harp5aqd02eh3', 'expires_in': 15759, 'refresh_token': 'f56blnott4u856pwlay1sxrvxbsdlu9vg5v7mn4olhf0cnkz0d', 'scope': ['user:read:follows', 'user:read:subscriptions'], 'token_type': 'bearer', 'expires_at': 1709954343.5376744}
The log is complaining about that total
error at that stage again.
I restarted Home Assistant, and the error went away. With the exact same token I used before the restart!?!?!
So not sure it even IS related to auth?
Meanwhile I have fixed the bug in Home Assistant that exposed the problem. It was indeed a token refresh bug.
ae975e3725101f50cd72cfa0e61dd393f1576ff9 should fix this issue by correctly raising a UnauthorizedException even if auto_refresh_auth is set to false
That certainly looks good to me.