#Callbacks from other classes (scope)

3 messages · Page 1 of 1 (latest)

hot plank
#

In my current project I have a button handler that is listening for several esphome mouse events:

self.hass.bus.async_listen("esphome.button_release", self.handle_button_release)
self.hass.bus.async_listen("esphome.button_click", self.handle_button_click)```
When those event handlers get called, they look up a device and make a nice data package. Then the button manager takes the package and fires off a second event. 
```        event_name = f"connected_button.button_{event_type}"
        self.hass.bus.async_fire(event_name, {
            "device_id": device['id'],
            "device_name": device['name'],
            "mac_address": mac_address,
            "event_data": event_data,
            "event_type": event_type
        })```
The second event goes out fine and gets picked up in my __init__.py (main class).

That is where things start going sideways. My button handler in my main class needs the hass object but is marked as @callback so it is at the root level and not in my async_setup_entry function. How can I pass the button event handler the hass object without getting too messy?

```...
    hass.bus.async_listen("connected_button.button_click", lambda event: handle_button_click(hass, event))   
    return True


#-------------------------------------------------------------------------------
@callback
async def handle_button_click(hass, event):
#

Callbacks from other classes (scope)

haughty fog
#

These kinds of questions (regarding custom integrations you're making) should be in #1257019582112334014 (need to mark yourself with the developer role for access).
#1284966582375813201 is more for help with using existing integrations