hassio-addons / addon-unifi

UniFi Network Application - Home Assistant Community Add-ons
https://addons.community
MIT License
278 stars 138 forks source link

Lost connection to UniFi Network / Unexpected error 'ip' #449

Closed mgartin closed 1 year ago

mgartin commented 1 year ago

Problem/Motivation

Recently, my HA installation has started having issues with the unifi integration. Tracking of devices has started to go from Home/Away to Unknown.

I believe the issue is related to a nearby Unifi device (from a neighbor) that is in Adoption mode and therefore lacks some basic metadata. Not sure.

I have checked, and currently there is one device available in Adoption mode in my Unifi web interface. The problem started in HA version 2023.8.3 I think. I upgraded today to version 2023.9.0 see if that helped, but it did not. The output below is from 2023.9.0. I'm running HA core from a rpi 4. The Unifi controller is on another rpi 4, running version 7.4.162. On port 8443.

I tried to reload, and to reconfiigure the Unifi integration after upgrading to HA version 2023.9.0, but it did not help.

Proposed changes

I believe the integration should gracefully accept that the key 'ip' is not present in the metadata.

Debug output (sensitive info removed):


2023-09-08 22:25:52.972 INFO (MainThread) [homeassistant.components.unifi] Will try to reconnect to UniFi Network
2023-09-08 22:25:52.974 DEBUG (MainThread) [aiounifi.interfaces.connectivity] sending (to https://192.168.1.2:8443) get, None, {'allow_redirects': False}
2023-09-08 22:25:53.013 DEBUG (MainThread) [aiounifi.interfaces.connectivity] received (from https://192.168.1.2:8443) 302 application/octet-stream <ClientResponse(https://192.168.1.2:8443) [302 ]>
<CIMultiDictProxy('Location': '/manage', 'Content-Length': '0', 'Date': 'Fri, 08 Sep 2023 20:25:53 GMT')>

2023-09-08 22:25:53.013 DEBUG (MainThread) [aiounifi.interfaces.connectivity] data (from https://192.168.1.2:8443) b''
2023-09-08 22:25:53.013 DEBUG (MainThread) [aiounifi.interfaces.connectivity] Talking to UniFi OS device: False
2023-09-08 22:25:53.013 DEBUG (MainThread) [aiounifi.interfaces.connectivity] sending (to https://192.168.1.2:8443/api/login) post, {'username': 'yeah_no', 'password': 'you_bet', 'remember': True}, {}
2023-09-08 22:25:53.339 DEBUG (MainThread) [aiounifi.interfaces.connectivity] received (from https://192.168.1.2:8443/api/login) 200 application/json <ClientResponse(https://192.168.1.2:8443/api/login) [200 ]>
<CIMultiDictProxy('Vary': 'Origin', 'Access-Control-Allow-Credentials': 'true', 'Access-Control-Expose-Headers': 'Access-Control-Allow-Origin,Access-Control-Allow-Credentials', 'Set-Cookie': 'unifises=xxx; Path=/; Secure; HttpOnly', 'Set-Cookie': 'csrf_token=yyy; Path=/; Secure', 'X-Frame-Options': 'DENY', 'Content-Type': 'application/json;charset=UTF-8', 'Content-Length': '30', 'Date': 'Fri, 08 Sep 2023 20:25:53 GMT')>

2023-09-08 22:25:53.339 DEBUG (MainThread) [aiounifi.interfaces.connectivity] data (from https://192.168.1.2:8443/api/login) b'{"meta":{"rc":"ok"},"data":[]}'
2023-09-08 22:25:53.339 DEBUG (MainThread) [aiounifi.websocket] Websocket WebsocketState.STARTING
2023-09-08 22:25:53.346 DEBUG (MainThread) [aiounifi.websocket] Websocket WebsocketState.RUNNING
2023-09-08 22:25:53.405 ERROR (MainThread) [homeassistant.util.logging] Exception in async_signal_reachable_callback when dispatching 'unifi-reachable-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx': ()
Traceback (most recent call last):
  File "/srv/homeassistant-py3.11/lib/python3.11/site-packages/homeassistant/components/unifi/entity.py", line 214, in async_signal_reachable_callback
    self.async_signalling_callback(ItemEvent.ADDED, self._obj_id)
  File "/srv/homeassistant-py3.11/lib/python3.11/site-packages/homeassistant/components/unifi/entity.py", line 209, in async_signalling_callback
    self.async_write_ha_state()
  File "/srv/homeassistant-py3.11/lib/python3.11/site-packages/homeassistant/helpers/entity.py", line 730, in async_write_ha_state
    self._async_write_ha_state()
  File "/srv/homeassistant-py3.11/lib/python3.11/site-packages/homeassistant/helpers/entity.py", line 830, in _async_write_ha_state
    state, attr = self._async_generate_attributes()
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant-py3.11/lib/python3.11/site-packages/homeassistant/helpers/entity.py", line 773, in _async_generate_attributes
    attr.update(self.state_attributes or {})
                ^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant-py3.11/lib/python3.11/site-packages/homeassistant/components/device_tracker/config_entry.py", line 411, in state_attributes
    if ip_address := self.ip_address:
                     ^^^^^^^^^^^^^^^
  File "/srv/homeassistant-py3.11/lib/python3.11/site-packages/homeassistant/components/unifi/device_tracker.py", line 254, in ip_address
    return self.entity_description.ip_address_fn(self.controller.api, self._obj_id)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant-py3.11/lib/python3.11/site-packages/homeassistant/components/unifi/device_tracker.py", line 197, in <lambda>
    ip_address_fn=lambda api, obj_id: api.devices[obj_id].ip,
                                      ^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant-py3.11/lib/python3.11/site-packages/aiounifi/models/device.py", line 719, in ip
    return self.raw["ip"]
           ~~~~~~~~^^^^^^
KeyError: 'ip'

2023-09-08 22:25:53.515 INFO (MainThread) [homeassistant.components.unifi] Connected to UniFi Network
2023-09-08 22:25:53.538 DEBUG (MainThread) [aiounifi.websocket] {"meta":{"rc":"ok","message":"alert"},"data":[{"category":"SYSTEM","id":"xxxxxxxxxxxxxxxxxxxxxxxx","key":"ADMIN_ACCESS","message":"xxxxxxx opened UniFi Network via the web.","message_raw":"{ADMIN} opened UniFi Network via the {PLATFORM}.","parameters":{"ADMIN":{"id":"xxxxxxxxxxxxxxxxxxxxxxxx","name":"xxxxxxx"},"CONSOLE_NAME":{"id":"Xxxx","name":"Xxxx"},"PLATFORM":{"name":"web"}},"replace_alerts":[],"severity":"INFO","show_on_dashboard":false,"status":"NEW","target":"ADMIN","timestamp":1694204753345,"title_raw":"Admin Accessed UniFi Network","type":"ADMIN_ACCESS"}]}
2023-09-08 22:25:58.294 ERROR (MainThread) [aiounifi.websocket] Unexpected error 'ip'
2023-09-08 22:25:58.295 DEBUG (MainThread) [aiounifi.websocket] Websocket WebsocketState.DISCONNECTED
2023-09-08 22:25:58.295 WARNING (MainThread) [homeassistant.components.unifi] Lost connection to UniFi Network

...

2023-09-08 22:26:56.711 INFO (MainThread) [homeassistant.components.unifi] Connected to UniFi Network                                                                                                                                       
2023-09-08 22:26:56.751 DEBUG (MainThread) [aiounifi.websocket] {"meta":{"rc":"ok","message":"alert"},"data":[{"category":"SYSTEM","id":"..." ... }]}}
...
2023-09-08 22:27:05.565 ERROR (MainThread) [aiounifi.websocket] Unexpected error 'ip'                                                                                                                                                       
2023-09-08 22:27:05.567 DEBUG (MainThread) [aiounifi.websocket] Websocket WebsocketState.DISCONNECTED                                                                                                                                       
2023-09-08 22:27:05.567 WARNING (MainThread) [homeassistant.components.unifi] Lost connection to UniFi Network                                                                                                                              
sinclairpaul commented 1 year ago

This is the repo for the addon, not the integration, you would need to log your issue against the Home Assistant repo as there is really nothing that can be done here.

mgartin commented 1 year ago

Ok, sorry. For other landing here, there seems to already be an issue on this for the integration: https://github.com/home-assistant/core/issues/98729