|
| 1 | +""" Dimmer controller LimitlessLED group. """ |
| 2 | + |
| 3 | +import time |
| 4 | + |
| 5 | +from limitlessled import util |
| 6 | +from limitlessled.group import Group, rate |
| 7 | +from limitlessled.util import steps |
| 8 | + |
| 9 | +DIMMER = 'dimmer' |
| 10 | + |
| 11 | + |
| 12 | +class DimmerGroup(Group): |
| 13 | + """ Dimmer LimitlessLED group. """ |
| 14 | + |
| 15 | + def __init__(self, bridge, number, name): |
| 16 | + """ Initialize dimmer group. |
| 17 | +
|
| 18 | + Brightness must be initialized |
| 19 | + to some value. |
| 20 | +
|
| 21 | + :param bridge: Associated bridge. |
| 22 | + :param number: Group number (1-4). |
| 23 | + :param name: Group name. |
| 24 | + """ |
| 25 | + super().__init__(bridge, number, name, DIMMER) |
| 26 | + |
| 27 | + @property |
| 28 | + def brightness(self): |
| 29 | + """ Brightness property. |
| 30 | +
|
| 31 | + :returns: Brightness. |
| 32 | + """ |
| 33 | + return self._brightness |
| 34 | + |
| 35 | + @brightness.setter |
| 36 | + def brightness(self, brightness): |
| 37 | + """ Set the brightness. |
| 38 | +
|
| 39 | + :param brightness: Value to set (0.0-1.0). |
| 40 | + """ |
| 41 | + try: |
| 42 | + cmd = self.command_set.brightness(brightness) |
| 43 | + self.send(cmd) |
| 44 | + self._brightness = brightness |
| 45 | + except AttributeError: |
| 46 | + self._setter('_brightness', brightness, |
| 47 | + self._dimmest, self._brightest, |
| 48 | + self._to_brightness) |
| 49 | + |
| 50 | + def transition(self, duration, brightness=None): |
| 51 | + """ Transition wrapper. |
| 52 | +
|
| 53 | + Short-circuit transition if necessary. |
| 54 | +
|
| 55 | + :param duration: Duration of transition. |
| 56 | + :param brightness: Transition to this brightness. |
| 57 | + """ |
| 58 | + if duration == 0: |
| 59 | + if brightness is not None: |
| 60 | + self.brightness = brightness |
| 61 | + return |
| 62 | + if brightness != self.brightness: |
| 63 | + self._transition(duration, brightness) |
| 64 | + |
| 65 | + @rate(reps=1) |
| 66 | + def _transition(self, duration, brightness): |
| 67 | + """ Complete a transition. |
| 68 | +
|
| 69 | + :param duration: Duration of transition. |
| 70 | + :param brightness: Transition to this brightness. |
| 71 | + """ |
| 72 | + # Set initial value. |
| 73 | + b_start = self.brightness |
| 74 | + # Compute ideal step amount. |
| 75 | + b_steps = 0 |
| 76 | + if brightness is not None: |
| 77 | + b_steps = steps(self.brightness, brightness, |
| 78 | + self.command_set.brightness_steps) |
| 79 | + # Compute ideal step amount (at least one). |
| 80 | + # Calculate wait. |
| 81 | + wait = self._wait(duration, b_steps, b_steps) |
| 82 | + # Scale down steps if no wait time. |
| 83 | + if wait == 0: |
| 84 | + b_steps = self._scale_steps(duration, b_steps, |
| 85 | + b_steps) |
| 86 | + # Perform transition. |
| 87 | + for i in range(b_steps): |
| 88 | + # Brightness. |
| 89 | + if b_steps > 0: |
| 90 | + self.brightness = util.transition(i, b_steps, |
| 91 | + b_start, brightness) |
| 92 | + time.sleep(wait) |
| 93 | + |
| 94 | + def _setter(self, attr, value, bottom, top, to_step): |
| 95 | + """ Set a value. |
| 96 | +
|
| 97 | + :param attr: Attribute to set. |
| 98 | + :param value: Value to use. |
| 99 | + :param bottom: Get to bottom value. |
| 100 | + :param top: Get to top value. |
| 101 | + :param to_step: Get to intermediary value. |
| 102 | + """ |
| 103 | + if value < 0 or value > 1: |
| 104 | + raise ValueError("out of range") |
| 105 | + if value == 0.0: |
| 106 | + bottom() |
| 107 | + elif value == 1.0: |
| 108 | + top() |
| 109 | + else: |
| 110 | + to_step(value) |
| 111 | + setattr(self, attr, value) |
| 112 | + |
| 113 | + def _to_brightness(self, brightness): |
| 114 | + """ Step to a given brightness. |
| 115 | +
|
| 116 | + :param brightness: Get to this brightness. |
| 117 | + """ |
| 118 | + self._to_value(self._brightness, brightness, |
| 119 | + self.command_set.brightness_steps, |
| 120 | + self._dimmer, self._brighter) |
| 121 | + |
| 122 | + @rate(reps=1) |
| 123 | + def _to_value(self, current, target, max_steps, step_down, step_up): |
| 124 | + """ Step to a value |
| 125 | +
|
| 126 | + :param current: Current value. |
| 127 | + :param target: Target value. |
| 128 | + :param max_steps: Maximum number of steps. |
| 129 | + :param step_down: Down function. |
| 130 | + :param step_up: Up function. |
| 131 | + """ |
| 132 | + for _ in range(steps(current, target, max_steps)): |
| 133 | + if (current - target) > 0: |
| 134 | + step_down() |
| 135 | + else: |
| 136 | + step_up() |
| 137 | + |
| 138 | + @rate(wait=0.025, reps=2) |
| 139 | + def _brightest(self): |
| 140 | + """ Group as bright as possible. """ |
| 141 | + for _ in range(steps(self.brightness, 1.0, |
| 142 | + self.command_set.brightness_steps)): |
| 143 | + self._brighter() |
| 144 | + |
| 145 | + @rate(wait=0.025, reps=2) |
| 146 | + def _dimmest(self): |
| 147 | + """ Group brightness as dim as possible. """ |
| 148 | + for _ in range(steps(self.brightness, 0.0, |
| 149 | + self.command_set.brightness_steps)): |
| 150 | + self._dimmer() |
| 151 | + |
| 152 | + def _brighter(self): |
| 153 | + """ One step brighter. """ |
| 154 | + self.send(self.command_set.brighter()) |
| 155 | + |
| 156 | + def _dimmer(self): |
| 157 | + """ One step dimmer. """ |
| 158 | + self.send(self.command_set.dimmer()) |
0 commit comments