[PROJECT] Driver for Unifi Protect Controllers

Updated Version(s):

  • UnifiProtectAPI.groovy = 0.2.9

Change(s):

  • Handling for a null uptime value has now been implemented. Not sure HOW that happens but if it is a response the API can provide I need to handle it. Thanks to @Busthead for discovering this one and providing the data so I could figure out what was causing it.

Okay, I added your Bridge driver and that appears to have halted the errors.

May I suggest adding to the instructions a list of devices/device classes (APs) that make the Bridge driver required? It's not obvious that the Bridge driver must be installed or errors will result.

Actually... I snuck some changes to the directions in already, specifically mentioning that APs will likely be listed as bridge devices. :slight_smile:

I cannot REQUIRE the bridge driver just because it is possible for someone not to have an AP, or they might disable them from acting as a bridge, in which case no bridges will show up in the API.

This is another case where being able to have some dynamic capabilities for drivers would come in handy. I could have a more generic child driver, then tell it to only enable certain features/capabilities based on some variables set when it is created. Oh well. That feature request is already in.

One thing to mention is that the errors are actually intentional. The parent driver was failing to put data it received for a required child and that was because the child driver was not there. I did that so that it would help point people to getting the correct driver if they missed it originally. The original note did mention APs for bridges... but I did not have the error logging worded properly (it was far too generic before). Lesson learned. It always helps to have people provide feedback AND help me work through what the root cause was. So hopefully this will reduce other trouble in the future.

I'm seeing a lot of the errors below. I have already clicked Login on the Device page.

Was it working before or is this a new setup?

All of my posts in this thread are from the same setup - my first and only.

I don’t have any Apps using this driver yet. The i issues I have experienced are from initial install and configuration.

Just checking since it had been a bit so I assumed it was working for you after the Bridge child driver.

Ok, few possibilities:

  1. Is the login for the administrator (or equivalent permissions) correct? Do you get an error when you perform the login?
  2. What was your type of controller, and is it set correctly in the "Controller Type" preference?
  3. What is your Stats Refresh Rate? From the interval of the error (and previous mentions) it appears to be set for 5 minutes, but I wanted to check.
  4. In the State Variable fields do you have values listed for Cookie and Auth Key? Please do not send what they ARE in the thread, but just that there IS values listed.
  5. Last one for now... does the State Variable "Site" have a value other than "default"?
  1. Protect Admin. No error when clicking Login on the UniFiProtectAPI Device page
  2. UDM-Pro, yes it's set correctly
  3. 5 minutes
  4. Yes
  5. There is no State Variable named "Site" on my UniFiProtectAPI Device page

Hmm... All of those are reasonable. #5 was a goof on my part, the Protect API does not use Site like the Network API does.

One other thing I can think of... Do you have 2FA enabled?

No

@Busthead: Hmm... I am posting a new version that includes a new command "GetMotionEvents". It does not provide useful data (compared to Websockets) but it is an additional call to the API besides the bootstrap (which covers most everything). If you can give it a try and let me know if it gets a different result. The other changes in this version are not likely to help your case to be honest because I have not figured out what is wrong.

Updated Version(s):

  • UnifiProtectAPI.groovy = 0.2.10

Change(s):

  • Added a Controller Port # preference if a user selects the controller type "Other Unifi Controllers". This is based on newer versions of the Network API needing a different port, so I decided to be pre-emptive and implement it here as well. It sets the default to the 7443 that those controllers are currently using.
  • Added GetMotionEvents command. This is NOT very useful at this time (compared to Websockets). It allows the user to select a number of events to request and then it will provide the most recent X number of motion events that the API has. The data returned is pretty minimal (per event) and right now it is just dumped as a Trace log. Not certain if there will be further use for it but it IS another command the API allows for.
  • Removed the PollingOK check that was not used in this driver.
  • Removed excessive logging when Websockets were being used.

Updated but still seeing errors. I will delete the drivers and start from scratch.

Did deleting it and "starting from scratch" work?

Out of curiosity, do you use the Network driver also and are you having any issues with that one?

Still seeing errors but only in the early AM now:

You mean it works later on in the day? Is it only the range shown here: 1:50am to 2:45am, so 1 hour?

Maybe it is hitting something like when the Controller is performing a daily backup or something?
Another thought on a timed item... when is your Hubitat's database cleanup scheduled?

I am stretching here, I know... but it seems very odd there would be a 1 hour time every day where it would just no longer work. I even double-checked my logs but I have no errors on mine (even with my Trace logging on it goes back 3 days) to compare to. My database cleanup is scheduled for 2:15am so I am going to try to set things on mine to hit right around there and see if it causes trouble.

I haven't even begun to test the actual functionality. All the errors are solely from installation and configuration...

I don't know.

For when it works... if it is checking every 5 minutes but it only shows errors in that 1 hour timeframe then it should be working the rest of the time.

As for checking the database cleanup, it is located on the Settings - Backup and Restore page (at the bottom).

I upped my refresh rate to see if I can get any errors when the cleanup runs on mine... Guess I will see if it happens then in the morning.

I just setup this driver a couple of days ago (I've also been using your Unifi Controller driver for a while without issue). I was able to log into Protect APIs without issue and I even saw the Protect devices successfully getting updates from my Unifi Protect devices.

I am using a special read-only account that I created for the Hubitat devices (it's the same account that I use for the Unifi Controller driver). I also have tomw's Protect driver installed and use this same account with that driver without issue.

I am currently on version 0.2.10 of the device driver.

This is the last series of messages from the driver:

I'm happy to provide more details. Just let me know.

All... I finally was able to replicate the Unauthorized error with Bootstrap. It appears related to when the login occurs and then the refresh happens, probably the cookie timeout has changed. Unfortunately, this is a bit slow because I want to figure out when the cookie times out in the first place (approximately) and the Protect API is not an "official documented" API from Ubiquiti.

As soon as I have it working I will publish the update.

EDIT:
It appears the timeout is >= 10 minutes... I have changed the login rate accordingly in my driver and am going to wait to test it. As a side note, this ALSO appears to be affecting the Network API so I will be making a similar change there.

EDIT 2:
Updated Version(s):

  • UnifiProtectAPI.groovy = 0.2.11

Change(s):

  • Scheduled Login rate has been altered to every 10 minutes (used to be 1 hour) because it appears the Cookie timeout has changed (so cookies expire quicker). You MUST Save Preferences in order to update the schedule. This should solve the issue people were seeing with Unauthorized when the refresh occurred.

https://www.drdsnell.com/projects/hubitat/drivers/UnifiProtectAPI.groovy

is still returning 0.2.10