Ejemplo n.º 1
0
    def test_color_hs_to_xy(self):
        """Test color_hs_to_xy."""
        assert (0.151, 0.343) == \
            color_util.color_hs_to_xy(180, 100)

        assert (0.356, 0.321) == \
            color_util.color_hs_to_xy(350, 12.5)

        assert (0.229, 0.474) == \
            color_util.color_hs_to_xy(140, 50)

        assert (0.474, 0.317) == \
            color_util.color_hs_to_xy(0, 40)

        assert (0.323, 0.329) == \
            color_util.color_hs_to_xy(360, 0)

        assert (0.7, 0.299) == \
            color_util.color_hs_to_xy(0, 100, GAMUT)

        assert (0.215, 0.711) == \
            color_util.color_hs_to_xy(120, 100, GAMUT)

        assert (0.17, 0.34) == \
            color_util.color_hs_to_xy(180, 100, GAMUT)

        assert (0.138, 0.08) == \
            color_util.color_hs_to_xy(240, 100, GAMUT)

        assert (0.7, 0.299) == \
            color_util.color_hs_to_xy(360, 100, GAMUT)
Ejemplo n.º 2
0
    def test_color_hs_to_xy(self):
        """Test color_hs_to_xy."""
        self.assertEqual((0.151, 0.343), color_util.color_hs_to_xy(180, 100))

        self.assertEqual((0.352, 0.329), color_util.color_hs_to_xy(350, 12.5))

        self.assertEqual((0.228, 0.476), color_util.color_hs_to_xy(140, 50))

        self.assertEqual((0.465, 0.33), color_util.color_hs_to_xy(0, 40))

        self.assertEqual((0.32, 0.336), color_util.color_hs_to_xy(360, 0))
Ejemplo n.º 3
0
    def state_attributes(self):
        """Return optional state attributes."""
        data = {}
        supported_features = self.supported_features

        if supported_features & SUPPORT_COLOR_TEMP:
            data[ATTR_MIN_MIREDS] = self.min_mireds
            data[ATTR_MAX_MIREDS] = self.max_mireds

        if self.is_on:
            if supported_features & SUPPORT_BRIGHTNESS:
                data[ATTR_BRIGHTNESS] = self.brightness

            if supported_features & SUPPORT_COLOR_TEMP:
                data[ATTR_COLOR_TEMP] = self.color_temp

            if self.supported_features & SUPPORT_COLOR and self.hs_color:
                # pylint: disable=unsubscriptable-object,not-an-iterable
                hs_color = self.hs_color
                data[ATTR_HS_COLOR] = (
                    round(hs_color[0], 3),
                    round(hs_color[1], 3),
                )
                data[ATTR_RGB_COLOR] = color_util.color_hs_to_RGB(*hs_color)
                data[ATTR_XY_COLOR] = color_util.color_hs_to_xy(*hs_color)

            if supported_features & SUPPORT_WHITE_VALUE:
                data[ATTR_WHITE_VALUE] = self.white_value

            if supported_features & SUPPORT_EFFECT:
                data[ATTR_EFFECT_LIST] = self.effect_list
                data[ATTR_EFFECT] = self.effect

        return {key: val for key, val in data.items() if val is not None}
Ejemplo n.º 4
0
    def state_attributes(self):
        """Return optional state attributes."""
        data = {}

        if self.supported_features & SUPPORT_COLOR_TEMP:
            data[ATTR_MIN_MIREDS] = self.min_mireds
            data[ATTR_MAX_MIREDS] = self.max_mireds

        if self.is_on:
            for prop, attr in PROP_TO_ATTR.items():
                value = getattr(self, prop)
                if value is not None:
                    data[attr] = value

            # Expose current color also as RGB and XY
            if ATTR_HS_COLOR in data:
                data[ATTR_RGB_COLOR] = color_util.color_hs_to_RGB(
                    *data[ATTR_HS_COLOR])
                data[ATTR_XY_COLOR] = color_util.color_hs_to_xy(
                    *data[ATTR_HS_COLOR])
                data[ATTR_HS_COLOR] = (
                    round(data[ATTR_HS_COLOR][0], 3),
                    round(data[ATTR_HS_COLOR][1], 3),
                )

        return data
Ejemplo n.º 5
0
    def state_attributes(self):
        """Return state attributes."""
        if not self.is_on:
            return None

        data = {}
        supported_features = self.supported_features

        if supported_features & SUPPORT_BRIGHTNESS:
            data[ATTR_BRIGHTNESS] = self.brightness

        if supported_features & SUPPORT_COLOR_TEMP:
            data[ATTR_COLOR_TEMP] = self.color_temp

        if supported_features & SUPPORT_COLOR and self.hs_color:
            hs_color = self.hs_color
            data[ATTR_HS_COLOR] = (round(hs_color[0], 3), round(hs_color[1], 3))
            data[ATTR_RGB_COLOR] = color_util.color_hs_to_RGB(*hs_color)
            data[ATTR_XY_COLOR] = color_util.color_hs_to_xy(*hs_color)

        if supported_features & SUPPORT_WHITE_VALUE:
            data[ATTR_WHITE_VALUE] = self.white_value

        if supported_features & SUPPORT_EFFECT:
            data[ATTR_EFFECT] = self.effect

        return {key: val for key, val in data.items() if val is not None}
Ejemplo n.º 6
0
    def state_attributes(self):
        """Return optional state attributes."""
        data = {}

        if self.supported_features & SUPPORT_COLOR_TEMP:
            data[ATTR_MIN_MIREDS] = self.min_mireds
            data[ATTR_MAX_MIREDS] = self.max_mireds

        if self.is_on:
            for prop, attr in PROP_TO_ATTR.items():
                value = getattr(self, prop)
                if value is not None:
                    data[attr] = value

            # Expose current color also as RGB and XY
            if ATTR_HS_COLOR in data:
                data[ATTR_RGB_COLOR] = color_util.color_hs_to_RGB(
                    *data[ATTR_HS_COLOR])
                data[ATTR_XY_COLOR] = color_util.color_hs_to_xy(
                    *data[ATTR_HS_COLOR])
                data[ATTR_HS_COLOR] = (
                    round(data[ATTR_HS_COLOR][0], 3),
                    round(data[ATTR_HS_COLOR][1], 3),
                )

        return data
Ejemplo n.º 7
0
    def test_color_hs_to_xy(self):
        """Test color_hs_to_xy."""
        self.assertEqual((0.151, 0.343),
                         color_util.color_hs_to_xy(180, 100))

        self.assertEqual((0.356, 0.321),
                         color_util.color_hs_to_xy(350, 12.5))

        self.assertEqual((0.229, 0.474),
                         color_util.color_hs_to_xy(140, 50))

        self.assertEqual((0.474, 0.317),
                         color_util.color_hs_to_xy(0, 40))

        self.assertEqual((0.323, 0.329),
                         color_util.color_hs_to_xy(360, 0))
Ejemplo n.º 8
0
    async def async_turn_on(self, **kwargs):
        """Turn on light."""
        data = {'on': True}

        if ATTR_COLOR_TEMP in kwargs:
            data['ct'] = kwargs[ATTR_COLOR_TEMP]

        if ATTR_HS_COLOR in kwargs:
            data['xy'] = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])

        if ATTR_BRIGHTNESS in kwargs:
            data['bri'] = kwargs[ATTR_BRIGHTNESS]

        if ATTR_TRANSITION in kwargs:
            data['transitiontime'] = int(kwargs[ATTR_TRANSITION]) * 10

        if ATTR_FLASH in kwargs:
            if kwargs[ATTR_FLASH] == FLASH_SHORT:
                data['alert'] = 'select'
                del data['on']
            elif kwargs[ATTR_FLASH] == FLASH_LONG:
                data['alert'] = 'lselect'
                del data['on']

        if ATTR_EFFECT in kwargs:
            if kwargs[ATTR_EFFECT] == EFFECT_COLORLOOP:
                data['effect'] = 'colorloop'
            else:
                data['effect'] = 'none'

        await self._light.async_set_state(data)
Ejemplo n.º 9
0
    def test_color_hs_to_xy(self):
        """Test color_hs_to_xy."""
        self.assertEqual((0.151, 0.343),
                         color_util.color_hs_to_xy(180, 100))

        self.assertEqual((0.352, 0.329),
                         color_util.color_hs_to_xy(350, 12.5))

        self.assertEqual((0.228, 0.476),
                         color_util.color_hs_to_xy(140, 50))

        self.assertEqual((0.465, 0.33),
                         color_util.color_hs_to_xy(0, 40))

        self.assertEqual((0.32, 0.336),
                         color_util.color_hs_to_xy(360, 0))
Ejemplo n.º 10
0
    def turn_on(self, **kwargs):
        """Turn the light on."""
        xy_color = None

        brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness or 255)
        color_temp = kwargs.get(ATTR_COLOR_TEMP)
        hs_color = kwargs.get(ATTR_HS_COLOR)
        transition_time = int(kwargs.get(ATTR_TRANSITION, 0))

        if hs_color is not None:
            xy_color = color_util.color_hs_to_xy(*hs_color)

        turn_on_kwargs = {
            "level": brightness,
            "transition": transition_time,
            "force_update": False,
        }

        try:
            if xy_color is not None:
                self.wemo.set_color(xy_color, transition=transition_time)

            if color_temp is not None:
                self.wemo.set_temperature(mireds=color_temp,
                                          transition=transition_time)

            if self.wemo.turn_on(**turn_on_kwargs):
                self._state["onoff"] = WEMO_ON
        except ActionException as err:
            _LOGGER.warning("Error while turning on device %s (%s)", self.name,
                            err)
            self._available = False

        self.schedule_update_ha_state()
