FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Migrate triggerlogs to mashumaru by sdb9696 · Pull Request #1277 · python-kasa/python-kasa · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
15 changes: 9 additions & 6 deletions kasa/smart/modules/triggerlogs.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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)

Back | FazBrowse Home | New Git URL