Git Product home page Git Product logo

Comments (34)

bdraco avatar bdraco commented on May 30, 2024 3

It just at the proof of concept phase at this point. We need to register a callback with HA so we can trigger the entity to update as soon as the web socket gets an event and back off the camera and events endpoint polling since they are a bit cpu intensive because the protect server always drops the connection after the api response (no keep alive)

I didn't have time to get that far

from pyunifiprotect.

hjdhjd avatar hjdhjd commented on May 30, 2024 2

For your reading pleasure, I've documented the binary protocol for the updates API. You can read about it here:

https://github.com/hjdhjd/homebridge-unifi-protect/blob/master/docs/ProtectAPI.md

You can also take a look at my implementation for decoding the protocol here - it's fairly standalone, so aside from the logging bits, it should be plug and play:

https://github.com/hjdhjd/homebridge-unifi-protect/blob/master/src/protect-updates-api.ts

That should provide realtime update support for you in Home Assistant. Enjoy!

from pyunifiprotect.

hjdhjd avatar hjdhjd commented on May 30, 2024 2

I went through all the trouble of reverse engineering and documenting the API @adrum - you could at least read it. 😜

from pyunifiprotect.

briis avatar briis commented on May 30, 2024 1

THANKS. I will start scrutinising your code myself, and ask questions.
I just installed homebridge to give try your code, and this works really good and fast. Great job.

from pyunifiprotect.

hjdhjd avatar hjdhjd commented on May 30, 2024 1

Come to the dark side. 😄

from pyunifiprotect.

bdraco avatar bdraco commented on May 30, 2024 1

@bdraco This worked very well for me! Both ring and motion events were instantaneous in the logs.

How do you test this? Have you written a test program, you could share?

https://github.com/bdraco/pyunifiprotect/blob/ws/websocket_test.py

from pyunifiprotect.

bdraco avatar bdraco commented on May 30, 2024 1

https://github.com/bdraco/pyunifiprotect/tree/ws
https://github.com/bdraco/unifiprotect/tree/ws_part_1

I've got a preliminary version together.

I'm going to start testing soon so its going to change a bit as I fix things

from pyunifiprotect.

briis avatar briis commented on May 30, 2024

You are right. It was not me personally who added it, it was a PR from another user, which I merged in to the code.
Currently it is not used, as I have not figured out how to integrate it in to Home Assistant - Hopefully I can figure something out, and as soon as I am starting working on it, I would be happy for your help.

Thanks a lot for reaching out.

Bjarne

from pyunifiprotect.

hjdhjd avatar hjdhjd commented on May 30, 2024

Happy to. A couple of bits of advice...

If you look through my code...it’ll give you an idea.

In short - you’d need to refactor this codebase and stop using the “events” URL API. It’s slow and often inaccurate. What you really want is to grab the realtime events API via the websocket, which publishes almost every event that you’d care about.

Then to grab rings, you need to capture the bootstrap JSON and look for the last ring property.

Continuing to use /events is problematic on a number of fronts, I’ve found. Using the above approach what you’re able to do is get realtime event updates (except for ring events) for motion and other events and have a very low CPU / performance impact.

If you also poke around my code a bit more, you’ll find how you can approach auto configuration, if it’s an area of interest.

Finally...I just (yesterday) pushed an update to fully support two-way audio (with one major caveat).

Enjoy!

from pyunifiprotect.

hjdhjd avatar hjdhjd commented on May 30, 2024

Thanks!

from pyunifiprotect.

adrum avatar adrum commented on May 30, 2024

@hjdhjd I did in fact look at your homebridge plugin for the WS integration. We are using events WS endpoint for motion events, but falling back to the HTTP events endpoint for rings. It probably could be refactored to use bootstrap instead of events, as it looks like bootstrap is currently only used on setup.