Ejemplo n.º 11
0
 def _light_internal_convert_color(self, color_mode: str) -> dict:
     data: dict[str, tuple] = {}
     if color_mode == COLOR_MODE_HS and self.hs_color:
         hs_color = self.hs_color
         data[ATTR_HS_COLOR] = (round(hs_color[0], 3), round(hs_color[1], 3))
         data[ATTR_RGB_COLOR] = color_util.color_hs_to_RGB(*hs_color)
         data[ATTR_XY_COLOR] = color_util.color_hs_to_xy(*hs_color)
     elif color_mode == COLOR_MODE_XY and self.xy_color:
         xy_color = self.xy_color
         data[ATTR_HS_COLOR] = color_util.color_xy_to_hs(*xy_color)
         data[ATTR_RGB_COLOR] = color_util.color_xy_to_RGB(*xy_color)
         data[ATTR_XY_COLOR] = (round(xy_color[0], 6), round(xy_color[1], 6))
     elif color_mode == COLOR_MODE_RGB and self.rgb_color:
         rgb_color = self.rgb_color
         data[ATTR_HS_COLOR] = color_util.color_RGB_to_hs(*rgb_color)
         data[ATTR_RGB_COLOR] = tuple(int(x) for x in rgb_color[0:3])
         data[ATTR_XY_COLOR] = color_util.color_RGB_to_xy(*rgb_color)
     elif color_mode == COLOR_MODE_RGBW and self._light_internal_rgbw_color:
         rgbw_color = self._light_internal_rgbw_color
         rgb_color = color_util.color_rgbw_to_rgb(*rgbw_color)
         data[ATTR_HS_COLOR] = color_util.color_RGB_to_hs(*rgb_color)
         data[ATTR_RGB_COLOR] = tuple(int(x) for x in rgb_color[0:3])
         data[ATTR_RGBW_COLOR] = tuple(int(x) for x in rgbw_color[0:4])
         data[ATTR_XY_COLOR] = color_util.color_RGB_to_xy(*rgb_color)
     elif color_mode == COLOR_MODE_RGBWW and self.rgbww_color:
         rgbww_color = self.rgbww_color
         rgb_color = color_util.color_rgbww_to_rgb(
             *rgbww_color, self.min_mireds, self.max_mireds
         )
         data[ATTR_HS_COLOR] = color_util.color_RGB_to_hs(*rgb_color)
         data[ATTR_RGB_COLOR] = tuple(int(x) for x in rgb_color[0:3])
         data[ATTR_RGBWW_COLOR] = tuple(int(x) for x in rgbww_color[0:5])
         data[ATTR_XY_COLOR] = color_util.color_RGB_to_xy(*rgb_color)
     return data
Ejemplo n.º 12
0
    async def async_turn_on(self, **kwargs):
        """Turn the entity on."""
        duration = kwargs.get(light.ATTR_TRANSITION, DEFAULT_DURATION)
        duration = duration * 10  # tenths of s
        if light.ATTR_COLOR_TEMP in kwargs:
            temperature = kwargs[light.ATTR_COLOR_TEMP]
            await self._endpoint.light_color.move_to_color_temp(
                temperature, duration)
            self._color_temp = temperature

        if light.ATTR_HS_COLOR in kwargs:
            self._hs_color = kwargs[light.ATTR_HS_COLOR]
            xy_color = color_util.color_hs_to_xy(*self._hs_color)
            await self._endpoint.light_color.move_to_color(
                int(xy_color[0] * 65535),
                int(xy_color[1] * 65535),
                duration,
            )

        if self._brightness is not None:
            brightness = kwargs.get(light.ATTR_BRIGHTNESS, self._brightness
                                    or 255)
            self._brightness = brightness
            # Move to level with on/off:

            await self._endpoint.level.move_to_level_with_on_off(
                brightness, duration)
            self._state = 1
            self.async_schedule_update_ha_state()
            return

        await self._endpoint.on_off.on()
        self._state = 1
        self.async_update_ha_state(force_refresh=True)
Ejemplo n.º 13
0
    def test_color_hs_to_xy(self):
        """Test color_hs_to_xy."""
        assert (0.151, 0.343) == \
            color_util.color_hs_to_xy(180, 100)

        assert (0.356, 0.321) == \
            color_util.color_hs_to_xy(350, 12.5)

        assert (0.229, 0.474) == \
            color_util.color_hs_to_xy(140, 50)

        assert (0.474, 0.317) == \
            color_util.color_hs_to_xy(0, 40)

        assert (0.323, 0.329) == \
            color_util.color_hs_to_xy(360, 0)
Ejemplo n.º 14
0
 def turn_on(self, **kwargs):
     """Turn the switch on."""
     self._is_on = True
     self.schedule_update_ha_state()
     transition = 1
     if ATTR_TRANSITION in kwargs:
         transition = int(kwargs[ATTR_TRANSITION])
     if ATTR_BRIGHTNESS in kwargs:
         brightness = kwargs[ATTR_BRIGHTNESS]
         self._brightness = brightness
         brightness = int((brightness / 255) * 100)
         self.hass.data[ZIGATE_DOMAIN].action_move_level_onoff(
             self._device.addr, self._endpoint, 1, brightness, transition)
     else:
         self.hass.data[ZIGATE_DOMAIN].action_onoff(self._device.addr,
                                                    self._endpoint, 1)
     if ATTR_HS_COLOR in kwargs:
         h, s = kwargs[ATTR_HS_COLOR]
         if self.supported_features & SUPPORT_COLOR:
             x, y = color_util.color_hs_to_xy(h, s)
             self.hass.data[ZIGATE_DOMAIN].action_move_colour(
                 self._device.addr, self._endpoint, x, y, transition)
         elif self.supported_features & SUPPORT_HUE_COLOR:
             self.hass.data[ZIGATE_DOMAIN].action_move_hue_saturation(
                 self._device.addr, self._endpoint, int(h), int(s),
                 transition)
     elif ATTR_COLOR_TEMP in kwargs:
         temp = kwargs[ATTR_COLOR_TEMP]
         self.hass.data[ZIGATE_DOMAIN].action_move_temperature(
             self._device.addr, self._endpoint, int(temp), transition)
Ejemplo n.º 15
0
    def turn_on(self, **kwargs):
        """Turn the light on."""
        xy_color = None

        brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness or 255)
        color_temp = kwargs.get(ATTR_COLOR_TEMP)
        hs_color = kwargs.get(ATTR_HS_COLOR)
        transition_time = int(kwargs.get(ATTR_TRANSITION, 0))

        if hs_color is not None:
            xy_color = color_util.color_hs_to_xy(*hs_color)

        turn_on_kwargs = {
            "level": brightness,
            "transition": transition_time,
            "force_update": False,
        }

        with self._wemo_exception_handler("turn on"):
            if xy_color is not None:
                self.light.set_color(xy_color, transition=transition_time)

            if color_temp is not None:
                self.light.set_temperature(mireds=color_temp,
                                           transition=transition_time)

            self.light.turn_on(**turn_on_kwargs)

        self.schedule_update_ha_state()
Ejemplo n.º 16
0
    async def async_turn_on(self, **kwargs):
        """Turn on light."""
        data = {"on": True}

        if ATTR_BRIGHTNESS in kwargs:
            data["brightness"] = kwargs[ATTR_BRIGHTNESS]

        if ATTR_COLOR_TEMP in kwargs:
            data["color_temperature"] = kwargs[ATTR_COLOR_TEMP]

        if ATTR_HS_COLOR in kwargs:
            if COLOR_MODE_XY in self._attr_supported_color_modes:
                data["xy"] = color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
            else:
                data["hue"] = int(kwargs[ATTR_HS_COLOR][0] / 360 * 65535)
                data["saturation"] = int(kwargs[ATTR_HS_COLOR][1] / 100 * 255)

        if ATTR_XY_COLOR in kwargs:
            data["xy"] = kwargs[ATTR_XY_COLOR]

        if ATTR_TRANSITION in kwargs:
            data["transition_time"] = int(kwargs[ATTR_TRANSITION] * 10)
        elif "IKEA" in self._device.manufacturer:
            data["transition_time"] = 0

        if (alert := FLASH_TO_DECONZ.get(kwargs.get(ATTR_FLASH))) is not None:
            data["alert"] = alert
            del data["on"]
Ejemplo n.º 17
0
    async def async_turn_on(self, **kwargs: Any) -> None:
        """Turn on light."""
        data: SetStateAttributes = {"on": True}

        if ATTR_BRIGHTNESS in kwargs:
            data["brightness"] = kwargs[ATTR_BRIGHTNESS]

        if ATTR_COLOR_TEMP in kwargs:
            data["color_temperature"] = kwargs[ATTR_COLOR_TEMP]

        if ATTR_HS_COLOR in kwargs:
            if ColorMode.XY in self._attr_supported_color_modes:
                data["xy"] = color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
            else:
                data["hue"] = int(kwargs[ATTR_HS_COLOR][0] / 360 * 65535)
                data["saturation"] = int(kwargs[ATTR_HS_COLOR][1] / 100 * 255)

        if ATTR_XY_COLOR in kwargs:
            data["xy"] = kwargs[ATTR_XY_COLOR]

        if ATTR_TRANSITION in kwargs:
            data["transition_time"] = int(kwargs[ATTR_TRANSITION] * 10)
        elif "IKEA" in self._device.manufacturer:
            data["transition_time"] = 0

        if ATTR_FLASH in kwargs and kwargs[ATTR_FLASH] in FLASH_TO_DECONZ:
            data["alert"] = FLASH_TO_DECONZ[kwargs[ATTR_FLASH]]
            del data["on"]

        if ATTR_EFFECT in kwargs and kwargs[ATTR_EFFECT] in EFFECT_TO_DECONZ:
            data["effect"] = EFFECT_TO_DECONZ[kwargs[ATTR_EFFECT]]

        await self._device.set_state(**data)
Ejemplo n.º 18
0
    def state_attributes(self):
        """Return optional state attributes."""
        data = {}
        supported_features = self.supported_features

        if supported_features & SUPPORT_COLOR_TEMP:
            data[ATTR_MIN_MIREDS] = self.min_mireds
            data[ATTR_MAX_MIREDS] = self.max_mireds

        if self.is_on:
            if supported_features & SUPPORT_BRIGHTNESS:
                data[ATTR_BRIGHTNESS] = self.brightness

            if supported_features & SUPPORT_COLOR_TEMP:
                data[ATTR_COLOR_TEMP] = self.color_temp

            if self.supported_features & SUPPORT_COLOR and self.hs_color:
                # pylint: disable=unsubscriptable-object,not-an-iterable
                hs_color = self.hs_color
                data[ATTR_HS_COLOR] = (
                    round(hs_color[0], 3),
                    round(hs_color[1], 3),
                )
                data[ATTR_RGB_COLOR] = color_util.color_hs_to_RGB(*hs_color)
                data[ATTR_XY_COLOR] = color_util.color_hs_to_xy(*hs_color)

            if supported_features & SUPPORT_WHITE_VALUE:
                data[ATTR_WHITE_VALUE] = self.white_value

            if supported_features & SUPPORT_EFFECT:
                data[ATTR_EFFECT_LIST] = self.effect_list
                data[ATTR_EFFECT] = self.effect

        return {key: val for key, val in data.items() if val is not None}
