| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import datetime |
| 4 |
|
| 5 |
from .pattern import PresetPattern |
| 6 |
from .utils import utils |
| 7 |
|
| 8 |
|
| 9 |
class BuiltInTimer: |
| 10 |
sunrise = 0xA1 |
| 11 |
sunset = 0xA2 |
| 12 |
|
| 13 |
@staticmethod |
| 14 |
def valid(byte_value: int) -> bool: |
| 15 |
return byte_value == BuiltInTimer.sunrise or byte_value == BuiltInTimer.sunset |
| 16 |
|
| 17 |
@staticmethod |
| 18 |
def valtostr(pattern: int) -> str: |
| 19 |
for key, value in list(BuiltInTimer.__dict__.items()): |
| 20 |
if type(value) is int and value == pattern: |
| 21 |
return key.replace("_", " ").title() |
| 22 |
raise ValueError(f"{pattern} must be 0xA1 or 0xA2") |
| 23 |
|
| 24 |
|
| 25 |
class LedTimer: |
| 26 |
Mo = 0x02 |
| 27 |
Tu = 0x04 |
| 28 |
We = 0x08 |
| 29 |
Th = 0x10 |
| 30 |
Fr = 0x20 |
| 31 |
Sa = 0x40 |
| 32 |
Su = 0x80 |
| 33 |
Everyday = Mo | Tu | We | Th | Fr | Sa | Su |
| 34 |
Weekdays = Mo | Tu | We | Th | Fr |
| 35 |
Weekend = Sa | Su |
| 36 |
|
| 37 |
@staticmethod |
| 38 |
def dayMaskToStr(mask: int) -> str: |
| 39 |
for key, value in LedTimer.__dict__.items(): |
| 40 |
if type(value) is int and value == mask: |
| 41 |
return key |
| 42 |
raise ValueError( |
| 43 |
f"{mask} must be one of 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80" |
| 44 |
) |
| 45 |
|
| 46 |
def __init__( |
| 47 |
self, bytes: bytes | bytearray | None = None, length: int = 14 |
| 48 |
) -> None: |
| 49 |
self.cold_level = 0 |
| 50 |
self.pattern_code = 0 |
| 51 |
self.delay = 0 |
| 52 |
if bytes is not None: |
| 53 |
self.length = len(bytes) |
| 54 |
self.fromBytes(bytes) |
| 55 |
return |
| 56 |
|
| 57 |
self.length = length |
| 58 |
the_time = datetime.datetime.now() + datetime.timedelta(hours=1) |
| 59 |
self.setTime(the_time.hour, the_time.minute) |
| 60 |
self.setDate(the_time.year, the_time.month, the_time.day) |
| 61 |
self.setModeTurnOff() |
| 62 |
self.setActive(False) |
| 63 |
|
| 64 |
def setActive(self, active: bool = True) -> None: |
| 65 |
self.active = active |
| 66 |
|
| 67 |
def isActive(self) -> bool: |
| 68 |
return self.active |
| 69 |
|
| 70 |
def isExpired(self) -> bool: |
| 71 |
|
| 72 |
if self.repeat_mask != 0: |
| 73 |
return False |
| 74 |
if self.year != 0 and self.month != 0 and self.day != 0: |
| 75 |
dt = datetime.datetime( |
| 76 |
self.year, self.month, self.day, self.hour, self.minute |
| 77 |
) |
| 78 |
if utils.date_has_passed(dt): |
| 79 |
return True |
| 80 |
return False |
| 81 |
|
| 82 |
def setTime(self, hour: int, minute: int) -> None: |
| 83 |
self.hour = hour |
| 84 |
self.minute = minute |
| 85 |
|
| 86 |
def setDate(self, year: int, month: int, day: int) -> None: |
| 87 |
self.year = year |
| 88 |
self.month = month |
| 89 |
self.day = day |
| 90 |
self.repeat_mask = 0 |
| 91 |
|
| 92 |
def setRepeatMask(self, repeat_mask: int) -> None: |
| 93 |
self.year = 0 |
| 94 |
self.month = 0 |
| 95 |
self.day = 0 |
| 96 |
self.repeat_mask = repeat_mask |
| 97 |
|
| 98 |
def setModeDefault(self) -> None: |
| 99 |
self.mode = "default" |
| 100 |
self.pattern_code = 0 |
| 101 |
self.turn_on = True |
| 102 |
self.red = 0 |
| 103 |
self.green = 0 |
| 104 |
self.blue = 0 |
| 105 |
self.warmth_level = 0 |
| 106 |
self.cold_level = 0 |
| 107 |
|
| 108 |
def setModePresetPattern(self, pattern: int, speed: int) -> None: |
| 109 |
self.mode = "preset" |
| 110 |
self.warmth_level = 0 |
| 111 |
self.cold_level = 0 |
| 112 |
self.pattern_code = pattern |
| 113 |
self.delay = utils.speedToDelay(speed) |
| 114 |
self.turn_on = True |
| 115 |
|
| 116 |
def setModeColor(self, r: int, g: int, b: int) -> None: |
| 117 |
self.mode = "color" |
| 118 |
self.warmth_level = 0 |
| 119 |
self.cold_level = 0 |
| 120 |
self.red = r |
| 121 |
self.green = g |
| 122 |
self.blue = b |
| 123 |
self.pattern_code = 0x61 |
| 124 |
self.turn_on = True |
| 125 |
|
| 126 |
def setModeWarmWhite(self, level: int) -> None: |
| 127 |
self.mode = "ww" |
| 128 |
self.warmth_level = utils.percentToByte(level) |
| 129 |
self.cold_level = 0 |
| 130 |
self.pattern_code = 0x61 |
| 131 |
self.red = 0 |
| 132 |
self.green = 0 |
| 133 |
self.blue = 0 |
| 134 |
self.turn_on = True |
| 135 |
|
| 136 |
def setModeSunrise( |
| 137 |
self, startBrightness: int, endBrightness: int, duration: int |
| 138 |
) -> None: |
| 139 |
self.mode = "sunrise" |
| 140 |
self.turn_on = True |
| 141 |
self.pattern_code = BuiltInTimer.sunrise |
| 142 |
self.brightness_start = utils.percentToByte(startBrightness) |
| 143 |
self.brightness_end = utils.percentToByte(endBrightness) |
| 144 |
self.warmth_level = utils.percentToByte(endBrightness) |
| 145 |
self.cold_level = 0 |
| 146 |
self.duration = int(duration) |
| 147 |
|
| 148 |
def setModeSunset( |
| 149 |
self, startBrightness: int, endBrightness: int, duration: int |
| 150 |
) -> None: |
| 151 |
self.mode = "sunrise" |
| 152 |
self.turn_on = True |
| 153 |
self.pattern_code = BuiltInTimer.sunset |
| 154 |
self.brightness_start = utils.percentToByte(startBrightness) |
| 155 |
self.brightness_end = utils.percentToByte(endBrightness) |
| 156 |
self.warmth_level = utils.percentToByte(endBrightness) |
| 157 |
self.cold_level = 0 |
| 158 |
self.duration = int(duration) |
| 159 |
|
| 160 |
def setModeTurnOff(self) -> None: |
| 161 |
self.mode = "off" |
| 162 |
self.turn_on = False |
| 163 |
self.pattern_code = 0 |
| 164 |
|
| 165 |
""" |
| 166 |
|
| 167 |
timer are in six 14-byte structs |
| 168 |
f0 0f 08 10 10 15 00 00 25 1f 00 00 00 f0 0f |
| 169 |
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
| 170 |
|
| 171 |
0: f0 when active entry/ 0f when not active |
| 172 |
1: (0f=15) year when no repeat, else 0 |
| 173 |
2: month when no repeat, else 0 |
| 174 |
3: dayofmonth when no repeat, else 0 |
| 175 |
4: hour |
| 176 |
5: min |
| 177 |
6: 0 |
| 178 |
7: repeat mask, Mo=0x2,Tu=0x04, We 0x8, Th=0x10 Fr=0x20, Sa=0x40, Su=0x80 |
| 179 |
8: 61 for solid color or warm, or preset pattern code |
| 180 |
9: r (or delay for preset pattern) |
| 181 |
10: g |
| 182 |
11: b |
| 183 |
12: warm white level |
| 184 |
13: 0f = turn off, f0 = turn on |
| 185 |
|
| 186 |
timer are in six 15-byte structs for 9 byte devices |
| 187 |
f0 0f 08 10 10 15 00 00 25 1f 00 00 00 f0 0f |
| 188 |
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
| 189 |
|
| 190 |
0: f0 when active entry/ 0f when not active |
| 191 |
1: (0f=15) year when no repeat, else 0 |
| 192 |
2: month when no repeat, else 0 |
| 193 |
3: dayofmonth when no repeat, else 0 |
| 194 |
4: hour |
| 195 |
5: min |
| 196 |
6: 0 |
| 197 |
7: repeat mask, Mo=0x2,Tu=0x04, We 0x8, Th=0x10 Fr=0x20, Sa=0x40, Su=0x80 |
| 198 |
8: 61 for solid color or warm, or preset pattern code |
| 199 |
9: r (or delay for preset pattern) |
| 200 |
10: g |
| 201 |
11: b |
| 202 |
12: warm white level |
| 203 |
13: cold white level |
| 204 |
14: 0f = turn off, f0 = turn on |
| 205 |
""" |
| 206 |
|
| 207 |
def fromBytes(self, bytes: bytes | bytearray) -> None: |
| 208 |
self.red = 0 |
| 209 |
self.green = 0 |
| 210 |
self.blue = 0 |
| 211 |
if bytes[0] == 0xF0: |
| 212 |
self.active = True |
| 213 |
else: |
| 214 |
self.active = False |
| 215 |
self.year = bytes[1] + 2000 |
| 216 |
self.month = bytes[2] |
| 217 |
self.day = bytes[3] |
| 218 |
self.hour = bytes[4] |
| 219 |
self.minute = bytes[5] |
| 220 |
self.repeat_mask = bytes[7] |
| 221 |
|
| 222 |
if len(bytes) == 12: |
| 223 |
if bytes[8] == 0x23: |
| 224 |
self.turn_on = True |
| 225 |
else: |
| 226 |
self.turn_on = False |
| 227 |
self.mode = "off" |
| 228 |
return |
| 229 |
|
| 230 |
self.pattern_code = bytes[8] |
| 231 |
if self.pattern_code == 0x00: |
| 232 |
self.mode = "default" |
| 233 |
elif self.pattern_code == 0x61: |
| 234 |
self.mode = "color" |
| 235 |
self.red = bytes[9] |
| 236 |
self.green = bytes[10] |
| 237 |
self.blue = bytes[11] |
| 238 |
elif BuiltInTimer.valid(self.pattern_code): |
| 239 |
self.mode = BuiltInTimer.valtostr(self.pattern_code) |
| 240 |
self.duration = bytes[9] |
| 241 |
self.brightness_start = bytes[10] |
| 242 |
self.brightness_end = bytes[11] |
| 243 |
elif PresetPattern.valid(self.pattern_code): |
| 244 |
self.mode = "preset" |
| 245 |
self.delay = bytes[9] |
| 246 |
else: |
| 247 |
self.mode = "unknown" |
| 248 |
|
| 249 |
self.warmth_level = bytes[12] |
| 250 |
if self.warmth_level != 0: |
| 251 |
self.mode = "ww" |
| 252 |
|
| 253 |
if len(bytes) == 15: |
| 254 |
self.cold_level = bytes[13] |
| 255 |
on_byte = bytes[14] |
| 256 |
else: |
| 257 |
on_byte = bytes[13] |
| 258 |
|
| 259 |
if on_byte == 0xF0: |
| 260 |
self.turn_on = True |
| 261 |
else: |
| 262 |
self.turn_on = False |
| 263 |
self.mode = "off" |
| 264 |
|
| 265 |
def toBytes(self) -> bytearray: |
| 266 |
bytes = bytearray(self.length) |
| 267 |
if not self.active: |
| 268 |
bytes[0] = 0x0F |
| 269 |
|
| 270 |
return bytes |
| 271 |
|
| 272 |
bytes[0] = 0xF0 |
| 273 |
|
| 274 |
if self.year >= 2000: |
| 275 |
bytes[1] = self.year - 2000 |
| 276 |
else: |
| 277 |
bytes[1] = self.year |
| 278 |
bytes[2] = self.month |
| 279 |
bytes[3] = self.day |
| 280 |
bytes[4] = self.hour |
| 281 |
bytes[5] = self.minute |
| 282 |
|
| 283 |
bytes[7] = self.repeat_mask |
| 284 |
|
| 285 |
if self.length == 12: |
| 286 |
bytes[8] == 0x23 if self.turn_on else 0x24 |
| 287 |
return bytes |
| 288 |
|
| 289 |
on_byte_num = 14 if self.length == 15 else 13 |
| 290 |
if not self.turn_on: |
| 291 |
bytes[on_byte_num] = 0x0F |
| 292 |
return bytes |
| 293 |
bytes[on_byte_num] = 0xF0 |
| 294 |
|
| 295 |
bytes[8] = self.pattern_code |
| 296 |
if PresetPattern.valid(self.pattern_code): |
| 297 |
bytes[9] = self.delay |
| 298 |
bytes[10] = 0 |
| 299 |
bytes[11] = 0 |
| 300 |
elif BuiltInTimer.valid(self.pattern_code): |
| 301 |
bytes[9] = self.duration |
| 302 |
bytes[10] = self.brightness_start |
| 303 |
bytes[11] = self.brightness_end |
| 304 |
else: |
| 305 |
bytes[9] = self.red |
| 306 |
bytes[10] = self.green |
| 307 |
bytes[11] = self.blue |
| 308 |
bytes[12] = self.warmth_level |
| 309 |
if self.length == 15: |
| 310 |
bytes[13] = self.cold_level |
| 311 |
|
| 312 |
return bytes |
| 313 |
|
| 314 |
def __str__(self) -> str: |
| 315 |
txt = "" |
| 316 |
if not self.active: |
| 317 |
return "Unset" |
| 318 |
|
| 319 |
if self.turn_on: |
| 320 |
txt += "[ON ]" |
| 321 |
else: |
| 322 |
txt += "[OFF]" |
| 323 |
|
| 324 |
txt += " " |
| 325 |
|
| 326 |
txt += f"{self.hour:02}:{self.minute:02} " |
| 327 |
|
| 328 |
if self.repeat_mask == 0: |
| 329 |
txt += f"Once: {self.year:04}-{self.month:02}-{self.day:02}" |
| 330 |
else: |
| 331 |
bits = [ |
| 332 |
LedTimer.Su, |
| 333 |
LedTimer.Mo, |
| 334 |
LedTimer.Tu, |
| 335 |
LedTimer.We, |
| 336 |
LedTimer.Th, |
| 337 |
LedTimer.Fr, |
| 338 |
LedTimer.Sa, |
| 339 |
] |
| 340 |
for b in bits: |
| 341 |
if self.repeat_mask & b: |
| 342 |
txt += LedTimer.dayMaskToStr(b) |
| 343 |
else: |
| 344 |
txt += "--" |
| 345 |
txt += " " |
| 346 |
|
| 347 |
txt += " " |
| 348 |
if self.pattern_code == 0x61: |
| 349 |
if self.warmth_level != 0: |
| 350 |
txt += f"Warm White: {utils.byteToPercent(self.warmth_level)}%" |
| 351 |
else: |
| 352 |
color_str = utils.color_tuple_to_string( |
| 353 |
(self.red, self.green, self.blue) |
| 354 |
) |
| 355 |
txt += f"Color: {color_str}" |
| 356 |
|
| 357 |
elif PresetPattern.valid(self.pattern_code): |
| 358 |
pat = PresetPattern.valtostr(self.pattern_code) |
| 359 |
speed = utils.delayToSpeed(self.delay) |
| 360 |
txt += f"{pat} (Speed:{speed}%)" |
| 361 |
|
| 362 |
elif BuiltInTimer.valid(self.pattern_code): |
| 363 |
type = BuiltInTimer.valtostr(self.pattern_code) |
| 364 |
|
| 365 |
txt += f"{type} (Duration:{self.duration} minutes, Brightness: {utils.byteToPercent(self.brightness_start)}% -> {utils.byteToPercent(self.brightness_end)}%)" |
| 366 |
|
| 367 |
return txt |