mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-06 10:44:04 +00:00
Migrate IotLightPreset to mashumaru (#1275)
This commit is contained in:
@@ -3,14 +3,15 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Sequence
|
||||
from dataclasses import asdict
|
||||
from dataclasses import asdict, dataclass
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from pydantic.v1 import BaseModel, Field
|
||||
from mashumaro.config import BaseConfig
|
||||
|
||||
from ...exceptions import KasaException
|
||||
from ...interfaces import LightPreset as LightPresetInterface
|
||||
from ...interfaces import LightState
|
||||
from ...json import DataClassJSONMixin
|
||||
from ...module import Module
|
||||
from ..iotmodule import IotModule
|
||||
|
||||
@@ -21,21 +22,27 @@ if TYPE_CHECKING:
|
||||
# error: Signature of "__replace__" incompatible with supertype "LightState"
|
||||
|
||||
|
||||
class IotLightPreset(BaseModel, LightState): # type: ignore[override]
|
||||
@dataclass(kw_only=True, repr=False)
|
||||
class IotLightPreset(DataClassJSONMixin, LightState): # type: ignore[override]
|
||||
"""Light configuration preset."""
|
||||
|
||||
index: int = Field(kw_only=True)
|
||||
brightness: int = Field(kw_only=True)
|
||||
class Config(BaseConfig):
|
||||
"""Config class."""
|
||||
|
||||
omit_none = True
|
||||
|
||||
index: int
|
||||
brightness: int
|
||||
|
||||
# These are not available for effect mode presets on light strips
|
||||
hue: int | None = Field(kw_only=True, default=None)
|
||||
saturation: int | None = Field(kw_only=True, default=None)
|
||||
color_temp: int | None = Field(kw_only=True, default=None)
|
||||
hue: int | None = None
|
||||
saturation: int | None = None
|
||||
color_temp: int | None = None
|
||||
|
||||
# Variables for effect mode presets
|
||||
custom: int | None = Field(kw_only=True, default=None)
|
||||
id: str | None = Field(kw_only=True, default=None)
|
||||
mode: int | None = Field(kw_only=True, default=None)
|
||||
custom: int | None = None
|
||||
id: str | None = None
|
||||
mode: int | None = None
|
||||
|
||||
|
||||
class LightPreset(IotModule, LightPresetInterface):
|
||||
@@ -47,7 +54,7 @@ class LightPreset(IotModule, LightPresetInterface):
|
||||
async def _post_update_hook(self) -> None:
|
||||
"""Update the internal presets."""
|
||||
self._presets = {
|
||||
f"Light preset {index+1}": IotLightPreset(**vals)
|
||||
f"Light preset {index+1}": IotLightPreset.from_dict(vals)
|
||||
for index, vals in enumerate(self.data["preferred_state"])
|
||||
# Devices may list some light effects along with normal presets but these
|
||||
# are handled by the LightEffect module so exclude preferred states with id
|
||||
@@ -157,4 +164,4 @@ class LightPreset(IotModule, LightPresetInterface):
|
||||
if preset.index >= len(self._presets):
|
||||
raise KasaException("Invalid preset index")
|
||||
|
||||
return await self.call("set_preferred_state", preset.dict(exclude_none=True))
|
||||
return await self.call("set_preferred_state", preset.to_dict())
|
||||
|
Reference in New Issue
Block a user