Ejemplo n.º 19
0
    async def async_turn_on(self, **kwargs):
        """Turn on light."""
        data = {"on": True}

        if ATTR_COLOR_TEMP in kwargs:
            data["ct"] = kwargs[ATTR_COLOR_TEMP]

        if ATTR_HS_COLOR in kwargs:
            data["xy"] = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])

        if ATTR_BRIGHTNESS in kwargs:
            data["bri"] = kwargs[ATTR_BRIGHTNESS]

        if ATTR_TRANSITION in kwargs:
            data["transitiontime"] = int(kwargs[ATTR_TRANSITION] * 10)
        elif "IKEA" in self._device.manufacturer:
            data["transitiontime"] = 0

        if ATTR_FLASH in kwargs:
            if kwargs[ATTR_FLASH] == FLASH_SHORT:
                data["alert"] = "select"
                del data["on"]
            elif kwargs[ATTR_FLASH] == FLASH_LONG:
                data["alert"] = "lselect"
                del data["on"]

        if ATTR_EFFECT in kwargs:
            if kwargs[ATTR_EFFECT] == EFFECT_COLORLOOP:
                data["effect"] = "colorloop"
            else:
                data["effect"] = "none"

        await self._device.async_set_state(data)
Ejemplo n.º 20
0
    async def async_turn_on(self, **kwargs):
        """Turn on light."""
        data = {'on': True}

        if ATTR_COLOR_TEMP in kwargs:
            data['ct'] = kwargs[ATTR_COLOR_TEMP]

        if ATTR_HS_COLOR in kwargs:
            data['xy'] = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])

        if ATTR_BRIGHTNESS in kwargs:
            data['bri'] = kwargs[ATTR_BRIGHTNESS]

        if ATTR_TRANSITION in kwargs:
            data['transitiontime'] = int(kwargs[ATTR_TRANSITION]) * 10

        if ATTR_FLASH in kwargs:
            if kwargs[ATTR_FLASH] == FLASH_SHORT:
                data['alert'] = 'select'
                del data['on']
            elif kwargs[ATTR_FLASH] == FLASH_LONG:
                data['alert'] = 'lselect'
                del data['on']

        if ATTR_EFFECT in kwargs:
            if kwargs[ATTR_EFFECT] == EFFECT_COLORLOOP:
                data['effect'] = 'colorloop'
            else:
                data['effect'] = 'none'

        await self._light.async_set_state(data)
Ejemplo n.º 21
0
    async def async_turn_on(self, **kwargs):
        """Turn the entity on."""
        transition = kwargs.get(light.ATTR_TRANSITION)
        duration = transition * 10 if transition else DEFAULT_DURATION
        brightness = kwargs.get(light.ATTR_BRIGHTNESS)

        t_log = {}
        if (brightness is not None or transition) and \
                self._supported_features & light.SUPPORT_BRIGHTNESS:
            if brightness is not None:
                level = min(254, brightness)
            else:
                level = self._brightness or 254
            success = await self._level_channel.move_to_level_with_on_off(
                level, duration)
            t_log['move_to_level_with_on_off'] = success
            if not success:
                self.debug("turned on: %s", t_log)
                return
            self._state = bool(level)
            if level:
                self._brightness = level

        if brightness is None or brightness:
            success = await self._on_off_channel.on()
            t_log['on_off'] = success
            if not success:
                self.debug("turned on: %s", t_log)
                return
            self._state = True

        if light.ATTR_COLOR_TEMP in kwargs and \
                self.supported_features & light.SUPPORT_COLOR_TEMP:
            temperature = kwargs[light.ATTR_COLOR_TEMP]
            success = await self._color_channel.move_to_color_temp(
                temperature, duration)
            t_log['move_to_color_temp'] = success
            if not success:
                self.debug("turned on: %s", t_log)
                return
            self._color_temp = temperature

        if light.ATTR_HS_COLOR in kwargs and \
                self.supported_features & light.SUPPORT_COLOR:
            hs_color = kwargs[light.ATTR_HS_COLOR]
            xy_color = color_util.color_hs_to_xy(*hs_color)
            success = await self._color_channel.move_to_color(
                int(xy_color[0] * 65535),
                int(xy_color[1] * 65535),
                duration,
            )
            t_log['move_to_color'] = success
            if not success:
                self.debug("turned on: %s", t_log)
                return
            self._hs_color = hs_color

        self.debug("turned on: %s", t_log)
        self.async_schedule_update_ha_state()
Ejemplo n.º 22
0
    async def async_turn_on(self, **kwargs):
        """Turn the specified or all lights on."""
        command = {"on": True}

        if ATTR_TRANSITION in kwargs:
            command["transitiontime"] = int(kwargs[ATTR_TRANSITION] * 10)

        if ATTR_HS_COLOR in kwargs:
            if self.is_osram:
                command["hue"] = int(kwargs[ATTR_HS_COLOR][0] / 360 * 65535)
                command["sat"] = int(kwargs[ATTR_HS_COLOR][1] / 100 * 255)
            else:
                # Philips hue bulb models respond differently to hue/sat
                # requests, so we convert to XY first to ensure a consistent
                # color.
                xy_color = color.color_hs_to_xy(*kwargs[ATTR_HS_COLOR], self.gamut)
                command["xy"] = xy_color
        elif ATTR_COLOR_TEMP in kwargs:
            temp = kwargs[ATTR_COLOR_TEMP]
            command["ct"] = max(self.min_mireds, min(temp, self.max_mireds))

        if ATTR_BRIGHTNESS in kwargs:
            command["bri"] = hass_to_hue_brightness(kwargs[ATTR_BRIGHTNESS])

        flash = kwargs.get(ATTR_FLASH)

        if flash == FLASH_LONG:
            command["alert"] = "lselect"
            del command["on"]
        elif flash == FLASH_SHORT:
            command["alert"] = "select"
            del command["on"]
        elif (
            not self.is_innr
            and not self.is_ewelink
            and not self.is_livarno
            and not self.is_s31litezb
        ):
            command["alert"] = "none"

        if ATTR_EFFECT in kwargs:
            effect = kwargs[ATTR_EFFECT]
            if effect == EFFECT_COLORLOOP:
                command["effect"] = "colorloop"
            elif effect == EFFECT_RANDOM:
                command["hue"] = random.randrange(0, 65535)
                command["sat"] = random.randrange(150, 254)
            else:
                command["effect"] = "none"

        if self.is_group:
            await self.bridge.async_request_call(self.light.set_action, **command)
        else:
            await self.bridge.async_request_call(self.light.set_state, **command)

        await self.coordinator.async_request_refresh()
Ejemplo n.º 23
0
    async def async_turn_on(self, **kwargs):
        """Turn the entity on."""
        transition = kwargs.get(light.ATTR_TRANSITION)
        duration = transition * 10 if transition else DEFAULT_DURATION
        brightness = kwargs.get(light.ATTR_BRIGHTNESS)

        if (brightness is not None or transition) and \
                self._supported_features & light.SUPPORT_BRIGHTNESS:
            if brightness is not None:
                level = min(254, brightness)
            else:
                level = self._brightness or 254
            success = await self._level_channel.move_to_level_with_on_off(
                level,
                duration
            )
            if not success:
                return
            self._state = bool(level)
            if level:
                self._brightness = level

        if brightness is None or brightness:
            success = await self._on_off_channel.on()
            if not success:
                return
            self._state = True

        if light.ATTR_COLOR_TEMP in kwargs and \
                self.supported_features & light.SUPPORT_COLOR_TEMP:
            temperature = kwargs[light.ATTR_COLOR_TEMP]
            success = await self._color_channel.move_to_color_temp(
                temperature, duration)
            if not success:
                return
            self._color_temp = temperature

        if light.ATTR_HS_COLOR in kwargs and \
                self.supported_features & light.SUPPORT_COLOR:
            hs_color = kwargs[light.ATTR_HS_COLOR]
            xy_color = color_util.color_hs_to_xy(*hs_color)
            success = await self._color_channel.move_to_color(
                int(xy_color[0] * 65535),
                int(xy_color[1] * 65535),
                duration,
            )
            if not success:
                return
            self._hs_color = hs_color

        self.async_schedule_update_ha_state()
Ejemplo n.º 24
0
    async def async_turn_on(self, **kwargs):
        """Turn the specified or all lights on."""
        command = {'on': True}

        if ATTR_TRANSITION in kwargs:
            command['transitiontime'] = int(kwargs[ATTR_TRANSITION] * 10)

        if ATTR_HS_COLOR in kwargs:
            if self.is_osram:
                command['hue'] = int(kwargs[ATTR_HS_COLOR][0] / 360 * 65535)
                command['sat'] = int(kwargs[ATTR_HS_COLOR][1] / 100 * 255)
            else:
                # Philips hue bulb models respond differently to hue/sat
                # requests, so we convert to XY first to ensure a consistent
                # color.
                xy_color = color.color_hs_to_xy(*kwargs[ATTR_HS_COLOR],
                                                self.gamut)
                command['xy'] = xy_color
        elif ATTR_COLOR_TEMP in kwargs:
            temp = kwargs[ATTR_COLOR_TEMP]
            command['ct'] = max(self.min_mireds, min(temp, self.max_mireds))

        if ATTR_BRIGHTNESS in kwargs:
            command['bri'] = kwargs[ATTR_BRIGHTNESS]

        flash = kwargs.get(ATTR_FLASH)

        if flash == FLASH_LONG:
            command['alert'] = 'lselect'
            del command['on']
        elif flash == FLASH_SHORT:
            command['alert'] = 'select'
            del command['on']
        else:
            command['alert'] = 'none'

        if ATTR_EFFECT in kwargs:
            effect = kwargs[ATTR_EFFECT]
            if effect == EFFECT_COLORLOOP:
                command['effect'] = 'colorloop'
            elif effect == EFFECT_RANDOM:
                command['hue'] = random.randrange(0, 65535)
                command['sat'] = random.randrange(150, 254)
            else:
                command['effect'] = 'none'

        if self.is_group:
            await self.light.set_action(**command)
        else:
            await self.light.set_state(**command)
Ejemplo n.º 25
0
    async def async_turn_on(self, **kwargs):
        """Turn the specified or all lights on."""
        command = {'on': True}

        if ATTR_TRANSITION in kwargs:
            command['transitiontime'] = int(kwargs[ATTR_TRANSITION] * 10)

        if ATTR_HS_COLOR in kwargs:
            if self.is_osram:
                command['hue'] = int(kwargs[ATTR_HS_COLOR][0] / 360 * 65535)
                command['sat'] = int(kwargs[ATTR_HS_COLOR][1] / 100 * 255)
            else:
                # Philips hue bulb models respond differently to hue/sat
                # requests, so we convert to XY first to ensure a consistent
                # color.
                xy_color = color.color_hs_to_xy(*kwargs[ATTR_HS_COLOR],
                                                self.gamut)
                command['xy'] = xy_color
        elif ATTR_COLOR_TEMP in kwargs:
            temp = kwargs[ATTR_COLOR_TEMP]
            command['ct'] = max(self.min_mireds, min(temp, self.max_mireds))

        if ATTR_BRIGHTNESS in kwargs:
            command['bri'] = kwargs[ATTR_BRIGHTNESS]

        flash = kwargs.get(ATTR_FLASH)

        if flash == FLASH_LONG:
            command['alert'] = 'lselect'
            del command['on']
        elif flash == FLASH_SHORT:
            command['alert'] = 'select'
            del command['on']
        else:
            command['alert'] = 'none'

        effect = kwargs.get(ATTR_EFFECT)

        if effect == EFFECT_COLORLOOP:
            command['effect'] = 'colorloop'
        elif effect == EFFECT_RANDOM:
            command['hue'] = random.randrange(0, 65535)
            command['sat'] = random.randrange(150, 254)
        elif self.is_philips:
            command['effect'] = 'none'

        if self.is_group:
            await self.light.set_action(**command)
        else:
            await self.light.set_state(**command)
Ejemplo n.º 26
0
    async def async_turn_on(self, **kwargs):
        """Turn the entity on."""
        duration = kwargs.get(light.ATTR_TRANSITION, DEFAULT_DURATION)
        duration = duration * 10  # tenths of s

        if light.ATTR_COLOR_TEMP in kwargs and \
                self.supported_features & light.SUPPORT_COLOR_TEMP:
            temperature = kwargs[light.ATTR_COLOR_TEMP]
            success = await self._color_channel.move_to_color_temp(
                temperature, duration)
            if not success:
                return
            self._color_temp = temperature

        if light.ATTR_HS_COLOR in kwargs and \
                self.supported_features & light.SUPPORT_COLOR:
            hs_color = kwargs[light.ATTR_HS_COLOR]
            xy_color = color_util.color_hs_to_xy(*hs_color)
            success = await self._color_channel.move_to_color(
                int(xy_color[0] * 65535),
                int(xy_color[1] * 65535),
                duration,
            )
            if not success:
                return
            self._hs_color = hs_color

        if self._brightness is not None:
            brightness = kwargs.get(
                light.ATTR_BRIGHTNESS, self._brightness or 255)
            success = await self._level_channel.move_to_level_with_on_off(
                brightness,
                duration
            )
            if not success:
                return
            self._state = True
            self._brightness = brightness
            self.async_schedule_update_ha_state()
            return

        success = await self._on_off_channel.on()
        if not success:
            return

        self._state = True
        self.async_schedule_update_ha_state()
Ejemplo n.º 27
0
    async def async_turn_on(self, **kwargs):
        """Turn the entity on."""
        duration = kwargs.get(light.ATTR_TRANSITION, DEFAULT_DURATION)
        duration = duration * 10  # tenths of s

        if light.ATTR_COLOR_TEMP in kwargs and \
                self.supported_features & light.SUPPORT_COLOR_TEMP:
            temperature = kwargs[light.ATTR_COLOR_TEMP]
            success = await self._color_channel.move_to_color_temp(
                temperature, duration)
            if not success:
                return
            self._color_temp = temperature

        if light.ATTR_HS_COLOR in kwargs and \
                self.supported_features & light.SUPPORT_COLOR:
            hs_color = kwargs[light.ATTR_HS_COLOR]
            xy_color = color_util.color_hs_to_xy(*hs_color)
            success = await self._color_channel.move_to_color(
                int(xy_color[0] * 65535),
                int(xy_color[1] * 65535),
                duration,
            )
            if not success:
                return
            self._hs_color = hs_color

        if self._brightness is not None:
            brightness = kwargs.get(light.ATTR_BRIGHTNESS, self._brightness
                                    or 255)
            success = await self._level_channel.move_to_level_with_on_off(
                brightness, duration)
            if not success:
                return
            self._state = True
            self._brightness = brightness
            self.async_schedule_update_ha_state()
            return

        success = await self._on_off_channel.on()
        if not success:
            return

        self._state = True
        self.async_schedule_update_ha_state()
Ejemplo n.º 28
0
    def turn_on(self, **kwargs):
        """Turn the light on."""
        transitiontime = int(kwargs.get(ATTR_TRANSITION, 0))

        hs_color = kwargs.get(ATTR_HS_COLOR)

        if hs_color is not None:
            xy_color = color_util.color_hs_to_xy(*hs_color)
            self.wemo.set_color(xy_color, transition=transitiontime)

        if ATTR_COLOR_TEMP in kwargs:
            colortemp = kwargs[ATTR_COLOR_TEMP]
            self.wemo.set_temperature(mireds=colortemp, transition=transitiontime)

        if ATTR_BRIGHTNESS in kwargs:
            brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness or 255)
            self.wemo.turn_on(level=brightness, transition=transitiontime)
        else:
            self.wemo.turn_on(transition=transitiontime)
Ejemplo n.º 29
0
    def turn_on(self, **kwargs):
        """Turn the light on."""
        transitiontime = int(kwargs.get(ATTR_TRANSITION, 0))

        hs_color = kwargs.get(ATTR_HS_COLOR)

        if hs_color is not None:
            xy_color = color_util.color_hs_to_xy(*hs_color)
            self.wemo.set_color(xy_color, transition=transitiontime)

        if ATTR_COLOR_TEMP in kwargs:
            colortemp = kwargs[ATTR_COLOR_TEMP]
            self.wemo.set_temperature(mireds=colortemp,
                                      transition=transitiontime)

        if ATTR_BRIGHTNESS in kwargs:
            brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness or 255)
            self.wemo.turn_on(level=brightness, transition=transitiontime)
        else:
            self.wemo.turn_on(transition=transitiontime)
Ejemplo n.º 30
0
    async def async_turn_on(self, **kwargs):
        """Turn the entity on."""
        duration = kwargs.get(light.ATTR_TRANSITION, DEFAULT_DURATION)
        duration = duration * 10  # tenths of s
        if light.ATTR_COLOR_TEMP in kwargs:
            temperature = kwargs[light.ATTR_COLOR_TEMP]
            await self._endpoint.light_color.move_to_color_temp(
                temperature, duration)
            self._color_temp = temperature

        if light.ATTR_HS_COLOR in kwargs:
            self._hs_color = kwargs[light.ATTR_HS_COLOR]
            xy_color = color_util.color_hs_to_xy(*self._hs_color)
            await self._endpoint.light_color.move_to_color(
                int(xy_color[0] * 65535),
                int(xy_color[1] * 65535),
                duration,
            )

        if self._brightness is not None:
            brightness = kwargs.get(
                light.ATTR_BRIGHTNESS, self._brightness or 255)
            self._brightness = brightness
            # Move to level with on/off:
            await self._endpoint.level.move_to_level_with_on_off(
                brightness,
                duration
            )
            self._state = 1
            self.async_schedule_update_ha_state()
            return
        from zigpy.exceptions import DeliveryError
        try:
            await self._endpoint.on_off.on()
        except DeliveryError as ex:
            _LOGGER.error("Unable to turn the light on: %s", ex)
            return

        self._state = 1
        self.async_schedule_update_ha_state()
Ejemplo n.º 31
0
    async def async_turn_on(self, **kwargs):
        """Turn on light."""
        data = {"on": True}

        if ATTR_BRIGHTNESS in kwargs:
            data["brightness"] = kwargs[ATTR_BRIGHTNESS]

        if ATTR_COLOR_TEMP in kwargs:
            data["color_temperature"] = kwargs[ATTR_COLOR_TEMP]

        if ATTR_HS_COLOR in kwargs:
            if COLOR_MODE_XY in self._attr_supported_color_modes:
                data["xy"] = color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
            else:
                data["hue"] = int(kwargs[ATTR_HS_COLOR][0] / 360 * 65535)
                data["saturation"] = int(kwargs[ATTR_HS_COLOR][1] / 100 * 255)

        if ATTR_XY_COLOR in kwargs:
            data["xy"] = kwargs[ATTR_XY_COLOR]

        if ATTR_TRANSITION in kwargs:
            data["transition_time"] = int(kwargs[ATTR_TRANSITION] * 10)
        elif "IKEA" in self._device.manufacturer:
            data["transition_time"] = 0

        if ATTR_FLASH in kwargs:
            if kwargs[ATTR_FLASH] == FLASH_SHORT:
                data["alert"] = "select"
                del data["on"]
            elif kwargs[ATTR_FLASH] == FLASH_LONG:
                data["alert"] = "lselect"
                del data["on"]

        if ATTR_EFFECT in kwargs:
            if kwargs[ATTR_EFFECT] == EFFECT_COLORLOOP:
                data["effect"] = "colorloop"
            else:
                data["effect"] = "none"

        await self._device.set_state(**data)
Ejemplo n.º 32
0
    def turn_on(self, **kwargs):
        """Turn the switch on."""
        brightness = kwargs.get(ATTR_BRIGHTNESS)
        hs_color = kwargs.get(ATTR_HS_COLOR)
        color_temp_mired = kwargs.get(ATTR_COLOR_TEMP)

        state_kwargs = {}

        if hs_color:
            if self.wink.supports_xy_color():
                xy_color = color_util.color_hs_to_xy(*hs_color)
                state_kwargs['color_xy'] = xy_color
            if self.wink.supports_hue_saturation():
                state_kwargs['color_hue_saturation'] = hs_color

        if color_temp_mired:
            state_kwargs['color_kelvin'] = mired_to_kelvin(color_temp_mired)

        if brightness:
            state_kwargs['brightness'] = brightness / 255.0

        self.wink.set_state(True, **state_kwargs)
Ejemplo n.º 33
0
    async def __async_set_lamp_state(self, power_state: bool, **kwargs):
        lamp_state = LampState(power_on=power_state)
        if ATTR_TRANSITION in kwargs:
            lamp_state.transition_time = int(kwargs[ATTR_TRANSITION] * 1000)

        if ATTR_HS_COLOR in kwargs:
            xy_color = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
            lamp_state.x_chromaticity = xy_color[0]
            lamp_state.y_chromaticity = xy_color[1]
        elif ATTR_COLOR_TEMP in kwargs:
            temp = kwargs[ATTR_COLOR_TEMP]
            lamp_state.cct = 1000000 / temp

        if ATTR_BRIGHTNESS in kwargs:
            lamp_state.brightness = kwargs[ATTR_BRIGHTNESS] / 255.0

        if ATTR_WHITE_VALUE in kwargs:
            lamp_state.vibrancy = 1 - (kwargs[ATTR_WHITE_VALUE] / 255.0)

        await self._group.set_state(lamp_state)
        self._lamp_state = self._group.state
        self.schedule_update_ha_state(force_refresh=False)
