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 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
class LifxController: def __init__(self, bulb_mac=None, bulb_ip=None): logging.debug("Initialising LifxController.") self.lan = LifxLAN() self.bulbs = None self.groups = None self.bulb_labels: list = [] self.group_labels: list = [] self.bulb = None self.group = None self.bulb_mac = bulb_mac self.bulb_ip = bulb_ip # Logger config self.logger = logging.getLogger(__name__) self.logger.setLevel(logging.DEBUG) self.ch = logging.StreamHandler() self.ch.setLevel(logging.DEBUG) self.formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') self.ch.setFormatter(self.formatter) self.logger.addHandler(self.ch) # When loading config, we don't need to discover the bulb if self.bulb_mac and self.bulb_ip: from lifxlan import Light self.bulb = Light(self.bulb_mac, self.bulb_ip) def discover_bulbs(self): """ Discovers individual bulbs, then groups """ logging.debug("discover_bulbs: Discovering individual bulbs.") # Discover bulbs on the LAN self.bulbs = self.lan.get_devices() logging.debug( f"discover_bulbs: Discovery complete. {len(self.lan.devices)} bulbs found." ) for bulb in self.bulbs: # Build a list of bulb names bulb_name = bulb.get_label() if bulb_name not in self.bulb_labels: self.bulb_labels.append(bulb_name) # Figure out what groups exist from their group_labels # There is no way to simply discover what groups are available group = bulb.get_group_label() if group: if group not in self.group_labels: self.group_labels.append(group) def select_target(self): """ Creates menu to select target bulb or group. """ title = "Would you like to target a single bulb, or groups of bulbs?" options = ["Single", "Group"] _, selection = pick(options, title) print(type(selection), selection) if selection == 0: logging.debug("User is going to target a single bulb.") title = "Select the bulb to target" _, selection = pick(self.bulb_labels, title) self.bulb = self.bulbs[selection] elif selection == 1: logging.debug("User is going to target a group of bulbs.") title = "Select the target group" _, selection = pick(self.group_labels, title) self.group = self.groups[selection] def get_colour(self): """ Obtains the current colour of the bulb. """ if self.bulb: return self.bulb.get_color() elif self.group: return self.group.get_color() def set_colour(self, colour): """ Sets colour of selected bulb to input. Input is HSBK format, which seems to be specific to LifX and really poorly documented at the time of this comment. https://api.developer.lifx.com/docs/colors input: list """ if self.bulb: return self.bulb.set_color(colour) if self.group: return self.group.set_color(colour) def get_config(self): """ Returns bulb config, to save for later. return: dict """ if self.bulb: bulb_config: dict = { "mac_addr": self.bulb.get_mac_addr(), "ip_addr": self.bulb.get_ip_addr(), "label": self.bulb.get_label() } return bulb_config else: logging.debug("get_config: Returning group config")