Developers / Docs / Pg

Using Webhooks

We now have support in the interface module for node servers to use webhooks from external services.

Requirements

This feature is only available on eisy and polisy using PG3x.

PG3 remote access must be configured and active. To configure this, login to https://my.isy.io, and under you ISY, use: Select tools | Maintenance | PG3 remote access.

Make sure Remote access is active.

If events are not sent to your nodeserver, make sure you are running the latest version, and proceed with a reconfiguration of remote access.

Please note that configuring remote access will reboot your eisy/polisy.

Endpoint

If the webhook needs a response, the endpoint should be: https://my.isy.io/api/eisy/pg3/webhook/response/<uuid>/<slot>.

If the webhook does not require a response (other than an HTTP 200), then you can use https://my.isy.io/api/eisy/pg3/webhook/noresponse/<uuid>/<slot>.

Both endpoints supports all methods: GET, POST, PUT, DELETE, etc

Please note that the endpoint does not do any authentication. Your nodeserver is responsible for checking that the request is legitimate.

Nodeserver code

Setup

When initializing the polyglot interface, set the enableWebhooks flag to True

    polyglot = Interface([], { "enableWebhook": True })

Startup

Then when ready to receive events, call the webhookStart method. This tells portal that the plugin is ready to receive webhooks. Whenever the plugin stops, it will automatically tell portal to stop sending events to the plugin. Therefore, it is important to call polyglot.webhookStart() on every startup.

This method allows to pass an optional configuration dict. In most cases, only the name is useful. The name is used only for logging on portal.

    polyglot.webhookStart({"name": "Ring"})

For some specialized plugins which requires routing based on event data, such as Tesla, it's possible to create a map between plugin specific data (such as a VIN) and the plugin instance (uuid/profileNum). Implementing this requires development on portal. If you have a need for this, please contact UDI.

To create the map between assets and the plugin instance, you can pass a list of assets like this. This would create a relationship between the asset abc123, and the plugin instance.

    polyglot.webhookStart({"name": "Ring", "assets": [{ "id": "abc123" }]})

Webhook processing

Whenever a call is made to the endpoint, a corresponding event will be triggered on the nodeserver if subscribed to polyglot.WEBHOOK.

It will pass to the webhook handler the headers, querystring and body.

Here's sample code for a nodeserver:

polyglot.subscribe(polyglot.WEBHOOK, webhook)

# Available information: headers, query, body
def webhook(data):  
    LOGGER.info(f"Webhook received: { data }")

    response = {
        'abc': 123,
    }

    # If the webhook needs a response, use this:
    polyglot.webhookResponse(data)

Plugin termination

Whenever the plugin terminates, it should also tell the cloud service to stop sending webhook events to portal. Although PG3 will tell portal to stop sending events to the plugin, portal will still be receiving events, creating unnecessary load.