From eeb7374ffadcea5b2356618b8bd0dbe2433979e7 Mon Sep 17 00:00:00 2001 From: h4de5 Date: Sun, 7 Apr 2024 22:13:23 +0200 Subject: [PATCH] Start to fix deprecation warnings in HA.2024 --- custom_components/vimar/light.py | 45 +++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/custom_components/vimar/light.py b/custom_components/vimar/light.py index e5d1a6e..0d64e9f 100755 --- a/custom_components/vimar/light.py +++ b/custom_components/vimar/light.py @@ -1,17 +1,14 @@ """Platform for light integration.""" import logging - import homeassistant.util.color as color_util -from homeassistant.components.light import ATTR_BRIGHTNESS, ATTR_HS_COLOR, SUPPORT_BRIGHTNESS, SUPPORT_COLOR - +from homeassistant.components.light import ( + LightEntity, + ATTR_BRIGHTNESS, + ATTR_HS_COLOR, + ColorMode, +) from .vimar_entity import VimarEntity, vimar_setup_entry - -try: - from homeassistant.components.light import LightEntity -except ImportError: - from homeassistant.components.light import Light as LightEntity - from .const import DEVICE_TYPE_LIGHTS as CURR_PLATFORM _LOGGER = logging.getLogger(__name__) @@ -63,13 +60,26 @@ def hs_color(self): return color_util.color_RGB_to_hs(*self.rgb_color) @property - def supported_features(self): - """Flag supported features.""" - flags = 0 + def color_mode(self) -> ColorMode: + """Return the color mode of the light.""" + if self.has_state("red") and self.has_state("green") and self.has_state("blue"): + return ColorMode.RGB if self.has_state("value"): - flags |= SUPPORT_BRIGHTNESS + return ColorMode.BRIGHTNESS + return ColorMode.ONOFF + + @property + def supported_color_modes(self) -> set[ColorMode] | None: + """Flag supported color modes.""" + flags: set[ColorMode] = set() + if self.has_state("red") and self.has_state("green") and self.has_state("blue"): - flags |= SUPPORT_COLOR + flags.add(ColorMode.RGB) + # flags.add(ColorMode.HS) + elif self.has_state("value"): + flags.add(ColorMode.BRIGHTNESS) + else: + flags.add(ColorMode.ONOFF) return flags @@ -82,7 +92,12 @@ async def async_turn_on(self, **kwargs): else: if ATTR_BRIGHTNESS in kwargs and self.has_state("value"): brightness_value = self.calculate_brightness(kwargs[ATTR_BRIGHTNESS]) - self.change_state("value", brightness_value, "on/off", ("0", "1")[brightness_value > 0]) + self.change_state( + "value", + brightness_value, + "on/off", + ("0", "1")[brightness_value > 0], + ) if ATTR_HS_COLOR in kwargs and self.has_state("red"): rgb = color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR])