From 032786fcd86c981cb03962f57549c308143a5802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Kiss?= <70820303+g-kiss@users.noreply.github.com> Date: Tue, 9 Nov 2021 18:24:40 +0100 Subject: [PATCH] Improve Shelly color mode switch for dual mode bulbs (#58971) * Shelly color mode switch for SHCB-1 * Update light.py * Update light.py * Update homeassistant/components/shelly/light.py Co-authored-by: Shay Levy * Update light.py Co-authored-by: Shay Levy --- homeassistant/components/shelly/light.py | 36 ++++-------------------- 1 file changed, 6 insertions(+), 30 deletions(-) diff --git a/homeassistant/components/shelly/light.py b/homeassistant/components/shelly/light.py index 3e0fce43681..60810c9df4e 100644 --- a/homeassistant/components/shelly/light.py +++ b/homeassistant/components/shelly/light.py @@ -1,12 +1,10 @@ """Light for Shelly.""" from __future__ import annotations -import asyncio import logging from typing import Any, Final, cast from aioshelly.block_device import Block -import async_timeout from homeassistant.components.light import ( ATTR_BRIGHTNESS, @@ -35,10 +33,10 @@ from homeassistant.util.color import ( from . import BlockDeviceWrapper, RpcDeviceWrapper from .const import ( - AIOSHELLY_DEVICE_TIMEOUT_SEC, BLOCK, DATA_CONFIG_ENTRY, DOMAIN, + DUAL_MODE_LIGHT_MODELS, FIRMWARE_PATTERN, KELVIN_MAX_VALUE, KELVIN_MIN_VALUE_COLOR, @@ -136,7 +134,6 @@ class BlockShellyLight(ShellyBlockEntity, LightEntity): """Initialize light.""" super().__init__(wrapper, block) self.control_result: dict[str, Any] | None = None - self.mode_result: dict[str, Any] | None = None self._supported_color_modes: set[str] = set() self._supported_features: int = 0 self._min_kelvin: int = KELVIN_MIN_VALUE_WHITE @@ -185,8 +182,8 @@ class BlockShellyLight(ShellyBlockEntity, LightEntity): @property def mode(self) -> str: """Return the color mode of the light.""" - if self.mode_result: - return cast(str, self.mode_result["mode"]) + if self.control_result and self.control_result.get("mode"): + return cast(str, self.control_result["mode"]) if hasattr(self.block, "mode"): return cast(str, self.block.mode) @@ -369,9 +366,10 @@ class BlockShellyLight(ShellyBlockEntity, LightEntity): self.wrapper.model, ) - if await self.set_light_mode(set_mode): - self.control_result = await self.set_state(**params) + if set_mode and self.wrapper.model in DUAL_MODE_LIGHT_MODELS: + params["mode"] = set_mode + self.control_result = await self.set_state(**params) self.async_write_ha_state() async def async_turn_off(self, **kwargs: Any) -> None: @@ -387,32 +385,10 @@ class BlockShellyLight(ShellyBlockEntity, LightEntity): self.async_write_ha_state() - async def set_light_mode(self, set_mode: str | None) -> bool: - """Change device mode color/white if mode has changed.""" - if set_mode is None or self.mode == set_mode: - return True - - _LOGGER.debug("Setting light mode for entity %s, mode: %s", self.name, set_mode) - try: - async with async_timeout.timeout(AIOSHELLY_DEVICE_TIMEOUT_SEC): - self.mode_result = await self.wrapper.device.switch_light_mode(set_mode) - except (asyncio.TimeoutError, OSError) as err: - _LOGGER.error( - "Setting light mode for entity %s failed, state: %s, error: %s", - self.name, - set_mode, - repr(err), - ) - self.wrapper.last_update_success = False - return False - - return True - @callback def _update_callback(self) -> None: """When device updates, clear control & mode result that overrides state.""" self.control_result = None - self.mode_result = None super()._update_callback()