gurumitts/pylutron-caseta

Listen to Pico Button Presses

athorExpansas opened this issue · 7 comments

Hi everyone,
How does one listen for pico button presses using this library? I'd like to capture the event in my python code and then do something with it. I've searched high and low and can't quite figure this one out.

The buttons are available in bridge.buttons. Here's how Home Assistant subscribes to them: lutron_caseta/__init__.py:266-311

Thank you for the reply and forgive me for my ignorance. I'm newer to the programming world.
It looks to me like Home Assistant subscribes through this:

for button_id in button_devices_by_id:
        bridge_device.add_button_subscriber(
            str(button_id),
            lambda event_type, button_id=button_id: _async_button_event(
                button_id, event_type
            ),
        )

however, I don't get any responses that way. Here's my code:

def printThis(button_id, event_type):
        print(button_id + event_type)

  for b in buttons:
        print (b)
        bridge.add_button_subscriber(str(b), lambda event_type, button_id=b: printThis(button_id, event_type),)

I do get the buttons initially, but then my process finishes without actually 'listening' for the button presses.

There is a section in the HA that shows:

"""Subscribe to lutron events."""
    dev_reg = dr.async_get(hass)

however, as I dig deeper into that it doesn't appear to have much to do with lutron, though it may be more a fact that I'm just not understanding what's going on.

Your help is greatly appreciated!

Finally figured it out.
You have to issue the following to get pylutron to "listen" for the button presses after you subscribe for them.

await bridge._monitor()

bridge is an instance of a Smartbridge connection:

bridge = Smartbridge.create_tls(bridgeIp, "caseta.key", "caseta.crt", "caseta-bridge.crt")
await bridge.connect()

_monitor is a private method and you are not supposed to call it. Calling _monitor yourself after connect will most likely cause unexpected behavior.

_monitor is a private method and you are not supposed to call it. Calling _monitor yourself after connect will most likely cause unexpected behavior.

What's the expected use? Should I be calling "connect" after the add_button_subscriber? Or some other method?
Right now I'm calling connect before the add_button_subscriber and the script terminates after the add_button_subscriber loop is complete.

You need to wait using asyncio (not the regular sleep function). The connection remains open in the background.