Created
August 2, 2019 04:29
-
-
Save mulka/df682b4147bfb0fd152d873031282b56 to your computer and use it in GitHub Desktop.
Dash buttons controlling hue bulbs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import datetime | |
| import sys | |
| from scapy.all import * | |
| import requests | |
| last_probe = {} | |
| button_names = { | |
| 'mac_address': 'human readable name' | |
| } | |
| button_lights = { | |
| 'mac_address': 1, # 1 or other light number | |
| } | |
| button_hues = { | |
| 'mac_address': # hue integer | |
| } | |
| button_sats = { | |
| 'mac_address': # saturation integer | |
| } | |
| button_bris = { | |
| 'mac_address': # brightness integer | |
| } | |
| def got_probe(mac): | |
| hue = None | |
| sat = 254 | |
| bri = 254 | |
| if mac in button_names: | |
| name = button_names[mac] | |
| else: | |
| name = mac | |
| if mac in button_hues: | |
| hue = button_hues[mac] | |
| if mac in button_sats: | |
| sat = button_sats[mac] | |
| if mac in button_bris: | |
| bri = button_bris[mac] | |
| dup = False | |
| now = datetime.datetime.now() | |
| if mac in last_probe and now - last_probe[mac] < datetime.timedelta(minutes=1): | |
| dup = True | |
| last_probe[mac] = now | |
| light = 3 | |
| if mac in button_lights: | |
| light = button_lights[mac] | |
| if not dup: | |
| if hue is not None: | |
| change_light(light, hue, bri, sat) | |
| elif mac in ['mac_address1', 'mac_address2']: | |
| turn_off_light(light) | |
| print(now, name, "dup: %s" % dup) | |
| sys.stdout.flush() | |
| requests.post( | |
| 'http://example.com/webhook', | |
| json={"api_key": "API_KEY", "mac": mac} | |
| ) | |
| def send_light_data(light, data): | |
| requests.put( | |
| 'http://hue_hub_hostname/api/API_KEY/lights/%s/state' % light, | |
| json=data | |
| ) | |
| def change_light(light, hue, bri, sat): | |
| send_light_data(light, {"on": True, "sat": sat, "bri": bri,"hue": hue}) | |
| def turn_off_light(light): | |
| send_light_data(light, {"on": False}) | |
| def arp_display(pkt): | |
| if pkt.haslayer(ARP): | |
| if pkt[ARP].op == 1: #who-has (request) | |
| if pkt[ARP].psrc == '0.0.0.0': # ARP Probe | |
| got_probe(pkt[ARP].hwsrc) | |
| print(sniff(prn=arp_display, filter="arp", store=0, count=0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment