Best component class for handling external events?

I’m looking at building a couple of different entity components for handling external, asynchronous, instantaneous events. One example of this would be a handler for a device much like the Amazon Dash button and I have a couple of other similar cases. These inputs are not really binary sensors since there is no state per se. In a couple of cases (such as mapping events from ‘phantom’ buttons’ on Lutron control panels) I want to add this functionality on top of an existing platform. In general there will need to be some configuration to determine which external events are relevant and to which HA events these should be mapped.

My question is what is the best class to use for such a component, or does Home Assistant need a new component class to handle this?

Looking through the available classes that I can find, it seems to me that:

  • This is not a sensor or binary sensor, since there is no state to be reflected.
  • This does not belong in a platform hub. Platform hub configurations usually seem to be restricted to how to talk to the device or service, not how to interpret and process data from the device.
  • None of the more specific component classes seem to match.

So, does Home Assistant need a new component class for handing external instantaneous events or is there some existing model for doing this? If it needs a new class I’m happy to have a go at designing and building such a class but if there is an existing paradigm for dealing with this then I don’t want to reinvent the wheel. Any pointers would be gratefully accepted.

Don’t know if it suits your needs, but whenever I want to push custom events to HA I use the RESTful API. With that you simply push JSON and can build you automations based on these events.

The RESTful component doesn’t do what I need for a number of reasons. In one case I’m looking at having events pushed to me, rather than having HA poll for them and in another the events are already being streamed into an existing platform and I just need a way to configure the mapping of those events into HA events.

That’s exactly what you can use the API for. Instead of letting HA poll for events I have a custom server-app that publishes events to HA instantly. Or am I still misunderstanding something here?
Pushing events to you (a Smartphone etc.) would be done by using one of the available notify-components.

I understand how to do this from a purely functional point of view and I have working code in custom components. What I’m asking is where should code for doing this sort of thing go if I want to submit a patch.

Take for example mapping events from spare buttons on a Lutron keypad. At the moment there are components/lutron.py, components/cover/lutron.py and components/light/lutron.py files. None of these are the right place to put code that handles the presses of spare buttons on a control pad and as far as I can tell nor are any of the other subdirectories of components. I could produce some fake components/sensor/lutron.py which has a psudo ‘state’ containing the time of the last event or the number of previous events, so that people can write automations triggered by state change events, but that doesn’t provide any way to pass on parameters or metadata associated with the event; just stuffing metadata into attributes leads to a race condition if a second event happen before the first one is processed (which is quite possible if they are asynchronous).

So, in the absence of another class of component I’m wondering if what is needed is a new EventStream class and a components/eventstream directory to house them, so that it is easy to build all sorts of handlers for external events.

Just put it in the platform. I don’t see a reason not to. It’s what the homematic platform does.

OK. I can certainly dump it into the top level platform.