Comments (34)
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.
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.
I went through all the trouble of reverse engineering and documenting the API @adrum - you could at least read it. 😜
from pyunifiprotect.
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.
Come to the dark side. 😄
from pyunifiprotect.
@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.
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.
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.
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.
Thanks!
from pyunifiprotect.
@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.
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.
@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.
Awesome @hjdhjd
Will read through it and start the implementation. Great work - you almost made me move to Homekit completely 👍
from pyunifiprotect.
I've got the websocket decoding working here if you want to play around with it
from pyunifiprotect.
Woos - It’s late here, but I will give a go first thing in the morning.
from pyunifiprotect.
I'll also give it a run. Thanks @bdraco!
from pyunifiprotect.
@bdraco This worked very well for me! Both ring and motion events were instantaneous in the logs.
from pyunifiprotect.
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.
@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.
@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.
@bdraco has a much more isolated and easier way to test 😄
from pyunifiprotect.
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.
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.
I'm thinking to move this forward:
- Add support for subscriptions to the web socket events
- Create a async_update_cameras
- Create a async_update_events
- Update home assistant to only poll update cameras every few minutes
- 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
- If the cloud key doesn't support the web socket keep the existing behavior (hopefully it does)
from pyunifiprotect.
Sounds like a great plan - I'll make a post once I have tested with the CloudKey.
from pyunifiprotect.
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.
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.
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.
I pushed a few more fixes. At this point its ready for testing
from pyunifiprotect.
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.
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.
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.
Verified reconnect + backoff when protect is restarted
from pyunifiprotect.
Related Issues (20)
- License plate detection - Pass the plate number to HA HOT 2
- Please add support for new event types intoduced in Protect 2.9.16 HOT 1
- [Early Access] Protect 2.9.16 includes breaking changes: pyunifiprotect.exceptions.DataDecodeError: No modelKey HOT 4
- Doorbells and smart chimes not found HOT 6
- unifi-protect cameras save-video exporting empty file HOT 2
- Optional backup dependencies cause main cli fail if not installed HOT 1
- Authentication errors every 2h HOT 3
- Error setting up UniFi Protect integration: invalid literal for int() with base 16 HOT 3
- pyunifiprotect.exceptions.NvrError: Error requesting data from.id.ui.direct: Cannot connect to host.id.ui.direct:443 ssl:default [Name does not resolve] HOT 1
- HA logs: AttributeError: 'UUID' object has no attribute 'replace' HOT 3
- Connection Issue with UniFi Protect in Docker Environment
- Snapshot quality HOT 1
- AttributeError: 'URL' object has no attribute 'joinpath' HOT 1
- Heads up 3.2.12 is going to break Pyunifiprotect again HOT 4
- Long TTS media is garbeled HOT 1
- _process_nvr_update updating bootstrap.nvr with the wrong nvr data HOT 2
- _process_nvr_update updating bootstrap.nvr with the wrong nvr data (when the NVR's are stacked) HOT 10
- await outside function - Python - HOT 2
- Unifi OS 4. (EA) changed Cookie name HOT 12
- Request: Enable feature to manage/control 'Share Livestream' option for camera HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyunifiprotect.