diff --git a/.experiment.py b/.experiment.py index 0ead2f2..9714895 100644 --- a/.experiment.py +++ b/.experiment.py @@ -1,5 +1,15 @@ -from itertools import permutations +order ='brg' +rgb = 'rgb' -RGB_COMBINATIONS = [''.join(x) for x in permutations('rgb')] +map_to_order = [rgb.index(x) for x in order] +map_from_order = [order.index(x) for x in rgb] -print(RGB_COMBINATIONS) \ No newline at end of file + +_rgb = [ + rgb[x] for x in map_to_order + ] +_order = [ + _rgb[x] for x in map_from_order + ] + +print(_rgb, _order) \ No newline at end of file diff --git a/custom_components/mega/const.py b/custom_components/mega/const.py index 78cb64e..3517bf2 100644 --- a/custom_components/mega/const.py +++ b/custom_components/mega/const.py @@ -77,4 +77,5 @@ REMOVE_CONFIG = [ 'sensor', 'smooth', ] -RGB_COMBINATIONS = [''.join(x) for x in permutations('rgb')] \ No newline at end of file +RGB_COMBINATIONS = [''.join(x) for x in permutations('rgb')] +RGB = 'rgb' diff --git a/custom_components/mega/light.py b/custom_components/mega/light.py index d2d7e06..36305ab 100644 --- a/custom_components/mega/light.py +++ b/custom_components/mega/light.py @@ -33,9 +33,9 @@ from .const import ( CONF_SWITCH, DOMAIN, CONF_CUSTOM, - CONF_SKIP, CONF_LED, CONF_WS28XX, CONF_PORTS, CONF_WHITE_SEP, CONF_SMOOTH, CONF_ORDER, CONF_CHIP, + CONF_SKIP, CONF_LED, CONF_WS28XX, CONF_PORTS, CONF_WHITE_SEP, CONF_SMOOTH, CONF_ORDER, CONF_CHIP, RGB, ) -from .tools import int_ignore +from .tools import int_ignore, map_reorder_rgb lg = logging.getLogger(__name__) SCAN_INTERVAL = timedelta(seconds=5) @@ -119,14 +119,8 @@ class MegaRGBW(LightEntity, BaseMegaEntity): self._task: asyncio.Task = None self._restore = None self.smooth: timedelta = self.customize[CONF_SMOOTH] - self._color_map = self.customize.get(CONF_ORDER, 'rgb') + self._color_order = self.customize.get(CONF_ORDER, 'rgb') self._last_called: float = 0 - if self._color_map == 'rgb': - self._color_map = None - else: - self._color_map = { - x: i for i, x in enumerate(self._color_map) - } self._max_values = None @property @@ -189,11 +183,9 @@ class MegaRGBW(LightEntity, BaseMegaEntity): rgb = [ round(x * self.max_values[i]) for i, x in enumerate(rgb) ] - if self.is_ws and self._color_map is not None: + if self.is_ws: # восстанавливаем мэпинг - rgb = [ - rgb[self._color_map[x]] for x in 'rgb' - ] + rgb = map_reorder_rgb(rgb, RGB, self._color_order) return rgb async def async_turn_on(self, **kwargs): @@ -267,7 +259,10 @@ class MegaRGBW(LightEntity, BaseMegaEntity): else: w = None rgb = rgbw - + if self.is_ws: + rgb = map_reorder_rgb( + rgb, self._color_order, RGB + ) h, s, v = colorsys.rgb_to_hsv(*[x/self.max_values[i] for i, x in enumerate(rgb)]) h *= 360 s *= 100 diff --git a/custom_components/mega/manifest.json b/custom_components/mega/manifest.json index fcc9a88..fe36b41 100644 --- a/custom_components/mega/manifest.json +++ b/custom_components/mega/manifest.json @@ -15,5 +15,5 @@ "@andvikt" ], "issue_tracker": "https://github.com/andvikt/mega_hacs/issues", - "version": "v1.0.0b1" + "version": "v1.0.1" } \ No newline at end of file diff --git a/custom_components/mega/tools.py b/custom_components/mega/tools.py index 356736b..aab64b7 100644 --- a/custom_components/mega/tools.py +++ b/custom_components/mega/tools.py @@ -117,3 +117,8 @@ class PriorityLock(asyncio.Lock): fut.set_result(True) +def map_reorder_rgb(rgb: list, from_: str, to_: str): + if from_ == to_: + return rgb + mapping = [from_.index(x) for x in to_] + return [rgb[x] for x in mapping]