async def process_command(daemon, command): if " " not in command: options = "" else: command, options = command.split(" ", 1) if command.endswith("_json"): command = command[:command.rfind("_")] fltr = Filter.from_json_str(options) else: fltr = Filter.from_key_value_str(options) if command.startswith("serials"): async for device in daemon.serials(fltr): print(device.serial) elif command.startswith("info"): async for device in daemon.info(fltr): print(device.serial) print("\n".join(f" {line}" for line in json.dumps( device.info, sort_keys=True, indent=" ").split("\n"))) elif command.startswith("set_"): msg = ColourParser.msg(command[4:]) await daemon.sender(msg, daemon.reference(fltr)) else: print( dedent(""" commands are of the form '(serials|info|set_<color>) KEY=VALUE KEY=VALUE ...' or of the form '(serials_json|info_json|set_<color>_json) {"key": "value", "key": "value"}' """))
def color_message(self, state, keep_brightness): msg = ColourParser.msg(state.get("color", None), overrides=state) msg.res_required = False if keep_brightness: msg.brightness = 0 msg.set_brightness = False return msg
def assertCorrect(self, components, h, s, b, k): assert ColourParser.hsbk(components) == (h, s, b, k) ho = mock.Mock(name="hue_override") so = mock.Mock(name="saturation_override") bo = mock.Mock(name="brightness_override") ko = mock.Mock(name="kelvin_override") overrides = {"hue": ho} assert ColourParser.hsbk(components, overrides) == (ho, s, b, k) overrides["saturation"] = so assert ColourParser.hsbk(components, overrides) == (ho, so, b, k) overrides["brightness"] = bo assert ColourParser.hsbk(components, overrides) == (ho, so, bo, k) overrides["kelvin"] = ko assert ColourParser.hsbk(components, overrides) == (ho, so, bo, ko)
async def doit(collector): lan_target = collector.resolve_target("lan") parser = argparse.ArgumentParser() parser.add_argument("--reference", default="_") parser.add_argument("--brightness", type=float, default=1) args = parser.parse_args() reference = collector.reference_object(args.reference) power_on = DeviceMessages.SetPower(level=65535) spread = 1 color_names = [ "blue", "red", "orange", "yellow", "cyan", "green", "blue", "purple", "pink" ] color_msgs = [ ColourParser.msg( name, overrides={ "res_required": False, "duration": spread, "brightness": args.brightness, }, ) for name in color_names ] colors = Pipeline(*color_msgs, spread=spread, synchronized=True) pipeline = Pipeline(power_on, Repeater(colors, min_loop_time=len(color_names)), synchronized=True) def e(error): log.error(error) await lan_target.send(pipeline, reference, message_timeout=1, error_catcher=e)
def hsbk(*args, **kwargs): h, s, b, k = ColourParser.hsbk(*args, **kwargs) return hp.Color(h, s, b, k)
# Just color await server.assertCommand( "/v1/lifx/command", {"command": "transform", "args": {"transform": {"color": "red", "effect": "sine"}}}, json_output=expected, ) for device in devices: io = device.io["MEMORY"] want = Events.INCOMING( device, io, pkt=( LightMessages.SetWaveformOptional, ColourParser.msg( "red", overrides={"effect": "sine", "res_required": False} ).payload.as_dict(), ), ) assert devices.store(device).count(want) == 1, device devices.store(device).clear() # Power on and color for device in devices: await device.change_one("power", 0, event=None) await device.change_one(("color", "brightness"), 0.5, event=None) await devices["d073d5000001"].change_one("power", 0xFFFF, event=None) await devices["d073d5000003"].change_one("power", 0xFFFF, event=None) tv_light = devices.for_attribute("label", "tv", expect=1)[0] async with tv_light.offline():
await self.transform(runner, {"power": "on"}, expected=expected) async it "uses SetLightPower if we have duration", runner: msg = LightMessages.SetLightPower(level=0, duration=100, res_required=False) expected = {device: [msg] for device in runner.devices} await self.transform(runner, {"power": "off", "duration": 100}, expected=expected) await runner.reset_devices() msg = LightMessages.SetLightPower(level=65535, duration=20, res_required=False) expected = {device: [msg] for device in runner.devices} await self.transform(runner, {"power": "on", "duration": 20}, expected=expected) async it "just uses ColourParser.msg", runner: for color in (random.choice(["red", "blue", "green", "yellow"]), None): for state in generate_options(color, exclude=["power"]): want = ColourParser.msg(color, overrides=state) want.res_required = False await runner.reset_devices() expected = {device: [want] for device in runner.devices} await self.transform(runner, state, expected=expected) async it "can ignore brightness", runner: want = ColourParser.msg("hue:200 saturation:1") want.res_required = False expected = {device: [want] for device in runner.devices} await self.transform( runner, {"color": "hue:200 saturation:1 brightness:0.3"}, expected=expected,
async def doit(collector): lan_target = collector.resolve_target("lan") color_msg = ColourParser.msg("blue") on_msg = DeviceMessages.SetPower(level=65535) await lan_target.send([color_msg, on_msg], FoundSerials())
def hsbk(*args, **kwargs): h, s, b, k = ColourParser.hsbk(*args, **kwargs) return {"hue": h, "saturation": s, "brightness": b, "kelvin": k}
overrides = {"hue": ho} assert ColourParser.hsbk(components, overrides) == (ho, s, b, k) overrides["saturation"] = so assert ColourParser.hsbk(components, overrides) == (ho, so, b, k) overrides["brightness"] = bo assert ColourParser.hsbk(components, overrides) == (ho, so, bo, k) overrides["kelvin"] = ko assert ColourParser.hsbk(components, overrides) == (ho, so, bo, ko) it "supports random generation": for _ in range(100): h, s, b, k = ColourParser.hsbk("random") assert b is None assert k is None assert s == 1 assert h > -1 assert h < 361 it "supports just kelvin": self.assertCorrect("kelvin:2500", None, 0, None, 2500) self.assertCorrect("kelvin:3500", None, 0, None, 3500) with assertRaises(InvalidColor, "Unable to parse color"): ColourParser.hsbk("kelvin:-1") error = ValueOutOfRange( "Value was not within bounds",
async def light_msgs(self): color = self.aps["0d"]().apply_theme(self.theme) s = "kelvin:{} hue:{} saturation:{} brightness:{}".format( color.kelvin, color.hue, color.saturation, color.brightness ) yield ColourParser.msg(s, overrides={"duration": self.options.duration})