mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Port unmerged fixes from tuya_v2 (#57624)
This commit is contained in:
parent
cef34356e2
commit
d6d6929e2b
@ -114,7 +114,8 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
|
|||||||
def turn_on(self, **kwargs: Any) -> None:
|
def turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn on or control the light."""
|
"""Turn on or control the light."""
|
||||||
commands = []
|
commands = []
|
||||||
_LOGGER.debug("light kwargs-> %s", kwargs)
|
work_mode = self._work_mode()
|
||||||
|
_LOGGER.debug("light kwargs-> %s; work_mode %s", kwargs, work_mode)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
DPCode.LIGHT in self.tuya_device.status
|
DPCode.LIGHT in self.tuya_device.status
|
||||||
@ -124,27 +125,11 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
|
|||||||
else:
|
else:
|
||||||
commands += [{"code": DPCode.SWITCH_LED, "value": True}]
|
commands += [{"code": DPCode.SWITCH_LED, "value": True}]
|
||||||
|
|
||||||
if ATTR_BRIGHTNESS in kwargs:
|
colour_data = self._get_hsv()
|
||||||
if self._work_mode().startswith(WORK_MODE_COLOUR):
|
v_range = self._tuya_hsv_v_range()
|
||||||
colour_data = self._get_hsv()
|
send_colour_data = False
|
||||||
v_range = self._tuya_hsv_v_range()
|
|
||||||
colour_data["v"] = int(
|
|
||||||
self.remap(kwargs[ATTR_BRIGHTNESS], 0, 255, v_range[0], v_range[1])
|
|
||||||
)
|
|
||||||
commands += [
|
|
||||||
{"code": self.dp_code_colour, "value": json.dumps(colour_data)}
|
|
||||||
]
|
|
||||||
else:
|
|
||||||
new_range = self._tuya_brightness_range()
|
|
||||||
tuya_brightness = int(
|
|
||||||
self.remap(
|
|
||||||
kwargs[ATTR_BRIGHTNESS], 0, 255, new_range[0], new_range[1]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
commands += [{"code": self.dp_code_bright, "value": tuya_brightness}]
|
|
||||||
|
|
||||||
if ATTR_HS_COLOR in kwargs:
|
if ATTR_HS_COLOR in kwargs:
|
||||||
colour_data = self._get_hsv()
|
|
||||||
# hsv h
|
# hsv h
|
||||||
colour_data["h"] = int(kwargs[ATTR_HS_COLOR][0])
|
colour_data["h"] = int(kwargs[ATTR_HS_COLOR][0])
|
||||||
# hsv s
|
# hsv s
|
||||||
@ -161,16 +146,16 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
|
|||||||
)
|
)
|
||||||
# hsv v
|
# hsv v
|
||||||
ha_v = self.brightness
|
ha_v = self.brightness
|
||||||
v_range = self._tuya_hsv_v_range()
|
|
||||||
colour_data["v"] = int(self.remap(ha_v, 0, 255, v_range[0], v_range[1]))
|
colour_data["v"] = int(self.remap(ha_v, 0, 255, v_range[0], v_range[1]))
|
||||||
|
|
||||||
commands += [
|
commands += [
|
||||||
{"code": self.dp_code_colour, "value": json.dumps(colour_data)}
|
{"code": self.dp_code_colour, "value": json.dumps(colour_data)}
|
||||||
]
|
]
|
||||||
if self.tuya_device.status[DPCode.WORK_MODE] != "colour":
|
if work_mode != WORK_MODE_COLOUR:
|
||||||
commands += [{"code": DPCode.WORK_MODE, "value": "colour"}]
|
work_mode = WORK_MODE_COLOUR
|
||||||
|
commands += [{"code": DPCode.WORK_MODE, "value": work_mode}]
|
||||||
|
|
||||||
if ATTR_COLOR_TEMP in kwargs:
|
elif ATTR_COLOR_TEMP in kwargs:
|
||||||
# temp color
|
# temp color
|
||||||
new_range = self._tuya_temp_range()
|
new_range = self._tuya_temp_range()
|
||||||
color_temp = self.remap(
|
color_temp = self.remap(
|
||||||
@ -190,8 +175,29 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
|
|||||||
)
|
)
|
||||||
commands += [{"code": self.dp_code_bright, "value": int(tuya_brightness)}]
|
commands += [{"code": self.dp_code_bright, "value": int(tuya_brightness)}]
|
||||||
|
|
||||||
if self.tuya_device.status[DPCode.WORK_MODE] != "white":
|
if work_mode != WORK_MODE_WHITE:
|
||||||
commands += [{"code": DPCode.WORK_MODE, "value": "white"}]
|
work_mode = WORK_MODE_WHITE
|
||||||
|
commands += [{"code": DPCode.WORK_MODE, "value": WORK_MODE_WHITE}]
|
||||||
|
|
||||||
|
if ATTR_BRIGHTNESS in kwargs:
|
||||||
|
if work_mode == WORK_MODE_COLOUR:
|
||||||
|
colour_data["v"] = int(
|
||||||
|
self.remap(kwargs[ATTR_BRIGHTNESS], 0, 255, v_range[0], v_range[1])
|
||||||
|
)
|
||||||
|
send_colour_data = True
|
||||||
|
elif work_mode == WORK_MODE_WHITE:
|
||||||
|
new_range = self._tuya_brightness_range()
|
||||||
|
tuya_brightness = int(
|
||||||
|
self.remap(
|
||||||
|
kwargs[ATTR_BRIGHTNESS], 0, 255, new_range[0], new_range[1]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
commands += [{"code": self.dp_code_bright, "value": tuya_brightness}]
|
||||||
|
|
||||||
|
if send_colour_data:
|
||||||
|
commands += [
|
||||||
|
{"code": self.dp_code_colour, "value": json.dumps(colour_data)}
|
||||||
|
]
|
||||||
|
|
||||||
self._send_command(commands)
|
self._send_command(commands)
|
||||||
|
|
||||||
@ -232,6 +238,14 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
|
|||||||
bright_value = json.loads(bright_item.values)
|
bright_value = json.loads(bright_item.values)
|
||||||
return bright_value.get("min", 0), bright_value.get("max", 255)
|
return bright_value.get("min", 0), bright_value.get("max", 255)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def color_mode(self) -> str:
|
||||||
|
"""Return the color_mode of the light."""
|
||||||
|
work_mode = self._work_mode()
|
||||||
|
if work_mode == WORK_MODE_WHITE:
|
||||||
|
return COLOR_MODE_COLOR_TEMP
|
||||||
|
return COLOR_MODE_HS
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hs_color(self) -> tuple[float, float] | None:
|
def hs_color(self) -> tuple[float, float] | None:
|
||||||
"""Return the hs_color of the light."""
|
"""Return the hs_color of the light."""
|
||||||
@ -320,6 +334,12 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
|
|||||||
return self.tuya_device.status.get(DPCode.WORK_MODE, "")
|
return self.tuya_device.status.get(DPCode.WORK_MODE, "")
|
||||||
|
|
||||||
def _get_hsv(self) -> dict[str, int]:
|
def _get_hsv(self) -> dict[str, int]:
|
||||||
|
if (
|
||||||
|
self.dp_code_colour not in self.tuya_device.status
|
||||||
|
or len(self.tuya_device.status[self.dp_code_colour]) == 0
|
||||||
|
):
|
||||||
|
return {"h": 0, "s": 0, "v": 0}
|
||||||
|
|
||||||
return json.loads(self.tuya_device.status[self.dp_code_colour])
|
return json.loads(self.tuya_device.status[self.dp_code_colour])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
Loading…
x
Reference in New Issue
Block a user