Example #1
0
    def set_effect(self, effect) -> None:
        """Activate effect."""
        if not effect:
            return

        if effect == EFFECT_STOP:
            self._bulb.stop_flow(light_type=self.light_type)
            return

        if effect in self.custom_effects_names:
            flow = Flow(**self.custom_effects[effect])
        elif effect in EFFECTS_MAP:
            flow = Flow(count=0, transitions=EFFECTS_MAP[effect]())
        elif effect == EFFECT_FAST_RANDOM_LOOP:
            flow = Flow(count=0,
                        transitions=yee_transitions.randomloop(duration=250))
        elif effect == EFFECT_WHATSAPP:
            flow = Flow(count=2,
                        transitions=yee_transitions.pulse(37, 211, 102))
        elif effect == EFFECT_FACEBOOK:
            flow = Flow(count=2,
                        transitions=yee_transitions.pulse(59, 89, 152))
        elif effect == EFFECT_TWITTER:
            flow = Flow(count=2,
                        transitions=yee_transitions.pulse(0, 172, 237))
        else:
            return

        try:
            self._bulb.start_flow(flow, light_type=self.light_type)
            self._effect = effect
        except BulbException as ex:
            _LOGGER.error("Unable to set effect: %s", ex)
Example #2
0
    def set_effect(self, effect) -> None:
        """Activate effect."""
        if effect:
            from yeelight.transitions import (
                disco,
                temp,
                strobe,
                pulse,
                strobe_color,
                alarm,
                police,
                police2,
                christmas,
                rgb,
                randomloop,
                lsd,
                slowdown,
            )

            if effect == EFFECT_STOP:
                self._bulb.stop_flow(light_type=self.light_type)
                return

            effects_map = {
                EFFECT_DISCO: disco,
                EFFECT_TEMP: temp,
                EFFECT_STROBE: strobe,
                EFFECT_STROBE_COLOR: strobe_color,
                EFFECT_ALARM: alarm,
                EFFECT_POLICE: police,
                EFFECT_POLICE2: police2,
                EFFECT_CHRISTMAS: christmas,
                EFFECT_RGB: rgb,
                EFFECT_RANDOM_LOOP: randomloop,
                EFFECT_LSD: lsd,
                EFFECT_SLOWDOWN: slowdown,
            }

            if effect in self.custom_effects_names:
                flow = Flow(**self.custom_effects[effect])
            elif effect in effects_map:
                flow = Flow(count=0, transitions=effects_map[effect]())
            elif effect == EFFECT_FAST_RANDOM_LOOP:
                flow = Flow(count=0, transitions=randomloop(duration=250))
            elif effect == EFFECT_WHATSAPP:
                flow = Flow(count=2, transitions=pulse(37, 211, 102))
            elif effect == EFFECT_FACEBOOK:
                flow = Flow(count=2, transitions=pulse(59, 89, 152))
            elif effect == EFFECT_TWITTER:
                flow = Flow(count=2, transitions=pulse(0, 172, 237))

            try:
                self._bulb.start_flow(flow, light_type=self.light_type)
            except BulbException as ex:
                _LOGGER.error("Unable to set effect: %s", ex)
Example #3
0
    def set_effect(self, effect) -> None:
        """Activate effect."""
        if effect:
            from yeelight import (Flow, BulbException)
            from yeelight.transitions import (disco, temp, strobe, pulse,
                                              strobe_color, alarm, police,
                                              police2, christmas, rgb,
                                              randomloop, slowdown)
            if effect == EFFECT_STOP:
                self._bulb.stop_flow()
                return
            if effect == EFFECT_DISCO:
                flow = Flow(count=0, transitions=disco())
            if effect == EFFECT_TEMP:
                flow = Flow(count=0, transitions=temp())
            if effect == EFFECT_STROBE:
                flow = Flow(count=0, transitions=strobe())
            if effect == EFFECT_STROBE_COLOR:
                flow = Flow(count=0, transitions=strobe_color())
            if effect == EFFECT_ALARM:
                flow = Flow(count=0, transitions=alarm())
            if effect == EFFECT_POLICE:
                flow = Flow(count=0, transitions=police())
            if effect == EFFECT_POLICE2:
                flow = Flow(count=0, transitions=police2())
            if effect == EFFECT_CHRISTMAS:
                flow = Flow(count=0, transitions=christmas())
            if effect == EFFECT_RGB:
                flow = Flow(count=0, transitions=rgb())
            if effect == EFFECT_RANDOM_LOOP:
                flow = Flow(count=0, transitions=randomloop())
            if effect == EFFECT_FAST_RANDOM_LOOP:
                flow = Flow(count=0, transitions=randomloop(duration=250))
            if effect == EFFECT_SLOWDOWN:
                flow = Flow(count=0, transitions=slowdown())
            if effect == EFFECT_WHATSAPP:
                flow = Flow(count=2, transitions=pulse(37, 211, 102))
            if effect == EFFECT_FACEBOOK:
                flow = Flow(count=2, transitions=pulse(59, 89, 152))
            if effect == EFFECT_TWITTER:
                flow = Flow(count=2, transitions=pulse(0, 172, 237))

            try:
                self._bulb.start_flow(flow)
            except BulbException as ex:
                _LOGGER.error("Unable to set effect: %s", ex)
