From c955b1a12a9dfa804bd30df8eb0634555f763141 Mon Sep 17 00:00:00 2001 From: Lukas Winkler Date: Sun, 5 Aug 2018 20:15:20 +0200 Subject: [PATCH] make backup of hue script --- dash.py | 32 ++++++++++++++++++++++++++++++++ lights.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 dash.py create mode 100644 lights.py diff --git a/dash.py b/dash.py new file mode 100644 index 0000000..673e322 --- /dev/null +++ b/dash.py @@ -0,0 +1,32 @@ +import socket +import struct +import binascii +from lights import lights +# Written by Bob Steinbeiser (https://medium.com/@xtalker) + +rawSocket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, + socket.htons(0x0003)) +MAC = '50f5da0efe74' + +while True: + packet = rawSocket.recvfrom(2048) + + ethernet_header = packet[0][0:14] + ethernet_detailed = struct.unpack('!6s6s2s', ethernet_header) + + # skip non-ARP packets + ethertype = ethernet_detailed[2] + if ethertype != '\x08\x06': + continue + + arp_header = packet[0][14:42] + arp_detailed = struct.unpack('2s2s1s1s2s6s4s6s4s', arp_header) + + + source_mac = binascii.hexlify(arp_detailed[5]) + dest_ip = socket.inet_ntoa(arp_detailed[8]) + print(source_mac) + if source_mac == MAC: + print "Dash button pressed!, IP = " + dest_ip + lights() + diff --git a/lights.py b/lights.py new file mode 100644 index 0000000..6ca412d --- /dev/null +++ b/lights.py @@ -0,0 +1,41 @@ +#!/usr/bin/python +import time +from phue import Bridge + +b = Bridge('192.168.0.13') + +TRANSITIONTIME = 1 * 10 +WHITETEMP = 350 # 154 -- 500 +WHITEBRIGHT = 255 + +b.connect() + + +def lights(): + initial_lights = b.get_light() + + lights = b.get_light_objects("id") + gang = b.get_group(2, 'lights') + for light_id in gang: + l = lights[int(light_id)] + l.transitiontime = TRANSITIONTIME + l.on = True + l.colortemp = WHITETEMP + + time.sleep(TRANSITIONTIME / 10) + + for light_id in gang: + l = lights[int(light_id)] + l.transitiontime = TRANSITIONTIME + l.brightness = WHITEBRIGHT + + time.sleep(180) + + for l in b.get_light_objects(): + light_id = str(l.light_id) + old_light = initial_lights[light_id]["state"] + l.transitiontime = TRANSITIONTIME + l.xy = old_light["xy"] + l.brightness = old_light["bri"] + l.on = old_light["on"] +