Skip to content

Commit

Permalink
Fix async warning, and updated lightentityfeature and colormode for H…
Browse files Browse the repository at this point in the history
…A Core 2025.1 (#67)

* Fix async issue, and updated lightentityfeature and colormode

* Clean up

* Clean up

* Clean up

* removed ds_store file
  • Loading branch information
kbullet authored Nov 13, 2024
1 parent 4aba68b commit 92c10ad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
4 changes: 1 addition & 3 deletions custom_components/yeelight_bt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
36 changes: 22 additions & 14 deletions custom_components/yeelight_bt/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__)


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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)

0 comments on commit 92c10ad

Please # to comment.