LAB02-Research / HASS.Agent-Integration

HASS.Agent's Home Assistant integration. Adds notifications and mediaplayer capabilities to HASS.Agent - a Windows based client (companion app) for Home Assistant.
MIT License
101 stars 17 forks source link

Device stays in 'Failed to set up' state after device becomes available. #19

Open Flightkick opened 1 year ago

Flightkick commented 1 year ago

If a HASS.Agent host is down at the time of Home Assistant startup, the setup process aborts with an error 'max retries reached'. The device stays in the 'Failed to set up' state even after the HASS.Agent host becomes available (see Integrations overview screenshot). While sending commands then continues to work as expected, other services (like sending notifications) stay unavailable.

Screenshot showing device in error state

To reproduce this behavior

  1. Add configuration for a HASS.Agent host
  2. Hibernate the HASS.Agent host machine
  3. Restart Home Assistant
  4. Wait for the max attempts reached error
  5. Turn on the HASS.Agent host machine
  6. Note that the device is still marked as 'Failed to set up' in System > Devices & Services > Integrations and the 'Notifications: Send a notification with \<devicename>' is unavailable/missing in Developer Tools > Services.

Expected behavior Home Assistant should retry setting up the device after the device becomes available.

Error logs in Home Assistant

Error setting up entry XXXX for hass_agent
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/urllib3/connection.py", line 174, in _new_conn
    conn = connection.create_connection(
  File "/usr/local/lib/python3.10/site-packages/urllib3/util/connection.py", line 95, in create_connection
    raise err
  File "/usr/local/lib/python3.10/site-packages/urllib3/util/connection.py", line 85, in create_connection
    sock.connect(sa)
OSError: [Errno 113] Host is unreachable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 398, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/local/lib/python3.10/site-packages/urllib3/connection.py", line 239, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "/usr/local/lib/python3.10/http/client.py", line 1282, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/lib/python3.10/http/client.py", line 1328, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.10/http/client.py", line 1277, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.10/http/client.py", line 1037, in _send_output
    self.send(msg)
  File "/usr/local/lib/python3.10/http/client.py", line 975, in send
    self.connect()
  File "/usr/local/lib/python3.10/site-packages/urllib3/connection.py", line 205, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.10/site-packages/urllib3/connection.py", line 186, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f40526b3dc0>: Failed to establish a new connection: [Errno 113] Host is unreachable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/requests/adapters.py", line 489, in send
    resp = conn.urlopen(
  File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 787, in urlopen
    retries = retries.increment(
  File "/usr/local/lib/python3.10/site-packages/urllib3/util/retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='XXX.XXX.XXX.XXX', port=5115): Max retries exceeded with url: /info (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f40526b3dc0>: Failed to establish a new connection: [Errno 113] Host is unreachable'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 382, in async_setup
    result = await component.async_setup_entry(hass, self)
  File "/config/custom_components/hass_agent/__init__.py", line 120, in async_setup_entry
    response = await hass.async_add_executor_job(get_device_info)
  File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/hass_agent/__init__.py", line 118, in get_device_info
    return requests.get(f"{url}/info", timeout=10)
  File "/usr/local/lib/python3.10/site-packages/requests/api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/adapters.py", line 565, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='XXX.XXX.XXX.XXX', port=5115): Max retries exceeded with url: /info (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f40526b3dc0>: Failed to establish a new connection: [Errno 113] Host is unreachable'))
VNRARA commented 1 year ago

Still no reaction from dev?

DrR0X-glitch commented 8 months ago

Thanks for the information amazing report👍, unfortunately the previous Dev has been away for the past year. I will look into this soon.

spider7611 commented 7 months ago

Same problem. Manually reload configuration and after workig good, but after HA restart and the pc is offline the problem come back. :(

alphasixtyfive commented 6 months ago

If you have a WOL switch for the PC you could create an automation that reloads config entity when PC is turned on. In theory this might be a temporary solution.

  - service: homeassistant.reload_config_entry
    data:
      entity_id: media_player.YOUR_PC
DrR0X-glitch commented 6 months ago

If you have a WOL switch for the PC you could create an automation that reloads config entity when PC is turned on. In theory this might be a temporary solution.

  - service: homeassistant.reload_config_entry
    data:
      entity_id: media_player.YOUR_PC

I will have a look at this issue a bit more soon, but we are also considering removing support for local API in the forked version as there is a fair bit of stuff that it is disadvantaged in. As well as the fact that so many integrations are mqtt only now so it having it as a requirement isn't that much of an issue.

The problem with this issue is that it is difficult to tackle in a nice way. Because of the fact that local API is a one way data transfer it means that there is no way to send a message from hass.agent to ha when it is booted. The next best option is to check once every 60s or so if the PC is online, this is how other integrations tackle it, but it creates a lot of unnecessary traffic resource usage.

alphasixtyfive commented 6 months ago

If you have a WOL switch for the PC you could create an automation that reloads config entity when PC is turned on. In theory this might be a temporary solution.

  - service: homeassistant.reload_config_entry
    data:
      entity_id: media_player.YOUR_PC

I will have a look at this issue a bit more soon, but we are also considering removing support for local API in the forked version as there is a fair bit of stuff that it is disadvantaged in. As well as the fact that so many integrations are mqtt only now so it having it as a requirement isn't that much of an issue.

The problem with this issue is that it is difficult to tackle in a nice way. Because of the fact that local API is a one way data transfer it means that there is no way to send a message from hass.agent to ha when it is booted. The next best option is to check once every 60s or so if the PC is online, this is how other integrations tackle it, but it creates a lot of unnecessary traffic resource usage.

Simple heartbeat is probably the only way. I would not be concerned about extra traffic to be honest. The amount of telemetry IOT devices are sending it is more than acceptable.

DrR0X-glitch commented 6 months ago

Okay, I'll hopefully work on this later this week. But due to the current situation the only way it will be fixed for end users is manually installing and updating to v2.