Skip to content

Commit

Permalink
Update deprecated constants (#88)
Browse files Browse the repository at this point in the history
* update fan

* update vacuum

* update light
  • Loading branch information
xZetsubou committed Jan 1, 2024
1 parent 46ba6ea commit b248dc2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 47 deletions.
10 changes: 4 additions & 6 deletions custom_components/localtuya/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
DIRECTION_FORWARD,
DIRECTION_REVERSE,
DOMAIN,
SUPPORT_DIRECTION,
SUPPORT_OSCILLATE,
SUPPORT_SET_SPEED,
FanEntityFeature,
FanEntity,
)
from homeassistant.util.percentage import (
Expand Down Expand Up @@ -195,13 +193,13 @@ def supported_features(self) -> int:
features = 0

if self.has_config(CONF_FAN_OSCILLATING_CONTROL):
features |= SUPPORT_OSCILLATE
features |= FanEntityFeature.OSCILLATE

if self.has_config(CONF_FAN_SPEED_CONTROL):
features |= SUPPORT_SET_SPEED
features |= FanEntityFeature.SET_SPEED

if self.has_config(CONF_FAN_DIRECTION):
features |= SUPPORT_DIRECTION
features |= FanEntityFeature.DIRECTION

return features

Expand Down
52 changes: 29 additions & 23 deletions custom_components/localtuya/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
ATTR_EFFECT,
ATTR_HS_COLOR,
DOMAIN,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
SUPPORT_EFFECT,
LightEntityFeature,
ColorMode,
LightEntity,
)
from homeassistant.const import CONF_BRIGHTNESS, CONF_COLOR_TEMP, CONF_SCENE
Expand Down Expand Up @@ -210,8 +208,8 @@ def hs_color(self):
if self.is_color_mode:
return self._hs
if (
self.supported_features & SUPPORT_COLOR
and not self.supported_features & SUPPORT_COLOR_TEMP
ColorMode.HS in self.supported_color_modes
and not ColorMode.COLOR_TEMP in self.supported_color_modes
):
return [0, 0]
return None
Expand Down Expand Up @@ -260,18 +258,25 @@ def effect_list(self):
return self._effect_list
return None

@property
def supported_color_modes(self) -> set[ColorMode] | set[str] | None:
"""Flag supported color modes."""
color_modes: set[ColorMode] = set()

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)
return color_modes

@property
def supported_features(self):
"""Flag supported features."""
supports = 0
if self.has_config(CONF_BRIGHTNESS):
supports |= SUPPORT_BRIGHTNESS
if self.has_config(CONF_COLOR_TEMP):
supports |= SUPPORT_COLOR_TEMP
if self.has_config(CONF_COLOR):
supports |= SUPPORT_COLOR | SUPPORT_BRIGHTNESS
if self.has_config(CONF_SCENE) or self.has_config(CONF_MUSIC_MODE):
supports |= SUPPORT_EFFECT
supports |= LightEntityFeature.EFFECT
return supports

