From 1c8862f0bbf76d3de99cdd3a298cef5acd78b2da Mon Sep 17 00:00:00 2001 From: Alex X Date: Sun, 8 Sep 2024 11:14:25 +0300 Subject: [PATCH] Fix DIY modes for TX Ultimate #1424 --- custom_components/sonoff/light.py | 36 ++++++++++++++++--------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/custom_components/sonoff/light.py b/custom_components/sonoff/light.py index b7872ea..86cddee 100644 --- a/custom_components/sonoff/light.py +++ b/custom_components/sonoff/light.py @@ -1145,21 +1145,24 @@ async def async_turn_off(self, **kwargs) -> None: await self.ewelink.send(self.device, {"lightswitch": 0}) +T5_EFFECTS = { + "Night Light": 0, + "Party": 1, + "Leisure": 2, + "Color": 3, + "Childhood": 4, + "Wiper": 5, + "Fairy": 6, + "Starburst": 7, + "DIY 1": 101, + "DIY 2": 102, +} + + class XT5Light(XOnOffLight): params = {"lightSwitch", "lightMode"} - _attr_effect_list = [ - "Night Light", - "Party", - "Leisure", - "Color", - "Childhood", - "Wiper", - "Fairy", - "Starburst", - "DIY 1", - "DIY 2", - ] + _attr_effect_list = list(T5_EFFECTS.keys()) _attr_supported_features = LightEntityFeature.EFFECT def set_state(self, params: dict): @@ -1167,9 +1170,8 @@ def set_state(self, params: dict): self._attr_is_on = params["lightSwitch"] == "on" if "lightMode" in params: - i = int(params["lightMode"]) - self._attr_effect = ( - self._attr_effect_list[i] if i < len(self._attr_effect_list) else None + self._attr_effect = next( + (k for k, v in T5_EFFECTS.items() if v == params["lightMode"]), None ) async def async_turn_on( @@ -1177,8 +1179,8 @@ async def async_turn_on( ) -> None: params = {} - if effect: - params["lightMode"] = self._attr_effect_list.index(effect) + if effect and effect in T5_EFFECTS: + params["lightMode"] = T5_EFFECTS[effect] if not params: params["lightSwitch"] = "on"