{"action":"created","issue":{"url":"https://api.github.com/repos/50ButtonsEach/fliclib-linux-hci/issues/6","repository_url":"https://api.github.com/repos/50ButtonsEach/fliclib-linux-hci","labels_url":"https://api.github.com/repos/50ButtonsEach/fliclib-linux-hci/issues/6/labels{/name}","comments_url":"https://api.github.com/repos/50ButtonsEach/fliclib-linux-hci/issues/6/comments","events_url":"https://api.github.com/repos/50ButtonsEach/fliclib-linux-hci/issues/6/events","html_url":"https://github.com/50ButtonsEach/fliclib-linux-hci/issues/6","id":155247629,"number":6,"title":"How to handle events","user":{"login":"RobZe89","id":17066038,"avatar_url":"https://avatars.githubusercontent.com/u/17066038?v=3","gravatar_id":"","url":"https://api.github.com/users/RobZe89","html_url":"https://github.com/RobZe89","followers_url":"https://api.github.com/users/RobZe89/followers","following_url":"https://api.github.com/users/RobZe89/following{/other_user}","gists_url":"https://api.github.com/users/RobZe89/gists{/gist_id}","starred_url":"https://api.github.com/users/RobZe89/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RobZe89/subscriptions","organizations_url":"https://api.github.com/users/RobZe89/orgs","repos_url":"https://api.github.com/users/RobZe89/repos","events_url":"https://api.github.com/users/RobZe89/events{/privacy}","received_events_url":"https://api.github.com/users/RobZe89/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":12,"created_at":"2016-05-17T12:31:27Z","updated_at":"2016-08-12T06:52:17Z","closed_at":null,"body":"Dear team\r\n\r\nGlad to see, that there is a new linux release for the flic button. I'm using the old fliclib quite a long time with the known problems. My problem now is that I'm not as good as many of you. I want to migrate to the new version, but I didn't get to find out the way to handle the events coming from the flic button. Maybe some of you can show / write a sample for the 3 types of \"actions\", short, double, long click on a flic button. Like in this [comment](https://github.com/50ButtonsEach/fliclib-linux-dist/issues/10#issuecomment-185926306) for the old fliclib. \r\n\r\nIt could be in python or nodejs, doesn't matter.\r\n\r\nBest Regards\r\nRob"},"comment":{"url":"https://api.github.com/repos/50ButtonsEach/fliclib-linux-hci/issues/comments/239374105","html_url":"https://github.com/50ButtonsEach/fliclib-linux-hci/issues/6#issuecomment-239374105","issue_url":"https://api.github.com/repos/50ButtonsEach/fliclib-linux-hci/issues/6","id":239374105,"user":{"login":"PiRune","id":20454469,"avatar_url":"https://avatars.githubusercontent.com/u/20454469?v=3","gravatar_id":"","url":"https://api.github.com/users/PiRune","html_url":"https://github.com/PiRune","followers_url":"https://api.github.com/users/PiRune/followers","following_url":"https://api.github.com/users/PiRune/following{/other_user}","gists_url":"https://api.github.com/users/PiRune/gists{/gist_id}","starred_url":"https://api.github.com/users/PiRune/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PiRune/subscriptions","organizations_url":"https://api.github.com/users/PiRune/orgs","repos_url":"https://api.github.com/users/PiRune/repos","events_url":"https://api.github.com/users/PiRune/events{/privacy}","received_events_url":"https://api.github.com/users/PiRune/received_events","type":"User","site_admin":false},"created_at":"2016-08-12T06:52:17Z","updated_at":"2016-08-12T06:52:17Z","body":"In case any newbies (such as myself) want a template, here is a basic one that works:\r\n\r\n```\r\n#!/usr/bin/env /usr/bin/python3\r\n# -*- coding: utf-8 -*-\r\n\r\nimport fliclib\r\n\r\nclient = fliclib.FlicClient(\"localhost\")\r\n\r\nMyButton1 = '80:e4:da:71:xx:xx' #white flic button in bedroom\r\nMyButton2 = '80:e4:da:71:xx:xy' #turquoise flic button in bedroom\r\nMyButton3 = '80:e4:da:71:xx:yy' #black flic button in kitchen\r\nMyButton4 = '80:e4:da:71:xy:yy' #white flic button in living room\r\n\r\ndef got_button(bd_addr):\r\n\tcc = fliclib.ButtonConnectionChannel(bd_addr)\r\n\tcc.on_button_single_or_double_click_or_hold = some_handler\r\n\tcc.on_connection_status_changed = \\\r\n\t\tlambda channel, connection_status, disconnect_reason: \\\r\n                        print(channel.bd_addr + \" \" + str(connection_status) + (\" \" + str(disconnect_reason) if connection_status == fliclib.ConnectionStatus.Disconnected else \"\"))\r\n\tclient.add_connection_channel(cc)\r\n        \r\ndef got_info(items):\r\n\tprint(items)\r\n\tfor bd_addr in items[\"bd_addr_of_verified_buttons\"]:\r\n\t\tgot_button(bd_addr)\r\n\r\ndef some_handler(channel, click_type, was_queued, time_diff):\r\n    if channel.bd_addr == MyButton1:\r\n            try:\r\n                    if click_type == fliclib.ClickType.ButtonSingleClick:\r\n                        print(\"ButtonSingleClick has not been assigned an action\")                        \r\n                        \r\n                    if click_type == fliclib.ClickType.ButtonDoubleClick:\r\n                        print(\"ButtonDoubleClick has not been assigned an action\")\r\n                                       \r\n                    if click_type == fliclib.ClickType.ButtonHold:\r\n                        print(\"ButtonHold has not been assigned an action\")\r\n           \r\n            except Exception:\r\n                    import datetime\r\n                    print('An error occured: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))\r\n\r\n    if channel.bd_addr == MyButton2:\r\n            try:\r\n                    if click_type == fliclib.ClickType.ButtonSingleClick:\r\n                        print(\"ButtonSingleClick has not been assigned an action\")                        \r\n                        \r\n                    if click_type == fliclib.ClickType.ButtonDoubleClick:\r\n                        print(\"ButtonDoubleClick has not been assigned an action\")\r\n                                       \r\n                    if click_type == fliclib.ClickType.ButtonHold:\r\n                        print(\"ButtonHold has not been assigned an action\")\r\n           \r\n            except Exception:\r\n                    import datetime\r\n                    print('An error occured: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))\r\n\r\nclient.get_info(got_info)\r\n\r\nclient.on_new_verified_button = got_button\r\n\r\nclient.handle_events()\r\n\r\n```\r\n\r\nAnd here is the client script that I use everyday to control [Sonos](https://github.com/SoCo/SoCo), [Hue](https://github.com/studioimaginaire/phue) and the [WeMo](https://github.com/iancmcc/ouimeaux) insight switch:\r\n(can most likely be improved, but it works well)\r\n\r\n```\r\n#!/usr/bin/env /usr/bin/python3\r\n# -*- coding: utf-8 -*-\r\nfrom soco import SoCo\r\n\r\n#if __name__ == '__main__':\r\nsonos = SoCo('192.168.3.13') # IP of Sonos speaker\r\n\r\nfrom phue import Bridge\r\n\r\nb = Bridge('192.168.3.23') # IP of Hue bridge\r\n\r\nimport fliclib\r\n\r\nclient = fliclib.FlicClient(\"localhost\")\r\n\r\nMyButton1 = '80:e4:da:71:xx:xx' #white flic button in bedroom\r\nMyButton2 = '80:e4:da:71:xx:xy' #turquoise flic button in bedroom\r\nMyButton3 = '80:e4:da:71:xx:yy' #black flic button in kitchen\r\nMyButton4 = '80:e4:da:71:xy:yy' #white flic button in living room\r\n\r\ndef got_button(bd_addr):\r\n\tcc = fliclib.ButtonConnectionChannel(bd_addr)\r\n\tcc.on_button_single_or_double_click_or_hold = some_handler\r\n\tcc.on_connection_status_changed = \\\r\n\t\tlambda channel, connection_status, disconnect_reason: \\\r\n                        print(channel.bd_addr + \" \" + str(connection_status) + (\" \" + str(disconnect_reason) if connection_status == fliclib.ConnectionStatus.Disconnected else \"\"))\r\n\tclient.add_connection_channel(cc)\r\n        \r\ndef got_info(items):\r\n\tprint(items)\r\n\tfor bd_addr in items[\"bd_addr_of_verified_buttons\"]:\r\n\t\tgot_button(bd_addr)\r\n\r\ndef some_handler(channel, click_type, was_queued, time_diff):\r\n    \r\n    #volume control\r\n    if channel.bd_addr == MyButton1:\r\n            try:\r\n                    if click_type == fliclib.ClickType.ButtonSingleClick:\r\n                        old = sonos.volume\r\n                        new = old + 5\r\n                        sonos.volume = new\r\n                        print (\"Volume increased to: \" + str(sonos.volume) + \" / 50\" )\r\n                        \r\n                    if click_type == fliclib.ClickType.ButtonDoubleClick:\r\n                        old = sonos.volume\r\n                        new = old - 5\r\n                        sonos.volume = new\r\n                        print (\"Volume decreased to: \" + str(sonos.volume) + \" / 50\" )\r\n                                       \r\n                    if click_type == fliclib.ClickType.ButtonHold:\r\n                        print(\"ButtonHold has not been assigned an action\")\r\n            except Exception:\r\n                    import datetime\r\n                    print('An error occured (sonos volume): {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))\r\n\r\n    #sonos play/pause/next/prev\r\n    if channel.bd_addr == MyButton2:\r\n            try:\r\n                    if click_type == fliclib.ClickType.ButtonSingleClick:\r\n                        transport_info = sonos.get_current_transport_info()\r\n                        if transport_info['current_transport_state'] == 'PLAYING':\r\n                            sonos.pause()\r\n                            track = sonos.get_current_track_info()\r\n                            print (\"'\" + track['title'] + \"'\" + \" is paused\")\r\n                        else:\r\n                            sonos.play()\r\n                            track = sonos.get_current_track_info()\r\n                            print (\"Now playing: \" + \"'\" + track['title'] + \"'\")\r\n                        \r\n                    if click_type == fliclib.ClickType.ButtonDoubleClick:\r\n                        sonos.next()\r\n                        track = sonos.get_current_track_info()\r\n                        print (\"Skipping forward to \" + \"'\" + track['title'] + \"'\")\r\n                        \r\n                    if click_type == fliclib.ClickType.ButtonHold:\r\n                        sonos.previous()\r\n                        track = sonos.get_current_track_info()\r\n                        print (\"Skipping back to \" + \"'\" + track['title'] + \"'\")\r\n\r\n            except Exception:\r\n                    import datetime\r\n                    print('An error occurred (sonos play/pause/next/prev): {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))\r\n\r\n    #light control 1\r\n    if channel.bd_addr == MyButton3:\r\n            if click_type == fliclib.ClickType.ButtonSingleClick:\r\n                        if b.get_light( 1,'on') is False and b.get_light( 2,'on') is False and b.get_light( 4,'on') is False:\r\n                            b.set_group(1, 'on', True)\r\n                            import os\r\n                            os.system('wemo switch \"WeMo\" on')\r\n                            print(\"Living room lights on\")\r\n                        else:\r\n                            b.set_group(1, 'on', False)\r\n                            import os\r\n                            os.system('wemo switch \"WeMo\" off')\r\n                            print(\"Living room lights off\")\r\n                        \r\n                    if click_type == fliclib.ClickType.ButtonDoubleClick:\r\n                        import os\r\n                        os.system('wemo switch \"WeMo\" toggle')\r\n                        print(\"WeMo switch is: \")\r\n                        os.system('wemo -v switch \"WeMo\" status')\r\n                                       \r\n                    if click_type == fliclib.ClickType.ButtonHold:\r\n                        if b.get_light(4,'on') is False:\r\n                            b.set_light(4, 'on', True)\r\n                            print(\"Table lamp on\")\r\n                        else:\r\n                            b.set_light(4, 'on', False)\r\n                            print(\"Table lamp off\")\r\n\r\n    #light control 2\r\n    if channel.bd_addr == MyButton4:\r\n            if click_type == fliclib.ClickType.ButtonSingleClick:\r\n                        if b.get_light( 1,'on') is False and b.get_light( 2,'on') is False and b.get_light( 4,'on') is False:\r\n                            b.set_group(1, 'on', True)\r\n                            import os\r\n                            os.system('wemo switch \"WeMo\" on')\r\n                            print(\"Living room lights on\")\r\n                        else:\r\n                            b.set_group(1, 'on', False)\r\n                            import os\r\n                            os.system('wemo switch \"WeMo\" off')\r\n                            print(\"Living room lights off\")\r\n                        \r\n                    if click_type == fliclib.ClickType.ButtonDoubleClick:\r\n                        import os\r\n                        os.system('wemo switch \"WeMo\" toggle')\r\n                        print(\"WeMo switch is: \")\r\n                        os.system('wemo -v switch \"WeMo\" status')\r\n                        \r\n                    if click_type == fliclib.ClickType.ButtonHold:\r\n                        if b.get_light(4,'on') is False:\r\n                            b.set_light(4, 'on', True)\r\n                            print(\"Table lamp on\")\r\n                        else:\r\n                            b.set_light(4, 'on', False)\r\n                            print(\"Table lamp off\")\r\n\r\nclient.get_info(got_info)\r\n\r\nclient.on_new_verified_button = got_button\r\n\r\nclient.handle_events()\r\n\r\n```\r\nError handling (try-except) is necessary when using with sonos, since sending the \"next\" command will otherwise exit the script, if your sonos is at the end of a playlist and repeat is turned off.\r\n\r\nI had difficulties installing the Phue and SoCo library correctly in the python 3 folder, so I had to copy the libraries to the same folder as my client script to get it to work. Now it works and it is awesome :)\r\n"}}
{"actor":{"display_login":"PiRune"}}