3
3
from __future__ import annotations
4
4
5
5
from collections .abc import Sequence
6
- from dataclasses import asdict
6
+ from dataclasses import asdict , dataclass
7
7
from typing import TYPE_CHECKING
8
8
9
- from pydantic . v1 import BaseModel , Field
9
+ from mashumaro . config import BaseConfig
10
10
11
11
from ...exceptions import KasaException
12
12
from ...interfaces import LightPreset as LightPresetInterface
13
13
from ...interfaces import LightState
14
+ from ...json import DataClassJSONMixin
14
15
from ...module import Module
15
16
from ..iotmodule import IotModule
16
17
21
22
# error: Signature of "__replace__" incompatible with supertype "LightState"
22
23
23
24
24
- class IotLightPreset (BaseModel , LightState ): # type: ignore[override]
25
+ @dataclass (kw_only = True , repr = False )
26
+ class IotLightPreset (DataClassJSONMixin , LightState ): # type: ignore[override]
25
27
"""Light configuration preset."""
26
28
27
- index : int = Field (kw_only = True )
28
- brightness : int = Field (kw_only = True )
29
+ class Config (BaseConfig ):
30
+ """Config class."""
31
+
32
+ omit_none = True
33
+
34
+ index : int
35
+ brightness : int
29
36
30
37
# These are not available for effect mode presets on light strips
31
- hue : int | None = Field ( kw_only = True , default = None )
32
- saturation : int | None = Field ( kw_only = True , default = None )
33
- color_temp : int | None = Field ( kw_only = True , default = None )
38
+ hue : int | None = None
39
+ saturation : int | None = None
40
+ color_temp : int | None = None
34
41
35
42
# Variables for effect mode presets
36
- custom : int | None = Field ( kw_only = True , default = None )
37
- id : str | None = Field ( kw_only = True , default = None )
38
- mode : int | None = Field ( kw_only = True , default = None )
43
+ custom : int | None = None
44
+ id : str | None = None
45
+ mode : int | None = None
39
46
40
47
41
48
class LightPreset (IotModule , LightPresetInterface ):
@@ -47,7 +54,7 @@ class LightPreset(IotModule, LightPresetInterface):
47
54
async def _post_update_hook (self ) -> None :
48
55
"""Update the internal presets."""
49
56
self ._presets = {
50
- f"Light preset { index + 1 } " : IotLightPreset ( ** vals )
57
+ f"Light preset { index + 1 } " : IotLightPreset . from_dict ( vals )
51
58
for index , vals in enumerate (self .data ["preferred_state" ])
52
59
# Devices may list some light effects along with normal presets but these
53
60
# are handled by the LightEffect module so exclude preferred states with id
@@ -157,4 +164,4 @@ async def _deprecated_save_preset(self, preset: IotLightPreset) -> dict:
157
164
if preset .index >= len (self ._presets ):
158
165
raise KasaException ("Invalid preset index" )
159
166
160
- return await self .call ("set_preferred_state" , preset .dict ( exclude_none = True ))
167
+ return await self .call ("set_preferred_state" , preset .to_dict ( ))
0 commit comments