def main(): bulb = Light("d0:73:d5:28:0b:4a", "10.0.0.16") print("Selected {}".format(bulb.get_label())) # get original state original_power = bulb.get_power() original_color = bulb.get_color() bulb.set_power("on") sleep(0.2) # to look pretty print("Toggling power...") toggle_device_power(bulb, 0.2) print("Toggling color...") toggle_light_color(bulb, 0.2) # restore original color # color can be restored after the power is turned off as well print("Restoring original color and power...") bulb.set_color(original_color) sleep(1) # to look pretty. # restore original power bulb.set_power(original_power)
def state(self, device): bulbRef = Light(device['mac'], device['ip']) hsbk = bulbRef.get_color() dev = Device(device['mac'], device['ip'], bulbRef.get_power(), hsbk[0], hsbk[1], hsbk[2], hsbk[3]) return json.dumps(dev.__dict__)
def light_to_dict(light: Light): return { 'name': light.get_label(), 'power': bool(light.get_power()), 'color': light.get_color(), 'macAddress': light.get_mac_addr() }
def blink_light(mac, ip): """ Run a rapid blink on a light to provide a notification. """ light = Light(mac, ip) colors = light.get_color() power_state = light.get_power() repeats = 3 delay = 0.25 light.set_power(1) for _ in range(repeats): light.set_color(RED, rapid=True) sleep(delay) light.set_color(GREEN, rapid=True) sleep(delay) light.set_color(BLUE, rapid=True) sleep(delay) if power_state: light.set_color(colors) else: light.set_color(COLD_WHITE) sleep(1) light.set_color(colors) light.set_power(power_state)
def Main(): while True: i = GPIO.input(23) if i == 0: #When output from motion sensor is LOW now = datetime.now() dt_string = now.strftime("%m/%d/%Y %H:%M:%S") print dt_string, "No Motion Detected. Pin reads", i while True: try: powerstatus = Light.get_power(doorlight) except: now = datetime.now() dt_string = now.strftime("%m/%d/%Y %H:%M:%S") print dt_string, "Couldn't get powerstatus while no motion is detected. Return is", powerstatus continue break now = datetime.now() dt_string = now.strftime("%m/%d/%Y %H:%M:%S") print dt_string, "Before no motion power check", powerstatus if powerstatus != 0: #Check if lights are currently turned on now = datetime.now() dt_string = now.strftime("%m/%d/%Y %H:%M:%S") print dt_string, "After power check inside no motion", powerstatus officelights.set_power(0) #Turn lights off time.sleep(10) elif i == 1: #When output from motion sensor is HIGH now = datetime.now() dt_string = now.strftime("%m/%d/%Y %H:%M:%S") print dt_string, "Motion Detected. Pin reads", i while True: try: powerstatus = Light.get_power(doorlight) except: now = datetime.now() dt_string = now.strftime("%m/%d/%Y %H:%M:%S") print dt_string, "Couldn't get powerstatus while motion is detected. Return is", powerstatus continue break now = datetime.now() dt_string = now.strftime("%m/%d/%Y %H:%M:%S") print dt_string, "Motion detected powerstatus", powerstatus if powerstatus == 0: #Check if lights are currently turned off now = datetime.now() dt_string = now.strftime("%m/%d/%Y %H:%M:%S") print dt_string, "After power check inside motion detected", powerstatus officelights.set_power(65535) #Turn lights on full time.sleep(600)
class Bulb(object): def __init__(self, ip, mac, multizone=False): self.rapid = False if multizone: self.bulb = MultiZoneLight(mac, ip) else: self.bulb = Light(mac, ip) def supports_multizone(self): return self.bulb.supports_multizone() def get_color_zones(self): return self.bulb.get_color_zones() def set_zone_color(self, startZone, endZone, color, duration=5): return self.bulb.set_zone_color(startZone, endZone, color, duration=duration, rapid=self.rapid) def set_zone_colors(self, colors, duration=5): return self.bulb.set_zone_colors(colors, duration=duration, rapid=self.rapid) def set_power(self, power, duration=5): if duration: self.bulb.set_power(power, duration=duration, rapid=self.rapid) else: self.bulb.set_power(power) def get_power(self): return self.bulb.get_power() def set_color(self, color, duration=5): if duration: self.bulb.set_color(color, duration=duration, rapid=self.rapid) else: self.bulb.set_color(color) def get_color(self): return self.bulb.get_color() def get_label(self): return self.bulb.get_label() def get_brightness(self): return self.bulb.get_color()[2] def set_brightness(self, brightness, duration=5): return self.bulb.set_brightness(brightness, duration, rapid=self.rapid) def fast_mode(self, hostIP=None): self.rapid = True def slow_mode(self): self.rapid = False
def toggle_device_power(device: lifxlan.Light, interval=.5, num_cycles=3): original_power_state = device.get_power() device.set_power("off") rapid = True if interval < 1 else False for i in range(num_cycles): device.set_power("on", rapid) sleep(interval) device.set_power("off", rapid) sleep(interval) device.set_power(original_power_state)
def power_status(mac, ip): try: light = Light(mac, ip) power_state = light.get_power() return power_state except Exception as e: print(e)
class LifxLightBulb(LightBulb): def __init__(self, ip_addr, mac_addr): _LOGGER.info('creating ip_addr=%s mac_addr=%s', ip_addr, mac_addr) self._device = Light(mac_addr, ip_addr) def get_power(self): _LOGGER.info('getting power') return self._device.get_power() == 65536 def turn_on(self): _LOGGER.info('turning on') self._device.set_power(True) def turn_off(self): _LOGGER.info('turning off') self._device.set_power(False) def set_color(self, color): _LOGGER.debug('setting color=%s', color) self._device.set_color(to_hsbk(color), rapid=True)
class LightController: def __init__(self, mac = None, ip = None): self.bulb = None if mac != None and ip != None: self.bulb = Light(mac, ip) # put a try block in here later elif self.bulb == None: #put this in the catch block later lights = LifxLAN(1) self.bulb = lights.get_lights()[0] # just get whatever light you find else: lights = LifxLAN(1) self.bulb = lights.get_lights()[0] self.color = 0 #self.colors = [BLUE, CYAN, GREEN, ORANGE, PINK, PURPLE, RED, YELLOW, WHITE] self.colors = [BLUE, GREEN, RED, WHITE] def shiftColor(self): self.color = (1 + self.color) % len(self.colors) self.bulb.set_color(self.colors[self.color]) def togglePower(self): if self.bulb.get_power() == 65535: self.bulb.set_power(0) else: self.bulb.set_power(65535)
def set_lights(self, postvars={}): sched.pause() resp = 'no post data found' l = None if any(postvars): resp = 'vars!' mac = postvars.get('mac', None) if mac: ip = postvars.get('ip', None) if ip: l = Light(mac[0], ip[0]) light = postvars.get('light', None) if light: logging.debug(light) if light[0] in LIGHTS: logging.debug('found {}'.format(light[0])) light = LIGHTS.get(light[0]) mac = light.get('mac') ip = light.get('ip') colour = light.get('colour') l = Light(mac, ip) else: logging.debug(LIGHTS) l = self.devices.get_device_by_name(light[0]) if l: colour = l.get_color() if l: level = postvars.get('level', None) dim = postvars.get('dim', None) white = postvars.get('white', None) if level is not None: try: if (level[0] == 'full'): h, s, b, k = colour b = 65535 l.set_power('on') l.set_color([h, s, b, k], 300) else: l.set_power(level[0]) resp = 'set power {}'.format(level) except Exception as e: resp = 'err... {}'.format(repr(e)) elif dim is not None: switch_after_dim = False try: h, s, b, k = colour if l.get_power() == 0: switch_after_dim = True b = 0 dim = dim[0] if dim not in ('up', 'down'): dim = LIGHTS[l.get_label()].get('last_dim', None) if dim is None or b in (0, 65535): if b > 32000: dim = 'down' else: dim = 'up' if dim == 'down': b -= 6554 if dim == 'up': b += 6554 if b < 0: b = 0 if b > 65535: b = 65535 l.set_color([h, s, b, k], 600) if LIGHTS.get(l.get_label(), None) is None: LIGHTS[l.get_label()] = {'mac': l.get_mac_addr(), 'ip': l.get_ip_addr(), 'colour': l.get_color(), 'last_dim': dim} else: LIGHTS[l.get_label()]['colour'] = [h, s, b, k] LIGHTS[l.get_label()]['last_dim'] = dim if switch_after_dim is True: l.set_power('on') resp = 'set brightness {}'.format(b) except Exception as e: resp = 'dim... {}'.format(repr(e)) elif white is not None: try: h, s, b, k = colour white = white[0] if white not in ('warm', 'cool'): k = int(white) if white == 'warm': k -= 500 if white == 'cool': k += 500 if k < 2500: k = 2500 if k > 9000: k = 9000 l.set_color([h, s, b, k], 500) if LIGHTS.get(l.get_label(), None) is None: LIGHTS[l.get_label()] = {'mac': l.get_mac_addr(), 'ip': l.get_ip_addr(), 'colour': l.get_color()} else: LIGHTS[l.get_label()]['colour'] = [h, s, b, k] resp = 'set white level {}'.format(k) except Exception as e: resp = 'white... {}'.format(repr(e)) else: try: if l.get_power() > 0: l.set_power(0) else: l.set_power('on') except: resp = 'nope...' else: resp = "<p>Light not found ):</p>" sched.resume() return resp
import RPi.GPIO as GPIO import requests import time from lifxlan import LifxLAN, Light, WHITE from gpiozero import Button from time import sleep # Turn off GPIO warnings GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) # Variables - Add your MAC and IP number here. lifxlan = Light("d0:23:d5:2d:4e:60", "192.168.2.1") currentstate = 0 button = Button(14) previousstate = lifxlan.get_power() lightname = lifxlan.get_label() # Show current light state if previousstate == 0: print("The", lightname, "is currently OFF") elif previousstate == 65535: print("The", lightname, "is currently ON") try: print("Starting LiPiSwitch for the light named", lightname, "...") print("") print("lifXbutton Ready ...") print("") # Loop until user quits with CTRL-C