Ejemplo n.º 34
0
    async def async_turn_on(self, **kwargs):
        """Turn the entity on."""
        duration = kwargs.get(light.ATTR_TRANSITION, DEFAULT_DURATION)
        duration = duration * 10  # tenths of s
        if light.ATTR_COLOR_TEMP in kwargs:
            temperature = kwargs[light.ATTR_COLOR_TEMP]
            await self._endpoint.light_color.move_to_color_temp(
                temperature, duration)
            self._color_temp = temperature

        if light.ATTR_HS_COLOR in kwargs:
            self._hs_color = kwargs[light.ATTR_HS_COLOR]
            xy_color = color_util.color_hs_to_xy(*self._hs_color)
            await self._endpoint.light_color.move_to_color(
                int(xy_color[0] * 65535),
                int(xy_color[1] * 65535),
                duration,
            )

        if self._brightness is not None:
            brightness = kwargs.get(light.ATTR_BRIGHTNESS, self._brightness
                                    or 255)
            self._brightness = brightness
            # Move to level with on/off:
            await self._endpoint.level.move_to_level_with_on_off(
                brightness, duration)
            self._state = 1
            self.async_schedule_update_ha_state()
            return
        from zigpy.exceptions import DeliveryError
        try:
            await self._endpoint.on_off.on()
        except DeliveryError as ex:
            _LOGGER.error("Unable to turn the light on: %s", ex)
            return

        self._state = 1
        self.async_schedule_update_ha_state()
Ejemplo n.º 35
0
    def turn_on(self, **kwargs):
        """Turn the switch on."""
        brightness = kwargs.get(ATTR_BRIGHTNESS)
        hs_color = kwargs.get(ATTR_HS_COLOR)
        color_temp_mired = kwargs.get(ATTR_COLOR_TEMP)

        state_kwargs = {}

        if hs_color:
            if self.wink.supports_xy_color():
                xy_color = color_util.color_hs_to_xy(*hs_color)
                state_kwargs['color_xy'] = xy_color
            if self.wink.supports_hue_saturation():
                hs_scaled = hs_color[0] / 360, hs_color[1] / 100
                state_kwargs['color_hue_saturation'] = hs_scaled

        if color_temp_mired:
            state_kwargs['color_kelvin'] = mired_to_kelvin(color_temp_mired)

        if brightness:
            state_kwargs['brightness'] = brightness / 255.0

        self.wink.set_state(True, **state_kwargs)
Ejemplo n.º 36
0
    async def async_turn_on(self, **kwargs):
        self._call_ongoing = True
        """Turn the entity on."""
        duration = kwargs.get(light.ATTR_TRANSITION, DEFAULT_DURATION)
        duration = duration * 10  # tenths of s
        if light.ATTR_COLOR_TEMP in kwargs:
            temperature = kwargs[light.ATTR_COLOR_TEMP]
            await self._endpoint.light_color.move_to_color_temp(
                temperature, duration)
            self._color_temp = temperature

        if light.ATTR_HS_COLOR in kwargs:
            self._hs_color = kwargs[light.ATTR_HS_COLOR]
            xy_color = color_util.color_hs_to_xy(*self._hs_color)
            await self._endpoint.light_color.move_to_color(
                int(xy_color[0] * 65535),
                int(xy_color[1] * 65535),
                duration,
            )

        if self._brightness is not None:
            brightness = kwargs.get(light.ATTR_BRIGHTNESS, self._brightness)
            self._brightness = 2 if (brightness < 2) else brightness
            #            self._brightness = 2 if (brightness < 2 and self._state == False) else brightness
            # Move to level with on/off:
            _LOGGER.debug("[0x%04x:%s] move_to_level_w_onoff: %s ",
                          self._endpoint._device.nwk,
                          self._endpoint.endpoint_id, self._brightness)

            await self._endpoint.level.move_to_level_with_on_off(
                self._brightness, duration)
            self._state = True
        else:
            await self._endpoint.on_off.on()
            self._state = True
        await asyncio.sleep(duration / 10)
        self._call_ongoing = False
Ejemplo n.º 37
0
    async def async_turn_on(self, **kwargs):
        """Turn the device on.

        This method is a coroutine.
        """
        should_update = False
        on_command_type = self._config[CONF_ON_COMMAND_TYPE]

        if on_command_type == "first":
            mqtt.async_publish(
                self.hass,
                self._topic[CONF_COMMAND_TOPIC],
                self._payload["on"],
                self._config[CONF_QOS],
                self._config[CONF_RETAIN],
            )
            should_update = True

        # If brightness is being used instead of an on command, make sure
        # there is a brightness input.  Either set the brightness to our
        # saved value or the maximum value if this is the first call
        elif on_command_type == "brightness":
            if ATTR_BRIGHTNESS not in kwargs:
                kwargs[
                    ATTR_BRIGHTNESS] = self._brightness if self._brightness else 255

        if ATTR_HS_COLOR in kwargs and self._topic[
                CONF_RGB_COMMAND_TOPIC] is not None:

            hs_color = kwargs[ATTR_HS_COLOR]

            # If there's a brightness topic set, we don't want to scale the RGB
            # values given using the brightness.
            if self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is not None:
                brightness = 255
            else:
                brightness = kwargs.get(
                    ATTR_BRIGHTNESS,
                    self._brightness if self._brightness else 255)
            rgb = color_util.color_hsv_to_RGB(hs_color[0], hs_color[1],
                                              brightness / 255 * 100)
            tpl = self._command_templates[CONF_RGB_COMMAND_TEMPLATE]
            if tpl:
                rgb_color_str = tpl({
                    "red": rgb[0],
                    "green": rgb[1],
                    "blue": rgb[2]
                })
            else:
                rgb_color_str = f"{rgb[0]},{rgb[1]},{rgb[2]}"

            mqtt.async_publish(
                self.hass,
                self._topic[CONF_RGB_COMMAND_TOPIC],
                rgb_color_str,
                self._config[CONF_QOS],
                self._config[CONF_RETAIN],
            )

            if self._optimistic_rgb:
                self._hs = kwargs[ATTR_HS_COLOR]
                should_update = True

        if ATTR_HS_COLOR in kwargs and self._topic[
                CONF_HS_COMMAND_TOPIC] is not None:

            hs_color = kwargs[ATTR_HS_COLOR]
            mqtt.async_publish(
                self.hass,
                self._topic[CONF_HS_COMMAND_TOPIC],
                f"{hs_color[0]},{hs_color[1]}",
                self._config[CONF_QOS],
                self._config[CONF_RETAIN],
            )

            if self._optimistic_hs:
                self._hs = kwargs[ATTR_HS_COLOR]
                should_update = True

        if ATTR_HS_COLOR in kwargs and self._topic[
                CONF_XY_COMMAND_TOPIC] is not None:

            xy_color = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
            mqtt.async_publish(
                self.hass,
                self._topic[CONF_XY_COMMAND_TOPIC],
                f"{xy_color[0]},{xy_color[1]}",
                self._config[CONF_QOS],
                self._config[CONF_RETAIN],
            )

            if self._optimistic_xy:
                self._hs = kwargs[ATTR_HS_COLOR]
                should_update = True

        if (ATTR_BRIGHTNESS in kwargs
                and self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is not None):
            brightness_normalized = kwargs[ATTR_BRIGHTNESS] / 255
            brightness_scale = self._config[CONF_BRIGHTNESS_SCALE]
            device_brightness = min(
                round(brightness_normalized * brightness_scale),
                brightness_scale)
            # Make sure the brightness is not rounded down to 0
            device_brightness = max(device_brightness, 1)
            mqtt.async_publish(
                self.hass,
                self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC],
                device_brightness,
                self._config[CONF_QOS],
                self._config[CONF_RETAIN],
            )

            if self._optimistic_brightness:
                self._brightness = kwargs[ATTR_BRIGHTNESS]
                should_update = True
        elif (ATTR_BRIGHTNESS in kwargs and ATTR_HS_COLOR not in kwargs
              and self._topic[CONF_RGB_COMMAND_TOPIC] is not None):
            hs_color = self._hs if self._hs is not None else (0, 0)
            rgb = color_util.color_hsv_to_RGB(
                hs_color[0], hs_color[1], kwargs[ATTR_BRIGHTNESS] / 255 * 100)
            tpl = self._command_templates[CONF_RGB_COMMAND_TEMPLATE]
            if tpl:
                rgb_color_str = tpl({
                    "red": rgb[0],
                    "green": rgb[1],
                    "blue": rgb[2]
                })
            else:
                rgb_color_str = f"{rgb[0]},{rgb[1]},{rgb[2]}"

            mqtt.async_publish(
                self.hass,
                self._topic[CONF_RGB_COMMAND_TOPIC],
                rgb_color_str,
                self._config[CONF_QOS],
                self._config[CONF_RETAIN],
            )

            if self._optimistic_brightness:
                self._brightness = kwargs[ATTR_BRIGHTNESS]
                should_update = True

        if (ATTR_COLOR_TEMP in kwargs
                and self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC] is not None):
            color_temp = int(kwargs[ATTR_COLOR_TEMP])
            tpl = self._command_templates[CONF_COLOR_TEMP_COMMAND_TEMPLATE]

            if tpl:
                color_temp = tpl({"value": color_temp})

            mqtt.async_publish(
                self.hass,
                self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC],
                color_temp,
                self._config[CONF_QOS],
                self._config[CONF_RETAIN],
            )

            if self._optimistic_color_temp:
                self._color_temp = kwargs[ATTR_COLOR_TEMP]
                should_update = True

        if ATTR_EFFECT in kwargs and self._topic[
                CONF_EFFECT_COMMAND_TOPIC] is not None:
            effect = kwargs[ATTR_EFFECT]
            if effect in self._config.get(CONF_EFFECT_LIST):
                mqtt.async_publish(
                    self.hass,
                    self._topic[CONF_EFFECT_COMMAND_TOPIC],
                    effect,
                    self._config[CONF_QOS],
                    self._config[CONF_RETAIN],
                )

                if self._optimistic_effect:
                    self._effect = kwargs[ATTR_EFFECT]
                    should_update = True

        if (ATTR_WHITE_VALUE in kwargs
                and self._topic[CONF_WHITE_VALUE_COMMAND_TOPIC] is not None):
            percent_white = float(kwargs[ATTR_WHITE_VALUE]) / 255
            white_scale = self._config[CONF_WHITE_VALUE_SCALE]
            device_white_value = min(round(percent_white * white_scale),
                                     white_scale)
            mqtt.async_publish(
                self.hass,
                self._topic[CONF_WHITE_VALUE_COMMAND_TOPIC],
                device_white_value,
                self._config[CONF_QOS],
                self._config[CONF_RETAIN],
            )

            if self._optimistic_white_value:
                self._white_value = kwargs[ATTR_WHITE_VALUE]
                should_update = True

        if on_command_type == "last":
            mqtt.async_publish(
                self.hass,
                self._topic[CONF_COMMAND_TOPIC],
                self._payload["on"],
                self._config[CONF_QOS],
                self._config[CONF_RETAIN],
            )
            should_update = True

        if self._optimistic:
            # Optimistically assume that the light has changed state.
            self._state = True
            should_update = True

        if should_update:
            self.async_write_ha_state()
