mirror of
https://github.com/python-kasa/python-kasa.git
synced 2024-12-22 19:23:34 +00:00
Migrate triggerlogs to mashumaru (#1277)
This commit is contained in:
parent
bbe68a5fe9
commit
df48c21900
@ -2,19 +2,22 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import datetime
|
from dataclasses import dataclass
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
from pydantic.v1 import BaseModel, Field, parse_obj_as
|
from mashumaro import DataClassDictMixin
|
||||||
|
from mashumaro.types import Alias
|
||||||
|
|
||||||
from ..smartmodule import SmartModule
|
from ..smartmodule import SmartModule
|
||||||
|
|
||||||
|
|
||||||
class LogEntry(BaseModel):
|
@dataclass
|
||||||
|
class LogEntry(DataClassDictMixin):
|
||||||
"""Presentation of a single log entry."""
|
"""Presentation of a single log entry."""
|
||||||
|
|
||||||
id: int
|
id: int
|
||||||
event_id: str = Field(alias="eventId")
|
event_id: Annotated[str, Alias("eventId")]
|
||||||
timestamp: datetime
|
timestamp: int
|
||||||
event: str
|
event: str
|
||||||
|
|
||||||
|
|
||||||
@ -31,4 +34,4 @@ class TriggerLogs(SmartModule):
|
|||||||
@property
|
@property
|
||||||
def logs(self) -> list[LogEntry]:
|
def logs(self) -> list[LogEntry]:
|
||||||
"""Return logs."""
|
"""Return logs."""
|
||||||
return parse_obj_as(list[LogEntry], self.data["logs"])
|
return [LogEntry.from_dict(log) for log in self.data["logs"]]
|
||||||
|
22
tests/smart/modules/test_triggerlogs.py
Normal file
22
tests/smart/modules/test_triggerlogs.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
from kasa import Device, Module
|
||||||
|
|
||||||
|
from ...device_fixtures import parametrize
|
||||||
|
|
||||||
|
triggerlogs = parametrize(
|
||||||
|
"has trigger_logs",
|
||||||
|
component_filter="trigger_log",
|
||||||
|
protocol_filter={"SMART", "SMART.CHILD"},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@triggerlogs
|
||||||
|
async def test_trigger_logs(dev: Device):
|
||||||
|
"""Test that features are registered and work as expected."""
|
||||||
|
triggerlogs = dev.modules.get(Module.TriggerLogs)
|
||||||
|
assert triggerlogs is not None
|
||||||
|
if logs := triggerlogs.logs:
|
||||||
|
first = logs[0]
|
||||||
|
assert isinstance(first.id, int)
|
||||||
|
assert isinstance(first.timestamp, int)
|
||||||
|
assert isinstance(first.event, str)
|
||||||
|
assert isinstance(first.event_id, str)
|
Loading…
Reference in New Issue
Block a user