예제 #1
0
 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"])
예제 #2
0
 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"])
예제 #3
0
파일: test_light.py 프로젝트: 1e1/core-1
async def test_effects(hass: HomeAssistant):
    """Test effects."""
    yaml_configuration = {
        DOMAIN: {
            CONF_DEVICES:
            YAML_CONFIGURATION[DOMAIN][CONF_DEVICES],
            CONF_CUSTOM_EFFECTS: [
                {
                    CONF_NAME: "mock_effect",
                    CONF_FLOW_PARAMS: {
                        ATTR_COUNT:
                        3,
                        ATTR_TRANSITIONS: [
                            {
                                YEELIGHT_HSV_TRANSACTION: [300, 50, 500, 50]
                            },
                            {
                                YEELIGHT_RGB_TRANSITION:
                                [100, 100, 100, 300, 30]
                            },
                            {
                                YEELIGHT_TEMPERATURE_TRANSACTION:
                                [3000, 200, 20]
                            },
                            {
                                YEELIGHT_SLEEP_TRANSACTION: [800]
                            },
                        ],
                    },
                },
            ],
        }
    }

    mocked_bulb = _mocked_bulb()
    with patch(f"{MODULE}.Bulb", return_value=mocked_bulb):
        assert await async_setup_component(hass, DOMAIN, yaml_configuration)
        await hass.async_block_till_done()

    assert hass.states.get(ENTITY_LIGHT).attributes.get(
        "effect_list") == YEELIGHT_COLOR_EFFECT_LIST + ["mock_effect"]

    async def _async_test_effect(name, target=None, called=True):
        mocked_start_flow = MagicMock()
        type(mocked_bulb).start_flow = mocked_start_flow
        await hass.services.async_call(
            "light",
            SERVICE_TURN_ON,
            {
                ATTR_ENTITY_ID: ENTITY_LIGHT,
                ATTR_EFFECT: name
            },
            blocking=True,
        )
        if not called:
            return
        mocked_start_flow.assert_called_once()
        if target is None:
            return
        args, _ = mocked_start_flow.call_args
        flow = args[0]
        assert flow.count == target.count
        assert flow.action == target.action
        assert str(flow.transitions) == str(target.transitions)

    effects = {
        "mock_effect":
        Flow(
            count=3,
            transitions=[
                HSVTransition(300, 50, 500, 50),
                RGBTransition(100, 100, 100, 300, 30),
                TemperatureTransition(3000, 200, 20),
                SleepTransition(800),
            ],
        ),
        EFFECT_DISCO:
        Flow(transitions=transitions.disco()),
        EFFECT_FAST_RANDOM_LOOP:
        None,
        EFFECT_WHATSAPP:
        Flow(count=2, transitions=transitions.pulse(37, 211, 102)),
        EFFECT_FACEBOOK:
        Flow(count=2, transitions=transitions.pulse(59, 89, 152)),
        EFFECT_TWITTER:
        Flow(count=2, transitions=transitions.pulse(0, 172, 237)),
    }

    for name, target in effects.items():
        await _async_test_effect(name, target)
    await _async_test_effect("not_existed", called=False)
