mrlt8 / docker-wyze-bridge

WebRTC/RTSP/RTMP/LL-HLS bridge for Wyze cams in a docker container
GNU Affero General Public License v3.0
2.47k stars 151 forks source link

Camera Motion webhooks with Home Assistant #1044

Open domolys opened 7 months ago

domolys commented 7 months ago

(Using docker-wyze-bridge v2.5.1 with Home Assistant) This issue is concerning the wiki

The webhook automation trigger object in Home Assistant does not contain a dictionary of key value pairs for the headers of the incoming request. (GET/HEAD)

So, actually, it is not possible to use the image url and title that are available in the request header, using MOTION_WEBHOOKS :

X-Title: Motion detected on cam-name
X-Attach: https://...

The only way to use MOTION_WEBHOOKS option with Home Assistant is to create a different Webhook ID trigger for each camera in an automation to know from witch camera the motion is detected.

It's possible to use the MOTION_WEBHOOKS with Home Assistant, but not the way the doc is written for Home Assistant: https://github.com/mrlt8/docker-wyze-bridge/wiki/Camera-Motion#webhooks

Example of an automation triggered by two differents webhook_id:

description: "WYZE-BRIDGE-MOTION_WEBHOOKS"
mode: single
trigger:
  - platform: webhook
    allowed_methods:
      - POST
      - PUT
      - GET
      - HEAD
    local_only: true
    webhook_id: MY-CAM-1-random_id1239743908
  - platform: webhook
    allowed_methods:
      - POST
      - PUT
      - GET
      - HEAD
    local_only: true
    webhook_id: MY-CAM-2-random_id7139244517

Part of config used in Wyze Bridge for triggering the automation above:

CAM_OPTIONS:
  - CAM_NAME: MY-CAM-1
    MOTION_WEBHOOKS: http://home-assistant-host:8123/api/webhook/MY-CAM-1-random_id1239743908
  - CAM_NAME: MY-CAM-2
    MOTION_WEBHOOKS: http://home-assistant-host:8123/api/webhook/MY-CAM-2-random_id7139244517 

[WyzeBridge] [MOTION] Motion detected on my-cam-1 A motion event will trigger the Home Assistant automation above, but the only available trigger data for the user is something like this:

{'platform': 'webhook', 'webhook_id': 'MY-CAM-1-random_id1239743908', 'data': <MultiDictProxy()>, 'query': <MultiDictProxy()>, 'description': 'webhook', 'id': '0', 'idx': '0', 'alias': None}

...otherwise, the addon rocks!

mrlt8 commented 7 months ago

Thanks for the report. Do we need to post data to the endpoint? Would it work with a query parameter? e.g.:

MOTION_WEBHOOKS: http://home-assistant-host:8123/api/webhook/MY-CAM-1-random_id1239743908?some_key=some_value&anotherKey=otherValue
domolys commented 7 months ago

Yes it works, receiving for trigger.query:

<MultiDictProxy('some_key': 'some_value', 'anotherKey': 'otherValue')>

So receiving for: trigger.query.some_key: some_value trigger.query.anotherKey: otherValue

Thanks!

mrlt8 commented 7 months ago

In theory, you should be able to reuse the same web hooks endpoint with multiple cameras by passing the camera name variable {cam_name} as a query parameter:

MOTION_WEBHOOKS: http://0.0.0.0:123/webhooks/endpoint?camera={cam_name}

I've also added {img} variable in the dev branch, so you will also be able to pass the snapshot url with something like:

MOTION_WEBHOOKS: http://0.0.0.0:123/webhooks/endpoint?camera={cam_name}&snapshot={img}
domolys commented 7 months ago

Tested passing variables as query parameter with the same webhook endpoint:

MOTION_API: true
CAM_OPTIONS:
  - CAM_NAME: Cam 1
    MOTION_WEBHOOKS: http://0.0.0.0:8123/api/webhook/random_id?camera={cam_name}&snapshot={img}
  - CAM_NAME: Cam 2
    MOTION_WEBHOOKS: http://0.0.0.0:8123/api/webhook/random_id?camera={cam_name}&snapshot={img}
  - CAM_NAME: Cam 3
    MOTION_WEBHOOKS: http://0.0.0.0:8123/api/webhook/random_id?camera={cam_name}&snapshot={img}

Setup of Home Assistant automation trigger:

platform: webhook
allowed_methods:
  - GET
  - HEAD
local_only: true
webhook_id: random_id

Both triggers trigger.query.camera and trigger.query.snapshot are receiving the variables values

Home Assistant webhook endpoint: http://your-home-assistant:8123/api/webhook/<webhook_id> Home Assistant webhook trigger doc: https://www.home-assistant.io/docs/automation/trigger/#webhook-trigger

Thank you!

mrlt8 commented 7 months ago

Awesome, will merger this into the main branch! I believe you should be able to configure the main MOTION_WEBHOOKS with the variables instead of configuring each one separately.

kakins commented 6 months ago

Question about this. Not sure if I'm doing something wrong. I grab the snapshot variable from the webhook event, along with all of the other header values, and use that to try and construct a request to AWS to get the image. I keep getting 403s.

mrlt8 commented 6 months ago

The AWS urls will expire after some time so you may need to cache them or pull a new snapshot from the bridge.