Example #4
0
    def set_effect(self, effect) -> None:
        """Activate effect."""
        if effect:
            from yeelight import (Flow, BulbException)
            from yeelight.transitions import (disco, temp, strobe, pulse,
                                              strobe_color, alarm, police,
                                              police2, christmas, rgb,
                                              randomloop, slowdown)
            if effect == EFFECT_STOP:
                self._bulb.stop_flow()
                return
            if effect == EFFECT_DISCO:
                flow = Flow(count=0, transitions=disco())
            if effect == EFFECT_TEMP:
                flow = Flow(count=0, transitions=temp())
            if effect == EFFECT_STROBE:
                flow = Flow(count=0, transitions=strobe())
            if effect == EFFECT_STROBE_COLOR:
                flow = Flow(count=0, transitions=strobe_color())
            if effect == EFFECT_ALARM:
                flow = Flow(count=0, transitions=alarm())
            if effect == EFFECT_POLICE:
                flow = Flow(count=0, transitions=police())
            if effect == EFFECT_POLICE2:
                flow = Flow(count=0, transitions=police2())
            if effect == EFFECT_CHRISTMAS:
                flow = Flow(count=0, transitions=christmas())
            if effect == EFFECT_RGB:
                flow = Flow(count=0, transitions=rgb())
            if effect == EFFECT_RANDOM_LOOP:
                flow = Flow(count=0, transitions=randomloop())
            if effect == EFFECT_FAST_RANDOM_LOOP:
                flow = Flow(count=0, transitions=randomloop(duration=250))
            if effect == EFFECT_SLOWDOWN:
                flow = Flow(count=0, transitions=slowdown())
            if effect == EFFECT_WHATSAPP:
                flow = Flow(count=2, transitions=pulse(37, 211, 102))
            if effect == EFFECT_FACEBOOK:
                flow = Flow(count=2, transitions=pulse(59, 89, 152))
            if effect == EFFECT_TWITTER:
                flow = Flow(count=2, transitions=pulse(0, 172, 237))

            try:
                self._bulb.start_flow(flow)
            except BulbException as ex:
                _LOGGER.error("Unable to set effect: %s", ex)
Example #5
0
    def set_effect(self, effect) -> None:
        """Activate effect."""
        if effect:
            from yeelight import (Flow, BulbException)
            from yeelight.transitions import (disco, temp, strobe, pulse,
                                              strobe_color, alarm, police,
                                              police2, christmas, rgb,
                                              randomloop, lsd, slowdown)
            if effect == EFFECT_STOP:
                self._bulb.stop_flow()
                return

            effects_map = {
                EFFECT_DISCO: disco,
                EFFECT_TEMP: temp,
                EFFECT_STROBE: strobe,
                EFFECT_STROBE_COLOR: strobe_color,
                EFFECT_ALARM: alarm,
                EFFECT_POLICE: police,
                EFFECT_POLICE2: police2,
                EFFECT_CHRISTMAS: christmas,
                EFFECT_RGB: rgb,
                EFFECT_RANDOM_LOOP: randomloop,
                EFFECT_LSD: lsd,
                EFFECT_SLOWDOWN: slowdown,
            }

            if effect in self.custom_effects_names:
                flow = Flow(**self.custom_effects[effect])
            elif effect in effects_map:
                flow = Flow(count=0, transitions=effects_map[effect]())
            elif effect == EFFECT_FAST_RANDOM_LOOP:
                flow = Flow(count=0, transitions=randomloop(duration=250))
            elif effect == EFFECT_WHATSAPP:
                flow = Flow(count=2, transitions=pulse(37, 211, 102))
            elif effect == EFFECT_FACEBOOK:
                flow = Flow(count=2, transitions=pulse(59, 89, 152))
            elif effect == EFFECT_TWITTER:
                flow = Flow(count=2, transitions=pulse(0, 172, 237))

            try:
                self._bulb.start_flow(flow)
            except BulbException as ex:
                _LOGGER.error("Unable to set effect: %s", ex)
Example #6
0
def pulse(hex_color, pulses):
    """Pulse the bulb in a specific color."""
    red, green, blue = hex_color_to_rgb(hex_color)
    transitions = tr.pulse(red, green, blue)

    for bulb in BULBS:
        # Get the initial bulb state.
        if bulb.get_properties().get("power", "off") == "off":
            action = yeelight.Flow.actions.off
        else:
            action = yeelight.Flow.actions.recover

        bulb.start_flow(yeelight.Flow(count=pulses, action=action, transitions=transitions))
Example #7
0
def pulse(red, green, blue, duration=250, brightness=100):
    """
    Pulse a single color once (mainly to be used for notifications).

    :param int red: The red color component to pulse (0-255).
    :param int green: The green color component to pulse (0-255).
    :param int blue: The blue color component to pulse (0-255).
    :param int duration: The duration to pulse for, in milliseconds.
    :param int brightness: The brightness to pulse at (1-100).

    :returns: A Flow consisting of 2 transitions, after which the bulb returns to its previous state.
    :rtype: Flow
    """
    return Flow(count=1, action=Action.recover, transitions=transitions.pulse(red, green, blue, duration, brightness))
Example #8
0
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)
Example #9
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)
Example #10
0
from yeelight import Bulb, Flow, transitions
from sys import argv

ip = argv[1]
red, green, blue = argv[2].split(',')
duration = int(argv[3])
if argv[4] == 'false':
    auto_on = False
else:
    auto_on = True
brightness = int(argv[5])

desk = Bulb(ip, effect='sudden', auto_on=auto_on)

flow = Flow(count=1,
            transitions=transitions.pulse(int(red),
                                          int(green),
                                          int(blue),
                                          duration=duration,
                                          brightness=brightness))

desk.start_flow(flow)
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)