Unifi Controller Integration not working

Can anyone offer any advice on what’s going on here?

I’m trying to use the Unifi integration for presence detection by detecting when our phones connect to our wifi network.

I’m 90% sure I have the config right but when I restart HA (Hass.io) I see the error below in the logs.

Full log below shows issues with connection.py file.
I’m lost at this point, any help would be appreciated…

Log Details (ERROR)
Sun Apr 21 2019 11:11:55 GMT+0800 (Australian Western Standard Time)
Error setting up platform unifi
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 159, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw)
  File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 80, in create_connection
    raise err
  File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 70, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 343, in _make_request
    self._validate_conn(conn)
  File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 839, in _validate_conn
    conn.connect()
  File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 301, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 168, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7f8224b3c8>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/local/lib/python3.7/site-packages/urllib3/util/retry.py", line 398, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='192.168.1.24', port=8443): Max retries exceeded with url: /api/login (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f8224b3c8>: Failed to establish a new connection: [Errno 111] Connection refused'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/device_tracker/__init__.py", line 177, in async_setup_platform
    platform.get_scanner, hass, {DOMAIN: p_config})
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/unifi/device_tracker.py", line 80, in get_scanner
    site_id=site_id, ssl_verify=verify_ssl)
  File "/usr/local/lib/python3.7/site-packages/pyunifi/controller.py", line 89, in __init__
    self._login()
  File "/usr/local/lib/python3.7/site-packages/pyunifi/controller.py", line 137, in _login
    r = self.session.post(login_url, json=params)
  File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 581, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 646, in send

configuration.yaml

sensor: !include sensors.yaml
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
cover: !include cover.yaml
scene: !include scenes.yaml
device_tracker: !include device_tracker.yaml

device_tracker.yaml

- platform: unifi
  host: 192.168.1.24
  username: !secret ubiquiti_username
  password: !secret ubiquiti_password
  verify_ssl: false
  detection_time: 60
  ssid_filter:
    - '[MY SSID]'

EDIT:
So, device tracker seemed to work for a short period then stopped.
Now only getting this error:

Log Details (ERROR)
Sun Apr 21 2019 12:05:07 GMT+0800 (Australian Western Standard Time)
Error doing job: SSL handshake failed
Traceback (most recent call last):
  File "uvloop/sslproto.pyx", line 500, in uvloop.loop.SSLProtocol._on_handshake_complete
  File "uvloop/sslproto.pyx", line 484, in uvloop.loop.SSLProtocol._do_handshake
  File "/usr/local/lib/python3.7/ssl.py", line 763, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: SSLV3_ALERT_CERTIFICATE_UNKNOWN] sslv3 alert certificate unknown (_ssl.c:1056)

is your cloudkey/ cloudkey software up and running? I.E. Can you log into your unify account and access your devices?

I think that you may need to add the site_id.
this is how mine looks:
> - platform: unifi

        host: 192.168.X.XXX
        port: 8443
        username: username
        password: password
        verify_ssl: false
        site_id: XXXXX
        ssid_filter:
          - 'XXXXX'

UPDATE: All working.

Had to change the verify_ssl: false to verify_ssl: False
Didn’t seem to need to include site_id: xxxxxx
Also, deleted the original Unifi Controller integration and re-installed.

However, presence detection seems to be very slow. It generally takes around 60 - 90 seconds to update from not-home to home.

Can anyone recommend any faster options for presence detection?
I’m hoping to get it down to around 20 seconds.

I don’t have the Unifi Cloud key. I have Home Assistant (Hass.io) running on a RPI 3B+ and the Unifi Controller software running on my iMac which is always on to access my Unifi USG and 2 x Unifi UAP-AC-LITE Access Points.

The issue was with the config.

Was just making sure. Cloud key was a source of my issues before I moved to the software running in docker.

That’s good to know. I was actually thinking of getting the Cloud Key. Might not worry about it now.

Cloudkey 2.0 might be better. 1.0 has issues shutting down. If it shuts down during a write the database gets corrupted and nothing works. This is my main issue with the device. That and I ran into an issue where I couldn’t upgrade the firmware because they removed the git page for the software. If you’re running the software, there is no need for the cloudkey.

From looking at the source code, as far as I can tell, it uses the default scan interval of 12 seconds. So it should be looking to see if your device is connected every 12 seconds.