Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listen to Pico Button Presses #102

Open
athorExpansas opened this issue Jul 10, 2022 · 7 comments
Open

Listen to Pico Button Presses #102

athorExpansas opened this issue Jul 10, 2022 · 7 comments

Comments

@athorExpansas
Copy link

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.

@mdonoughe
Copy link
Collaborator

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

@athorExpansas
Copy link
Author

athorExpansas commented Jul 21, 2022

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!

@athorExpansas
Copy link
Author

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()

@athorExpansas
Copy link
Author

athorExpansas commented Jul 25, 2022

bridge is an instance of a Smartbridge connection:

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

@mdonoughe
Copy link
Collaborator

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

@athorExpansas
Copy link
Author

athorExpansas commented Jul 26, 2022

_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.

@athorExpansas athorExpansas reopened this Jul 26, 2022
@mdonoughe
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants