class YeetBulb: def __init__(self, name, ip): self.name = name self.ip = ip self.bulb = Bulb(self.ip, effect="smooth", duration=750, auto_on=True) self.properties = self.bulb.get_properties() def __getitem__(self, key): return getattr(self, key) def turnoff(self): self.bulb.turn_off() def turnon(self): self.bulb.turn_on() def toggle(self): self.bulb.toggle() def loadPreset(self, preset): if os.path.isfile(preset): with open(preset, 'r') as f: config = json.load(f) if 'preset' in config: preset = config['preset'] if ('rgb' in preset): try: self.bulb.set_rgb(preset['rgb'][0], preset['rgb'][1], preset['rgb'][2]) except: print('not supported by bulb') if ('hsv' in preset): try: self.bulb.set_hsv(preset['hsv'][0], preset['hsv'][1], preset['hsv'][2]) except: print('not supported by bulb') if ('color_temp' in preset): try: self.bulb.set_color_temp(preset['color_temp']) except: print('not supported by bulb') if ('brightness' in preset): try: self.bulb.set_brightness(preset['brightness']) except: print('not supported by bulb') if ('color_temp' in preset): try: self.bulb.set_color_temp(preset['color_temp']) except: print('not supported by bulb') else: print('File not found: ' + preset) def updateProperties(self): self.properties = self.bulb.get_properties()
class Tests(unittest.TestCase): def setUp(self): self.socket = SocketMock() self.bulb = Bulb(ip="", auto_on=True) self.bulb._Bulb__socket = self.socket def test_rgb1(self): self.bulb.set_rgb(255, 255, 0) self.assertEqual(self.socket.sent["method"], "set_rgb") self.assertEqual(self.socket.sent["params"], [16776960, "smooth", 300]) def test_rgb2(self): self.bulb.effect = "sudden" self.bulb.set_rgb(255, 255, 0) self.assertEqual(self.socket.sent["method"], "set_rgb") self.assertEqual(self.socket.sent["params"], [16776960, "sudden", 300]) def test_rgb3(self): self.bulb.set_rgb(255, 255, 0, effect="sudden") self.assertEqual(self.socket.sent["method"], "set_rgb") self.assertEqual(self.socket.sent["params"], [16776960, "sudden", 300]) def test_hsv1(self): self.bulb.set_hsv(200, 100, effect="sudden") self.assertEqual(self.socket.sent["method"], "set_hsv") self.assertEqual(self.socket.sent["params"], [200, 100, "sudden", 300]) def test_hsv2(self): self.bulb.set_hsv(200, 100, 10, effect="sudden", duration=500) self.assertEqual(self.socket.sent["method"], "start_cf") self.assertEqual(self.socket.sent["params"], [1, 1, "50, 1, 43263, 10"]) def test_hsv3(self): self.bulb.set_hsv(200, 100, 10, effect="smooth", duration=1000) self.assertEqual(self.socket.sent["method"], "start_cf") self.assertEqual(self.socket.sent["params"], [1, 1, "1000, 1, 43263, 10"]) def test_hsv4(self): self.bulb.effect = "sudden" self.bulb.set_hsv(200, 100, 10, effect="smooth", duration=1000) self.assertEqual(self.socket.sent["method"], "start_cf") self.assertEqual(self.socket.sent["params"], [1, 1, "1000, 1, 43263, 10"]) def test_toggle1(self): self.bulb.toggle() self.assertEqual(self.socket.sent["method"], "toggle") self.assertEqual(self.socket.sent["params"], ["smooth", 300]) self.bulb.toggle(duration=3000) self.assertEqual(self.socket.sent["params"], ["smooth", 3000]) def test_turn_off1(self): self.bulb.turn_off() self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["off", "smooth", 300]) self.bulb.turn_off(duration=3000) self.assertEqual(self.socket.sent["params"], ["off", "smooth", 3000]) def test_turn_on1(self): self.bulb.turn_on() self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "smooth", 300]) self.bulb.turn_on(duration=3000) self.assertEqual(self.socket.sent["params"], ["on", "smooth", 3000]) def test_turn_on2(self): self.bulb.effect = "sudden" self.bulb.turn_on() self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "sudden", 300]) def test_turn_on3(self): self.bulb.turn_on(effect="sudden", duration=50) self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "sudden", 50]) def test_turn_on4(self): self.bulb.power_mode = enums.PowerMode.MOONLIGHT self.bulb.turn_on() self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual( self.socket.sent["params"], ["on", "smooth", 300, enums.PowerMode.MOONLIGHT.value]) def test_turn_on5(self): self.bulb.turn_on(power_mode=enums.PowerMode.MOONLIGHT) self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual( self.socket.sent["params"], ["on", "smooth", 300, enums.PowerMode.MOONLIGHT.value]) def test_set_power_mode1(self): self.bulb.set_power_mode(enums.PowerMode.MOONLIGHT) self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual( self.socket.sent["params"], ["on", "smooth", 300, enums.PowerMode.MOONLIGHT.value]) def test_set_power_mode2(self): self.bulb.set_power_mode(enums.PowerMode.NORMAL) self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "smooth", 300, enums.PowerMode.NORMAL.value]) def test_set_power_mode3(self): self.bulb.set_power_mode(enums.PowerMode.LAST) self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "smooth", 300]) def test_color_temp1(self): self.bulb.set_color_temp(1400) self.assertEqual(self.socket.sent["method"], "set_ct_abx") self.assertEqual(self.socket.sent["params"], [1700, "smooth", 300]) self.bulb.set_color_temp(1400, duration=3000) self.assertEqual(self.socket.sent["params"], [1700, "smooth", 3000]) def test_color_temp2(self): self.bulb.set_color_temp(8400, effect="sudden") self.assertEqual(self.socket.sent["method"], "set_ct_abx") self.assertEqual(self.socket.sent["params"], [6500, "sudden", 300]) def test_color_temp_with_model_declared(self): self.bulb.model = "ceiling2" self.bulb.set_color_temp(1800) self.assertEqual(self.socket.sent["method"], "set_ct_abx") self.assertEqual(self.socket.sent["params"], [2700, "smooth", 300]) def test_start_flow(self): transitions = [ TemperatureTransition(1700, duration=40000), TemperatureTransition(6500, duration=40000) ] flow = Flow(count=1, action=Action.stay, transitions=transitions) self.bulb.start_flow(flow) self.assertEqual(self.socket.sent["method"], "start_cf") self.assertEqual(self.socket.sent["params"], [2, 1, "40000, 2, 1700, 100, 40000, 2, 6500, 100"]) def test_set_scene_color(self): self.bulb.set_scene(SceneClass.COLOR, 255, 255, 0, 10) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual(self.socket.sent["params"], ["color", 16776960, 10]) def test_set_scene_color_ambilight(self): self.bulb.set_scene(SceneClass.COLOR, 255, 255, 0, 10, light_type=LightType.Ambient) self.assertEqual(self.socket.sent["method"], "bg_set_scene") self.assertEqual(self.socket.sent["params"], ["color", 16776960, 10]) def test_set_scene_color_temperature(self): self.bulb.set_scene(SceneClass.CT, 2000, 15) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual(self.socket.sent["params"], ["ct", 2000, 15]) def test_set_scene_hsv(self): self.bulb.set_scene(SceneClass.HSV, 200, 100, 10) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual(self.socket.sent["params"], ["hsv", 200, 100, 10]) def test_set_scene_color_flow(self): transitions = [ TemperatureTransition(1700, duration=40000), TemperatureTransition(6500, duration=40000) ] flow = Flow(count=1, action=Action.stay, transitions=transitions) self.bulb.set_scene(SceneClass.CF, flow) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual( self.socket.sent["params"], ["cf", 2, 1, "40000, 2, 1700, 100, 40000, 2, 6500, 100"]) def test_set_scene_auto_delay_off(self): self.bulb.set_scene(SceneClass.AUTO_DELAY_OFF, 20, 1) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual(self.socket.sent["params"], ["auto_delay_off", 20, 1])
class Tests(unittest.TestCase): def setUp(self): self.socket = SocketMock() self.bulb = Bulb(ip="", auto_on=True) self.bulb._Bulb__socket = self.socket def test_rgb1(self): self.bulb.set_rgb(255, 255, 0) self.assertEqual(self.socket.sent["method"], "set_rgb") self.assertEqual(self.socket.sent["params"], [16776960, 'smooth', 300]) def test_rgb2(self): self.bulb.effect = "sudden" self.bulb.set_rgb(255, 255, 0) self.assertEqual(self.socket.sent["method"], "set_rgb") self.assertEqual(self.socket.sent["params"], [16776960, 'sudden', 300]) def test_rgb3(self): self.bulb.set_rgb(255, 255, 0, effect="sudden") self.assertEqual(self.socket.sent["method"], "set_rgb") self.assertEqual(self.socket.sent["params"], [16776960, 'sudden', 300]) def test_hsv1(self): self.bulb.set_hsv(200, 100, effect="sudden") self.assertEqual(self.socket.sent["method"], "set_hsv") self.assertEqual(self.socket.sent["params"], [200, 100, 'sudden', 300]) def test_hsv2(self): self.bulb.set_hsv(200, 100, 10, effect="sudden", duration=500) self.assertEqual(self.socket.sent["method"], "start_cf") self.assertEqual(self.socket.sent["params"], [1, 1, "50, 1, 43263, 10"]) def test_hsv3(self): self.bulb.set_hsv(200, 100, 10, effect="smooth", duration=1000) self.assertEqual(self.socket.sent["method"], "start_cf") self.assertEqual(self.socket.sent["params"], [1, 1, "1000, 1, 43263, 10"]) def test_hsv4(self): self.bulb.effect = "sudden" self.bulb.set_hsv(200, 100, 10, effect="smooth", duration=1000) self.assertEqual(self.socket.sent["method"], "start_cf") self.assertEqual(self.socket.sent["params"], [1, 1, "1000, 1, 43263, 10"]) def test_toggle1(self): self.bulb.toggle() self.assertEqual(self.socket.sent["method"], "toggle") self.assertEqual(self.socket.sent["params"], ["smooth", 300]) def test_turn_off1(self): self.bulb.turn_off() self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["off", "smooth", 300]) def test_turn_on1(self): self.bulb.turn_on() self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "smooth", 300]) def test_turn_on2(self): self.bulb.effect = "sudden" self.bulb.turn_on() self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "sudden", 300]) def test_turn_on3(self): self.bulb.turn_on(effect="sudden", duration=50) self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "sudden", 50]) def test_color_temp1(self): self.bulb.set_color_temp(1400) self.assertEqual(self.socket.sent["method"], "set_ct_abx") self.assertEqual(self.socket.sent["params"], [1700, "smooth", 300]) def test_color_temp2(self): self.bulb.set_color_temp(8400, effect="sudden") self.assertEqual(self.socket.sent["method"], "set_ct_abx") self.assertEqual(self.socket.sent["params"], [6500, "sudden", 300])
class Tests(unittest.TestCase): def setUp(self): self.socket = SocketMock() self.bulb = Bulb(ip="", auto_on=True) self.bulb._Bulb__socket = self.socket def test_rgb1(self): self.bulb.set_rgb(255, 255, 0) self.assertEqual(self.socket.sent["method"], "set_rgb") self.assertEqual(self.socket.sent["params"], [16776960, "smooth", 300]) def test_rgb2(self): self.bulb.effect = "sudden" self.bulb.set_rgb(255, 255, 0) self.assertEqual(self.socket.sent["method"], "set_rgb") self.assertEqual(self.socket.sent["params"], [16776960, "sudden", 300]) def test_rgb3(self): self.bulb.set_rgb(255, 255, 0, effect="sudden") self.assertEqual(self.socket.sent["method"], "set_rgb") self.assertEqual(self.socket.sent["params"], [16776960, "sudden", 300]) def test_hsv1(self): self.bulb.set_hsv(200, 100, effect="sudden") self.assertEqual(self.socket.sent["method"], "set_hsv") self.assertEqual(self.socket.sent["params"], [200, 100, "sudden", 300]) def test_hsv2(self): self.bulb.set_hsv(200, 100, 10, effect="sudden", duration=500) self.assertEqual(self.socket.sent["method"], "start_cf") self.assertEqual(self.socket.sent["params"], [1, 1, "50, 1, 43263, 10"]) def test_hsv3(self): self.bulb.set_hsv(200, 100, 10, effect="smooth", duration=1000) self.assertEqual(self.socket.sent["method"], "start_cf") self.assertEqual(self.socket.sent["params"], [1, 1, "1000, 1, 43263, 10"]) def test_hsv4(self): self.bulb.effect = "sudden" self.bulb.set_hsv(200, 100, 10, effect="smooth", duration=1000) self.assertEqual(self.socket.sent["method"], "start_cf") self.assertEqual(self.socket.sent["params"], [1, 1, "1000, 1, 43263, 10"]) def test_toggle1(self): self.bulb.toggle() self.assertEqual(self.socket.sent["method"], "toggle") self.assertEqual(self.socket.sent["params"], ["smooth", 300]) self.bulb.toggle(duration=3000) self.assertEqual(self.socket.sent["params"], ["smooth", 3000]) def test_turn_off1(self): self.bulb.turn_off() self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["off", "smooth", 300]) self.bulb.turn_off(duration=3000) self.assertEqual(self.socket.sent["params"], ["off", "smooth", 3000]) def test_turn_on1(self): self.bulb.turn_on() self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "smooth", 300]) self.bulb.turn_on(duration=3000) self.assertEqual(self.socket.sent["params"], ["on", "smooth", 3000]) def test_turn_on2(self): self.bulb.effect = "sudden" self.bulb.turn_on() self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "sudden", 300]) def test_turn_on3(self): self.bulb.turn_on(effect="sudden", duration=50) self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "sudden", 50]) def test_turn_on4(self): self.bulb.power_mode = enums.PowerMode.MOONLIGHT self.bulb.turn_on() self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual( self.socket.sent["params"], ["on", "smooth", 300, enums.PowerMode.MOONLIGHT.value]) def test_turn_on5(self): self.bulb.turn_on(power_mode=enums.PowerMode.MOONLIGHT) self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual( self.socket.sent["params"], ["on", "smooth", 300, enums.PowerMode.MOONLIGHT.value]) def test_set_power_mode1(self): self.bulb.set_power_mode(enums.PowerMode.MOONLIGHT) self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual( self.socket.sent["params"], ["on", "smooth", 300, enums.PowerMode.MOONLIGHT.value]) def test_set_power_mode2(self): self.bulb.set_power_mode(enums.PowerMode.NORMAL) self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "smooth", 300, enums.PowerMode.NORMAL.value]) def test_set_power_mode3(self): self.bulb.set_power_mode(enums.PowerMode.LAST) self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "smooth", 300]) def test_color_temp1(self): self.bulb.set_color_temp(1400) self.assertEqual(self.socket.sent["method"], "set_ct_abx") self.assertEqual(self.socket.sent["params"], [1700, "smooth", 300]) self.bulb.set_color_temp(1400, duration=3000) self.assertEqual(self.socket.sent["params"], [1700, "smooth", 3000]) def test_color_temp2(self): self.bulb.set_color_temp(8400, effect="sudden") self.assertEqual(self.socket.sent["method"], "set_ct_abx") self.assertEqual(self.socket.sent["params"], [6500, "sudden", 300]) def test_color_temp_with_model_declared(self): self.bulb._model = "ceiling2" self.bulb.set_color_temp(1800) self.assertEqual(self.socket.sent["method"], "set_ct_abx") self.assertEqual(self.socket.sent["params"], [2700, "smooth", 300]) def test_start_flow(self): transitions = [ TemperatureTransition(1700, duration=40000), TemperatureTransition(6500, duration=40000) ] flow = Flow(count=1, action=Action.stay, transitions=transitions) self.bulb.start_flow(flow) self.assertEqual(self.socket.sent["method"], "start_cf") self.assertEqual(self.socket.sent["params"], [2, 1, "40000, 2, 1700, 100, 40000, 2, 6500, 100"]) def test_set_scene_color(self): self.bulb.set_scene(SceneClass.COLOR, 255, 255, 0, 10) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual(self.socket.sent["params"], ["color", 16776960, 10]) def test_set_scene_color_ambilight(self): self.bulb.set_scene(SceneClass.COLOR, 255, 255, 0, 10, light_type=LightType.Ambient) self.assertEqual(self.socket.sent["method"], "bg_set_scene") self.assertEqual(self.socket.sent["params"], ["color", 16776960, 10]) def test_set_scene_color_temperature(self): self.bulb.set_scene(SceneClass.CT, 2000, 15) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual(self.socket.sent["params"], ["ct", 2000, 15]) def test_set_scene_hsv(self): self.bulb.set_scene(SceneClass.HSV, 200, 100, 10) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual(self.socket.sent["params"], ["hsv", 200, 100, 10]) def test_set_scene_color_flow(self): transitions = [ TemperatureTransition(1700, duration=40000), TemperatureTransition(6500, duration=40000) ] flow = Flow(count=1, action=Action.stay, transitions=transitions) self.bulb.set_scene(SceneClass.CF, flow) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual( self.socket.sent["params"], ["cf", 2, 1, "40000, 2, 1700, 100, 40000, 2, 6500, 100"]) def test_set_scene_auto_delay_off(self): self.bulb.set_scene(SceneClass.AUTO_DELAY_OFF, 20, 1) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual(self.socket.sent["params"], ["auto_delay_off", 20, 1]) def test_sunrise(self): flow = flows.sunrise() self.bulb.set_scene(SceneClass.CF, flow) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual(self.socket.sent["params"], [ "cf", 3, 1, "50, 1, 16731392, 1, 360000, 2, 1700, 10, 540000, 2, 2700, 100" ]) def test_sunset(self): flow = flows.sunset() self.bulb.set_scene(SceneClass.CF, flow) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual(self.socket.sent["params"], [ "cf", 3, 2, "50, 2, 2700, 10, 180000, 2, 1700, 5, 420000, 1, 16731136, 1" ]) def test_romance(self): flow = flows.romance() self.bulb.set_scene(SceneClass.CF, flow) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual( self.socket.sent["params"], ["cf", 0, 1, "4000, 1, 5838189, 1, 4000, 1, 6689834, 1"]) def test_happy_birthday(self): flow = flows.happy_birthday() self.bulb.set_scene(SceneClass.CF, flow) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual( self.socket.sent["params"], [ "cf", 0, 1, "1996, 1, 14438425, 80, 1996, 1, 14448670, 80, 1996, 1, 11153940, 80" ], ) def test_candle_flicker(self): flow = flows.candle_flicker() self.bulb.set_scene(SceneClass.CF, flow) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual( self.socket.sent["params"], [ "cf", 0, 0, "800, 2, 2700, 50, 800, 2, 2700, 30, 1200, 2, 2700, 80, 800, 2, 2700, 60, 1200, 2, 2700, 90, 2400, 2, 2700, 50, 1200, 2, 2700, 80, 800, 2, 2700, 60, 400, 2, 2700, 70", ], ) def test_home(self): flow = flows.home() self.bulb.set_scene(SceneClass.CF, flow) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual(self.socket.sent["params"], ["cf", 0, 0, "500, 2, 3200, 80"]) def test_night_mode(self): flow = flows.night_mode() self.bulb.set_scene(SceneClass.CF, flow) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual(self.socket.sent["params"], ["cf", 0, 0, "500, 1, 16750848, 1"]) def test_date_night(self): flow = flows.date_night() self.bulb.set_scene(SceneClass.CF, flow) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual(self.socket.sent["params"], ["cf", 0, 0, "500, 1, 16737792, 50"]) def test_movie(self): flow = flows.movie() self.bulb.set_scene(SceneClass.CF, flow) self.assertEqual(self.socket.sent["method"], "set_scene") self.assertEqual(self.socket.sent["params"], ["cf", 0, 0, "500, 1, 1315890, 50"]) def test_notification(self): notification_event = threading.Event() listening_stopped_event = threading.Event() shutdown = False def _callback(new_properties): notification_event.set() def _listen(): self.bulb.listen(_callback) listening_stopped_event.set() def _blocking_recv(size): time.sleep(0.1) if shutdown: raise IOError return b'{"method": "props", "params": {"power": "on"}}' def _shutdown(type): shutdown = True # noqa: F841 socket = mock.MagicMock() type(socket).recv = mock.MagicMock(side_effect=_blocking_recv) type(socket).shutdown = mock.MagicMock(side_effect=_shutdown) with mock.patch("yeelight.main.socket.socket", return_value=socket): assert self.bulb.last_properties == {} thread = threading.Thread(target=_listen) thread.start() assert notification_event.wait(0.5) is True assert self.bulb.last_properties == {"power": "on"} self.bulb.stop_listening() assert listening_stopped_event.wait(0.5) is True
red = 1 green = 1 blue = 1 try: image = ImageGrab.grab() width = image.size[0] height = image.size[1] image = image.load() for y in range(0, height, DECIMATE): for x in range(0, width, DECIMATE): color = image[x, y] red = red + color[0] green = green + color[1] blue = blue + color[2] red = ((red / ((height / DECIMATE) * (width / DECIMATE)))) green = ((green / ((height / DECIMATE) * (width / DECIMATE)))) blue = ((blue / ((height / DECIMATE) * (width / DECIMATE)))) h = colorsys.rgb_to_hsv(red / 255, green / 255, blue / 255) goal = [int(h[0] * 360), int(h[1] * 100), int(h[2] * 100)] bulb.set_hsv(goal[0], goal[1], goal[2]) old_hsv = goal except Exception as e: pass import atexit atexit.register(lambda: bulb.stop_music())
class Tests(unittest.TestCase): def setUp(self): self.socket = SocketMock() self.bulb = Bulb(ip="", auto_on=True) self.bulb._Bulb__socket = self.socket def test_rgb1(self): self.bulb.set_rgb(255, 255, 0) self.assertEqual(self.socket.sent["method"], "set_rgb") self.assertEqual(self.socket.sent["params"], [16776960, "smooth", 300]) def test_rgb2(self): self.bulb.effect = "sudden" self.bulb.set_rgb(255, 255, 0) self.assertEqual(self.socket.sent["method"], "set_rgb") self.assertEqual(self.socket.sent["params"], [16776960, "sudden", 300]) def test_rgb3(self): self.bulb.set_rgb(255, 255, 0, effect="sudden") self.assertEqual(self.socket.sent["method"], "set_rgb") self.assertEqual(self.socket.sent["params"], [16776960, "sudden", 300]) def test_hsv1(self): self.bulb.set_hsv(200, 100, effect="sudden") self.assertEqual(self.socket.sent["method"], "set_hsv") self.assertEqual(self.socket.sent["params"], [200, 100, "sudden", 300]) def test_hsv2(self): self.bulb.set_hsv(200, 100, 10, effect="sudden", duration=500) self.assertEqual(self.socket.sent["method"], "start_cf") self.assertEqual(self.socket.sent["params"], [1, 1, "50, 1, 43263, 10"]) def test_hsv3(self): self.bulb.set_hsv(200, 100, 10, effect="smooth", duration=1000) self.assertEqual(self.socket.sent["method"], "start_cf") self.assertEqual(self.socket.sent["params"], [1, 1, "1000, 1, 43263, 10"]) def test_hsv4(self): self.bulb.effect = "sudden" self.bulb.set_hsv(200, 100, 10, effect="smooth", duration=1000) self.assertEqual(self.socket.sent["method"], "start_cf") self.assertEqual(self.socket.sent["params"], [1, 1, "1000, 1, 43263, 10"]) def test_toggle1(self): self.bulb.toggle() self.assertEqual(self.socket.sent["method"], "toggle") self.assertEqual(self.socket.sent["params"], ["smooth", 300]) self.bulb.toggle(duration=3000) self.assertEqual(self.socket.sent["params"], ["smooth", 3000]) def test_turn_off1(self): self.bulb.turn_off() self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["off", "smooth", 300]) self.bulb.turn_off(duration=3000) self.assertEqual(self.socket.sent["params"], ["off", "smooth", 3000]) def test_turn_on1(self): self.bulb.turn_on() self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "smooth", 300, enums.PowerMode.LAST.value]) self.bulb.turn_on(duration=3000) self.assertEqual(self.socket.sent["params"], ["on", "smooth", 3000, enums.PowerMode.LAST.value]) def test_turn_on2(self): self.bulb.effect = "sudden" self.bulb.turn_on() self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "sudden", 300, enums.PowerMode.LAST.value]) def test_turn_on3(self): self.bulb.turn_on(effect="sudden", duration=50) self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "sudden", 50, enums.PowerMode.LAST.value]) def test_turn_on4(self): self.bulb.power_mode = enums.PowerMode.MOONLIGHT self.bulb.turn_on() self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual( self.socket.sent["params"], ["on", "smooth", 300, enums.PowerMode.MOONLIGHT.value]) def test_turn_on5(self): self.bulb.turn_on(power_mode=enums.PowerMode.MOONLIGHT) self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual( self.socket.sent["params"], ["on", "smooth", 300, enums.PowerMode.MOONLIGHT.value]) def test_set_power_mode1(self): self.bulb.set_power_mode(enums.PowerMode.MOONLIGHT) self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual( self.socket.sent["params"], ["on", "smooth", 300, enums.PowerMode.MOONLIGHT.value]) def test_set_power_mode2(self): self.bulb.set_power_mode(enums.PowerMode.NORMAL) self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "smooth", 300, enums.PowerMode.NORMAL.value]) def test_set_power_mode3(self): self.bulb.set_power_mode(enums.PowerMode.LAST) self.assertEqual(self.socket.sent["method"], "set_power") self.assertEqual(self.socket.sent["params"], ["on", "smooth", 300, enums.PowerMode.LAST.value]) def test_color_temp1(self): self.bulb.set_color_temp(1400) self.assertEqual(self.socket.sent["method"], "set_ct_abx") self.assertEqual(self.socket.sent["params"], [1700, "smooth", 300]) self.bulb.set_color_temp(1400, duration=3000) self.assertEqual(self.socket.sent["params"], [1700, "smooth", 3000]) def test_color_temp2(self): self.bulb.set_color_temp(8400, effect="sudden") self.assertEqual(self.socket.sent["method"], "set_ct_abx") self.assertEqual(self.socket.sent["params"], [6500, "sudden", 300])
class Device(BaseDevice): typesDevice = ["light"] name = DEVICE_NAME addConfig = AddDevice( fields=False, description= "1. Through the original application, you must enable device management over the local network.\n2. Enter the ip-address of the device (it can be viewed in the same application)." ) editConfig = EditDevice(address=True, fields=EditField(icon=True)) def __init__(self, *args, **kwargs): super().__init__(**kwargs) self.device = Bulb(self.coreAddress) try: values = self.device.get_properties() self.minmaxValue = self.device.get_model_specs() if (not look_for_param(self.values, "state") and "power" in values): val = "0" if (values["power"] == "on"): val = "1" self.values.append( DeviceElement(name="state", systemName=self.systemName, control=True, high=1, low=0, type="binary", icon="fas fa-power-off", value=val)) if (not look_for_param(self.values, "brightness") and "current_brightness" in values): self.values.append( DeviceElement(name="brightness", systemName=self.systemName, control=True, high=100, low=0, type="number", icon="far fa-sun", value=values["current_brightness"])) if (not look_for_param(self.values, "night_light") and self.minmaxValue["night_light"] != False): self.values.append( DeviceElement(name="night_light", systemName=self.systemName, control=True, high="1", low=0, type="binary", icon="fab fa-moon", value=values["active_mode"])) if (not look_for_param(self.values, "color") and values["hue"] != None): self.values.append( DeviceElement(name="color", systemName=self.systemName, control=True, high=360, low=0, type="number", icon="fab fa-medium-m", value=values["hue"])) if (not look_for_param(self.values, "saturation") and values["sat"] != None): self.values.append( DeviceElement(name="saturation", systemName=self.systemName, control=True, high=100, low=0, type="number", icon="fab fa-medium-m", value=values["sat"])) if (not look_for_param(self.values, "temp") and "ct" in values): self.values.append( DeviceElement(name="temp", systemName=self.systemName, control=True, high=self.minmaxValue["color_temp"]["max"], low=self.minmaxValue["color_temp"]["min"], type="number", icon="fas fa-adjust", value=values["ct"])) super().save() except Exception as e: logger.warning(f"yeelight initialize error. {e}") self.device = None def update_value(self, *args, **kwargs): values = self.device.get_properties() state = look_for_param(self.values, "state") if (state and "power" in values): val = "0" if (values["power"] == "on"): val = "1" saveNewDate(state, val) brightness = look_for_param(self.values, "brightness") if (brightness and "current_brightness" in values): saveNewDate(brightness, values["current_brightness"]) mode = look_for_param(self.values, "night_light") if (mode and "active_mode" in values): saveNewDate(mode, values["active_mode"]) temp = look_for_param(self.values, "temp") if (temp and "ct" in values): saveNewDate(temp, values["ct"]) color = look_for_param(self.values, "color") if (color and "hue" in values): saveNewDate(color, values["hue"]) saturation = look_for_param(self.values, "saturation") if (saturation and "sat" in values): saveNewDate(saturation, values["sat"]) def get_value(self, name): self.update_value() return super().get_value(name) def get_values(self): self.update_value() return super().get_values() def set_value(self, name, status): status = super().set_value(name, status) if (name == "state"): if (int(status) == 1): self.device.turn_on() else: self.device.turn_off() if (name == "brightness"): self.device.set_brightness(int(status)) if (name == "temp"): self.device.set_power_mode(PowerMode.NORMAL) self.device.set_color_temp(int(status)) if (name == "night_light"): if (int(status) == 1): self.device.set_power_mode(PowerMode.MOONLIGHT) if (int(status) == 0): self.device.set_power_mode(PowerMode.NORMAL) if (name == "color"): self.device.set_power_mode(PowerMode.HSV) saturation = look_for_param(self.values, "saturation") self.device.set_hsv(int(status), int(saturation.get())) if (name == "saturation"): self.device.set_power_mode(PowerMode.HSV) color = look_for_param(self.values, "color") self.device.set_hsv(int(color.get()), int(status)) def get_All_Info(self): self.update_value() return super().get_All_Info()
#!/usr/bin/python3 from subprocess import Popen, PIPE from yeelight import Bulb import time from globals import * response = Popen(['owread', '28.2E40B4010000/temperature'], stdout=PIPE).communicate()[0].decode('utf-8').strip() bulb1 = Bulb(BULB_DICT["DIN03"]) print(response) if (float(response) > 50.0): bulb1.set_hsv(1, 100, 100) else: bulb1.set_color_temp(4700) response = Popen(['owread', '28.FF6AB4010000/temperature'], stdout=PIPE).communicate()[0].decode('utf-8').strip() bulb2 = Bulb(BULB_DICT["DIN01"]) print(response) if (float(response) > 50.0): bulb2.set_hsv(1, 100, 100) else: bulb2.set_color_temp(4700) response = Popen(['owread', '28.D94FB4010000/temperature'], stdout=PIPE).communicate()[0].decode('utf-8').strip() bulb3 = Bulb(BULB_DICT["DIN02"]) print(response) if (float(response) > 50.0): bulb3.set_hsv(1, 100, 100) else:
bulb.start_music() # Sample audio rate fs = 44100 # Duration of recording seconds = 0.01 try: while True: #record audio for x seconds myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=1, blocking=True) #get the maximum value of the audio input using numpy max() function b = int((myrecording[:, 0].max()) * 100) #set the max brightness value to 100 in the event we get a maxval that's greater than 100 if b >= 100: b = 100 #scale hue value between 0-359 by using the brightness value h = int((b) / (100) * (359)) #set hue,saturation,brighness value. Below saturation is set to 100 i.e. max saturation bulb.set_hsv(h, 100, b) #print(h,maxval) except KeyboardInterrupt: print('Interrupted')
class LightController: MIN_BRIGHTNESS = 1 MAX_BRIGHTNESS = 100 MIN_TEMPERATURE = 1700 MAX_TEMPERATURE = 6500 MIN_HUE = 0 MAX_HUE = 359 brightness = MIN_BRIGHTNESS temperature = MIN_TEMPERATURE hue = MIN_HUE mode = 0 # TEMP def __init__(self): self.ip = CONFIG["bulbIP"] self.brightnessFactor = CONFIG["brightnessFactor"] self.temperatureFactor = CONFIG["temperatureFactor"] self.hueFactor = CONFIG["hueFactor"] self.reconnectInterval = CONFIG["reconnectInterval"] def initializeBulb(self): try: self.bulb = Bulb(self.ip, effect="smooth", duration=1000) print("Initialized bulb successfully") except: print("Bulb initializatio failed") try: self.bulb.start_music() print("Started music mode successfully") except: print("Music mode failed") threading.Timer(self.reconnectInterval, self.initializeBulb).start() def changeBrightness(self, value): new_value = round(clamp(self.brightness + value * self.brightnessFactor, self.MIN_BRIGHTNESS, self.MAX_BRIGHTNESS)) if not new_value == self.brightness: self.brightness = new_value self.setBrightness() def setBrightness(self): try: self.bulb.set_brightness(self.brightness) except: print("Setting brightness failed") def changeColor(self, value): if self.mode == 0: value = clamp(value, -50, 50) new_value = round(clamp(self.temperature + value * self.temperatureFactor, self.MIN_TEMPERATURE, self.MAX_TEMPERATURE)) if not new_value == self.temperature: self.temperature = new_value self.setTemperature() if self.mode == 1: new_value = round((self.hue + value * self.hueFactor) % self.MAX_HUE) if not new_value == self.hue: self.hue = new_value self.setHue() def setTemperature(self): try: self.bulb.set_color_temp(self.temperature) except: print("Setting temperature failed") def setHue(self): try: self.bulb.set_hsv(self.hue, 100) except: print("Setting hue failed") def toggleLight(self, value): if value: try: self.bulb.toggle() print("Toggle!") except: print("Toggling failed") def toggleMode(self, value): if value: self.mode = (self.mode + 1) % len(MODE) if self.mode == 0: self.setTemperature() if (self.mode == 1): self.setHue()