How to create routes?

Hi,
i am currently experimenting with the register_endpoint and register_route featured, and am a bit lost.

What i currently have:

import hassapi as hass

class Spielwiese(hass.Hass):
def initialize(self):
    self.log("Hello from AppDaemon")
    self.register_endpoint(self.routetest, "test") 

async def routetest(self, request, kwargs):
  response = {"message": "Hello world"}
  return response, 200

The app is called “spielwiese” as well.

Now, when i try to access https://appdaemon.mydomain.tld/spielwiese/test (like specified in the documentation as http://AD_IP:Port/app/route i get a 404. Same if i do not use register_endpoint but register_route.

So the two questions i have are:

  • What’s the difference between register_endpoint and register_route?
  • What am i doing wrong?

Ultimately i want to have a scenario where i call a URL and trigger the start of an automation then.

+1 here too.

if you use register_route in your example above, you should then be able to access http://AD_IP:Port/app/test, as least using your local IP address (depending on your AppDaemon config.

It seems like both these functions only support GET requests and not POST or PUT but it would be great to get some more info.

I’m trying to find out whether it’s possible to upload a file using PUT or POST and saving the binary file using request. It works fine in a standalone python application but assume I’ll need to use these register functions if trying to use this within appdaemon?

Finally, just for reference I think you’ll also need to use terminate, so any changes get accounted for using the handles you save on registration i.e.:

 def terminate(self):
        self.deregister_endpoint(self.your_handle_endpoint)
        self.deregister_route(self.your_handle_route)

It would be great if someone could shed some more light on this - Many thanks.

try it without async.

@simonszu,

As @flinthamm said u got to make use of the right url to access the end point.
If your idea was to trigger externally via REST, then rather than do this, instead use an app to register a service and trigger that service call. All services are accessible as POST requests http://AD_IP:Port/api/appdaemon/service/<namespace>/<domain>/<service>. But you could still do it your way.

register_route supports GET request only. and register_endpoint (original) supports POST only.
Yes it is possible to upload a file, and it was the reason it was allowed to pass the request object to the app, when using async callback only. In that case, you can extract the data from the object and do as you like.

No need to deregister in terminate, as AD handles that internally automatically when the app closes. So the user need not worry about that.

Hope it helps.

Thank you both for the feedback and information.

I was missing that endpoints are accessed via /api/appdaemon/ and routes are accessed via /app/, which is why the POST was not working! Like the register_route documentation, perhaps this hint should be added to the register_endpoint. What is the best way to request or help update the docs?

If there are any examples using any or all of these functions it would be very useful.

I’ll need to research async/await request uploads next.