I noticed Two-Way audio was being added to homebridge. I'm going to read through your plugin to see how it's been implemented. I actually switched over to HomeAssistant mainly for their HomeKit integration, which has been much more reliable for me than homebridge was (as of 2 years ago -- I'm sure a lot has changed). It's probably worth opening an Issue in this repo to discuss a possible implementation for this feature.

from pyunifiprotect.

hjdhjd avatar hjdhjd commented on May 30, 2024

There’s a lot of evolution in the HomeKit space when it comes to homebridge.

Using HTTP endpoints (it’s not really an endpoint, but it’s been used as such by some plugins) is an extraordinarily unreliable method, I’ve found. It’s slow, and misses events, sometimes for minutes at a time.

As to HomeAssistant versus Homebridge...I wish these projects would work together more to reduce the amount of work. There’s little sense in me having implemented a complete HomeKit implementation for UniFi Protect, for instance, and it to have to be reimplemented for HomeAssistant, versus finding better bridging solutions across both platforms and plugging into it.

Best of luck...thanks for the acknowledgment.

from pyunifiprotect.

briis avatar briis commented on May 30, 2024

@adrum: I did currently disable your code, as it did create an error on shutdown of HA. I assume it is because no Disconnect was send to the WS. I would be happy if you could keep working on this. I have installed @hjdhjd plugin on a Homebridge setup, and it works fantastic - so if we could get to something like that it would be great.
Then I might need help in adding it to the HA Integration, as I am not completely sure how to setup a Push model here.

from pyunifiprotect.

briis avatar briis commented on May 30, 2024

Awesome @hjdhjd
Will read through it and start the implementation. Great work - you almost made me move to Homekit completely 👍

from pyunifiprotect.

bdraco avatar bdraco commented on May 30, 2024

@briis @adrum

I've got the websocket decoding working here if you want to play around with it

bdraco@765e17d

from pyunifiprotect.

briis avatar briis commented on May 30, 2024

Woos - It’s late here, but I will give a go first thing in the morning.

from pyunifiprotect.

adrum avatar adrum commented on May 30, 2024

I'll also give it a run. Thanks @bdraco!

from pyunifiprotect.

adrum avatar adrum commented on May 30, 2024

@bdraco This worked very well for me! Both ring and motion events were instantaneous in the logs.

from pyunifiprotect.

hjdhjd avatar hjdhjd commented on May 30, 2024

I’d toggle the smart motion triggers. The problem is that only G4 cameras do object detection. The G3 series can’t. You’re also going to run the risk of duplicate events, I believe, because motion and smartdetectzone events can happen for the same underlying event.

Other things in my quick glance:

  • I’d bounds-check any LCD message. 30 characters is the limit.
  • I’d also note for folks that they need to make sure the clock on the Protect controller and the clock on the HA controller need to be reasonably in sync.
  • Intelligent reconnection with backoff for the websocket to avoid any unintentional self-induced DoS events. 😄

Great work!

from pyunifiprotect.

briis avatar briis commented on May 30, 2024

@bdraco This worked very well for me! Both ring and motion events were instantaneous in the logs.

How do you test this? Have you written a test program, you could share?

from pyunifiprotect.

adrum avatar adrum commented on May 30, 2024

@briis So I actually just clone home assistant and run it locally in an isolated environment using venv. Once it's ready to go, I install the library I'm working on by running pip3 install -e path/to/repo or by symlinking the library inside of the venv folder.

https://developers.home-assistant.io/docs/development_environment

Since the custom_component isn't ready for real time updates, I turned the log level for pyunifiprotect to debug and watch the output when ringing a doorbell or walking in front of a camera.

from pyunifiprotect.

adrum avatar adrum commented on May 30, 2024

@bdraco has a much more isolated and easier way to test 😄

from pyunifiprotect.

bdraco avatar bdraco commented on May 30, 2024

I don't have any cloud keys anymore. Does anyone know if this works without unifi os? I won't be able to try it for a week or so when I can try it at the neighbors who still do

from pyunifiprotect.

briis avatar briis commented on May 30, 2024

This for the Test program, I'll give a go a bit later and hopefully start contributing.
My production environment is a UDMP, but I still have my CloudKey+ available - I will try and get it up and running with a camera or two attached, to see how this works.

from pyunifiprotect.

bdraco avatar bdraco commented on May 30, 2024

I'm thinking to move this forward:

  1. Add support for subscriptions to the web socket events
  2. Create a async_update_cameras
  3. Create a async_update_events
  4. Update home assistant to only poll update cameras every few minutes
  5. Update home assistant to subscribe to the web socket and write state, poll events for detail when a new one comes in, write state again. Ideally we can set this up to only write state for ones that change to avoid overhead
  6. If the cloud key doesn't support the web socket keep the existing behavior (hopefully it does)

from pyunifiprotect.

briis avatar briis commented on May 30, 2024

Sounds like a great plan - I'll make a post once I have tested with the CloudKey.

from pyunifiprotect.

hjdhjd avatar hjdhjd commented on May 30, 2024

CloudKey does not support the events websocket API. Only UniFiOS devices support it. Review the documentation link in one of my prior posts. 😀

from pyunifiprotect.

adrum avatar adrum commented on May 30, 2024

Yup, it doesn't have the events WebSocket API. The output was this:

DEBUG:pyunifiprotect.unifi_protect_server:Authenticated successfully!
DEBUG:pyunifiprotect.unifi_protect_server:WS connecting to: wss://192.168.1.2:7443/ws/updates?lastUpdateId=<uuid>
ERROR:asyncio:Unclosed client session

from pyunifiprotect.

bdraco avatar bdraco commented on May 30, 2024

I've pushed up a few more adjustments.

Everything appears to be working. All my cameras are now showing the motion event right away.

I don't have a doorbell so I can't test with that.

from pyunifiprotect.

bdraco avatar bdraco commented on May 30, 2024

I pushed a few more fixes. At this point its ready for testing

from pyunifiprotect.

bdraco avatar bdraco commented on May 30, 2024

CloudKey does not support the events websocket API. Only UniFiOS devices support it. Review the documentation link in one of my prior posts. 😀

Hopefully they add it in the future as it seems like they are leaving the non unifios devices behind

from pyunifiprotect.

bdraco avatar bdraco commented on May 30, 2024

Also with the changes, the cloud keys users should benefit as well as we aren't updating all the entities each time and only updating when we have a new event or camera update

from pyunifiprotect.

briis avatar briis commented on May 30, 2024

Ubiquiti are moving in their own mysterious ways, but I would guess that eventually, they will move to UnifiOS on the CloudKey or make it EOL.

All 3 of you - Thanks a lot for helping out on this - A fantastic way for a guy like me to learn.

from pyunifiprotect.

bdraco avatar bdraco commented on May 30, 2024

Verified reconnect + backoff when protect is restarted

from pyunifiprotect.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.