def test_set_power(): bulb = lazylights.Bulb(GATEWAY, '\x01\x02\x03\x04\x05\x06', ('192.168.100.100', 56700)) with patch('lazylights.socket') as socket_mock: lazylights.set_power([bulb], True) socket_mock.socket.assert_called_once_with( socket_mock.AF_INET, socket_mock.SOCK_DGRAM, socket_mock.IPPROTO_UDP) socket_mock.socket().sendto.assert_called_once_with( build_packet(lazylights.REQ_SET_POWER_STATE, bulb.gateway_mac, bulb.mac, '2s', '\xff\xff'), bulb.addr)
def setBulbPower(bulbs, power): elapsedTime = 0 # exit if bulb is already in requested state bulbStatePre = lazylights.get_state(bulbs) if powerState(bulbStatePre) and power == 1: return if not powerState(bulbStatePre) and power == 0: return lazylights.set_power(bulbs, power) # wait for status update to ensure action was completed while elapsedTime < 5: bulbState = lazylights.get_state(bulbs) if powerState(bulbState) == power: print(getLabels(bulbState) + "Bulbs powered " + ("ON" if power else "OFF")) return time.sleep(0.5) elapsedTime += 1
def lightChanger(): global sliderValue, KELVIN global bpm, idName global beginBPMSlidder #mac and IP MUST be associated by pair mac1 = '' mac2 = '' mac3 = '' mac4 = '' mac5 = '' i = 0 ip1 = sys.argv[i + 1] ip2 = sys.argv[i + 2] ip3 = sys.argv[i + 3] ip4 = sys.argv[i + 4] ip5 = sys.argv[i + 5] myBulb1 = createBulb(ip1, mac1) myBulb2 = createBulb(ip2, mac2) myBulb3 = createBulb(ip3, mac3) myBulb4 = createBulb(ip4, mac4) myBulb5 = createBulb(ip5, mac5) bulbs = [myBulb1, myBulb2, myBulb3, myBulb4, myBulb5] # now bulbs can be called by their names bulbs_by_name = { state.label.strip(" \x00"): state.bulb for state in lazylights.get_state(bulbs) } if (len(bulbs) == 0): print( "No LIFX bulbs found. Make sure you're on the same WiFi network and try again" ) sys.exit(1) # turn on lazylights.set_power(bulbs, True) # do nothing during a tenth of a second time.sleep(0.1) # init counters/accumulators red = 0 green = 0 blue = 0 redReg = 0 greenReg = 0 blueReg = 0 begin1 = time.time() beatLenghtReg = DURATION + 1 # to force it to a state beatLenghtReg not equal to beatLenght beginBPM = time.time() countBeat = 1 cReg = Color(rgb=(1, 0, 0)) global cHue cHue = cReg.hue sliderValueReg = sliderValue # the music changed while True: graphInterfaceUpdate() if bpm != 0: beatLenght = SLOW_DOWN * 60000.0 / bpm else: beatLenght = DURATION if beatLenghtReg != beatLenght: beginBPM = time.time() countBeat = 0 beatLenghtReg = beatLenght print("music changed BPM is now: ", bpm, " track name from spotify is ", idName) # no music is playing (e.g. pause or just only watching youtube music) elif (beatLenght == DURATION): #i.e. bpm == 0 cHue += 0.01 time.sleep( 0.2 ) # 20 wifi commands per seconds, can be increased if no checking lazylights.set_state(bulbs, (cHue + 0.5) * 360, 1, 1, KELVIN, 200, False) # LIFX 246D45 lazylights.set_state(bulbs, cHue * 360, 1, 1, KELVIN, 200, False) #print(cHue) # 31ea4e # while music is playing else: #if sliderValue != sliderValueReg beginBPMSlidder = beginBPM + 2 * sliderValue * beatLenght / 100000 #slider value is 0->100 #print("beginBPMSlidder", beginBPMSlidder) #sliderValueReg = sliderValueReg # this is the same music a = (beatLenght / 1000) - (time.time() - beginBPMSlidder) % (beatLenght / 1000.0) a = max(0, a) time.sleep(a) # should not sleep if 0 countBeat += 1 red = random.uniform(0, 1) #find RGB that is not looking the same as the previous RGB while abs(red - redReg) < 0.15: red = random.uniform(0, 1) while abs(blue - blueReg) < 0.15: blue = random.uniform(0, 1) while abs(green - greenReg) < 0.15: green = random.uniform(0, 1) c = Color( rgb=(red, green, blue) ) # display a random color but sufficently different from the previous one # on even numbers, first lifx is light on if countBeat % 2 == 0: lazylights.set_state(bulbs, cReg.hue * 360, cReg.saturation, INTENSITY, KELVIN, 0, False) # on odd numbers it is the other one else: cReg = c redReg = red # save each previous color blueReg = blue greenReg = green lazylights.set_state(bulbs, c.hue * 360, c.saturation, INTENSITY, KELVIN, 0, False)
def off(self, bulbobj): lazylights.set_power([bulbobj.bulb], False) return True
def on(self, bulbobj): lazylights.set_power([bulbobj.bulb], True) return True
dest='state', required=False, help='[Input] lights on or off') requiredNamed.add_argument('-b', '--brightness', dest='brightness', required=False, help='[Input] brightness of lights: 0-100') results = parser.parse_args() print results myBulb1 = createBulb('10.0.0.x', 'xx:xx:xx:xx:xx:xx') #myBulb2 = createBulb('10.0.0.x','xx:xx:xx:xx:xx:xx') bulbs = [myBulb1] #bulbs = lazylights.find_bulbs(expected_bulbs=6) if results.brightness: print "Setting Brightness" val = int(results.brightness) / float(100) print val lazylights.set_state(bulbs, 1, 0, val, 4000, 100, raw=False) exit(0) if results.state == 'on': print 'Turning on' lazylights.set_power(bulbs, True) else: print 'Turning off' lazylights.set_power(bulbs, False)
def toggle_power(self): """ Toggles the bulb collection's power on or off """ self.current_state.is_on = not self.current_state.is_on lazylights.set_power(self.bulbs, self.current_state.is_on)
def setState(self, inRoom, state): print "Setting lights in room", inRoom,"to state",state if inRoom < 0 or inRoom > 1 or state < 0 or state > 2: print "inRoom or state out of range" return if inRoom == 0: # Stue self.states[0] = state if state == 0: for i in range(0,self.resends): for device in self.devices: label = device.label.decode('utf8').encode('ascii','ignore') if "Spisebord" in label: lazylights.set_power([device.bulb], False) elif "Stuehjrne" in label: lazylights.set_power([device.bulb], False) sleep(0.5) self._rfOff(0) self._rfOff(1) self._rfOff(2) elif state == 1: for i in range(0,self.resends): for device in self.devices: label = device.label.decode('utf8').encode('ascii','ignore') if "Spisebord" in label: lazylights.set_power([device.bulb], True) lazylights.set_state([device.bulb], 49.0, 0.1, 0.4, 3000, 500) elif "Stuehjrne" in label: lazylights.set_power([device.bulb], True) lazylights.set_state([device.bulb], 49.0, 0.1, 0.3, 3200, 500) sleep(0.5) self._rfOn(0) self._rfOn(1) self._rfOn(2) elif state == 2: for i in range(0,self.resends): for device in self.devices: label = device.label.decode('utf8').encode('ascii','ignore') if "Spisebord" in label: lazylights.set_power([device.bulb], True) lazylights.set_state([device.bulb], 0.0, 0.0, 0.95, 3000, 500) elif "Stuehjrne" in label: lazylights.set_power([device.bulb], True) lazylights.set_state([device.bulb], 0.0, 0.0, 0.67, 3200, 500) sleep(0.5) self._rfOn(0) self._rfOn(1) self._rfOn(2) elif inRoom == 1: # Kontor self.states[1] = state if state == 0: for i in range(0,self.resends): for device in self.devices: label = device.label.decode('utf8').encode('ascii','ignore') if "Idagulv" in label: lazylights.set_power([device.bulb], False) elif "Leselys" in label: lazylights.set_power([device.bulb], False) elif "Lskule" in label: lazylights.set_power([device.bulb], False) elif "PC lys" in label: lazylights.set_power([device.bulb], False) elif state == 1: for i in range(0,self.resends): for device in self.devices: label = device.label.decode('utf8').encode('ascii','ignore') if "Idagulv" in label: lazylights.set_power([device.bulb], True) lazylights.set_state([device.bulb], 49.0, 0.2, 0.4, 3000, 500) elif "Leselys" in label: lazylights.set_power([device.bulb], True) lazylights.set_state([device.bulb], 0.0, 0.2, 0.4, 3000, 500) elif "Lskule" in label: lazylights.set_power([device.bulb], True) lazylights.set_state([device.bulb], 49.0, 0.2, 0.2, 3000, 500) elif "PC lys" in label: lazylights.set_power([device.bulb], True) lazylights.set_state([device.bulb], 49.0, 0.2, 0.2, 3200, 500) elif state == 2: for i in range(0,self.resends): for device in self.devices: label = device.label.decode('utf8').encode('ascii','ignore') if "Idagulv" in label: lazylights.set_power([device.bulb], True) lazylights.set_state([device.bulb], 0.0, 0.0, 1.0, 3200, 500) elif "Leselys" in label: lazylights.set_power([device.bulb], True) lazylights.set_state([device.bulb], 0.0, 0.0, 1.0, 3000, 500) elif "Lskule" in label: lazylights.set_power([device.bulb], True) lazylights.set_state([device.bulb], 0.0, 0.0, 0.4, 3200, 500) elif "PC lys" in label: lazylights.set_power([device.bulb], True) lazylights.set_state([device.bulb], 0.0, 0.0, 0.35, 3200, 500)
try: status = appleout[1] except IndexError: print "error parsing" print status N += 1 if (status == savedStatus) and (firstTime == False) and ( N % 10): print "No Change." continue print "********************** Periodic Check *******************" try: lazylights.set_power(bulbs,True) except: # catch *all* exceptions e = sys.exc_info()[0] print("Unexpected error:", sys.exc_info()[0]) if (status == 'Available'): print "Status Detected!(%s)" % status c = Color("green") elif (status == 'Busy') or (status == 'Do Not Disturb') or (status == 'In a Meeting'): print "Status Detected!(%s)" % status c = Color("red") elif (status == 'Away') or (status == 'Be Right Back') or (status == 'Off Work'): print "Status Detected(%s)" % status