예제 #4
0
async def test_effects(hass: HomeAssistant):
    """Test effects."""
    assert await async_setup_component(
        hass,
        DOMAIN,
        {
            DOMAIN: {
                CONF_CUSTOM_EFFECTS: [
                    {
                        CONF_NAME: "mock_effect",
                        CONF_FLOW_PARAMS: {
                            ATTR_COUNT: 3,
                            ATTR_TRANSITIONS: [
                                {YEELIGHT_HSV_TRANSACTION: [300, 50, 500, 50]},
                                {YEELIGHT_RGB_TRANSITION: [100, 100, 100, 300, 30]},
                                {YEELIGHT_TEMPERATURE_TRANSACTION: [3000, 200, 20]},
                                {YEELIGHT_SLEEP_TRANSACTION: [800]},
                            ],
                        },
                    }
                ]
            }
        },
    )

    config_entry = MockConfigEntry(domain=DOMAIN, data=CONFIG_ENTRY_DATA)
    config_entry.add_to_hass(hass)

    mocked_bulb = _mocked_bulb()
    with _patch_discovery(), _patch_discovery_interval(), patch(
        f"{MODULE}.AsyncBulb", return_value=mocked_bulb
    ):
        assert await hass.config_entries.async_setup(config_entry.entry_id)
        await hass.async_block_till_done()

    assert hass.states.get(ENTITY_LIGHT).attributes.get(
        "effect_list"
    ) == YEELIGHT_COLOR_EFFECT_LIST + ["mock_effect"]

    async def _async_test_effect(name, target=None, called=True):
        async_mocked_start_flow = AsyncMock()
        mocked_bulb.async_start_flow = async_mocked_start_flow
        await hass.services.async_call(
            "light",
            SERVICE_TURN_ON,
            {ATTR_ENTITY_ID: ENTITY_LIGHT, ATTR_EFFECT: name},
            blocking=True,
        )
        if not called:
            return
        async_mocked_start_flow.assert_called_once()
        if target is None:
            return
        args, _ = async_mocked_start_flow.call_args
        flow = args[0]
        assert flow.count == target.count
        assert flow.action == target.action
        assert str(flow.transitions) == str(target.transitions)

    effects = {
        "mock_effect": Flow(
            count=3,
            transitions=[
                HSVTransition(300, 50, 500, 50),
                RGBTransition(100, 100, 100, 300, 30),
                TemperatureTransition(3000, 200, 20),
                SleepTransition(800),
            ],
        ),
        EFFECT_DISCO: Flow(transitions=transitions.disco()),
        EFFECT_FAST_RANDOM_LOOP: None,
        EFFECT_WHATSAPP: Flow(count=2, transitions=transitions.pulse(37, 211, 102)),
        EFFECT_FACEBOOK: Flow(count=2, transitions=transitions.pulse(59, 89, 152)),
        EFFECT_TWITTER: Flow(count=2, transitions=transitions.pulse(0, 172, 237)),
        EFFECT_HOME: Flow(
            count=0,
            action=Action.recover,
            transitions=[
                TemperatureTransition(degrees=3200, duration=500, brightness=80)
            ],
        ),
        EFFECT_NIGHT_MODE: Flow(
            count=0,
            action=Action.recover,
            transitions=[RGBTransition(0xFF, 0x99, 0x00, duration=500, brightness=1)],
        ),
        EFFECT_DATE_NIGHT: Flow(
            count=0,
            action=Action.recover,
            transitions=[RGBTransition(0xFF, 0x66, 0x00, duration=500, brightness=50)],
        ),
        EFFECT_MOVIE: Flow(
            count=0,
            action=Action.recover,
            transitions=[
                RGBTransition(
                    red=0x14, green=0x14, blue=0x32, duration=500, brightness=50
                )
            ],
        ),
        EFFECT_SUNRISE: Flow(
            count=1,
            action=Action.stay,
            transitions=[
                RGBTransition(
                    red=0xFF, green=0x4D, blue=0x00, duration=50, brightness=1
                ),
                TemperatureTransition(degrees=1700, duration=360000, brightness=10),
                TemperatureTransition(degrees=2700, duration=540000, brightness=100),
            ],
        ),
        EFFECT_SUNSET: Flow(
            count=1,
            action=Action.off,
            transitions=[
                TemperatureTransition(degrees=2700, duration=50, brightness=10),
                TemperatureTransition(degrees=1700, duration=180000, brightness=5),
                RGBTransition(
                    red=0xFF, green=0x4C, blue=0x00, duration=420000, brightness=1
                ),
            ],
        ),
        EFFECT_ROMANCE: Flow(
            count=0,
            action=Action.stay,
            transitions=[
                RGBTransition(
                    red=0x59, green=0x15, blue=0x6D, duration=4000, brightness=1
                ),
                RGBTransition(
                    red=0x66, green=0x14, blue=0x2A, duration=4000, brightness=1
                ),
            ],
        ),
        EFFECT_HAPPY_BIRTHDAY: Flow(
            count=0,
            action=Action.stay,
            transitions=[
                RGBTransition(
                    red=0xDC, green=0x50, blue=0x19, duration=1996, brightness=80
                ),
                RGBTransition(
                    red=0xDC, green=0x78, blue=0x1E, duration=1996, brightness=80
                ),
                RGBTransition(
                    red=0xAA, green=0x32, blue=0x14, duration=1996, brightness=80
                ),
            ],
        ),
        EFFECT_CANDLE_FLICKER: Flow(
            count=0,
            action=Action.recover,
            transitions=[
                TemperatureTransition(degrees=2700, duration=800, brightness=50),
                TemperatureTransition(degrees=2700, duration=800, brightness=30),
                TemperatureTransition(degrees=2700, duration=1200, brightness=80),
                TemperatureTransition(degrees=2700, duration=800, brightness=60),
                TemperatureTransition(degrees=2700, duration=1200, brightness=90),
                TemperatureTransition(degrees=2700, duration=2400, brightness=50),
                TemperatureTransition(degrees=2700, duration=1200, brightness=80),
                TemperatureTransition(degrees=2700, duration=800, brightness=60),
                TemperatureTransition(degrees=2700, duration=400, brightness=70),
            ],
        ),
    }

    for name, target in effects.items():
        await _async_test_effect(name, target)
    await _async_test_effect("not_existed", called=False)
    def showPanel(self):
        self.bulb = Bulb(self.ipEntry.get(), effect="smooth", duration=1000)
        self.confirmIp.destroy()
        self.ipEntry.destroy()
        self.ipText.destroy()

        # -------------------------------------| RGB FRAME |---------------------------------------------
        self.rgb_handler = tk.Frame(master=self.window, bg='black')
        self.rgb_handler.grid(row=0,
                              column=0,
                              sticky=tk.N,
                              pady=(15, 0),
                              padx=(50, 0))

        self.redLabel = tk.Label(master=self.rgb_handler,
                                 text="Red",
                                 bg="red",
                                 fg='white')
        self.greenLabel = tk.Label(master=self.rgb_handler,
                                   text="Green",
                                   bg="green",
                                   fg='white')
        self.blueLabel = tk.Label(master=self.rgb_handler,
                                  text="Blue",
                                  bg="blue",
                                  fg='white')
        self.redLabel.grid(row=0, column=0, sticky=tk.S, pady=(12, 2))
        self.greenLabel.grid(row=0, column=1, sticky=tk.S, pady=(12, 2))
        self.blueLabel.grid(row=0, column=2, sticky=tk.S, pady=(12, 2))

        self.redSlider = tk.Scale(master=self.rgb_handler,
                                  from_=1,
                                  to=255,
                                  tickinterval=60,
                                  orient=tk.VERTICAL,
                                  bg="red",
                                  fg='white',
                                  command=self.setRedText)
        self.greenSlider = tk.Scale(master=self.rgb_handler,
                                    from_=1,
                                    to=255,
                                    tickinterval=60,
                                    orient=tk.VERTICAL,
                                    bg="green",
                                    fg='white',
                                    command=self.setGreenText)
        self.blueSlider = tk.Scale(master=self.rgb_handler,
                                   from_=1,
                                   to=255,
                                   tickinterval=60,
                                   orient=tk.VERTICAL,
                                   bg="blue",
                                   fg='white',
                                   command=self.setBlueText)

        self.redSlider.grid(row=1,
                            column=0,
                            sticky=tk.W,
                            padx=(10, 10),
                            pady=10)
        self.greenSlider.grid(row=1, column=1, sticky=tk.W, padx=(10, 10))
        self.blueSlider.grid(row=1, column=2, sticky=tk.W, padx=(10, 10))

        self.redInput = tk.Entry(master=self.rgb_handler,
                                 width=5,
                                 bg='red',
                                 fg='white')
        self.greenInput = tk.Entry(master=self.rgb_handler,
                                   width=5,
                                   bg='green',
                                   fg='white')
        self.blueInput = tk.Entry(master=self.rgb_handler,
                                  width=5,
                                  bg='blue',
                                  fg='white')
        self.redInput.insert(tk.END, 1)
        self.greenInput.insert(tk.END, 1)
        self.blueInput.insert(tk.END, 1)
        self.redInput.grid(row=2, column=0, sticky=tk.N, padx=(10, 10))
        self.greenInput.grid(row=2, column=1, sticky=tk.N, padx=(10, 10))
        self.blueInput.grid(row=2, column=2, sticky=tk.N, padx=(10, 10))

        self.colorFrame = tk.Frame(master=self.rgb_handler,
                                   bg="black",
                                   width=40,
                                   height=40,
                                   relief=tk.RIDGE,
                                   borderwidth=2)
        self.colorFrame.grid(row=3,
                             column=1,
                             sticky=tk.S,
                             padx=(10, 10),
                             pady=(10, 15))

        self.applyButton = tk.Button(master=self.rgb_handler,
                                     command=self.setColors,
                                     text="Apply Color")
        self.applyButton.grid(row=4, column=1, sticky=tk.S)

        # ---------------------------------| POWER FRAME |----------------------------------------

        self.power_handler = tk.Frame(master=self.window, bg='black')
        self.power_handler.grid(row=0,
                                column=1,
                                sticky=tk.N,
                                padx=(60, 60),
                                pady=20)

        self.title = tk.Label(master=self.power_handler,
                              text='Xiaomi led Bulb remote control',
                              fg='white',
                              bg='black',
                              font=('Helvetica', 14, 'bold'))
        self.title.grid(row=0, column=1, sticky=tk.N)

        self.buttonsHandler = tk.Frame(master=self.power_handler, bg='black')
        self.buttonsHandler.grid(row=1, column=1, sticky=tk.N, pady=10)

        self.turnOnButton = tk.Button(master=self.buttonsHandler,
                                      text="ON",
                                      width=15,
                                      command=self.turnOnBulb,
                                      padx=5)
        self.turnOnButton.grid(row=0, column=0, sticky=tk.N, padx=5)

        self.turnOffButton = tk.Button(master=self.buttonsHandler,
                                       text="OFF",
                                       width=15,
                                       command=self.turnOffBulb)
        self.turnOffButton.grid(row=0, column=1, sticky=tk.N, padx=5)

        self.title = tk.Label(master=self.power_handler,
                              text='Brightness',
                              fg='white',
                              bg='black',
                              font=('Helvetica', 12, 'bold'))
        self.title.grid(row=2, column=1, sticky=tk.N, pady=(20, 5))

        self.brightnessSlider = tk.Scale(master=self.power_handler,
                                         from_=0,
                                         to=100,
                                         tickinterval=20,
                                         orient=tk.HORIZONTAL,
                                         bg="#FFFFFF",
                                         fg='#000000',
                                         length=200)
        self.brightnessSlider.set(80)
        self.brightnessSlider.grid(row=3, column=1, sticky=tk.N, pady=10)

        self.applyBrightnessButton = tk.Button(master=self.power_handler,
                                               text="Apply Brightness",
                                               width=15,
                                               command=self.setBrightness)
        self.applyBrightnessButton.grid(row=4, column=1, sticky=tk.N, pady=10)

        # ---------------------------| MODES FRAME |----------------------------------

        self.modes_handler = tk.Frame(master=self.window, bg='black')
        self.modes_handler.grid(row=0, column=2, sticky=tk.N, padx=30, pady=50)

        self.mode_1 = tk.Button(master=self.modes_handler,
                                text="Flow 1 (smooth)",
                                width=15,
                                command=lambda: self.showFlow([
                                    RGBTransition(20, 255, 20, 2000),
                                    RGBTransition(255, 20, 20, 2000),
                                    RGBTransition(20, 20, 255, 2000),
                                    RGBTransition(255, 255, 20, 2000),
                                    RGBTransition(255, 20, 255, 2000),
                                    RGBTransition(20, 255, 255, 2000)
                                ], 10))
        self.mode_1.grid(column=0, row=0, pady=(0, 15))

        self.mode_2 = tk.Button(master=self.modes_handler,
                                text="Flow 2 (speed)",
                                width=15,
                                command=lambda: self.showFlow([
                                    RGBTransition(20, 255, 20, 100),
                                    RGBTransition(255, 20, 20, 100),
                                    RGBTransition(20, 20, 255, 100),
                                    RGBTransition(255, 255, 20, 100),
                                    RGBTransition(255, 20, 255, 100),
                                    RGBTransition(20, 255, 255, 100)
                                ], 30))
        self.mode_2.grid(column=0, row=1, pady=(0, 15))

        self.mode_3 = tk.Button(master=self.modes_handler,
                                text="Flow 3 (temp)",
                                width=15,
                                command=lambda: self.showFlow([
                                    TemperatureTransition(6500, 1000),
                                    TemperatureTransition(5200, 1000),
                                    TemperatureTransition(4000, 1000),
                                    TemperatureTransition(2800, 1000),
                                    TemperatureTransition(1700, 1000)
                                ], 5))
        self.mode_3.grid(column=0, row=2, pady=(0, 15))

        self.mode_4 = tk.Button(master=self.modes_handler,
                                text="Flow 4 (disco)",
                                width=15,
                                command=lambda: self.showFlow(disco(), 5))

        self.mode_4.grid(column=0, row=3, pady=(0, 15))

        self.mode_5 = tk.Button(master=self.modes_handler,
                                text="Flow 5 (strobe)",
                                width=15,
                                command=lambda: self.showFlow(strobe(), 20))

        self.mode_5.grid(column=0, row=4, pady=(0, 15))

        self.mode_6 = tk.Button(master=self.modes_handler,
                                text="Flow 6 (alarm)",
                                width=15,
                                command=lambda: self.showFlow(alarm(), 5))

        self.mode_6.grid(column=0, row=5, pady=(0, 15))

        self.mode_7 = tk.Button(master=self.modes_handler,
                                text="Flow 7 (police)",
                                width=15,
                                command=lambda: self.showFlow(police(), 10))

        self.mode_7.grid(column=0, row=6, pady=(0, 15))

        self.mode_8 = tk.Button(master=self.modes_handler,
                                text="Flow 8 (smooth)",
                                width=15,
                                command=lambda: self.showFlow(lsd(), 5))

        self.mode_8.grid(column=0, row=7, pady=(0, 15))

        self.mode_9 = tk.Button(master=self.modes_handler,
                                text="Flow 9 (christmas)",
                                width=15,
                                command=lambda: self.showFlow(christmas(), 5))

        self.mode_9.grid(column=0, row=8, pady=(0, 15))

        self.mode_10 = tk.Button(
            master=self.modes_handler,
            text="Flow 10 (random)",
            width=15,
            command=lambda: self.showFlow(random_loop(), 5))

        self.mode_10.grid(column=0, row=9, pady=(0, 15))

        self.quitBT = tk.Button(master=self.window,
                                text="Quit",
                                font=('Helvetica', 12, 'bold'),
                                command=self.quit)
        self.quitBT.grid(column=1, row=1, pady=(0, 15))
