def theme(self): theme = Theme() theme.add_hsbk(0, 1, 1, 3500) theme.add_hsbk(100, 1, 1, 3500) theme.add_hsbk(200, 1, 1, 3500) theme.add_hsbk(300, 1, 1, 3500) return theme
async def do_apply_theme(target, reference, afr, options): aps = appliers[options.theme] theme = Theme() for color in options.colors: theme.add_hsbk(color.hue, color.saturation, color.brightness, color.kelvin) tasks = [] async for pkt, _, _ in target.script(DeviceMessages.GetVersion()).run_with( reference, afr): serial = pkt.serial capability = capability_for_ids(pkt.product, pkt.vendor) if capability.has_multizone: log.info(hp.lc("Found a strip", serial=serial)) t = hp.async_as_background( apply_zone(aps["1d"], target, afr, pkt.serial, theme, options.overrides)) elif capability.has_chain: log.info(hp.lc("Found a tile", serial=serial)) t = hp.async_as_background( apply_tile(aps["2d"], target, afr, pkt.serial, theme, options.overrides)) else: log.info(hp.lc("Found a light", serial=serial)) t = hp.async_as_background( apply_light(aps["0d"], target, afr, pkt.serial, theme, options.overrides)) tasks.append((serial, t)) results = {} for serial, t in tasks: try: await t except Exception as error: results[serial] = error else: results[serial] = "ok" return results
def script(kls, options): aps = appliers[options.theme] theme = Theme() for color in options.colors: theme.add_hsbk(color.hue, color.saturation, color.brightness, color.kelvin) async def gen(reference, sender, **kwargs): instance = kls(reference, sender, kwargs, aps, theme, options) # Turn on the device yield LightMessages.SetLightPower(level=65535, duration=options.duration) # Yield messages to turn on the theme for this device plans = sender.make_plans("capability") async for serial, _, info in sender.gatherer.gather(plans, reference): async for m in instance.apply(info["cap"]): yield m # Use gen per device to apply the theme return FromGeneratorPerSerial(gen)
def assertTileHues(t, got, *expect): t.assertEqual(len(got), len(expect)) got_hues = [float("{:.3f}".format(c.hue)) for c in got] expect_hues = [float("{:.3f}".format(h)) for h in expect] t.assertEqual(got_hues, expect_hues) @contextmanager def no_shuffle(theme): shuffled = mock.Mock(name="shuffled", return_value=theme) with mock.patch.object(theme, "shuffled", shuffled): yield shuffled.assert_called_once_with() describe TestCase, "TileApplierVerticalStripe": it "applies a vertical stripe": theme = Theme() theme.add_hsbk(0, 1, 1, 3500) theme.add_hsbk(10, 1, 1, 3500) theme.add_hsbk(50, 1, 1, 3500) theme.add_hsbk(300, 1, 1, 3500) user_coords_and_sizes = [ ((1, 1), (6, 6)) , ((1, 0), (6, 6)) , ((2, 0), (6, 6)) ] applier = TileApplierVerticalStripe.from_user_coords(user_coords_and_sizes) with no_shuffle(theme): tiles = applier.apply_theme(theme)
if e is None: assert g == ThemeColor(0, 0, 0.3, 3500) got_hues.append(0) expected_hues.append(0) else: assert g.saturation == 1 assert g.brightness == 1 assert g.kelvin == 3500 got_hues.append("{:.3f}".format(g.hue)) expected_hues.append("{:.3f}".format(e)) assert got_hues == expected_hues it "can create the canvas": theme = Theme() theme.add_hsbk(0, 1, 1, 3500) theme.add_hsbk(100, 1, 1, 3500) theme.add_hsbk(200, 1, 1, 3500) theme.add_hsbk(300, 1, 1, 3500) user_coords_and_sizes = [ ((0.5, 1.5), (8, 8)), ((0.5, 0.5), (8, 8)), ((1.5, 0.5), (8, 8)), ] applier = TileApplierSplotch.from_user_coords(user_coords_and_sizes) applier.just_points = True tiles = applier.apply_theme(theme)
((1, 1), (8, 8)) , ((2, 2), (10, 9)) , ((3, 3), (6, 6)) ] applier = TileApplier.from_user_coords(coords_and_sizes) self.assertEqual(applier.coords_and_sizes , [ ((8 - 4, 8 + 4), (8, 8)) , ((20 - 5, int(18 + 4.5)), (10, 9)) , ((18 - 3, 18 + 3), (6, 6)) ] ) describe TestCase, "TileApplierPattern": it "can apply a theme using the color func": theme = Theme() class Applier(TileApplierPattern): def color_func_generator(s, t, c): self.assertIs(t, theme) def f(i, j): return ThemeColor(i * 10 + j, 1, 1, 3500) return f user_coords_and_sizes = [ ((2, 2), (6, 6)) , ((3, 3), (6, 6)) ] applier = Applier.from_user_coords(user_coords_and_sizes)
# coding: spec from photons_themes.appliers.single import LightApplier from photons_themes.theme import Theme, ThemeColor from photons_app.test_helpers import TestCase import mock describe TestCase, "LightApplier": it "just returns a random color from the theme": color = mock.Mock(name="color") theme = mock.Mock(name="theme") theme.shuffled.return_value = theme theme.random.return_value = color applier = LightApplier() self.assertIs(applier.apply_theme(theme), color) it "returns white if there is no colors in the theme": theme = Theme() applier = LightApplier() self.assertEqual(applier.apply_theme(theme), ThemeColor(0, 0, 1, 3500))
def theme(self): theme = Theme() theme.add_hsbk(0, 0, 1, 3500) theme.add_hsbk(100, 1, 0, 3400) theme.add_hsbk(320, 1, 1, 5900) return theme
colors = [ThemeColor(hue, 1, 1, 3500) for hue in (100, 20, 30)] color = ThemeColor.average(colors) self.assertColorAlmostEqual(color, ThemeColor(48.2227, 1, 1, 3500)) colors = [ThemeColor(hue, 1, 1, 3500) for hue in (100, 20, 30, 300)] color = ThemeColor.average(colors) self.assertColorAlmostEqual(color, ThemeColor(24.2583, 1, 1, 3500)) colors = [ThemeColor(hue, 1, 1, 3500) for hue in (100, 300)] color = ThemeColor.average(colors) self.assertColorAlmostEqual(color, ThemeColor(20, 1, 1, 3500)) describe TestCase, "Theme": it "has colors": theme = Theme() self.assertEqual(theme.colors, []) it "can have colors added": theme = Theme() self.assertEqual(theme.colors, []) theme.add_hsbk(320, 1, 0, 3500) theme.add_hsbk(100, 0.5, 0.3, 3400) self.assertEqual(theme.colors , [ ThemeColor(320, 1, 0, 3500) , ThemeColor(100, 0.5, 0.3, 3400) ] )