diff --git a/readme.md b/readme.md index 50f941f8..ae2eb018 100644 --- a/readme.md +++ b/readme.md @@ -131,6 +131,7 @@ MyOpenhabRule() - Added HABApp.util.RateLimiter - Added CompressedMidnightRotatingFileHandler - Updated dependencies +- Small improvement for RGB and HSB types #### 23.11.0 (2023-11-23) - Fix for very small float values (#425) diff --git a/requirements.txt b/requirements.txt index 9bcc4000..aa857f7a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,8 +6,8 @@ # ----------------------------------------------------------------------------- # Packages for source formatting # ----------------------------------------------------------------------------- -pre-commit >= 3.5, < 3.6 -ruff >= 0.1.6, < 0.2 +pre-commit >= 3.6, < 4 +ruff >= 0.1.8, < 0.2 # ----------------------------------------------------------------------------- # Packages for other developement tasks diff --git a/requirements_setup.txt b/requirements_setup.txt index 8264b60c..a634b771 100644 --- a/requirements_setup.txt +++ b/requirements_setup.txt @@ -1,10 +1,10 @@ aiohttp == 3.9.1 pydantic == 2.5.2 -msgspec == 0.18.4 +msgspec == 0.18.5 pendulum == 2.1.2 bidict == 0.22.1 watchdog == 3.0.0 -ujson == 5.8.0 +ujson == 5.9.0 aiomqtt == 1.2.1 immutables == 0.20 @@ -15,7 +15,7 @@ colorama == 0.4.6 voluptuous == 0.14.1 -typing-extensions == 4.8.0 +typing-extensions == 4.9.0 aiohttp-sse-client == 0.2.1 diff --git a/requirements_tests.txt b/requirements_tests.txt index 34746f45..c6dc925e 100644 --- a/requirements_tests.txt +++ b/requirements_tests.txt @@ -8,4 +8,4 @@ # ----------------------------------------------------------------------------- packaging == 23.2 pytest == 7.4.3 -pytest-asyncio == 0.21.1 +pytest-asyncio == 0.23.2 diff --git a/src/HABApp/core/types/color.py b/src/HABApp/core/types/color.py index 52d4be76..66dec2e4 100644 --- a/src/HABApp/core/types/color.py +++ b/src/HABApp/core/types/color.py @@ -1,6 +1,6 @@ from colorsys import hsv_to_rgb as _hsv_to_rgb from colorsys import rgb_to_hsv as _rgb_to_hsv -from typing import Tuple, Union, Optional +from typing import Optional, Tuple, Union from typing_extensions import Self @@ -90,19 +90,30 @@ def __str__(self): def __eq__(self, other): if isinstance(other, self.__class__): return self._r == other._r and self._g == other._g and self._b == other._b - elif isinstance(other, HSB): + if isinstance(other, HSB): return self == self.__class__.from_hsb(other) - else: - return NotImplemented - - def __getitem__(self, item: int) -> int: - if item == 0: - return self._r - if item == 1: - return self._g - if item == 2: - return self._b - raise IndexError() + return NotImplemented + + def __getitem__(self, item: Union[int, str]) -> int: + if isinstance(item, int): + if item == 0: + return self._r + if item == 1: + return self._g + if item == 2: + return self._b + raise IndexError() + + if isinstance(item, str): + if item in ('r', 'red'): + return self._r + if item in ('g', 'green'): + return self._g + if item in ('b', 'blue'): + return self._b + raise KeyError() + + raise TypeError() # ------------------------------------------------------------------------------------------------------------------ # Conversions @@ -227,17 +238,28 @@ def __eq__(self, other): return self._hue == other._hue and \ self._saturation == other._saturation and \ self._brightness == other._brightness - else: - return NotImplemented - - def __getitem__(self, item: int) -> float: - if item == 0: - return self._hue - if item == 1: - return self._saturation - if item == 2: - return self._brightness - raise IndexError() + return NotImplemented + + def __getitem__(self, item: Union[int, str]) -> float: + if isinstance(item, int): + if item == 0: + return self._hue + if item == 1: + return self._saturation + if item == 2: + return self._brightness + raise IndexError() + + if isinstance(item, str): + if item in ('h', 'hue'): + return self._hue + if item in ('s', 'saturation'): + return self._saturation + if item in ('b', 'brightness'): + return self._brightness + raise KeyError() + + raise TypeError() # ------------------------------------------------------------------------------------------------------------------ # Conversions diff --git a/tests/test_core/test_types/test_color.py b/tests/test_core/test_types/test_color.py index f464a86a..375b04f5 100644 --- a/tests/test_core/test_types/test_color.py +++ b/tests/test_core/test_types/test_color.py @@ -1,6 +1,6 @@ import pytest -from HABApp.core.types.color import RGB, HSB +from HABApp.core.types.color import HSB, RGB def test_rgb(): @@ -10,8 +10,16 @@ def test_rgb(): assert rgb.b == rgb.blue == 3 assert rgb[0] == 1 + assert rgb['r'] == 1 + assert rgb['red'] == 1 + assert rgb[1] == 2 + assert rgb['g'] == 2 + assert rgb['green'] == 2 + assert rgb[2] == 3 + assert rgb['b'] == 3 + assert rgb['blue'] == 3 r, g, b = rgb assert r == rgb.r @@ -69,8 +77,16 @@ def test_hsb(): assert hsb.b == hsb.brightness == 3 assert hsb[0] == 1 + assert hsb['h'] == 1 + assert hsb['hue'] == 1 + assert hsb[1] == 2 + assert hsb['s'] == 2 + assert hsb['saturation'] == 2 + assert hsb[2] == 3 + assert hsb['b'] == 3 + assert hsb['brightness'] == 3 h, s, b = hsb assert h == hsb.h