예제 #6
0
    def set_effect(self, effect) -> None:
        """Activate effect."""
        if effect:
            from yeelight import (RGBTransition, TemperatureTransition,
                                  SleepTransition, Flow, BulbException)
            transition = int(self.config[CONF_TRANSITION])
            if effect == EFFECT_ALARM:
                count = 0
                duration = 100

                transitions = list()
                transitions.append(
                    RGBTransition(255, 0, 0, brightness=100,
                                  duration=duration))
                transitions.append(
                    RGBTransition(255, 0, 0, brightness=60, duration=duration))
                flow = Flow(count=count, transitions=transitions)

            if effect == EFFECT_DISCO:
                count = 0
                duration = 300

                transitions = list()
                transitions.append(
                    RGBTransition(255,
                                  0,
                                  0,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(255, 0, 0, brightness=1, duration=duration))
                transitions.append(
                    RGBTransition(128,
                                  255,
                                  0,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(128, 255, 0, brightness=1,
                                  duration=duration))
                transitions.append(
                    RGBTransition(0,
                                  255,
                                  255,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(0, 255, 255, brightness=1,
                                  duration=duration))
                transitions.append(
                    RGBTransition(128,
                                  0,
                                  255,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(128, 0, 255, brightness=1,
                                  duration=duration))
                flow = Flow(count=count, transitions=transitions)

            if effect == EFFECT_STROBE:
                count = 0
                duration = 50

                transitions = list()
                transitions.append(
                    RGBTransition(255,
                                  255,
                                  255,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(255,
                                  255,
                                  255,
                                  brightness=1,
                                  duration=duration))
                flow = Flow(count=count, transitions=transitions)

            if effect == EFFECT_COLOR_STROBE:
                count = 0
                duration = 50

                transitions = list()
                transitions.append(
                    RGBTransition(0,
                                  0,
                                  255,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(255,
                                  255,
                                  0,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(255,
                                  0,
                                  127,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(255,
                                  0,
                                  0,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(0,
                                  255,
                                  255,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(255,
                                  128,
                                  0,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(0,
                                  255,
                                  0,
                                  brightness=self.brightness,
                                  duration=duration))
                flow = Flow(count=count, transitions=transitions)

            if effect == EFFECT_POLICE:
                count = 0
                duration = 300

                transitions = list()
                transitions.append(
                    RGBTransition(255,
                                  0,
                                  0,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(0,
                                  0,
                                  255,
                                  brightness=self.brightness,
                                  duration=duration))
                flow = Flow(count=count, transitions=transitions)

            if effect == EFFECT_POLICE2:
                count = 0
                duration = 200

                transitions = list()
                transitions.append(
                    RGBTransition(255,
                                  0,
                                  0,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(255, 0, 0, brightness=1, duration=duration))
                transitions.append(
                    RGBTransition(255,
                                  0,
                                  0,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(SleepTransition(duration=duration))
                transitions.append(
                    RGBTransition(0,
                                  0,
                                  255,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(0, 0, 255, brightness=1, duration=duration))
                transitions.append(
                    RGBTransition(0,
                                  0,
                                  255,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(SleepTransition(duration=duration))
                flow = Flow(count=count, transitions=transitions)

            if effect == EFFECT_CHRISTMAS:
                count = 0
                duration = 300

                transitions = list()
                transitions.append(
                    RGBTransition(255,
                                  0,
                                  0,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(SleepTransition(duration=3000))
                transitions.append(
                    RGBTransition(0,
                                  255,
                                  0,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(SleepTransition(duration=3000))
                flow = Flow(count=count, transitions=transitions)

            if effect == EFFECT_RGB:
                count = 0
                duration = 300

                transitions = list()
                transitions.append(
                    RGBTransition(255,
                                  0,
                                  0,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(SleepTransition(duration=3000))
                transitions.append(
                    RGBTransition(0,
                                  255,
                                  0,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(SleepTransition(duration=3000))
                transitions.append(
                    RGBTransition(0,
                                  0,
                                  255,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(SleepTransition(duration=3000))
                flow = Flow(count=count, transitions=transitions)

            if effect == EFFECT_RGB_TRANS:
                count = 0
                duration = 10000

                transitions = list()
                transitions.append(
                    RGBTransition(255,
                                  0,
                                  0,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(0,
                                  255,
                                  0,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(0,
                                  0,
                                  255,
                                  brightness=self.brightness,
                                  duration=duration))
                flow = Flow(count=count, transitions=transitions)

            if effect == EFFECT_ENJOY:
                count = 0
                duration = 40000

                transitions = list()
                transitions.append(
                    TemperatureTransition(1700, duration=duration))
                transitions.append(
                    TemperatureTransition(6500, duration=duration))
                flow = Flow(count=count, transitions=transitions)

            if effect == EFFECT_RANDOM_LOOP:
                count = 0
                duration = 3000

                transitions = list()
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=duration))
                flow = Flow(count=count, transitions=transitions)

            if effect == EFFECT_RANDOM_FASTLOOP:
                count = 0
                duration = 800

                transitions = list()
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=duration))
                flow = Flow(count=count, transitions=transitions)

            if effect == EFFECT_SLOWDOWN:
                count = 0

                transitions = list()
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=250))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=500))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=1000))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=200))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=3000))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=4000))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=5000))
                transitions.append(
                    RGBTransition(random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  random.randrange(0, 255),
                                  brightness=self.brightness,
                                  duration=6000))
                flow = Flow(count=count, transitions=transitions)

            if effect == EFFECT_WHATSAPP_NOTIFY:
                count = 1
                duration = 250

                transitions = list()
                transitions.append(
                    RGBTransition(37,
                                  211,
                                  102,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(SleepTransition(duration=transition))
                transitions.append(
                    RGBTransition(37,
                                  211,
                                  102,
                                  brightness=10,
                                  duration=duration))
                transitions.append(
                    RGBTransition(37,
                                  211,
                                  102,
                                  brightness=self.brightness,
                                  duration=duration))
                flow = Flow(count=count, transitions=transitions)

            if effect == EFFECT_FACEBOOK_NOTIFY:
                count = 1
                duration = 250

                transitions = list()
                transitions.append(
                    RGBTransition(59,
                                  89,
                                  152,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(SleepTransition(duration=transition))
                transitions.append(
                    RGBTransition(59,
                                  89,
                                  152,
                                  brightness=10,
                                  duration=duration))
                transitions.append(
                    RGBTransition(59,
                                  89,
                                  152,
                                  brightness=self.brightness,
                                  duration=duration))
                flow = Flow(count=count, transitions=transitions)

            if effect == EFFECT_TWITTER_NOTIFY:
                count = 1
                duration = 250

                transitions = list()
                transitions.append(
                    RGBTransition(0,
                                  172,
                                  237,
                                  brightness=self.brightness,
                                  duration=duration))
                transitions.append(SleepTransition(duration=transition))
                transitions.append(
                    RGBTransition(0,
                                  172,
                                  237,
                                  brightness=10,
                                  duration=duration))
                transitions.append(
                    RGBTransition(0,
                                  172,
                                  237,
                                  brightness=self.brightness,
                                  duration=duration))
                flow = Flow(count=count, transitions=transitions)

            if effect == EFFECT_STOP:
                self._bulb.stop_flow()
                return
            try:
                self._bulb.start_flow(flow)
            except BulbException as ex:
                _LOGGER.error("Unable to set effect: %s", ex)
예제 #7
0
async def test_effects(hass: HomeAssistant):
    """Test effects."""
    assert await async_setup_component(
        hass,
        DOMAIN,
        {
            DOMAIN: {
                CONF_CUSTOM_EFFECTS: [
                    {
                        CONF_NAME: "mock_effect",
                        CONF_FLOW_PARAMS: {
                            ATTR_COUNT:
                            3,
                            ATTR_TRANSITIONS: [
                                {
                                    YEELIGHT_HSV_TRANSACTION:
                                    [300, 50, 500, 50]
                                },
                                {
                                    YEELIGHT_RGB_TRANSITION:
                                    [100, 100, 100, 300, 30]
                                },
                                {
                                    YEELIGHT_TEMPERATURE_TRANSACTION:
                                    [3000, 200, 20]
                                },
                                {
                                    YEELIGHT_SLEEP_TRANSACTION: [800]
                                },
                            ],
                        },
                    },
                ],
            },
        },
    )

    config_entry = MockConfigEntry(
        domain=DOMAIN,
        data={
            CONF_ID: "",
            CONF_HOST: IP_ADDRESS,
            CONF_TRANSITION: DEFAULT_TRANSITION,
            CONF_MODE_MUSIC: DEFAULT_MODE_MUSIC,
            CONF_SAVE_ON_CHANGE: DEFAULT_SAVE_ON_CHANGE,
            CONF_NIGHTLIGHT_SWITCH: DEFAULT_NIGHTLIGHT_SWITCH,
        },
    )
    config_entry.add_to_hass(hass)

    mocked_bulb = _mocked_bulb()
    with _patch_discovery(MODULE), patch(f"{MODULE}.Bulb",
                                         return_value=mocked_bulb):
        assert await hass.config_entries.async_setup(config_entry.entry_id)
        await hass.async_block_till_done()

    assert hass.states.get(ENTITY_LIGHT).attributes.get(
        "effect_list") == YEELIGHT_COLOR_EFFECT_LIST + ["mock_effect"]

    async def _async_test_effect(name, target=None, called=True):
        mocked_start_flow = MagicMock()
        type(mocked_bulb).start_flow = mocked_start_flow
        await hass.services.async_call(
            "light",
            SERVICE_TURN_ON,
            {
                ATTR_ENTITY_ID: ENTITY_LIGHT,
                ATTR_EFFECT: name
            },
            blocking=True,
        )
        if not called:
            return
        mocked_start_flow.assert_called_once()
        if target is None:
            return
        args, _ = mocked_start_flow.call_args
        flow = args[0]
        assert flow.count == target.count
        assert flow.action == target.action
        assert str(flow.transitions) == str(target.transitions)

    effects = {
        "mock_effect":
        Flow(
            count=3,
            transitions=[
                HSVTransition(300, 50, 500, 50),
                RGBTransition(100, 100, 100, 300, 30),
                TemperatureTransition(3000, 200, 20),
                SleepTransition(800),
            ],
        ),
        EFFECT_DISCO:
        Flow(transitions=transitions.disco()),
        EFFECT_FAST_RANDOM_LOOP:
        None,
        EFFECT_WHATSAPP:
        Flow(count=2, transitions=transitions.pulse(37, 211, 102)),
        EFFECT_FACEBOOK:
        Flow(count=2, transitions=transitions.pulse(59, 89, 152)),
        EFFECT_TWITTER:
        Flow(count=2, transitions=transitions.pulse(0, 172, 237)),
    }

    for name, target in effects.items():
        await _async_test_effect(name, target)
    await _async_test_effect("not_existed", called=False)