Skip to content

Migrate IotLightPreset to mashumaru #1275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions kasa/iot/iotbulb.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ class IotBulb(IotDevice):

Bulb configuration presets can be accessed using the :func:`presets` property:

>>> bulb.presets
[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)]
>>> [ preset.to_dict() for preset in bulb.presets }
[{'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}]

To modify an existing preset, pass :class:`~kasa.interfaces.light.LightPreset`
instance to :func:`save_preset` method:
Expand Down
33 changes: 20 additions & 13 deletions kasa/iot/modules/lightpreset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -21,21 +22,27 @@
# 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):
Expand All @@ -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
Expand Down Expand Up @@ -157,4 +164,4 @@ async def _deprecated_save_preset(self, preset: IotLightPreset) -> dict:
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())
Loading