Ejemplo n.º 38
0
    async def async_turn_on(self, **kwargs):
        """Turn the device on.

        This method is a coroutine.
        """
        should_update = False
        on_command_type = self._config.get(CONF_ON_COMMAND_TYPE)

        if on_command_type == 'first':
            mqtt.async_publish(
                self.hass, self._topic[CONF_COMMAND_TOPIC],
                self._payload['on'], self._config.get(CONF_QOS),
                self._config.get(CONF_RETAIN))
            should_update = True

        # If brightness is being used instead of an on command, make sure
        # there is a brightness input.  Either set the brightness to our
        # saved value or the maximum value if this is the first call
        elif on_command_type == 'brightness':
            if ATTR_BRIGHTNESS not in kwargs:
                kwargs[ATTR_BRIGHTNESS] = self._brightness if \
                                          self._brightness else 255

        if ATTR_HS_COLOR in kwargs and \
           self._topic[CONF_RGB_COMMAND_TOPIC] is not None:

            hs_color = kwargs[ATTR_HS_COLOR]

            # If there's a brightness topic set, we don't want to scale the RGB
            # values given using the brightness.
            if self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is not None:
                brightness = 255
            else:
                brightness = kwargs.get(
                    ATTR_BRIGHTNESS, self._brightness if self._brightness else
                    255)
            rgb = color_util.color_hsv_to_RGB(
                hs_color[0], hs_color[1], brightness / 255 * 100)
            tpl = self._templates[CONF_RGB_COMMAND_TEMPLATE]
            if tpl:
                rgb_color_str = tpl.async_render({
                    'red': rgb[0],
                    'green': rgb[1],
                    'blue': rgb[2],
                })
            else:
                rgb_color_str = '{},{},{}'.format(*rgb)

            mqtt.async_publish(
                self.hass, self._topic[CONF_RGB_COMMAND_TOPIC],
                rgb_color_str, self._config.get(CONF_QOS),
                self._config.get(CONF_RETAIN))

            if self._optimistic_rgb:
                self._hs = kwargs[ATTR_HS_COLOR]
                should_update = True

        if ATTR_HS_COLOR in kwargs and \
           self._topic[CONF_HS_COMMAND_TOPIC] is not None:

            hs_color = kwargs[ATTR_HS_COLOR]
            mqtt.async_publish(
                self.hass, self._topic[CONF_HS_COMMAND_TOPIC],
                '{},{}'.format(*hs_color), self._config.get(CONF_QOS),
                self._config.get(CONF_RETAIN))

            if self._optimistic_hs:
                self._hs = kwargs[ATTR_HS_COLOR]
                should_update = True

        if ATTR_HS_COLOR in kwargs and \
           self._topic[CONF_XY_COMMAND_TOPIC] is not None:

            xy_color = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
            mqtt.async_publish(
                self.hass, self._topic[CONF_XY_COMMAND_TOPIC],
                '{},{}'.format(*xy_color), self._config.get(CONF_QOS),
                self._config.get(CONF_RETAIN))

            if self._optimistic_xy:
                self._hs = kwargs[ATTR_HS_COLOR]
                should_update = True

        if ATTR_BRIGHTNESS in kwargs and \
           self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is not None:
            percent_bright = float(kwargs[ATTR_BRIGHTNESS]) / 255
            brightness_scale = self._config.get(CONF_BRIGHTNESS_SCALE)
            device_brightness = \
                min(round(percent_bright * brightness_scale), brightness_scale)
            mqtt.async_publish(
                self.hass, self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC],
                device_brightness, self._config.get(CONF_QOS),
                self._config.get(CONF_RETAIN))

            if self._optimistic_brightness:
                self._brightness = kwargs[ATTR_BRIGHTNESS]
                should_update = True
        elif ATTR_BRIGHTNESS in kwargs and ATTR_HS_COLOR not in kwargs and\
                self._topic[CONF_RGB_COMMAND_TOPIC] is not None:
            rgb = color_util.color_hsv_to_RGB(
                self._hs[0], self._hs[1], kwargs[ATTR_BRIGHTNESS] / 255 * 100)
            tpl = self._templates[CONF_RGB_COMMAND_TEMPLATE]
            if tpl:
                rgb_color_str = tpl.async_render({
                    'red': rgb[0],
                    'green': rgb[1],
                    'blue': rgb[2],
                })
            else:
                rgb_color_str = '{},{},{}'.format(*rgb)

            mqtt.async_publish(
                self.hass, self._topic[CONF_RGB_COMMAND_TOPIC],
                rgb_color_str, self._config.get(CONF_QOS),
                self._config.get(CONF_RETAIN))

            if self._optimistic_brightness:
                self._brightness = kwargs[ATTR_BRIGHTNESS]
                should_update = True

        if ATTR_COLOR_TEMP in kwargs and \
           self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC] is not None:
            color_temp = int(kwargs[ATTR_COLOR_TEMP])
            tpl = self._templates[CONF_COLOR_TEMP_COMMAND_TEMPLATE]

            if tpl:
                color_temp = tpl.async_render({
                    'value': color_temp,
                })

            mqtt.async_publish(
                self.hass, self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC],
                color_temp, self._config.get(CONF_QOS),
                self._config.get(CONF_RETAIN))

            if self._optimistic_color_temp:
                self._color_temp = kwargs[ATTR_COLOR_TEMP]
                should_update = True

        if ATTR_EFFECT in kwargs and \
           self._topic[CONF_EFFECT_COMMAND_TOPIC] is not None:
            effect = kwargs[ATTR_EFFECT]
            if effect in self._config.get(CONF_EFFECT_LIST):
                mqtt.async_publish(
                    self.hass, self._topic[CONF_EFFECT_COMMAND_TOPIC],
                    effect, self._config.get(CONF_QOS),
                    self._config.get(CONF_RETAIN))

                if self._optimistic_effect:
                    self._effect = kwargs[ATTR_EFFECT]
                    should_update = True

        if ATTR_WHITE_VALUE in kwargs and \
           self._topic[CONF_WHITE_VALUE_COMMAND_TOPIC] is not None:
            percent_white = float(kwargs[ATTR_WHITE_VALUE]) / 255
            white_scale = self._config.get(CONF_WHITE_VALUE_SCALE)
            device_white_value = \
                min(round(percent_white * white_scale), white_scale)
            mqtt.async_publish(
                self.hass, self._topic[CONF_WHITE_VALUE_COMMAND_TOPIC],
                device_white_value, self._config.get(CONF_QOS),
                self._config.get(CONF_RETAIN))

            if self._optimistic_white_value:
                self._white_value = kwargs[ATTR_WHITE_VALUE]
                should_update = True

        if on_command_type == 'last':
            mqtt.async_publish(self.hass, self._topic[CONF_COMMAND_TOPIC],
                               self._payload['on'], self._config.get(CONF_QOS),
                               self._config.get(CONF_RETAIN))
            should_update = True

        if self._optimistic:
            # Optimistically assume that the light has changed state.
            self._state = True
            should_update = True

        if should_update:
            self.async_schedule_update_ha_state()
Ejemplo n.º 39
0
    async def async_turn_on(self, **kwargs):
        """Turn the entity on."""
        transition = kwargs.get(light.ATTR_TRANSITION)
        duration = (transition *
                    10 if transition else self._default_transition *
                    10 if self._default_transition else DEFAULT_TRANSITION)
        brightness = kwargs.get(light.ATTR_BRIGHTNESS)
        effect = kwargs.get(light.ATTR_EFFECT)
        flash = kwargs.get(light.ATTR_FLASH)

        if brightness is None and self._off_brightness is not None:
            brightness = self._off_brightness

        t_log = {}
        if (brightness is not None or transition
            ) and self._supported_features & light.SUPPORT_BRIGHTNESS:
            if brightness is not None:
                level = min(254, brightness)
            else:
                level = self._brightness or 254
            result = await self._level_channel.move_to_level_with_on_off(
                level, duration)
            t_log["move_to_level_with_on_off"] = result
            if not isinstance(result, list) or result[1] is not Status.SUCCESS:
                self.debug("turned on: %s", t_log)
                return
            self._state = bool(level)
            if level:
                self._brightness = level

        if brightness is None or (self._FORCE_ON and brightness):
            # since some lights don't always turn on with move_to_level_with_on_off,
            # we should call the on command on the on_off cluster if brightness is not 0.
            result = await self._on_off_channel.on()
            t_log["on_off"] = result
            if not isinstance(result, list) or result[1] is not Status.SUCCESS:
                self.debug("turned on: %s", t_log)
                return
            self._state = True
        if (light.ATTR_COLOR_TEMP in kwargs
                and self.supported_features & light.SUPPORT_COLOR_TEMP):
            temperature = kwargs[light.ATTR_COLOR_TEMP]
            result = await self._color_channel.move_to_color_temp(
                temperature, duration)
            t_log["move_to_color_temp"] = result
            if not isinstance(result, list) or result[1] is not Status.SUCCESS:
                self.debug("turned on: %s", t_log)
                return
            self._color_temp = temperature
            self._hs_color = None

        if (light.ATTR_HS_COLOR in kwargs
                and self.supported_features & light.SUPPORT_COLOR):
            hs_color = kwargs[light.ATTR_HS_COLOR]
            xy_color = color_util.color_hs_to_xy(*hs_color)
            result = await self._color_channel.move_to_color(
                int(xy_color[0] * 65535), int(xy_color[1] * 65535), duration)
            t_log["move_to_color"] = result
            if not isinstance(result, list) or result[1] is not Status.SUCCESS:
                self.debug("turned on: %s", t_log)
                return
            self._hs_color = hs_color
            self._color_temp = None

        if (effect == light.EFFECT_COLORLOOP
                and self.supported_features & light.SUPPORT_EFFECT):
            result = await self._color_channel.color_loop_set(
                UPDATE_COLORLOOP_ACTION
                | UPDATE_COLORLOOP_DIRECTION
                | UPDATE_COLORLOOP_TIME,
                0x2,  # start from current hue
                0x1,  # only support up
                transition if transition else 7,  # transition
                0,  # no hue
            )
            t_log["color_loop_set"] = result
            self._effect = light.EFFECT_COLORLOOP
        elif (self._effect == light.EFFECT_COLORLOOP
              and effect != light.EFFECT_COLORLOOP
              and self.supported_features & light.SUPPORT_EFFECT):
            result = await self._color_channel.color_loop_set(
                UPDATE_COLORLOOP_ACTION,
                0x0,
                0x0,
                0x0,
                0x0,  # update action only, action off, no dir, time, hue
            )
            t_log["color_loop_set"] = result
            self._effect = None

        if flash is not None and self._supported_features & light.SUPPORT_FLASH:
            result = await self._identify_channel.trigger_effect(
                FLASH_EFFECTS[flash], EFFECT_DEFAULT_VARIANT)
            t_log["trigger_effect"] = result

        self._off_brightness = None
        self.debug("turned on: %s", t_log)
        self.async_write_ha_state()
