Skip to content

Commit

Permalink
Fix supported color modes warning 2024.23 #157
Browse files Browse the repository at this point in the history
  • Loading branch information
xZetsubou committed Mar 1, 2024
1 parent b83c193 commit 137eadd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
10 changes: 6 additions & 4 deletions custom_components/localtuya/common.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Code shared between all platforms."""

import asyncio
import logging
import time
from datetime import timedelta
from typing import Callable, Coroutine, NamedTuple
from typing import Any, Callable, Coroutine, NamedTuple

from homeassistant.core import HomeAssistant, CALLBACK_TYPE, callback
from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -635,8 +636,8 @@ def has_config(self, attr) -> bool:
value = self._config.get(attr, "-1")
return value is not None and value != "-1"

def dp_value(self, key):
"""Return cached value for DPS index or Entity Config Key."""
def dp_value(self, key, default=None) -> Any | None:
"""Return cached value for DPS index or Entity Config Key. else default None"""
requested_dp = str(key)
# If requested_dp in DP ID, get cached value.
if (value := self._status.get(requested_dp)) or value is not None:
Expand All @@ -648,7 +649,8 @@ def dp_value(self, key):
return value

if value is None:
self.debug(f"{self.name}: is requesting unknown DP Value {key}", force=True)
value = default
# self.debug(f"{self.name}: is requesting unknown DP Value {key}", force=True)

return value

Expand Down
37 changes: 29 additions & 8 deletions custom_components/localtuya/light.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Platform to locally control Tuya-based light devices."""

import logging
import textwrap
from functools import partial
Expand Down Expand Up @@ -257,15 +258,20 @@ def supported_color_modes(self) -> set[ColorMode] | set[str] | None:
if self.has_config(CONF_COLOR_TEMP):
color_modes.add(ColorMode.COLOR_TEMP)
if self.has_config(CONF_COLOR):
color_modes.update({ColorMode.HS, ColorMode.BRIGHTNESS})
if self.has_config(CONF_BRIGHTNESS):
color_modes.add(ColorMode.BRIGHTNESS)
color_modes.add(ColorMode.HS)

if not color_modes and self.has_config(CONF_BRIGHTNESS):
return {ColorMode.BRIGHTNESS}

if not color_modes:
return {ColorMode.ONOFF}

return color_modes

@property
def supported_features(self):
def supported_features(self) -> LightEntityFeature:
"""Flag supported features."""
supports = 0
supports = LightEntityFeature(0)
if self.has_config(CONF_SCENE) or self.has_config(CONF_MUSIC_MODE):
supports |= LightEntityFeature.EFFECT
return supports
Expand Down Expand Up @@ -294,6 +300,18 @@ def is_music_mode(self):
color_mode = self.__get_color_mode()
return color_mode is not None and color_mode == MODE_MUSIC

@property
def color_mode(self) -> ColorMode:
"""Return the color_mode of the light."""
if self.is_color_mode:
return ColorMode.HS
if self.is_white_mode:
return ColorMode.COLOR_TEMP
if self._brightness:
return ColorMode.BRIGHTNESS

return ColorMode.ONOFF

def __is_color_rgb_encoded(self):
return len(self.dp_value(CONF_COLOR)) > 12

Expand Down Expand Up @@ -329,7 +347,9 @@ async def async_turn_on(self, **kwargs):
elif kwargs[ATTR_EFFECT] == SCENE_MUSIC:
states[self._config.get(CONF_COLOR_MODE)] = MODE_MUSIC

if ATTR_BRIGHTNESS in kwargs and ColorMode.BRIGHTNESS in color_modes:
if ATTR_BRIGHTNESS in kwargs and (
ColorMode.BRIGHTNESS in color_modes or self.has_config(CONF_BRIGHTNESS)
):
brightness = map_range(
int(kwargs[ATTR_BRIGHTNESS]),
0,
Expand Down Expand Up @@ -418,8 +438,9 @@ def status_updated(self):
self._state = self.dp_value(self._dp_id)
supported = self.supported_features
self._effect = None
if ColorMode.BRIGHTNESS in self.supported_color_modes:
self._brightness = self.dp_value(CONF_BRIGHTNESS)

if brightness_dp_value := self.dp_value(CONF_BRIGHTNESS, None):
self._brightness = brightness_dp_value

if ColorMode.HS in self.supported_color_modes:
color = self.dp_value(CONF_COLOR)
Expand Down

0 comments on commit 137eadd

Please sign in to comment.