UniFi Security Gateway

Ok, have added new parameter ‘version’ - set to ‘unifiOS’ and the pyunifi library should do the rest…

tried the new version but unfortunatly i get a:

Logger: custom_components.unifigateway.sensor
Source: custom_components/unifigateway/sensor.py:91
Integration: unifigateway (documentation)
First occurred: 18:06:01 (1 occurrences)
Last logged: 18:06:01

Failed to connect to Unifi Security Gateway: Login failed - status code: 401

Logger: homeassistant.components.sensor
Source: custom_components/unifigateway/sensor.py:88
Integration: Sensor (documentation, issues)
First occurred: 10:35:23 (1 occurrences)
Last logged: 10:35:23

Error while setting up unifigateway platform for sensor
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/urllib3/connection.py", line 169, in _new_conn
    conn = connection.create_connection(
  File "/usr/local/lib/python3.8/site-packages/urllib3/util/connection.py", line 73, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/usr/local/lib/python3.8/socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Try again

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
  File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 382, in _make_request
    self._validate_conn(conn)
  File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 1010, in _validate_conn
    conn.connect()
  File "/usr/local/lib/python3.8/site-packages/urllib3/connection.py", line 353, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.8/site-packages/urllib3/connection.py", line 181, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x7f70986cd0>: Failed to establish a new connection: [Errno -3] Try again

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/requests/adapters.py", line 439, in send
    resp = conn.urlopen(
  File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 755, in urlopen
    retries = retries.increment(
  File "/usr/local/lib/python3.8/site-packages/urllib3/util/retry.py", line 573, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='unifi', port=443): Max retries exceeded with url: /proxy/network/api/login (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f70986cd0>: Failed to establish a new connection: [Errno -3] Try again'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 199, in _async_setup_platform
    await asyncio.shield(task)
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/unifigateway/sensor.py", line 88, in setup_platform
    ctrl = Controller(host, username, password, port, version,
  File "/usr/local/lib/python3.8/site-packages/pyunifi/controller.py", line 89, in __init__
    self._login()
  File "/usr/local/lib/python3.8/site-packages/pyunifi/controller.py", line 161, in _login
    r = self.session.post(login_url, json=params)
  File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 590, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='unifi', port=443): Max retries exceeded with url: /proxy/network/api/login (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f70986cd0>: Failed to establish a new connection: [Errno -3] Try again'))

I have a Unifi Dream Machine with the latest controller and a local user what did I mis

My setup is:

sensor:
  - platform: unifigateway
    host: !secret unifihost
    port: 443
    version: unifiOS
    username: !secret unifiusername
    password: !secret unifipassword
    monitored_conditions:
      - www
      - wan
      - lan
      - wlan
      - alerts
      - firmware

Getting the same error as Alwin, using a UDM Pro;

2021-01-06 06:39:41 ERROR (SyncWorker_15) [custom_components.unifigateway.sensor] Failed to connect to Unifi Security Gateway: Login failed - status code: 401

Could you guys try installing the PyUnifi library directly (e.g. on your main machine) here https://github.com/finish06/pyunifi

There are a number of command line scripts included that can be run, supplying the appropriate parameters. It would be useful to get feedback if you are seeing the same error and if not, confirmation of what parameters you are supplying.

unifi-ls-clients -c 192.168.1.1 -u mylocalusername -p mylocalpassword -v unifiOS -s default -V
/usr/lib/python3/dist-packages/urllib3/connectionpool.py:999: InsecureRequestWarning: Unverified HTTPS request is being made to host '192.168.1.1'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  warnings.warn(
Traceback (most recent call last):
  File "/usr/local/bin/unifi-ls-clients", line 23, in <module>
    c = Controller(args.controller, args.username, args.password, args.port, args.version, args.siteid, ssl_verify=ssl_verify)
  File "/usr/local/lib/python3.8/dist-packages/pyunifi/controller.py", line 89, in __init__
    self._login()
  File "/usr/local/lib/python3.8/dist-packages/pyunifi/controller.py", line 163, in _login
    raise APIError("Login failed - status code: %i" % r.status_code)
pyunifi.controller.APIError: Login failed - status code: 401

That confirms that it is an issue with the underlying PyUnifi library not be able to login.

My component is simply returning back that same error.

With this fork I get most sensors work, I don’t know what he does different, but maybe you can figure it out with him :wink:

You could rework to use aiounifi rather than pyunifi or even move the custom component pieces over to core integration

I see that you are the creator of that library, do you have any documentation on how to use it? I’d like to play around with it a bit. But as far is my google-fu goes, I couldn’t find any documentation unfortunately.

Documentation? :grimacing: Sorry, no documentation. You can find how it is used by looking at main.py or through the home assistant integration. The tests are quite comprehensive as well.

@Alwin_Hummels - were you able to get the sensors for the Firmware and Alerts working at all? Using the fork you mention?

Screen Shot 2021-01-11 at 11.05.26 am

I just saw this thread but had already posted a similar issue here: Unifi OS 6.0.43/Cloud Key 2.0.24 not compatible with Unifi Integration?

While I don’t use the USG, I do run my network off a Cloud Key Gen2 and found the same issue you did.

Is this integration the same supported Unifi integration that comes native with HomeAssistant?

No sorry that are the only sensors that doesn’t work. Thats why I was hopefull for the updated intergration.

No this only applies to a USG, UDM or UDM Pro.

Great component, thank you very much for all this hard work.

Since because of the breach in Unifi, it requested us to use 2FA and i started using it. However now the component is not working. Is there any plans to include 2FA in your component?

Thanks again…

Hi all,

What is the fix for this at the moment best to use the fork mentioned above?

You need to create a new local user without 2FA and use that instead.

from reading the thread 2FA isn’t a problem if you create a local admin (not on ui.com) with read only access.
The issue if i understand correctly is the unifiOS has broken the integration?

Hi, sorry picking up an old thread but seemed to have hit a wall at this point and wondered if there were any pointers you could recall!

From Home Assistant can ping my USG but this integration throws connection errors on start up. Manually navigating to the URL (unifi:8843/api/login and also tried IP instead of hostname) and get a “connection isn’t private…” type response which I suspect in my problem.

I’ve never done anything with SSL certs or the like and don’t want to run the risk of breaking other things, so just wondered if there was a quick skip around this I could investigate (have tried the config with verify_ssl true and false!). Started down the road of research on certs but seems like something I really need to understand before attempting!

Thanks in advance for any suggestions!

Yeah, I actually gave up on it with the self-signed cert and just went with the controller integration which is now actually living in NodeRed and using the Unifi NPM package.

1 Like