Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for s3_signed_url and access URL #44

Closed
jritsema opened this issue Oct 13, 2024 · 9 comments
Closed

Wait for s3_signed_url and access URL #44

jritsema opened this issue Oct 13, 2024 · 9 comments

Comments

@jritsema
Copy link
Contributor

Hello. Thank you for this useful component.

This s3.signurl service generates an event of type s3_signed_url which you can use as a trigger in a subsequent automation.

I'm curious why the put action is synchronous and the signurl action is asynchronous? Meaning that signurl generates an event that has to be listened to.

I'm trying to create an automation that uploads an image to s3, generates a signed url, and then accesses the output URL, all in the same automation. Something like:

      - service: s3.put
        data:
          bucket: my-bucket
          file_path: img.jpeg
          key: img.jpeg
      - service: s3.signurl
        data:
          bucket: my-bucket
          key: img.jpeg
          duration: 7200
      - wait_for_trigger:
          - platform: event
            event_type: s3_signed_url
      - service: notify.mobile_app_johns_iphone
        data:
          message: {{ trigger.payload_json["after"]["label"] }}
          data:
            image: "{{ wait.trigger.event.data.URL }}"

It seems that wait_for_trigger never fires. Does anyone have any tips on how to get this to work?

@robmarkcole
Copy link
Owner

This component has had contributions from people with far more experience of async than I have. I defer to them for comment

@jritsema
Copy link
Contributor Author

Thanks @robmarkcole. I probably shouldn't have used the word async. I think my question is more home assistant related. Is it possible to call an action that returns data? Is so, this would solve my issue. If not, I will need to figure out how to pass context along with the handled event. If the code above worked I would have the state available in the automation. If a new automation needs to handle the event, I'll need some way to pass in the context. Not sure if that makes more sense.

@jritsema
Copy link
Contributor Author

This is what I'm currently doing as a workaround for this issue. I'd be open to better solutions.

From what I can tell, home assistant services can't really return data. This means that the result of the pre-sign operation needs to be handled by listening for an event. Because the event does not fire in the same automation, I'm having to handle it in a separate automation, and thus, I need some context data along with the output URL.

Hope this may be useful to others.

@nukefrenzy
Copy link

I use the generate signed url service and name the key something related to the automation. So for my doorbell camera it's "snapshot_doorbell.jpg"
You could add the time/date to this name so you don't replace existing images, but I don't care for this use case.

Then when I get the s3_signed_url trigger I use this condition to make sure its for the right automation:
"{{ 'doorbell' in trigger.event.data.URL }}"

@jritsema
Copy link
Contributor Author

jritsema commented Nov 3, 2024

Thanks @nukefrenzy. So it sounds like also have a separate automation to handle the response (good to know I'm not alone). Good idea to use the key name to identify the automation. I generally need more detail like the camera as well as the label, sub-label, zone, confidence, etc., which would be a cumbersome to do based on the s3 key.

@nukefrenzy Is your HASS install private by chance? And have you run into this trying to use a pre-signed URL as the notification image? home-assistant/companion.home-assistant#1125 I still have not heard a definitive word on why this doesn't work for me. Also see https://community.home-assistant.io/t/ios-notifications-with-public-images-on-a-private-server/782914

@jritsema jritsema closed this as completed Nov 3, 2024
@nukefrenzy
Copy link

nukefrenzy commented Nov 4, 2024

Yes, I have a second automation to handle the response because I don't know how long it will take. I generally don't want an automation to sit around waiting. You can also then make the response automation more general and handle all of your camera notifications by looking at the response and tailoring the notification to it. Also, I think in your original example, your automation has already triggered, so you aren't going to get a second trigger with the response URL in it after the automation is already running.

For your second question, I assume we're both going this route because our HASS is not exposed to the internet, not because we want the added complexity of using Amazon S3, ha. The below code works for me. It shows the thumbnail on the notification, I long press to see it even bigger, and when I tap on it it takes me to the actual url so I can zoom in. I also tried the default code from your forum post and it worked for me so there's something up with your config.

action: notify.mobile_app_iphone
data:
  title: Person at the Front Door
  message: at {{ as_timestamp (now()) | timestamp_custom('%I:%M %p') }}
  data:
    image: "{{trigger.event.data.URL}}"
    url: "{{trigger.event.data.URL}}"
    push:
      badge: 0
    group: home_assistant
    actions:
      - action: SNOOZE_DOORBELL
        title: Snooze Notifications

Try adding that URL path with the image link in it to see if tapping your notification brings you to the picture on Amazon S3 or not. If that doesn't work then there's more wrong with your HA than just images in notifications.

@jritsema
Copy link
Contributor Author

jritsema commented Nov 9, 2024

Got it, thanks @nukefrenzy! Did you have to do anything special with config or did it just work out of the box? Doh, I just realized I'm running Home Assistant 2023.1.0. Wondering if this is something they fixed in later versions.

@nukefrenzy
Copy link

@jritsema No, I don't remember having to do anything in particular. I run HA OS directly on a Dell machine so I have access to the internet without having to jump through any hoops. Do you have docker, virtual machine, or some more complicated setup that may interfere? Do you have adblocking or a VPN on your network or phone that may interfere? Good luck!

@jritsema
Copy link
Contributor Author

@nukefrenzy I'm running HASS in a container, but it should have host network access which should be fine. The notifications work fine if my phone is connected to the LAN. I'm hoping simply upgrading will fix this. I did a quick test and ran into some HASS errors, so will to find more time to complete the full upgrade. Thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants