4
4
5
5
import logging
6
6
import re
7
+ from dataclasses import dataclass
7
8
from enum import Enum
8
- from typing import cast
9
+ from typing import Annotated , cast
9
10
10
- from pydantic .v1 import BaseModel , Field , root_validator
11
+ from mashumaro import DataClassDictMixin
12
+ from mashumaro .config import BaseConfig
13
+ from mashumaro .types import Alias
11
14
12
15
from ..device_type import DeviceType
13
16
from ..deviceconfig import DeviceConfig
@@ -35,9 +38,12 @@ class BehaviorMode(str, Enum):
35
38
Last = "last_status"
36
39
#: Use chosen preset.
37
40
Preset = "customize_preset"
41
+ #: Circadian
42
+ Circadian = "circadian"
38
43
39
44
40
- class TurnOnBehavior (BaseModel ):
45
+ @dataclass
46
+ class TurnOnBehavior (DataClassDictMixin ):
41
47
"""Model to present a single turn on behavior.
42
48
43
49
:param int preset: the index number of wanted preset.
@@ -48,34 +54,30 @@ class TurnOnBehavior(BaseModel):
48
54
to contain either the preset index, or ``None`` for the last known state.
49
55
"""
50
56
51
- #: Index of preset to use, or ``None`` for the last known state.
52
- preset : int | None = Field (alias = "index" , default = None )
53
- #: Wanted behavior
54
- mode : BehaviorMode
55
-
56
- @root_validator
57
- def _mode_based_on_preset (cls , values : dict ) -> dict :
58
- """Set the mode based on the preset value."""
59
- if values ["preset" ] is not None :
60
- values ["mode" ] = BehaviorMode .Preset
61
- else :
62
- values ["mode" ] = BehaviorMode .Last
57
+ class Config (BaseConfig ):
58
+ """Serialization config."""
63
59
64
- return values
60
+ omit_none = True
61
+ serialize_by_alias = True
65
62
66
- class Config :
67
- """Configuration to make the validator run when changing the values."""
68
-
69
- validate_assignment = True
63
+ #: Wanted behavior
64
+ mode : BehaviorMode
65
+ #: Index of preset to use, or ``None`` for the last known state.
66
+ preset : Annotated [int | None , Alias ("index" )] = None
67
+ brightness : int | None = None
68
+ color_temp : int | None = None
69
+ hue : int | None = None
70
+ saturation : int | None = None
70
71
71
72
72
- class TurnOnBehaviors (BaseModel ):
73
+ @dataclass
74
+ class TurnOnBehaviors (DataClassDictMixin ):
73
75
"""Model to contain turn on behaviors."""
74
76
75
77
#: The behavior when the bulb is turned on programmatically.
76
- soft : TurnOnBehavior = Field ( alias = "soft_on" )
78
+ soft : Annotated [ TurnOnBehavior , Alias ( "soft_on" )]
77
79
#: The behavior when the bulb has been off from mains power.
78
- hard : TurnOnBehavior = Field ( alias = "hard_on" )
80
+ hard : Annotated [ TurnOnBehavior , Alias ( "hard_on" )]
79
81
80
82
81
83
TPLINK_KELVIN = {
@@ -303,7 +305,7 @@ async def get_light_details(self) -> dict[str, int]:
303
305
304
306
async def get_turn_on_behavior (self ) -> TurnOnBehaviors :
305
307
"""Return the behavior for turning the bulb on."""
306
- return TurnOnBehaviors .parse_obj (
308
+ return TurnOnBehaviors .from_dict (
307
309
await self ._query_helper (self .LIGHT_SERVICE , "get_default_behavior" )
308
310
)
309
311
@@ -314,7 +316,7 @@ async def set_turn_on_behavior(self, behavior: TurnOnBehaviors) -> dict:
314
316
you should use :func:`get_turn_on_behavior` to get the current settings.
315
317
"""
316
318
return await self ._query_helper (
317
- self .LIGHT_SERVICE , "set_default_behavior" , behavior .dict ( by_alias = True )
319
+ self .LIGHT_SERVICE , "set_default_behavior" , behavior .to_dict ( )
318
320
)
319
321
320
322
async def get_light_state (self ) -> dict [str , dict ]:
0 commit comments