@property
Expand Down Expand Up @@ -321,7 +326,7 @@ async def async_turn_on(self, **kwargs):
states[self._dp_id] = True
features = self.supported_features
brightness = None
if ATTR_EFFECT in kwargs and (features & SUPPORT_EFFECT):
if ATTR_EFFECT in kwargs and (features & LightEntityFeature.EFFECT):
scene = self._scenes.get(kwargs[ATTR_EFFECT])
if scene is not None:
if scene.startswith(MODE_SCENE):
Expand All @@ -332,7 +337,7 @@ 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 (features & SUPPORT_BRIGHTNESS):
if ATTR_BRIGHTNESS in kwargs and self._brightness:
brightness = map_range(
int(kwargs[ATTR_BRIGHTNESS]),
0,
Expand Down Expand Up @@ -364,7 +369,7 @@ async def async_turn_on(self, **kwargs):
states[self._config.get(CONF_COLOR)] = color
states[self._config.get(CONF_COLOR_MODE)] = MODE_COLOR

if ATTR_HS_COLOR in kwargs and (features & SUPPORT_COLOR):
if ATTR_HS_COLOR in kwargs and self._hs:
if brightness is None:
brightness = self._brightness
hs = kwargs[ATTR_HS_COLOR]
Expand All @@ -391,7 +396,7 @@ async def async_turn_on(self, **kwargs):
states[self._config.get(CONF_COLOR)] = color
states[self._config.get(CONF_COLOR_MODE)] = MODE_COLOR

if ATTR_COLOR_TEMP in kwargs and (features & SUPPORT_COLOR_TEMP):
if ATTR_COLOR_TEMP in kwargs and self._color_temp:
if brightness is None:
brightness = self._brightness
mired = int(kwargs[ATTR_COLOR_TEMP])
Expand All @@ -409,6 +414,7 @@ async def async_turn_on(self, **kwargs):
states[self._config.get(CONF_COLOR_MODE)] = MODE_WHITE
states[self._config.get(CONF_BRIGHTNESS)] = brightness
states[self._config.get(CONF_COLOR_TEMP)] = color_temp

await self._device.set_dps(states)

async def async_turn_off(self, **kwargs):
Expand All @@ -420,12 +426,12 @@ def status_updated(self):
self._state = self.dp_value(self._dp_id)
supported = self.supported_features
self._effect = None
if supported & SUPPORT_BRIGHTNESS and self.has_config(CONF_BRIGHTNESS):
if ColorMode.BRIGHTNESS in self.supported_color_modes:
self._brightness = self.dp_value(CONF_BRIGHTNESS)

if supported & SUPPORT_COLOR:
if ColorMode.HS in self.supported_color_modes:
color = self.dp_value(CONF_COLOR)
if color is not None and not self.is_white_mode:
if color is not None:
if self.__is_color_rgb_encoded():
hue = int(color[6:10], 16)
sat = int(color[10:12], 16)
Expand All @@ -439,10 +445,10 @@ def status_updated(self):
self._hs = [hue, sat / 10.0]
self._brightness = value

if supported & SUPPORT_COLOR_TEMP:
if ColorMode.COLOR_TEMP in self.supported_color_modes:
self._color_temp = self.dp_value(CONF_COLOR_TEMP)

if self.is_scene_mode and supported & SUPPORT_EFFECT:
if self.is_scene_mode and supported & LightEntityFeature.EFFECT:
if self.dp_value(CONF_COLOR_MODE) != MODE_SCENE:
self._effect = self.__find_scene_by_scene_data(
self.dp_value(CONF_COLOR_MODE)
Expand All @@ -457,7 +463,7 @@ def status_updated(self):
elif SCENE_CUSTOM in self._effect_list:
self._effect_list.remove(SCENE_CUSTOM)

if self.is_music_mode and supported & SUPPORT_EFFECT:
if self.is_music_mode and supported & LightEntityFeature.EFFECT:
self._effect = SCENE_MUSIC


Expand Down
28 changes: 10 additions & 18 deletions custom_components/localtuya/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@
STATE_IDLE,
STATE_PAUSED,
STATE_RETURNING,
SUPPORT_BATTERY,
SUPPORT_FAN_SPEED,
SUPPORT_LOCATE,
SUPPORT_PAUSE,
SUPPORT_RETURN_HOME,
SUPPORT_START,
SUPPORT_STATE,
SUPPORT_STATUS,
SUPPORT_STOP,
VacuumEntityFeature,
StateVacuumEntity,
)

Expand Down Expand Up @@ -123,21 +115,21 @@ def __init__(self, device, config_entry, switchid, **kwargs):
def supported_features(self):
"""Flag supported features."""
supported_features = (
SUPPORT_START
| SUPPORT_PAUSE
| SUPPORT_STOP
| SUPPORT_STATUS
| SUPPORT_STATE
VacuumEntityFeature.START
| VacuumEntityFeature.PAUSE
| VacuumEntityFeature.STOP
| VacuumEntityFeature.STATUS
| VacuumEntityFeature.STATE
)

if self.has_config(CONF_RETURN_MODE):
supported_features = supported_features | SUPPORT_RETURN_HOME
supported_features = supported_features | VacuumEntityFeature.RETURN_HOME
if self.has_config(CONF_FAN_SPEED_DP):
supported_features = supported_features | SUPPORT_FAN_SPEED
supported_features = supported_features | VacuumEntityFeature.FAN_SPEED
if self.has_config(CONF_BATTERY_DP):
supported_features = supported_features | SUPPORT_BATTERY
supported_features = supported_features | VacuumEntityFeature.BATTERY
if self.has_config(CONF_LOCATE_DP):
supported_features = supported_features | SUPPORT_LOCATE
supported_features = supported_features | VacuumEntityFeature.LOCATE

return supported_features

Expand Down

0 comments on commit b248dc2

Please sign in to comment.