Help with HTTP Communication between Addon and Integration

Hello All!

I’m working on a new addon and integration that will work together to provide a complete LoRaWAN server solution. The addon is responsible for receiving device packets and forwarding them to the integration to then update device state. I’m limited to using HTTP to forward the device data to the integration, and I’m looking for advice on the best method to use. Ideally, the receiving endpoint for the integration would be a pre-determined fixed URL. That way, no configuration would be needed on the integration side by the user, and the addon would always send to the same URL. I’ve read the /api/webhook is an option, but from my understanding, a unique webhook ID is created when the integration is installed. Are there any other options that would be suitable for my needs?

Thanks!
-Kent

When you say you are limited to http, does this preclude websockets? I would think this would be one of the best options.

It does unfortunately. I’m trying to leverage existing output options from the service used in the addon.

OK, so you can register a custom view and use that with your addon to send/receive data into your integration.

Basic example of a view def. Handle your data being received in the post method.

class ExampleView(HomeAssistantView):
    """Example view."""

    requires_auth = False
    url = "/api/example_endpoint"
    name = "api:custom_integration:example"

    async def get(self, request: web.Request):
        """Handle a get request."""
        _LOGGER.info("REQUEST: %s", request)
        return web.Response(status=HTTPStatus.OK)

    async def post(self, request: web.Request, data: dict[str, str]):
        """Handle a post request."""
        _LOGGER.info("REQUEST: %s", request)
        return web.Response(status=HTTPStatus.OK)

Then you register it with:

hass.http.register_view(ExampleView())

You can test by using your browser

http://homeassistant.local:8123/api/example_endpoint

EDIT: You may want to add some type of security to ensure endpoint is not abused. This is used for many things in HA. You can look at this as an example or the shelly integration.

This is exactly what I was looking for!

Thank you so much!

Just for completeness of the answer…

If you set

requires_auth = True

This will require a bearer token to be provided in the http request.

When using an addon, you can utilise the SUPERVISOR_TOKEN environment variable that is available in your addon container (providing your addon uses the standard HA addon base image) as this bearer token to authenticate the request.

See below for docs on this

1 Like

Got it, thank you!