Ejemplo n.º 40
0
    async def async_turn_on(self, **kwargs):
        """Turn the device on.

        This method is a coroutine.
        """
        should_update = False

        if self._on_command_type == 'first':
            mqtt.async_publish(
                self.hass, self._topic[CONF_COMMAND_TOPIC],
                self._payload['on'], self._qos, self._retain)
            should_update = True

        # If brightness is being used instead of an on command, make sure
        # there is a brightness input.  Either set the brightness to our
        # saved value or the maximum value if this is the first call
        elif self._on_command_type == 'brightness':
            if ATTR_BRIGHTNESS not in kwargs:
                kwargs[ATTR_BRIGHTNESS] = self._brightness if \
                                          self._brightness else 255

        if ATTR_HS_COLOR in kwargs and \
           self._topic[CONF_RGB_COMMAND_TOPIC] is not None:

            hs_color = kwargs[ATTR_HS_COLOR]
            brightness = kwargs.get(
                ATTR_BRIGHTNESS, self._brightness if self._brightness else 255)
            rgb = color_util.color_hsv_to_RGB(
                hs_color[0], hs_color[1], brightness / 255 * 100)
            tpl = self._templates[CONF_RGB_COMMAND_TEMPLATE]
            if tpl:
                rgb_color_str = tpl.async_render({
                    'red': rgb[0],
                    'green': rgb[1],
                    'blue': rgb[2],
                })
            else:
                rgb_color_str = '{},{},{}'.format(*rgb)

            mqtt.async_publish(
                self.hass, self._topic[CONF_RGB_COMMAND_TOPIC],
                rgb_color_str, self._qos, self._retain)

            if self._optimistic_rgb:
                self._hs = kwargs[ATTR_HS_COLOR]
                should_update = True

        if ATTR_HS_COLOR in kwargs and \
           self._topic[CONF_XY_COMMAND_TOPIC] is not None:

            xy_color = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
            mqtt.async_publish(
                self.hass, self._topic[CONF_XY_COMMAND_TOPIC],
                '{},{}'.format(*xy_color), self._qos,
                self._retain)

            if self._optimistic_xy:
                self._hs = kwargs[ATTR_HS_COLOR]
                should_update = True

        if ATTR_BRIGHTNESS in kwargs and \
           self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is not None:
            percent_bright = float(kwargs[ATTR_BRIGHTNESS]) / 255
            device_brightness = int(percent_bright * self._brightness_scale)
            mqtt.async_publish(
                self.hass, self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC],
                device_brightness, self._qos, self._retain)

            if self._optimistic_brightness:
                self._brightness = kwargs[ATTR_BRIGHTNESS]
                should_update = True

        if ATTR_COLOR_TEMP in kwargs and \
           self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC] is not None:
            color_temp = int(kwargs[ATTR_COLOR_TEMP])
            mqtt.async_publish(
                self.hass, self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC],
                color_temp, self._qos, self._retain)

            if self._optimistic_color_temp:
                self._color_temp = kwargs[ATTR_COLOR_TEMP]
                should_update = True

        if ATTR_EFFECT in kwargs and \
           self._topic[CONF_EFFECT_COMMAND_TOPIC] is not None:
            effect = kwargs[ATTR_EFFECT]
            if effect in self._effect_list:
                mqtt.async_publish(
                    self.hass, self._topic[CONF_EFFECT_COMMAND_TOPIC],
                    effect, self._qos, self._retain)

                if self._optimistic_effect:
                    self._effect = kwargs[ATTR_EFFECT]
                    should_update = True

        if ATTR_WHITE_VALUE in kwargs and \
           self._topic[CONF_WHITE_VALUE_COMMAND_TOPIC] is not None:
            percent_white = float(kwargs[ATTR_WHITE_VALUE]) / 255
            device_white_value = int(percent_white * self._white_value_scale)
            mqtt.async_publish(
                self.hass, self._topic[CONF_WHITE_VALUE_COMMAND_TOPIC],
                device_white_value, self._qos, self._retain)

            if self._optimistic_white_value:
                self._white_value = kwargs[ATTR_WHITE_VALUE]
                should_update = True

        if self._on_command_type == 'last':
            mqtt.async_publish(self.hass, self._topic[CONF_COMMAND_TOPIC],
                               self._payload['on'], self._qos, self._retain)
            should_update = True

        if self._optimistic:
            # Optimistically assume that switch has changed state.
            self._state = True
            should_update = True

        if should_update:
            self.async_schedule_update_ha_state()
Ejemplo n.º 41
0
    async def async_turn_on(self, **kwargs):
        """Turn the device on.

        This method is a coroutine.
        """
        should_update = False

        message = {'state': 'ON'}

        if ATTR_HS_COLOR in kwargs and (self._hs_support or self._rgb
                                        or self._xy):
            hs_color = kwargs[ATTR_HS_COLOR]
            message['color'] = {}
            if self._rgb:
                # If there's a brightness topic set, we don't want to scale the
                # RGB values given using the brightness.
                if self._brightness is not None:
                    brightness = 255
                else:
                    brightness = kwargs.get(
                        ATTR_BRIGHTNESS,
                        self._brightness if self._brightness else 255)
                rgb = color_util.color_hsv_to_RGB(hs_color[0], hs_color[1],
                                                  brightness / 255 * 100)
                message['color']['r'] = rgb[0]
                message['color']['g'] = rgb[1]
                message['color']['b'] = rgb[2]
            if self._xy:
                xy_color = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
                message['color']['x'] = xy_color[0]
                message['color']['y'] = xy_color[1]
            if self._hs_support:
                message['color']['h'] = hs_color[0]
                message['color']['s'] = hs_color[1]

            if self._optimistic:
                self._hs = kwargs[ATTR_HS_COLOR]
                should_update = True

        if ATTR_FLASH in kwargs:
            flash = kwargs.get(ATTR_FLASH)

            if flash == FLASH_LONG:
                message['flash'] = self._flash_times[CONF_FLASH_TIME_LONG]
            elif flash == FLASH_SHORT:
                message['flash'] = self._flash_times[CONF_FLASH_TIME_SHORT]

        if ATTR_TRANSITION in kwargs:
            message['transition'] = int(kwargs[ATTR_TRANSITION])

        if ATTR_BRIGHTNESS in kwargs:
            message['brightness'] = int(kwargs[ATTR_BRIGHTNESS] /
                                        float(DEFAULT_BRIGHTNESS_SCALE) *
                                        self._brightness_scale)

            if self._optimistic:
                self._brightness = kwargs[ATTR_BRIGHTNESS]
                should_update = True

        if ATTR_COLOR_TEMP in kwargs:
            message['color_temp'] = int(kwargs[ATTR_COLOR_TEMP])

            if self._optimistic:
                self._color_temp = kwargs[ATTR_COLOR_TEMP]
                should_update = True

        if ATTR_EFFECT in kwargs:
            message['effect'] = kwargs[ATTR_EFFECT]

            if self._optimistic:
                self._effect = kwargs[ATTR_EFFECT]
                should_update = True

        if ATTR_WHITE_VALUE in kwargs:
            message['white_value'] = int(kwargs[ATTR_WHITE_VALUE])

            if self._optimistic:
                self._white_value = kwargs[ATTR_WHITE_VALUE]
                should_update = True

        mqtt.async_publish(self.hass, self._topic[CONF_COMMAND_TOPIC],
                           json.dumps(message), self._qos, self._retain)

        if self._optimistic:
            # Optimistically assume that the light has changed state.
            self._state = True
            should_update = True

        if should_update:
            self.async_schedule_update_ha_state()
Ejemplo n.º 42
0
    def async_turn_on(self, **kwargs):
        """Turn the device on.

        This method is a coroutine.
        """
        should_update = False

        message = {'state': 'ON'}

        if ATTR_HS_COLOR in kwargs and (self._rgb or self._xy):
            hs_color = kwargs[ATTR_HS_COLOR]
            message['color'] = {}
            if self._rgb:
                brightness = kwargs.get(
                    ATTR_BRIGHTNESS,
                    self._brightness if self._brightness else 255)
                rgb = color_util.color_hsv_to_RGB(
                    hs_color[0], hs_color[1], brightness / 255 * 100)
                message['color']['r'] = rgb[0]
                message['color']['g'] = rgb[1]
                message['color']['b'] = rgb[2]
            if self._xy:
                xy_color = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
                message['color']['x'] = xy_color[0]
                message['color']['y'] = xy_color[1]

            if self._optimistic:
                self._hs = kwargs[ATTR_HS_COLOR]
                should_update = True

        if ATTR_FLASH in kwargs:
            flash = kwargs.get(ATTR_FLASH)

            if flash == FLASH_LONG:
                message['flash'] = self._flash_times[CONF_FLASH_TIME_LONG]
            elif flash == FLASH_SHORT:
                message['flash'] = self._flash_times[CONF_FLASH_TIME_SHORT]

        if ATTR_TRANSITION in kwargs:
            message['transition'] = int(kwargs[ATTR_TRANSITION])

        if ATTR_BRIGHTNESS in kwargs:
            message['brightness'] = int(kwargs[ATTR_BRIGHTNESS] /
                                        float(DEFAULT_BRIGHTNESS_SCALE) *
                                        self._brightness_scale)

            if self._optimistic:
                self._brightness = kwargs[ATTR_BRIGHTNESS]
                should_update = True

        if ATTR_COLOR_TEMP in kwargs:
            message['color_temp'] = int(kwargs[ATTR_COLOR_TEMP])

            if self._optimistic:
                self._color_temp = kwargs[ATTR_COLOR_TEMP]
                should_update = True

        if ATTR_EFFECT in kwargs:
            message['effect'] = kwargs[ATTR_EFFECT]

            if self._optimistic:
                self._effect = kwargs[ATTR_EFFECT]
                should_update = True

        if ATTR_WHITE_VALUE in kwargs:
            message['white_value'] = int(kwargs[ATTR_WHITE_VALUE])

            if self._optimistic:
                self._white_value = kwargs[ATTR_WHITE_VALUE]
                should_update = True

        mqtt.async_publish(
            self.hass, self._topic[CONF_COMMAND_TOPIC], json.dumps(message),
            self._qos, self._retain)

        if self._optimistic:
            # Optimistically assume that the light has changed state.
            self._state = True
            should_update = True

        if should_update:
            self.async_schedule_update_ha_state()
