Ejemplo n.º 1
0
    def _transition(self, duration, brightness):
        """ Complete a transition.

        :param duration: Duration of transition.
        :param brightness: Transition to this brightness.
        """
        # Set initial value.
        b_start = self.brightness
        # Compute ideal step amount.
        b_steps = 0
        if brightness is not None:
            b_steps = steps(self.brightness, brightness,
                            self.command_set.brightness_steps)
        # Compute ideal step amount (at least one).
        # Calculate wait.
        wait = self._wait(duration, b_steps, b_steps)
        # Scale down steps if no wait time.
        if wait == 0:
            b_steps = self._scale_steps(duration, b_steps, b_steps)
        # Perform transition.
        for i in range(b_steps):
            # Brightness.
            if b_steps > 0:
                self.brightness = util.transition(i, b_steps, b_start,
                                                  brightness)
            time.sleep(wait)
Ejemplo n.º 2
0
 def test_steps(self):
     self.assertEqual(util.steps(0, 1.0, 100), 100)
     self.assertEqual(util.steps(0.5, 1.0, 100), 50)
     self.assertEqual(util.steps(1.0, 1.0, 100), 0)
     with self.assertRaises(ValueError):
         util.steps(-1, 1.0, 100)
     with self.assertRaises(ValueError):
         util.steps(0, 2.0, 100)
Ejemplo n.º 3
0
    def _transition(self, duration, hue=None, brightness=None):
        """ Transition.

        :param duration: Time to transition.
        :param hue: Transition to this hue.
        :param brightness: Transition to this brightness.
        """
        # Calculate brightness steps.
        b_steps = 0
        if brightness is not None:
            b_steps = steps(self.brightness,
                            brightness, self.command_set.brightness_steps)
            b_start = self.brightness
        # Calculate hue steps.
        h_steps = 0
        if hue is not None:
            h_steps = steps(self.hue,
                            hue, self.command_set.hue_steps)
            h_start = self.hue
        # Compute ideal step amount (at least one).
        total_steps = max(b_steps, h_steps, 1)
        total_commands = b_steps + h_steps
        # Calculate wait.
        wait = self._wait(duration, total_steps, total_commands)
        # Scale down steps if no wait time.
        if wait == 0:
            b_steps, h_steps = self._scale_steps(duration, total_commands,
                                                 b_steps, h_steps)
            total_steps = max(b_steps, h_steps, 1)
        # Perform transition.
        for i in range(total_steps):
            # Brightness.
            if b_steps > 0 and i % math.ceil(total_steps/b_steps) == 0:
                self.brightness = util.transition(i, total_steps,
                                                  b_start, brightness)
            # Hue.
            if h_steps > 0 and i % math.ceil(total_steps/h_steps) == 0:
                self.hue = util.transition(i, total_steps,
                                           h_start, hue)
            # Wait.
            time.sleep(wait)
Ejemplo n.º 4
0
    def _transition(self, duration, brightness, temperature):
        """ Complete a transition.

        :param duration: Duration of transition.
        :param brightness: Transition to this brightness.
        :param temperature: Transition to this temperature.
        """
        # Set initial value.
        b_start = self.brightness
        t_start = self.temperature
        # Compute ideal step amount.
        b_steps = 0
        if brightness is not None:
            b_steps = steps(self.brightness, brightness,
                            self.command_set.brightness_steps)
        t_steps = 0
        if temperature is not None:
            t_steps = steps(self.temperature, temperature,
                            self.command_set.temperature_steps)
        # Compute ideal step amount (at least one).
        total_steps = max(b_steps, t_steps, 1)
        total_commands = b_steps + t_steps
        # Calculate wait.
        wait = self._wait(duration, total_steps, total_commands)
        # Scale down steps if no wait time.
        if wait == 0:
            b_steps, t_steps = self._scale_steps(duration, total_commands,
                                                 b_steps, t_steps)
            total_steps = max(b_steps, t_steps, 1)
        # Perform transition.
        for i in range(total_steps):
            # Brightness.
            if b_steps > 0 and i % (total_steps / b_steps) == 0:
                self.brightness = util.transition(i, total_steps, b_start,
                                                  brightness)
            # Temperature.
            elif t_steps > 0:
                self.temperature = util.transition(i, total_steps, t_start,
                                                   temperature)
            # Wait.
            time.sleep(wait)
