tomasbedrich / home-assistant-hikconnect

A Home Assistant integration to communicate with Hikvision smart doorbells via Hik-Connect cloud.
40 stars 9 forks source link

log flooded with error 401 unauthorized messages #27

Closed vobele closed 10 months ago

vobele commented 2 years ago

Those errors began suddenly last night and didn't stop until a fresh reboot of home assistant in the morning. During 8 hours the home-assistant.log file grow up to 2.4MB with over 33tsd of those lines. The block of messages repeated itself after 20 seconds and then after 40 seconds. (so two times every minute)

The last line said 'error 401 Unauthorized', so probably the reboot solved this issue by a logging into the hik-connect service again.

The only sensor of the HikConnect integration I am using (and therefore haven't deactivated) is the call_status of an intercom device (DS-KD8003). And it didn't show any signs of being not available during that time.

I would have the following questions: 1) was the integration really not working during this period (since the sensor was still available)? 2) what could be the reason behind it (I mean, may be it was just one unsuccessful login)? And could the integration be improved to perform the same fresh login process which obviously solved the issue after I have rebooted home assistant? 3) apart from looking regularly into the log file: is there a way to be notified when this situation will happen again?

THX.

2022-03-17 22:33:05 ERROR (MainThread) [custom_components.hikconnect.sensor] Update of call status failed 10 times in a row
Traceback (most recent call last):
  File "/config/custom_components/hikconnect/sensor.py", line 63, in async_update
    res = await asyncio.wait_for(get_call_status_coro, SCAN_INTERVAL_TIMEOUT.seconds)
  File "/usr/local/lib/python3.9/asyncio/tasks.py", line 481, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.9/site-packages/hikconnect/api.py", line 224, in get_call_status
    async with self.client.get(
  File "/usr/local/lib/python3.9/site-packages/aiohttp/client.py", line 1138, in __aenter__
    self._resp = await self._coro
  File "/usr/local/lib/python3.9/site-packages/aiohttp/client.py", line 640, in _request
    resp.raise_for_status()
  File "/usr/local/lib/python3.9/site-packages/aiohttp/client_reqrep.py", line 1004, in raise_for_status
    raise ClientResponseError(
aiohttp.client_exceptions.ClientResponseError: 401, message='Unauthorized', url=URL('https://api.hik-connect.com/v3/devconfig/v1/call/<SENSITIVE>/status')
2022-03-17 22:33:05 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.videoklingel_call_status fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 535, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 744, in async_device_update
    raise exc
  File "/config/custom_components/hikconnect/sensor.py", line 63, in async_update
    res = await asyncio.wait_for(get_call_status_coro, SCAN_INTERVAL_TIMEOUT.seconds)
  File "/usr/local/lib/python3.9/asyncio/tasks.py", line 481, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.9/site-packages/hikconnect/api.py", line 224, in get_call_status
    async with self.client.get(
  File "/usr/local/lib/python3.9/site-packages/aiohttp/client.py", line 1138, in __aenter__
    self._resp = await self._coro
  File "/usr/local/lib/python3.9/site-packages/aiohttp/client.py", line 640, in _request
    resp.raise_for_status()
  File "/usr/local/lib/python3.9/site-packages/aiohttp/client_reqrep.py", line 1004, in raise_for_status
    raise ClientResponseError(
aiohttp.client_exceptions.ClientResponseError: 401, message='Unauthorized', url=URL('https://api.hik-connect.com/v3/devconfig/v1/call/<SENSITIVE>/status')
vobele commented 2 years ago

Exact the same pattern again today, started around 6am (last time around 22pm). Issue remained until reboot of HA.

Question 1) is hereby answered: the integration (of course) is not working when it happens.

Probably something (may be an invalid token?) prevents any further login retries.

Edit: 2 days later again, this time around 13am.

aleksaka commented 11 months ago

I can't log in to my account

aleksaka commented 11 months ago

image

tomasbedrich commented 11 months ago

Hello, when you try to login using your username via Hik-Connect app, does it show CAPTCHA or not?

aleksaka commented 11 months ago

No, it is not displayed

tomasbedrich commented 11 months ago

Does restart of Home Assistant also solve the issue for you? The same as for @vobele?

aleksaka commented 11 months ago

I guess that could be because of this

aleksaka commented 11 months ago

image

aleksaka commented 11 months ago

when you log in, you need to solve the captcha, but there is no such thing in the integration

aleksaka commented 11 months ago

thank you, it turned out to log into my account

tomasbedrich commented 10 months ago

when you log in, you need to solve the captcha, but there is no such thing in the integration

So you needed to solve the CAPTCHA and afterwards the integration started working? Because that would be exactly the expected behavior.

Also relevant to https://github.com/tomasbedrich/hikconnect/pull/12 : I saw on the forum, that folks are creating REST sensor, which I assume is repeatedly trying to login. This approach could also introduce this issue.