tomasbedrich / home-assistant-hikconnect

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

Integration doesn't start after reboot of Home Assistant #8

Closed pepsonEL closed 2 years ago

pepsonEL commented 2 years ago

After reboot my Home Assistant i have status red ? for entities status

Logs show this:

Logger: homeassistant.components.lock
Source: custom_components/hikconnect/lock.py:18
Integration: Zamek (documentation, issues)
First occurred: 13:12:22 (1 occurrences)
Last logged: 13:12:22

Error while setting up hikconnect platform for lock
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/httpx/_transports/default.py", line 61, in map_httpcore_exceptions
    yield
  File "/usr/local/lib/python3.9/site-packages/httpx/_transports/default.py", line 281, in handle_async_request
    ) = await self._pool.handle_async_request(
  File "/usr/local/lib/python3.9/site-packages/httpcore/_async/connection_pool.py", line 234, in handle_async_request
    response = await connection.handle_async_request(
  File "/usr/local/lib/python3.9/site-packages/httpcore/_async/connection.py", line 136, in handle_async_request
    self.socket = await self._open_socket(timeout)
  File "/usr/local/lib/python3.9/site-packages/httpcore/_async/connection.py", line 163, in _open_socket
    return await self._backend.open_tcp_stream(
  File "/usr/local/lib/python3.9/site-packages/httpcore/_backends/auto.py", line 44, in open_tcp_stream
    return await self.backend.open_tcp_stream(
  File "/usr/local/lib/python3.9/site-packages/httpcore/_backends/anyio.py", line 154, in open_tcp_stream
    stream = await TLSStream.wrap(
  File "/usr/local/lib/python3.9/contextlib.py", line 137, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/usr/local/lib/python3.9/site-packages/httpcore/_exceptions.py", line 12, in map_exceptions
    raise to_exc(exc) from None
httpcore.ConnectTimeout

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

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 249, in _async_setup_platform
    await asyncio.shield(task)
  File "/config/custom_components/hikconnect/lock.py", line 18, in async_setup_entry
    await api.login(entry.data["username"], entry.data["password"])
  File "/usr/local/lib/python3.9/site-packages/hikconnect/api.py", line 57, in login
    res = await self.client.post(f"{self.BASE_URL}/v3/users/login/v2", data=data)
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1825, in post
    return await self.request(
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1494, in request
    response = await self.send(
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1586, in send
    response = await self._send_handling_auth(
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1616, in _send_handling_auth
    response = await self._send_handling_redirects(
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1655, in _send_handling_redirects
    response = await self._send_single_request(request, timeout)
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1699, in _send_single_request
    ) = await transport.handle_async_request(
  File "/usr/local/lib/python3.9/site-packages/httpx/_transports/default.py", line 276, in handle_async_request
    (
  File "/usr/local/lib/python3.9/contextlib.py", line 137, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/usr/local/lib/python3.9/site-packages/httpx/_transports/default.py", line 78, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ConnectTimeout

When i reload again Integration Hik-Connect all start work ok and status has no RED !

tomasbedrich commented 2 years ago

Hey, thanks for a report!

It seems that there is some race condition between network startup (when internet is ready) and the integration setup code. This must be addressed by some retry logic inside the integration itself apparently.

pepsonEL commented 2 years ago

Yes. I thought so too, but I don't know programming like you

pepsonEL commented 2 years ago

For me now it works ok with latest version.

tomasbedrich commented 2 years ago

Today happened to me as well:

Logger: homeassistant.components.lock
Source: custom_components/hikconnect/lock.py:18
Integration: Lock (documentation, issues)
First occurred: 23:19:23 (1 occurrences)
Last logged: 23:19:23

Error while setting up hikconnect platform for lock
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/httpx/_transports/default.py", line 61, in map_httpcore_exceptions
    yield
  File "/usr/local/lib/python3.9/site-packages/httpx/_transports/default.py", line 281, in handle_async_request
    ) = await self._pool.handle_async_request(
  File "/usr/local/lib/python3.9/site-packages/httpcore/_async/connection_pool.py", line 234, in handle_async_request
    response = await connection.handle_async_request(
  File "/usr/local/lib/python3.9/site-packages/httpcore/_async/connection.py", line 136, in handle_async_request
    self.socket = await self._open_socket(timeout)
  File "/usr/local/lib/python3.9/site-packages/httpcore/_async/connection.py", line 163, in _open_socket
    return await self._backend.open_tcp_stream(
  File "/usr/local/lib/python3.9/site-packages/httpcore/_backends/auto.py", line 44, in open_tcp_stream
    return await self.backend.open_tcp_stream(
  File "/usr/local/lib/python3.9/site-packages/httpcore/_backends/anyio.py", line 154, in open_tcp_stream
    stream = await TLSStream.wrap(
  File "/usr/local/lib/python3.9/contextlib.py", line 137, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/usr/local/lib/python3.9/site-packages/httpcore/_exceptions.py", line 12, in map_exceptions
    raise to_exc(exc) from None
httpcore.ConnectTimeout

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

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 249, in _async_setup_platform
    await asyncio.shield(task)
  File "/config/custom_components/hikconnect/lock.py", line 18, in async_setup_entry
    await api.login(entry.data["username"], entry.data["password"])
  File "/usr/local/lib/python3.9/site-packages/hikconnect/api.py", line 57, in login
    res = await self.client.post(f"{self.BASE_URL}/v3/users/login/v2", data=data)
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1825, in post
    return await self.request(
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1494, in request
    response = await self.send(
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1586, in send
    response = await self._send_handling_auth(
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1616, in _send_handling_auth
    response = await self._send_handling_redirects(
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1655, in _send_handling_redirects
    response = await self._send_single_request(request, timeout)
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1699, in _send_single_request
    ) = await transport.handle_async_request(
  File "/usr/local/lib/python3.9/site-packages/httpx/_transports/default.py", line 276, in handle_async_request
    (
  File "/usr/local/lib/python3.9/contextlib.py", line 137, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/usr/local/lib/python3.9/site-packages/httpx/_transports/default.py", line 78, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ConnectTimeout
pepsonEL commented 2 years ago

Look THomas is help from HikVision https://community.home-assistant.io/t/hik-connect/342202/27

tomasbedrich commented 2 years ago

Hopefully resolved by 2bef70c8064d72784939c38641f93e56624c3215.

tomasbedrich commented 2 years ago

Nope :( It crashes as soon as network is disconnected in meantime. The integration must be reworked to use something more stable, such as DataUpdateCoordinator.

tomasbedrich commented 2 years ago

Ok, hopefully resolved by ad958003c3c49cc94254f28a75af8414a78625c3. Please try and let me know.

I have changed a few other things – some preparations to be able to display camera + lock entities under the same device in future... It might be needed to wipe+reinstall the integration.