8000 Tuya: Add ColorMode.WHITE when bulb supports WorkMode.WHITE but does … · home-assistant/core@afc2cc2 · GitHub
[go: up one dir, main page]

Skip to content

Commit afc2cc2

Browse files
author
Jason-LaDu
committed
Tuya: Add ColorMode.WHITE when bulb supports WorkMode.WHITE but does not have temp_value
1 parent f8274cd commit afc2cc2

File tree

2 files changed

+75
-17
lines changed

2 files changed

+75
-17
lines changed

homeassistant/components/tuya/light.py

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import annotations
44

5+
import logging #at the top of the file
6+
57
from dataclasses import dataclass, field
68
import json
79
from typing import Any, cast
@@ -12,6 +14,7 @@
1214
ATTR_BRIGHTNESS,
1315
ATTR_COLOR_TEMP,
1416
ATTR_HS_COLOR,
17+
ATTR_WHITE,
1518
ColorMode,
1619
LightEntity,
1720
LightEntityDescription,
@@ -27,6 +30,7 @@
2730
from .entity import IntegerTypeData, TuyaEntity
2831
from .util import remap_value
2932

33+
LOGGER = logging.getLogger(__package__) #at the top of the file after all the imports
3034

3135
@dataclass
3236
class ColorTypeData:
@@ -456,6 +460,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
456460
_color_data_type: ColorTypeData | None = None
457461
_color_mode: DPCode | None = None
458462
_color_temp: IntegerTypeData | None = None
463+
_white_color_mode: ColorMode | None = None
459464
_fixed_color_mode: ColorMode | None = None
460465

461466
def __init__(
@@ -474,6 +479,7 @@ def __init__(
474479
self._color_mode_dpcode = self.find_dpcode(
475480
description.color_mode, prefer_function=True
476481
)
482+
LOGGER.warning("Tuya light.py.__init__ \"{}\" _color_mode_dpcode {}".format(getattr(self.device, "name"), str(self._color_mode_dpcode)))
477483

478484
if int_type := self.find_dpcode(
479485
description.brightness, dptype=DPType.INTEGER, prefer_function=True
@@ -492,6 +498,17 @@ def __init__(
492498
):
493499
self._color_temp = int_type
494500
color_modes.add(ColorMode.COLOR_TEMP)
501+
self._white_color_mode = ColorMode.COLOR_TEMP
502+
elif workmode_type := self.find_dpcode( # If entity does not have color_temp, check if it has work_mode "white"
503+
description.color_mode, dptype=DPType.ENUM, prefer_function=True
504+
):
505+
LOGGER.warning("Tuya light.py.__init__ \"{}\" workmode_type {}".format(getattr(self.device, "name"), str(workmode_type.range)))
506+
LOGGER.warning("Tuya light.py.__init__ \"{}\" WorkMode.WHITE.value {}".format(getattr(self.device, "name"), WorkMode.WHITE.value))
507+
LOGGER.warning("Tuya light.py.__init__ \"{}\" WorkMode.WHITE.value in workmode_type.range {}".format(getattr(self.device, "name"), str(WorkMode.WHITE.value in workmode_type.range)))
508+
if WorkMode.WHITE.value in workmode_type.range:
509+
LOGGER.warning("Tuya light.py.__init__ \"{}\" light does not have color_temp, but does have work_mode 'white'. Adding ColorMode.WHITE".format(getattr(self.device, "name")))
510+
color_modes.add(ColorMode.WHITE)
511+
self._white_color_mode = ColorMode.WHITE
495512

496513
if (
497514
dpcode := self.find_dpcode(description.color_data, prefer_function=True)
@@ -518,8 +535,13 @@ def __init__(
518535
):
519536
self._color_data_type = DEFAULT_COLOR_TYPE_DATA_V2
520537

538+
LOGGER.warning("Tuya light.py.__init__ \"{}\" color_modes before filter: {}".format(getattr(self.device, "name"), str([c.name for c in color_modes])))
521539
self._attr_supported_color_modes = filter_supported_color_modes(color_modes)
540+
541+
LOGGER.warning("Tuya light.py.__init__ \"{}\" color_modes after filter: {}".format(getattr(self.device, "name"), str([c.name for c in self._attr_supported_color_modes])))
522542
if len(self._attr_supported_color_modes) == 1:
543+
544+
LOGGER.warning("Tuya light.py.__init__ \"{}\" color_modes fixed color: {}".format(getattr(self.device, "name"), str([c.name for c in self._attr_supported_color_modes])))
523545
# If the light supports only a single color mode, set it now
524546
self._fixed_color_mode = next(iter(self._attr_supported_color_modes))
525547

@@ -530,36 +552,71 @@ def is_on(self) -> bool:
530552

531553
def turn_on(self, **kwargs: Any) -> None:
532554
"""Turn on or control the light."""
555+
LOGGER.warning("Tuya light.py.turn_on kwargs: " + json.dumps(kwargs))
533556
commands = [{"code": self.entity_description.key, "value": True}]
534557

535-
if self._color_temp and ATTR_COLOR_TEMP in kwargs:
558+
# if self._color_temp and ATTR_COLOR_TEMP in kwargs:
559+
# if self._color_mode_dpcode:
560+
# commands += [
561+
# {
562+
# "code": self._color_mode_dpcode,
563+
# "value": WorkMode.WHITE,
564+
# },
565+
# ]
566+
567+
# commands += [
568+
# {
569+
# "code": self._color_temp.dpcode,
570+
# "value": round(
571+
# self._color_temp.remap_value_from(
572+
# kwargs[ATTR_COLOR_TEMP],
573+
# self.min_mireds,
574+
# self.max_mireds,
575+
# reverse=True,
576+
# )
577+
# ),
578+
# },
579+
# ]
580+
581+
# if ATTR_WHITE in kwargs: #set work mode to white if kwargs contains white
582+
# if self._color_mode_dpcode:
583+
# commands += [
584+
# {
585+
# "code": self._color_mode_dpcode,
586+
# "value": WorkMode.WHITE,
587+
# },
588+
# ]
589+
590+
if ATTR_WHITE in kwargs or ATTR_COLOR_TEMP in kwargs:
536591
if self._color_mode_dpcode:
537592
commands += [
538593
{
539594
"code": self._color_mode_dpcode,
540595
"value": WorkMode.WHITE,
541596
},
542597
]
543-
544-
commands += [
545-
{
546-
"code": self._color_temp.dpcode,
547-
"value": round(
548-
self._color_temp.remap_value_from(
549-
kwargs[ATTR_COLOR_TEMP],
550-
self.min_mireds,
551-
self.max_mireds,
552-
reverse=True,
553-
)
554-
),
555-
},
556-
]
598+
599+
if self._white_color_mode == ColorMode.COLOR_TEMP and self._color_temp:
600+
commands += [
601+
{
602+
"code": self._color_temp.dpcode,
603+
"value": round(
604+
self._color_temp.remap_value_from(
605+
kwargs[ATTR_COLOR_TEMP],
606+
self.min_mireds,
607+
self.max_mireds,
608+
reverse=True,
609+
)
610+
),
611+
},
612+
]
557613

558614
if self._color_data_type and (
559615
ATTR_HS_COLOR in kwargs
560616
or (
561617
ATTR_BRIGHTNESS in kwargs
562618
and self.color_mode == ColorMode.HS
619+
and ATTR_WHITE not in kwargs
563620
and ATTR_COLOR_TEMP not in kwargs
564621
)
565622
):
@@ -727,7 +784,7 @@ def color_mode(self) -> ColorMode:
727784
and self.device.status.get(self._color_mode_dpcode) != WorkMode.WHITE
728785
):
729786
return ColorMode.HS
730-
return ColorMode.COLOR_TEMP
787+
return self._white_color_mode
731788

732789
def _get_color_data(self) -> ColorData | None:
733790
"""Get current color data from device."""

homeassistant/components/tuya/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@
4343
"integration_type": "hub",
4444
"iot_class": "cloud_push",
4545
"loggers": ["tuya_iot"],
46-
"requirements": ["tuya-device-sharing-sdk==0.1.9"]
46+
"requirements": ["tuya-device-sharing-sdk==0.1.9"],
47+
"version": "0.0.1"
4748
}

0 commit comments

Comments
 (0)
0