8000 Migrate triggerlogs to mashumaru by sdb9696 · Pull Request #1277 · python-kasa/python-kasa · GitHub
[go: up one dir, main page]

Skip to content

Migrate triggerlogs to mashumaru #1277

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 3 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
15 changes: 9 additions & 6 deletions kasa/smart/modules/triggerlogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

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


class LogEntry(BaseModel):
@dataclass
class LogEntry(DataClassDictMixin):
"""Presentation of a single log entry."""

id: int
event_id: str = Field(alias="eventId")
timestamp: datetime
event_id: Annotated[str, Alias("eventId")]
timestamp: int
Comment on lines -16 to +20
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note the change from datetime to int as the timestamp is an int.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's what it shows to me on the water leak sensor with the current master branch, so datetime is actually correct:

$ kasa --host 192.168.xx device logs --child asdf
id=114 event_id='d595356d-4953-5654-d59d-b92b6aca9ab2' timestamp=datetime.datetime(2024, 10, 9, 13, 31, 57, tzinfo=datetime.timezone.utc) event='waterDry'
id=113 event_id='c43fc234-4ff2-ac03-d4bf-0254ff2ac03d' timestamp=datetime.datetime(2024, 10, 9, 13, 31, 54, tzinfo=datetime.timezone.utc) event='waterLeak'
id=112 event_id='3e68c39e-b027-e405-7d41-d714fd81bfa8' timestamp=datetime.datetime(2024, 10, 9, 10, 52, 9, tzinfo=datetime.timezone.utc) event='waterDry'
id=111 event_id='0e8743a9-d46a-bdde-67bb-d562b9542219' timestamp=datetime.datetime(2024, 10, 9, 10, 52, 3, tzinfo=datetime.timezone.utc) event='waterLeak'

I'm not sure how to tell mashumaro to handle this though, and we should probably also handle the timezone correctly (at some point, so worth adding a comment on that).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to keep as an int so there's no magic timezone unaware conversion going on, then we can add a cached property that get's the timezone from the device and does a proper conversion to a timezone aware datetime. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, we could do that too, then this PR is good to go!

event: str


Expand All @@ -31,4 +34,4 @@ def query(self) -> dict:
@property
def logs(self) -> list[LogEntry]:
"""Return logs."""
return parse_obj_as(list[LogEntry], self.data["logs"])
return [LogEntry.from_dict(log) for log in self.data["logs"]]
22 changes: 22 additions & 0 deletions tests/smart/modules/test_triggerlogs.py
Original file line number Diff line number Diff line change
@@ -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
0