core: Amcrest Integration creates duplicate entities

The problem

Sometimes on a restart of home assistant, the Amcrest integration is creating duplicate entities postfixed with _#. This is problematic for using these devices in automations. It seems to happen when the device is not available at the time of restart. Additionally, if this happens, the integration will no longer retry and come online when the device is available. HA needs to be restarted for it to work.

What version of Home Assistant Core has the issue?

core-2022.5.4

What was the last working version of Home Assistant Core?

n/a

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Amcrest

Link to integration documentation on our website

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

Diagnostics information

No response

Example YAML snippet

  - host: 10.100.1.143
    username: admin
    password: !secret amcrest_frontdoor_password
    name: Front Door Bell
    resolution: high
    stream_source: rtsp
    scan_interval: 15
    binary_sensors:
      - online
    switches:
      - privacy_mode
  - host: 10.100.1.143
    username: admin
    password: !secret amcrest_frontdoor_password
    name: Front Door Bell Sub
    resolution: low
    stream_source: rtsp
    scan_interval: 305



### Anything in the logs that might be useful for us?

```txt
Logger: homeassistant.components.switch
Source: components/amcrest/__init__.py:205
Integration: Switch (documentation, issues)
First occurred: 7:35:40 AM (1 occurrences)
Last logged: 7:35:40 AM

amcrest: Error on device update!
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/anyio/_core/_sockets.py", line 127, in try_connect
    stream = await asynclib.connect_tcp(remote_host, remote_port, local_address)
  File "/usr/local/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 1518, in connect_tcp
    await get_running_loop().create_connection(StreamProtocol, host, port,
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 1056, in create_connection
    raise exceptions[0]
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 1041, in create_connection
    sock = await self._connect_sock(
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 955, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 502, in sock_connect
    return await fut
  File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 537, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
OSError: [Errno 113] Connect call failed ('10.100.1.143', 80)

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

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/httpcore/_exceptions.py", line 8, in map_exceptions
    yield
  File "/usr/local/lib/python3.9/site-packages/httpcore/backends/asyncio.py", line 101, in connect_tcp
    stream: anyio.abc.ByteStream = await anyio.connect_tcp(
  File "/usr/local/lib/python3.9/site-packages/anyio/_core/_sockets.py", line 184, in connect_tcp
    raise OSError('All connection attempts failed') from cause
OSError: All connection attempts failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/httpx/_transports/default.py", line 60, in map_httpcore_exceptions
    yield
  File "/usr/local/lib/python3.9/site-packages/httpx/_transports/default.py", line 353, in handle_async_request
    resp = await self._pool.handle_async_request(req)
  File "/usr/local/lib/python3.9/site-packages/httpcore/_async/connection_pool.py", line 253, in handle_async_request
    raise exc
  File "/usr/local/lib/python3.9/site-packages/httpcore/_async/connection_pool.py", line 237, in handle_async_request
    response = await connection.handle_async_request(request)
  File "/usr/local/lib/python3.9/site-packages/httpcore/_async/connection.py", line 86, in handle_async_request
    raise exc
  File "/usr/local/lib/python3.9/site-packages/httpcore/_async/connection.py", line 63, in handle_async_request
    stream = await self._connect(request)
  File "/usr/local/lib/python3.9/site-packages/httpcore/_async/connection.py", line 111, in _connect
    stream = await self._network_backend.connect_tcp(**kwargs)
  File "/usr/local/lib/python3.9/site-packages/httpcore/backends/auto.py", line 23, in connect_tcp
    return await self._backend.connect_tcp(
  File "/usr/local/lib/python3.9/site-packages/httpcore/backends/asyncio.py", line 101, in connect_tcp
    stream: anyio.abc.ByteStream = await anyio.connect_tcp(
  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)
httpcore.ConnectError: All connection attempts failed

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

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/amcrest/http.py", line 345, in _async_command
    resp = await client.get(url)
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1729, in get
    return await self.request(
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1506, in request
    return await self.send(request, auth=auth, follow_redirects=follow_redirects)
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1593, in send
    response = await self._send_handling_auth(
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1621, in _send_handling_auth
    response = await self._send_handling_redirects(
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1658, in _send_handling_redirects
    response = await self._send_single_request(request)
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1695, in _send_single_request
    response = await transport.handle_async_request(request)
  File "/usr/local/lib/python3.9/site-packages/httpx/_transports/default.py", line 353, in handle_async_request
    resp = await self._pool.handle_async_request(req)
  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 77, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ConnectError: All connection attempts failed

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 431, in _async_add_entity
    await entity.async_device_update(warning=False)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 736, in async_device_update
    await task
  File "/usr/src/homeassistant/homeassistant/components/amcrest/switch.py", line 89, in async_update
    io_res = (await self._api.async_privacy_config()).splitlines()[0].split("=")[1]
  File "/usr/local/lib/python3.9/site-packages/amcrest/privacy_mode.py", line 42, in async_privacy_config
    return await self._async_get_config("LeLensMask")
  File "/usr/local/lib/python3.9/site-packages/amcrest/http.py", line 464, in _async_get_config
    ret = await self.async_command(
  File "/usr/src/homeassistant/homeassistant/components/amcrest/__init__.py", line 205, in async_command
    ret = await super().async_command(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/amcrest/http.py", line 229, in async_command
    await self._async_generate_token()
  File "/usr/local/lib/python3.9/site-packages/amcrest/http.py", line 156, in _async_generate_token
    resp = (await self._async_command(cmd)).content.decode()
  File "/usr/local/lib/python3.9/site-packages/amcrest/http.py", line 361, in _async_command
    raise CommError(error) from error
amcrest.exceptions.CommError: All connection attempts failed

The above is the cause for the issue.

I do see entries for the device coming back online but the cameras now never do.

Logger: homeassistant.components.amcrest
Source: components/amcrest/__init__.py:261
Integration: Amcrest (documentation, issues)
First occurred: 7:36:42 AM (2 occurrences)
Last logged: 7:36:57 AM

Front Door Bell camera back online

Additional information

Screenshot_2022-05-16_08-17-10

We can see the camera.* are still offline. and the new entity.

About this issue

Most upvoted comments

Perhaps setup each camera as an integration, vs. yaml. If startup doesn’t detect a camera you may be able to reload it. Without having to restart HA.

There hasn’t been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.