Ejemplo n.º 43
0
    async def async_turn_on(self, **kwargs):
        """Turn the device on.

        This method is a coroutine.
        """
        should_update = False
        on_command_type = self._config[CONF_ON_COMMAND_TYPE]

        if on_command_type == 'first':
            mqtt.async_publish(
                self.hass, self._topic[CONF_COMMAND_TOPIC],
                self._payload['on'], self._config[CONF_QOS],
                self._config[CONF_RETAIN])
            should_update = True

        # If brightness is being used instead of an on command, make sure
        # there is a brightness input.  Either set the brightness to our
        # saved value or the maximum value if this is the first call
        elif on_command_type == 'brightness':
            if ATTR_BRIGHTNESS not in kwargs:
                kwargs[ATTR_BRIGHTNESS] = self._brightness if \
                                          self._brightness else 255

        if ATTR_HS_COLOR in kwargs and \
           self._topic[CONF_RGB_COMMAND_TOPIC] is not None:

            hs_color = kwargs[ATTR_HS_COLOR]

            # If there's a brightness topic set, we don't want to scale the RGB
            # values given using the brightness.
            if self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is not None:
                brightness = 255
            else:
                brightness = kwargs.get(
                    ATTR_BRIGHTNESS, self._brightness if self._brightness else
                    255)
            rgb = color_util.color_hsv_to_RGB(
                hs_color[0], hs_color[1], brightness / 255 * 100)
            tpl = self._templates[CONF_RGB_COMMAND_TEMPLATE]
            if tpl:
                rgb_color_str = tpl.async_render({
                    'red': rgb[0],
                    'green': rgb[1],
                    'blue': rgb[2],
                })
            else:
                rgb_color_str = '{},{},{}'.format(*rgb)

            mqtt.async_publish(
                self.hass, self._topic[CONF_RGB_COMMAND_TOPIC],
                rgb_color_str, self._config[CONF_QOS],
                self._config[CONF_RETAIN])

            if self._optimistic_rgb:
                self._hs = kwargs[ATTR_HS_COLOR]
                should_update = True

        if ATTR_HS_COLOR in kwargs and \
           self._topic[CONF_HS_COMMAND_TOPIC] is not None:

            hs_color = kwargs[ATTR_HS_COLOR]
            mqtt.async_publish(
                self.hass, self._topic[CONF_HS_COMMAND_TOPIC],
                '{},{}'.format(*hs_color), self._config[CONF_QOS],
                self._config[CONF_RETAIN])

            if self._optimistic_hs:
                self._hs = kwargs[ATTR_HS_COLOR]
                should_update = True

        if ATTR_HS_COLOR in kwargs and \
           self._topic[CONF_XY_COMMAND_TOPIC] is not None:

            xy_color = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
            mqtt.async_publish(
                self.hass, self._topic[CONF_XY_COMMAND_TOPIC],
                '{},{}'.format(*xy_color), self._config[CONF_QOS],
                self._config[CONF_RETAIN])

            if self._optimistic_xy:
                self._hs = kwargs[ATTR_HS_COLOR]
                should_update = True

        if ATTR_BRIGHTNESS in kwargs and \
           self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is not None:
            percent_bright = float(kwargs[ATTR_BRIGHTNESS]) / 255
            brightness_scale = self._config[CONF_BRIGHTNESS_SCALE]
            device_brightness = \
                min(round(percent_bright * brightness_scale), brightness_scale)
            mqtt.async_publish(
                self.hass, self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC],
                device_brightness, self._config[CONF_QOS],
                self._config[CONF_RETAIN])

            if self._optimistic_brightness:
                self._brightness = kwargs[ATTR_BRIGHTNESS]
                should_update = True
        elif ATTR_BRIGHTNESS in kwargs and ATTR_HS_COLOR not in kwargs and\
                self._topic[CONF_RGB_COMMAND_TOPIC] is not None:
            rgb = color_util.color_hsv_to_RGB(
                self._hs[0], self._hs[1], kwargs[ATTR_BRIGHTNESS] / 255 * 100)
            tpl = self._templates[CONF_RGB_COMMAND_TEMPLATE]
            if tpl:
                rgb_color_str = tpl.async_render({
                    'red': rgb[0],
                    'green': rgb[1],
                    'blue': rgb[2],
                })
            else:
                rgb_color_str = '{},{},{}'.format(*rgb)

            mqtt.async_publish(
                self.hass, self._topic[CONF_RGB_COMMAND_TOPIC],
                rgb_color_str, self._config[CONF_QOS],
                self._config[CONF_RETAIN])

            if self._optimistic_brightness:
                self._brightness = kwargs[ATTR_BRIGHTNESS]
                should_update = True

        if ATTR_COLOR_TEMP in kwargs and \
           self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC] is not None:
            color_temp = int(kwargs[ATTR_COLOR_TEMP])
            tpl = self._templates[CONF_COLOR_TEMP_COMMAND_TEMPLATE]

            if tpl:
                color_temp = tpl.async_render({
                    'value': color_temp,
                })

            mqtt.async_publish(
                self.hass, self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC],
                color_temp, self._config[CONF_QOS],
                self._config[CONF_RETAIN])

            if self._optimistic_color_temp:
                self._color_temp = kwargs[ATTR_COLOR_TEMP]
                should_update = True

        if ATTR_EFFECT in kwargs and \
           self._topic[CONF_EFFECT_COMMAND_TOPIC] is not None:
            effect = kwargs[ATTR_EFFECT]
            if effect in self._config.get(CONF_EFFECT_LIST):
                mqtt.async_publish(
                    self.hass, self._topic[CONF_EFFECT_COMMAND_TOPIC],
                    effect, self._config[CONF_QOS],
                    self._config[CONF_RETAIN])

                if self._optimistic_effect:
                    self._effect = kwargs[ATTR_EFFECT]
                    should_update = True

        if ATTR_WHITE_VALUE in kwargs and \
           self._topic[CONF_WHITE_VALUE_COMMAND_TOPIC] is not None:
            percent_white = float(kwargs[ATTR_WHITE_VALUE]) / 255
            white_scale = self._config[CONF_WHITE_VALUE_SCALE]
            device_white_value = \
                min(round(percent_white * white_scale), white_scale)
            mqtt.async_publish(
                self.hass, self._topic[CONF_WHITE_VALUE_COMMAND_TOPIC],
                device_white_value, self._config[CONF_QOS],
                self._config[CONF_RETAIN])

            if self._optimistic_white_value:
                self._white_value = kwargs[ATTR_WHITE_VALUE]
                should_update = True

        if on_command_type == 'last':
            mqtt.async_publish(self.hass, self._topic[CONF_COMMAND_TOPIC],
                               self._payload['on'], self._config[CONF_QOS],
                               self._config[CONF_RETAIN])
            should_update = True

        if self._optimistic:
            # Optimistically assume that the light has changed state.
            self._state = True
            should_update = True

        if should_update:
            self.async_write_ha_state()
Ejemplo n.º 44
0
    async def async_turn_on(self, **kwargs):
        """Turn the entity on."""
        from zigpy.exceptions import DeliveryError

        duration = kwargs.get(light.ATTR_TRANSITION, DEFAULT_DURATION)
        duration = duration * 10  # tenths of s
        if light.ATTR_COLOR_TEMP in kwargs and \
                self.supported_features & light.SUPPORT_COLOR_TEMP:
            temperature = kwargs[light.ATTR_COLOR_TEMP]
            try:
                res = await self._endpoint.light_color.move_to_color_temp(
                    temperature, duration)
                _LOGGER.debug("%s: moved to %i color temp: %s",
                              self.entity_id, temperature, res)
            except DeliveryError as ex:
                _LOGGER.error("%s: Couldn't change color temp: %s",
                              self.entity_id, ex)
                return
            self._color_temp = temperature

        if light.ATTR_HS_COLOR in kwargs and \
                self.supported_features & light.SUPPORT_COLOR:
            self._hs_color = kwargs[light.ATTR_HS_COLOR]
            xy_color = color_util.color_hs_to_xy(*self._hs_color)
            try:
                res = await self._endpoint.light_color.move_to_color(
                    int(xy_color[0] * 65535),
                    int(xy_color[1] * 65535),
                    duration,
                )
                _LOGGER.debug("%s: moved XY color to (%1.2f, %1.2f): %s",
                              self.entity_id, xy_color[0], xy_color[1], res)
            except DeliveryError as ex:
                _LOGGER.error("%s: Couldn't change color temp: %s",
                              self.entity_id, ex)
                return

        if self._brightness is not None:
            brightness = kwargs.get(
                light.ATTR_BRIGHTNESS, self._brightness or 255)
            # Move to level with on/off:
            try:
                res = await self._endpoint.level.move_to_level_with_on_off(
                    brightness,
                    duration
                )
                _LOGGER.debug("%s: moved to %i level with on/off: %s",
                              self.entity_id, brightness, res)
            except DeliveryError as ex:
                _LOGGER.error("%s: Couldn't change brightness level: %s",
                              self.entity_id, ex)
                return
            self._state = 1
            self._brightness = brightness
            self.async_schedule_update_ha_state()
            return

        try:
            res = await self._endpoint.on_off.on()
            _LOGGER.debug("%s was turned on: %s", self.entity_id, res)
        except DeliveryError as ex:
            _LOGGER.error("%s: Unable to turn the light on: %s",
                          self.entity_id, ex)
            return

        self._state = 1
        self.async_schedule_update_ha_state()