|
4 | 4 | import logging |
5 | 5 | import time |
6 | 6 | import json |
| 7 | +import colorsys |
| 8 | +from dataclasses import dataclass, field, InitVar |
| 9 | +from typing import NamedTuple, Optional, Union |
7 | 10 | import requests |
8 | 11 |
|
| 12 | + |
9 | 13 | logger = logging.getLogger(__name__) |
10 | 14 |
|
11 | 15 | API_BASE_URL = 'https://smartapi.vesync.com' |
|
22 | 26 | USER_TYPE = '1' |
23 | 27 | BYPASS_APP_V = "VeSync 3.0.51" |
24 | 28 |
|
| 29 | +NUMERIC = Optional[Union[int, float, str]] |
| 30 | + |
25 | 31 |
|
26 | 32 | class Helpers: |
27 | 33 | """VeSync Helper Functions.""" |
@@ -276,3 +282,128 @@ def bypass_header(): |
276 | 282 | 'Content-Type': 'application/json; charset=UTF-8', |
277 | 283 | 'User-Agent': 'okhttp/3.12.1', |
278 | 284 | } |
| 285 | + |
| 286 | + @staticmethod |
| 287 | + def named_tuple_to_str(named_tuple: NamedTuple) -> str: |
| 288 | + """Convert named tuple to string.""" |
| 289 | + tuple_str = '' |
| 290 | + for key, val in named_tuple._asdict().items(): |
| 291 | + tuple_str += f'{key}: {val}, ' |
| 292 | + return tuple_str |
| 293 | + |
| 294 | + |
| 295 | +class HSV(NamedTuple): |
| 296 | + """HSV color space.""" |
| 297 | + |
| 298 | + hue: float |
| 299 | + saturation: float |
| 300 | + value: float |
| 301 | + |
| 302 | + |
| 303 | +class RGB(NamedTuple): |
| 304 | + """RGB color space.""" |
| 305 | + |
| 306 | + red: float |
| 307 | + green: float |
| 308 | + blue: float |
| 309 | + |
| 310 | + |
| 311 | +@dataclass |
| 312 | +class Color: |
| 313 | + """Dataclass for color values. |
| 314 | +
|
| 315 | + For HSV, pass hue as value in degrees 0-360, saturation and value as values |
| 316 | + between 0 and 100. |
| 317 | +
|
| 318 | + For RGB, pass red, green and blue as values between 0 and 255. |
| 319 | +
|
| 320 | + To instantiate pass kw arguments for colors hue, saturation and value or |
| 321 | + red, green and blue. |
| 322 | +
|
| 323 | + Instance attributes are: |
| 324 | + hsv (nameduple) : hue (0-360), saturation (0-100), value (0-100) |
| 325 | +
|
| 326 | + rgb (namedtuple) : red (0-255), green (0-255), blue |
| 327 | +
|
| 328 | + """ |
| 329 | + |
| 330 | + red: InitVar[NUMERIC] = field(default=None, repr=False, compare=False) |
| 331 | + green: InitVar[NUMERIC] = field(default=None, repr=False, compare=False) |
| 332 | + blue: InitVar[NUMERIC] = field(default=None, repr=False, compare=False) |
| 333 | + hue: InitVar[NUMERIC] = field(default=None, repr=False, compare=False) |
| 334 | + saturation: InitVar[NUMERIC] = field(default=None, repr=False, |
| 335 | + compare=False) |
| 336 | + value: InitVar[NUMERIC] = field(default=None, repr=False, compare=False) |
| 337 | + hsv: HSV = field(init=False) |
| 338 | + rgb: RGB = field(init=False) |
| 339 | + |
| 340 | + def __post_init__(self, red, green, blue, hue, saturation, value): |
| 341 | + """Check HSV or RGB Values and create named tuples.""" |
| 342 | + if any(x is not None for x in [hue, saturation, value]): |
| 343 | + self.hsv = HSV(*self.valid_hsv(hue, saturation, value)) |
| 344 | + self.rgb = self.hsv_to_rgb(hue, saturation, value) |
| 345 | + elif any(x is not None for x in [red, green, blue]): |
| 346 | + self.rgb = RGB(*self.valid_rgb(red, green, blue)) |
| 347 | + self.hsv = self.rgb_to_hsv(red, green, blue) |
| 348 | + else: |
| 349 | + logger.error('No color values provided') |
| 350 | + |
| 351 | + @staticmethod |
| 352 | + def min_max(value: Union[int, float, str], min_val: float, |
| 353 | + max_val: float, default: float) -> float: |
| 354 | + """Check if value is within min and max values.""" |
| 355 | + try: |
| 356 | + val = max(min_val, (min(max_val, round(float(value), 2)))) |
| 357 | + except (ValueError, TypeError): |
| 358 | + val = default |
| 359 | + return val |
| 360 | + |
| 361 | + @classmethod |
| 362 | + def valid_hsv(cls, h: Union[int, float, str], |
| 363 | + s: Union[int, float, str], |
| 364 | + v: Union[int, float, str]) -> tuple: |
| 365 | + """Check if HSV values are valid.""" |
| 366 | + valid_hue = float(cls.min_max(h, 0, 360, 360)) |
| 367 | + valid_saturation = float(cls.min_max(s, 0, 100, 100)) |
| 368 | + valid_value = float(cls.min_max(v, 0, 100, 100)) |
| 369 | + return ( |
| 370 | + valid_hue, |
| 371 | + valid_saturation, |
| 372 | + valid_value |
| 373 | + ) |
| 374 | + |
| 375 | + @classmethod |
| 376 | + def valid_rgb(cls, r: float, g: float, b: float) -> list: |
| 377 | + """Check if RGB values are valid.""" |
| 378 | + rgb = [] |
| 379 | + for val in (r, g, b): |
| 380 | + valid_val = cls.min_max(val, 0, 255, 255) |
| 381 | + rgb.append(valid_val) |
| 382 | + return rgb |
| 383 | + |
| 384 | + @staticmethod |
| 385 | + def hsv_to_rgb(hue, saturation, value) -> RGB: |
| 386 | + """Convert HSV to RGB.""" |
| 387 | + return RGB( |
| 388 | + *tuple(round(i * 255, 0) for i in colorsys.hsv_to_rgb( |
| 389 | + hue / 360, |
| 390 | + saturation / 100, |
| 391 | + value / 100 |
| 392 | + )) |
| 393 | + ) |
| 394 | + |
| 395 | + @staticmethod |
| 396 | + def rgb_to_hsv(red, green, blue) -> HSV: |
| 397 | + """Convert RGB to HSV.""" |
| 398 | + hsv_tuple = colorsys.rgb_to_hsv( |
| 399 | + red / 255, |
| 400 | + green / 255, |
| 401 | + blue / 255 |
| 402 | + ) |
| 403 | + hsv_factors = [360, 100, 100] |
| 404 | + |
| 405 | + return HSV( |
| 406 | + float(round(hsv_tuple[0] * hsv_factors[0], 2)), |
| 407 | + float(round(hsv_tuple[1] * hsv_factors[1], 2)), |
| 408 | + float(round(hsv_tuple[2] * hsv_factors[2], 0)), |
| 409 | + ) |
0 commit comments