From 92c10ad8626dd2c346c8a2cef174c7edda3fe053 Mon Sep 17 00:00:00 2001 From: kbullet Date: Wed, 13 Nov 2024 11:43:43 +0700 Subject: [PATCH] Fix async warning, and updated lightentityfeature and colormode for HA Core 2025.1 (#67) * Fix async issue, and updated lightentityfeature and colormode * Clean up * Clean up * Clean up * removed ds_store file --- custom_components/yeelight_bt/__init__.py | 4 +-- custom_components/yeelight_bt/light.py | 36 ++++++++++++++--------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/custom_components/yeelight_bt/__init__.py b/custom_components/yeelight_bt/__init__.py index 74dba90..d1e48b1 100644 --- a/custom_components/yeelight_bt/__init__.py +++ b/custom_components/yeelight_bt/__init__.py @@ -39,9 +39,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: raise ConfigEntryNotReady(f"Could not find Yeelight with address {address}") hass.data[DOMAIN][entry.entry_id] = ble_device - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, "light") - ) + await hass.config_entries.async_forward_entry_setups(entry, ["light"]) return True diff --git a/custom_components/yeelight_bt/light.py b/custom_components/yeelight_bt/light.py index edaca24..7d5939d 100644 --- a/custom_components/yeelight_bt/light.py +++ b/custom_components/yeelight_bt/light.py @@ -13,10 +13,9 @@ ATTR_HS_COLOR, ENTITY_ID_FORMAT, PLATFORM_SCHEMA, - SUPPORT_BRIGHTNESS, - SUPPORT_COLOR, - SUPPORT_COLOR_TEMP, LightEntity, + LightEntityFeature, + ColorMode, ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_MAC, CONF_NAME, EVENT_HOMEASSISTANT_STOP @@ -46,9 +45,6 @@ LIGHT_EFFECT_LIST = ["flow", "none"] -SUPPORT_YEELIGHT_BT = SUPPORT_BRIGHTNESS # | SUPPORT_EFFECT -SUPPORT_YEELIGHT_BEDSIDE = SUPPORT_YEELIGHT_BT | SUPPORT_COLOR_TEMP | SUPPORT_COLOR - _LOGGER = logging.getLogger(__name__) @@ -106,7 +102,7 @@ async def async_added_to_hass(self) -> None: # schedule immediate refresh of lamp state: self.async_schedule_update_ha_state(force_refresh=True) - async def async_will_remove_from_hass(self) -> None: + async def async_will_remove_from_hass(self, event=None) -> None: """Run when entity will be removed from hass.""" _LOGGER.debug("Running async_will_remove_from_hass") try: @@ -196,11 +192,23 @@ def is_on(self) -> bool: return self._is_on @property - def supported_features(self) -> int: - """Flag supported features.""" + def supported_color_modes(self) -> set[str]: + """Return the supported color modes.""" if self._dev.model == MODEL_CANDELA: - return SUPPORT_YEELIGHT_BT - return SUPPORT_YEELIGHT_BEDSIDE + return {ColorMode.BRIGHTNESS} + return {ColorMode.COLOR_TEMP, ColorMode.HS} + + @property + def supported_features(self) -> int: + """Return the supported features using LightEntityFeature.""" + return LightEntityFeature.TRANSITION | LightEntityFeature.EFFECT + + @property + def color_mode(self) -> str: + """Return the current color mode of the light.""" + if self._ct > 0: + return ColorMode.COLOR_TEMP + return ColorMode.HS def _status_cb(self) -> None: _LOGGER.debug("Got state notification from the lamp") @@ -255,7 +263,7 @@ async def async_turn_on(self, **kwargs: int) -> None: await asyncio.sleep(0.5) # wait for the lamp to turn on self._is_on = True - if ATTR_HS_COLOR in kwargs: + if ATTR_HS_COLOR in kwargs and ColorMode.HS in self.supported_color_modes: rgb: tuple[int, int, int] = color_hs_to_RGB(*kwargs.get(ATTR_HS_COLOR)) self._rgb = rgb _LOGGER.debug( @@ -267,7 +275,7 @@ async def async_turn_on(self, **kwargs: int) -> None: await asyncio.sleep(0.7) # give time to transition before HA request update return - if ATTR_COLOR_TEMP in kwargs: + if ATTR_COLOR_TEMP in kwargs and ColorMode.COLOR_TEMP in self.supported_color_modes: mireds = kwargs[ATTR_COLOR_TEMP] temp_in_k = int(mired_to_kelvin(mireds)) scaled_temp_in_k = self.scale_temp(temp_in_k) @@ -323,4 +331,4 @@ def scale_temp_reversed(self, temp: int) -> int: new_temp = (mid - a) / (white - a) * temp - a * (mid - white) / (white - a) else: new_temp = (b - mid) / (b - white) * temp - b * (white - mid) / (b - white) - return round(new_temp) + return round(new_temp) \ No newline at end of file