2
2
3
3
from __future__ import annotations
4
4
5
+ import logging #at the top of the file
6
+
5
7
from dataclasses import dataclass , field
6
8
import json
7
9
from typing import Any , cast
12
14
ATTR_BRIGHTNESS ,
13
15
ATTR_COLOR_TEMP ,
14
16
ATTR_HS_COLOR ,
17
+ ATTR_WHITE ,
15
18
ColorMode ,
16
19
LightEntity ,
17
20
LightEntityDescription ,
27
30
from .entity import IntegerTypeData , TuyaEntity
28
31
from .util import remap_value
29
32
33
+ LOGGER = logging .getLogger (__package__ ) #at the top of the file after all the imports
30
34
31
35
@dataclass
32
36
class ColorTypeData :
@@ -456,6 +460,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
456
460
_color_data_type : ColorTypeData | None = None
457
461
_color_mode : DPCode | None = None
458
462
_color_temp : IntegerTypeData | None = None
463
+ _white_color_mode : ColorMode | None = None
459
464
_fixed_color_mode : ColorMode | None = None
460
465
461
466
def __init__ (
@@ -474,6 +479,7 @@ def __init__(
474
479
self ._color_mode_dpcode = self .find_dpcode (
475
480
description .color_mode , prefer_function = True
476
481
)
482
+ LOGGER .warning ("Tuya light.py.__init__ \" {}\" _color_mode_dpcode {}" .format (getattr (self .device , "name" ), str (self ._color_mode_dpcode )))
477
483
478
484
if int_type := self .find_dpcode (
479
485
description .brightness , dptype = DPType .INTEGER , prefer_function = True
@@ -492,6 +498,17 @@ def __init__(
492
498
):
493
499
self ._color_temp = int_type
494
500
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
495
512
496
513
if (
497
514
dpcode := self .find_dpcode (description .color_data , prefer_function = True )
@@ -518,8 +535,13 @@ def __init__(
518
535
):
519
536
self ._color_data_type = DEFAULT_COLOR_TYPE_DATA_V2
520
537
538
+ LOGGER .warning ("Tuya light.py.__init__ \" {}\" color_modes before filter: {}" .format (getattr (self .device , "name" ), str ([c .name for c in color_modes ])))
521
539
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 ])))
522
542
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 ])))
523
545
# If the light supports only a single color mode, set it now
524
546
self ._fixed_color_mode = next (iter (self ._attr_supported_color_modes ))
525
547
@@ -530,36 +552,71 @@ def is_on(self) -> bool:
530
552
531
553
def turn_on (self , ** kwargs : Any ) -> None :
532
554
"""Turn on or control the light."""
555
+ LOGGER .warning ("Tuya light.py.turn_on kwargs: " + json .dumps (kwargs ))
533
556
commands = [{"code" : self .entity_description .key , "value" : True }]
534
557
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 :
536
591
if self ._color_mode_dpcode :
537
592
commands += [
538
593
{
539
594
"code" : self ._color_mode_dpcode ,
540
595
"value" : WorkMode .WHITE ,
541
596
},
542
597
]
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
+ ]
557
613
558
614
if self ._color_data_type and (
559
615
ATTR_HS_COLOR in kwargs
560
616
or (
561
617
ATTR_BRIGHTNESS in kwargs
562
618
and self .color_mode == ColorMode .HS
619
+ and ATTR_WHITE not in kwargs
563
620
and ATTR_COLOR_TEMP not in kwargs
564
621
)
565
622
):
@@ -727,7 +784,7 @@ def color_mode(self) -> ColorMode:
727
784
and self .device .status .get (self ._color_mode_dpcode ) != WorkMode .WHITE
728
785
):
729
786
return ColorMode .HS
730
- return ColorMode . COLOR_TEMP
787
+ return self . _white_color_mode
731
788
732
789
def _get_color_data (self ) -> ColorData | None :
733
790
"""Get current color data from device."""
0 commit comments