Ejemplo n.º 5
0
    def _to_value(self, current, target, step_down, step_up):
        """ Step to a value

        :param current: Current value.
        :param target: Target value.
        :param step_down: Down function.
        :param step_up: Up function.
        """
        for _ in range(steps(current, target, STEPS)):
            if (current - target) > 0:
                step_down()
            else:
                step_up()
Ejemplo n.º 6
0
    def _transition(self, duration, brightness, temperature):
        """ Complete a transition.

        :param duration: Duration of transition.
        :param brightness: Transition to this brightness.
        :param temperature: Transition to this temperature.
        """
        # Set initial value.
        b_start = self.brightness
        t_start = self.temperature
        # Compute ideal step amount.
        b_steps = 0
        if brightness is not None:
            b_steps = steps(self.brightness, brightness, STEPS)
        t_steps = 0
        if temperature is not None:
            t_steps = steps(self.temperature, temperature, STEPS)
        total = b_steps + t_steps
        # Compute wait.
        wait = self._wait(duration, total)
        # Scale down steps if no wait time.
        if wait == 0:
            b_steps = self._scaled_steps(duration, b_steps, total)
            t_steps = self._scaled_steps(duration, t_steps, total)
            total = b_steps + t_steps
        # Perform transition.
        j = 0
        for i in range(total):
            # Brightness.
            if b_steps > 0 and i % (total / b_steps) == 0:
                j += 1
                self.brightness = util.transition(j, b_steps, b_start,
                                                  brightness)
            # Temperature.
            elif t_steps > 0:
                self.temperature = util.transition(i - j + 1, t_steps, t_start,
                                                   temperature)
            # Wait.
            time.sleep(wait)
Ejemplo n.º 7
0
    def _to_value(self, current, target, max_steps, step_down, step_up):
        """ Step to a value

        :param current: Current value.
        :param target: Target value.
        :param max_steps: Maximum number of steps.
        :param step_down: Down function.
        :param step_up: Up function.
        """
        for _ in range(steps(current, target, max_steps)):
            if (current - target) > 0:
                step_down()
            else:
                step_up()
Ejemplo n.º 8
0
    def _transition(self, duration, color, brightness):
        """ Transition.

        :param duration: Time to transition.
        :param color: Transition to this color.
        :param brightness: Transition to this brightness.
        """
        # Calculate brightness steps.
        b_steps = 0
        if brightness is not None:
            b_steps = steps(self.brightness,
                            brightness, BRIGHTNESS_STEPS)
        # Calculate color steps.
        c_steps = 0
        if color is not None:
            c_steps = abs(util.rgb_to_hue(*self.color)
                          - util.rgb_to_hue(*color))
        # Compute ideal step amount.
        total = c_steps + b_steps
        # Calculate wait.
        wait = self._wait(duration, total)
        # Scale down steps if no wait time.
        if wait == 0:
            total = self._scaled_steps(duration, total, total)
        # Calculate start and end.
        if total != b_steps:
            c_start = rgb_to_hsv(*self._color)
            c_end = rgb_to_hsv(*color)
        b_start = self.brightness
        # Perform transition.
        j = 0
        for i in range(total):
            # Brightness.
            if (b_steps > 0
                    and i % math.ceil(total/b_steps) == 0):
                j += 1
                self.brightness = util.transition(j, b_steps,
                                                  b_start, brightness)
            # Color.
            elif c_steps > 0:
                rgb = hsv_to_rgb(*util.transition3(i - j + 1,
                                                   total - b_steps,
                                                   c_start, c_end))
                self.color = Color(*rgb)
            # Wait.
            time.sleep(wait)
Ejemplo n.º 9
0
    def _transition(self, duration, color, brightness):
        """ Transition.

        :param duration: Time to transition.
        :param color: Transition to this color.
        :param brightness: Transition to this brightness.
        """
        # Calculate brightness steps.
        b_steps = 0
        if brightness is not None:
            b_steps = steps(self.brightness, brightness, BRIGHTNESS_STEPS)
            b_start = self.brightness
        # Calculate color steps.
        c_steps = 0
        if color is not None:
            c_steps = abs(
                util.rgb_to_hue(*self.color) - util.rgb_to_hue(*color))
            c_start = rgb_to_hsv(*self._color)
            c_end = rgb_to_hsv(*color)
        # Compute ideal step amount (at least one).
        total = max(c_steps + b_steps, 1)
        # Calculate wait.
        wait = self._wait(duration, total)
        # Scale down steps if no wait time.
        if wait == 0:
            total = self._scaled_steps(duration, total, total)
        # Perform transition.
        j = 0
        for i in range(total):
            # Brightness.
            if (b_steps > 0 and i % math.ceil(total / b_steps) == 0):
                j += 1
                self.brightness = util.transition(j, b_steps, b_start,
                                                  brightness)
            # Color.
            elif c_steps > 0:
                rgb = hsv_to_rgb(*util.transition3(i - j + 1, total -
                                                   b_steps, c_start, c_end))
                self.color = Color(*rgb)
            # Wait.
            time.sleep(wait)
