8000 Migrate IotLightPreset to mashumaru (#1275) · python-kasa/python-kasa@4a04651 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4a04651

Browse files
sdb9696rytilahti
authored andcommitted
Migrate IotLightPreset to mashumaru (#1275)
1 parent bfa7ab7 commit 4a04651

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

kasa/iot/iotbulb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ class IotBulb(IotDevice):
178178
179179
Bulb configuration presets can be accessed using the :func:`presets` property:
180180
181-
>>> bulb.presets
182-
[IotLightPreset(index=0, brightness=50, hue=0, saturation=0, color_temp=2700, custom=None, id=None, mode=None), IotLightPreset(index=1, brightness=100, hue=0, saturation=75, color_temp=0, custom=None, id=None, mode=None), IotLightPreset(index=2, brightness=100, hue=120, saturation=75, color_temp=0, custom=None, id=None, mode=None), IotLightPreset(index=3, brightness=100, hue=240, saturation=75, color_temp=0, custom=None, id=None, mode=None)]
181+
>>> [ preset.to_dict() for preset in bulb.presets }
182+
[{'brightness': 50, 'hue': 0, 'saturation': 0, 'color_temp': 2700, 'index': 0}, {'brightness': 100, 'hue': 0, 'saturation': 75, 'color_temp': 0, 'index': 1}, {'brightness': 100, 'hue': 120, 'saturation': 75, 'color_temp': 0, 'index': 2}, {'brightness': 100, 'hue': 240, 'saturation': 75, 'color_temp': 0, 'index': 3}]
183183
184184
To modify an existing preset, pass :class:`~kasa.interfaces.light.LightPreset`
185185
instance to :func:`save_preset` method:

kasa/iot/modules/lightpreset.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
from __future__ import annotations
44

55
from collections.abc import Sequence
6-
from dataclasses import asdict
6+
from dataclasses import asdict, dataclass
77
from typing import TYPE_CHECKING
88

9-
from pydantic.v1 import BaseModel, Field
9+
from mashumaro.config import BaseConfig
1010

1111
from ...exceptions import KasaException
1212
from ...interfaces import LightPreset as LightPresetInterface
1313
from ...interfaces import LightState
14+
from ...json import DataClassJSONMixin
1415
from ...module import Module
1516
from ..iotmodule import IotModule
1617

@@ -21,21 +22,27 @@
2122
# error: Signature of "__replace__" incompatible with supertype "LightState"
2223

2324

24-
class IotLightPreset(BaseModel, LightState): # type: ignore[override]
25+
@dataclass(kw_only=True, repr=False)
26+
class IotLightPreset(DataClassJSONMixin, LightState): # type: ignore[override]
2527
"""Light configuration preset."""
2628

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
2936

3037
# 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
3441

3542
# 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
3946

4047

4148
class LightPreset(IotModule, LightPresetInterface):
@@ -47,7 +54,7 @@ class LightPreset(IotModule, LightPresetInterface):
4754
async def _post_update_hook(self) -> None:
4855
"""Update the internal presets."""
4956
self._presets = {
50-
f"Light preset {index+1}": IotLightPreset(**vals)
57+
f"Light preset {index+1}": IotLightPreset.from_dict(vals)
5158
for index, vals in enumerate(self.data["preferred_state"])
5259
# Devices may list some light effects along with normal presets but these
5360
# 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:
157164
if preset.index >= len(self._presets):
158165
raise KasaException("Invalid preset index")
159166

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

Comments
 (0)
0