Ejemplo n.º 10
0
 def _coolest(self):
     """ Group temperature as cool as possible. """
     for _ in range(
             steps(self.temperature, 1.0,
                   self.command_set.temperature_steps)):
         self._cooler()
Ejemplo n.º 11
0
 def _warmest(self):
     """ Group temperature as warm as possible. """
     for _ in range(
             steps(self.temperature, 0.0,
                   self.command_set.temperature_steps)):
         self._warmer()
Ejemplo n.º 12
0
 def _dimmest(self):
     """ Group brightness as dim as possible. """
     for _ in range(
             steps(self.brightness, 0.0,
                   self.command_set.brightness_steps)):
         self._dimmer()
Ejemplo n.º 13
0
 def _brightest(self):
     """ Group as bright as possible. """
     for _ in range(
             steps(self.brightness, 1.0,
                   self.command_set.brightness_steps)):
         self._brighter()
Ejemplo n.º 14
0
 def _coolest(self):
     """ Group temperature as cool as possible. """
     for _ in range(steps(self.temperature, 0.0, STEPS)):
         self._cooler()
Ejemplo n.º 15
0
 def _dimmest(self):
     """ Group brightness as dim as possible. """
     for _ in range(steps(self.brightness, 0.0, STEPS)):
         self._dimmer()
Ejemplo n.º 16
0
 def _brightest(self):
     """ Group as bright as possible. """
     for _ in range(steps(self.brightness, 1.0, STEPS)):
         self._brighter()
Ejemplo n.º 17
0
    def _transition(self,
                    duration,
                    brightness,
                    hue=None,
                    saturation=None,
                    temperature=None):
        """ Transition.

        :param duration: Time to transition.
        :param brightness: Transition to this brightness.
        :param hue: Transition to this hue.
        :param saturation: Transition to this saturation.
        :param temperature: Transition to this temperature.
        """
        # Calculate brightness steps.
        b_steps = 0
        if brightness is not None:
            b_steps = steps(self.brightness, brightness,
                            self.command_set.brightness_steps)
            b_start = self.brightness
        # Calculate hue steps.
        h_steps = 0
        if hue is not None:
            h_steps = steps(self.hue, hue, self.command_set.hue_steps)
            h_start = self.hue
        # Calculate saturation steps.
        s_steps = 0
        if saturation is not None:
            s_steps = steps(self.saturation, saturation,
                            self.command_set.saturation_steps)
            s_start = self.saturation
        # Calculate temperature steps.
        t_steps = 0
        if temperature is not None:
            t_steps = steps(self.temperature, temperature,
                            self.command_set.temperature_steps)
            t_start = self.temperature
        # Compute ideal step amount (at least one).
        total = max(b_steps + h_steps + s_steps + t_steps, 1)
        # Calculate wait.
        wait = self._wait(duration, total)
        # Scale down steps if no wait time.
        if wait == 0:
            total = self._scaled_steps(duration, total, total)
        # Perform transition.
        b_iteration = h_iteration = s_iteration = t_iteration = 0
        for i in range(total):
            # Brightness.
            if b_steps > 0 and i % math.ceil(total / b_steps) == 0:
                b_iteration += 1
                self.brightness = util.transition(b_iteration, b_steps,
                                                  b_start, brightness)
            # Hue.
            if h_steps > 0 and i % math.ceil(total / h_steps) == 0:
                h_iteration += 1
                self.hue = util.transition(h_iteration, h_steps, h_start, hue)
            # Saturation.
            if s_steps > 0 and i % math.ceil(total / s_steps) == 0:
                s_iteration += 1
                self.saturation = util.transition(s_iteration, s_steps,
                                                  s_start, saturation)
            # Temperature.
            if t_steps > 0 and i % math.ceil(total / t_steps) == 0:
                t_iteration += 1
                self.temperature = util.transition(t_iteration, t_steps,
                                                   t_start, temperature)

            # Wait.
            time.sleep(wait)
Ejemplo n.º 18
0
 def _warmest(self):
     """ Group temperature as warm as possible. """
     for _ in range(steps(self.temperature, 1.0, STEPS)):
         self._warmer()