From 70c96b5a5d7bcced7e2be9f916842478154e63ad Mon Sep 17 00:00:00 2001 From: "Teemu R." Date: Fri, 1 Nov 2024 16:36:09 +0100 Subject: [PATCH] Initial trigger logs implementation (#900) Co-authored-by: Steven B. <51370195+sdb9696@users.noreply.github.com> --- kasa/cli/device.py | 10 +++++++++ kasa/module.py | 1 + kasa/smart/modules/__init__.py | 2 ++ kasa/smart/modules/triggerlogs.py | 34 +++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 kasa/smart/modules/triggerlogs.py diff --git a/kasa/cli/device.py b/kasa/cli/device.py index 4a933b87..9814108c 100644 --- a/kasa/cli/device.py +++ b/kasa/cli/device.py @@ -193,3 +193,13 @@ async def update_credentials(dev, username, password): click.confirm("Do you really want to replace the existing credentials?", abort=True) return await dev.update_credentials(username, password) + + +@device.command(name="logs") +@pass_dev_or_child +async def child_logs(dev): + """Print child device trigger logs.""" + if logs := dev.modules.get(Module.TriggerLogs): + await dev.update(update_children=True) + for entry in logs.logs: + print(entry) diff --git a/kasa/module.py b/kasa/module.py index e10b2d63..646755e5 100644 --- a/kasa/module.py +++ b/kasa/module.py @@ -127,6 +127,7 @@ class Module(ABC): WaterleakSensor: Final[ModuleName[smart.WaterleakSensor]] = ModuleName( "WaterleakSensor" ) + TriggerLogs: Final[ModuleName[smart.TriggerLogs]] = ModuleName("TriggerLogs") # SMARTCAMERA only modules Camera: Final[ModuleName[experimental.Camera]] = ModuleName("Camera") diff --git a/kasa/smart/modules/__init__.py b/kasa/smart/modules/__init__.py index 24d5749e..d1b8dc0b 100644 --- a/kasa/smart/modules/__init__.py +++ b/kasa/smart/modules/__init__.py @@ -27,6 +27,7 @@ from .reportmode import ReportMode from .temperaturecontrol import TemperatureControl from .temperaturesensor import TemperatureSensor from .time import Time +from .triggerlogs import TriggerLogs from .waterleaksensor import WaterleakSensor __all__ = [ @@ -56,6 +57,7 @@ __all__ = [ "WaterleakSensor", "ContactSensor", "MotionSensor", + "TriggerLogs", "FrostProtection", "SmartLightEffect", ] diff --git a/kasa/smart/modules/triggerlogs.py b/kasa/smart/modules/triggerlogs.py new file mode 100644 index 00000000..480c72f5 --- /dev/null +++ b/kasa/smart/modules/triggerlogs.py @@ -0,0 +1,34 @@ +"""Implementation of trigger logs module.""" + +from __future__ import annotations + +from datetime import datetime + +from pydantic.v1 import BaseModel, Field, parse_obj_as + +from ..smartmodule import SmartModule + + +class LogEntry(BaseModel): + """Presentation of a single log entry.""" + + id: int + event_id: str = Field(alias="eventId") + timestamp: datetime + event: str + + +class TriggerLogs(SmartModule): + """Implementation of trigger logs.""" + + REQUIRED_COMPONENT = "trigger_log" + MINIMUM_UPDATE_INTERVAL_SECS = 60 * 60 + + def query(self) -> dict: + """Query to execute during the update cycle.""" + return {"get_trigger_logs": {"start_id": 0}} + + @property + def logs(self) -> list[LogEntry]: + """Return logs.""" + return parse_obj_as(list[LogEntry], self.data["logs"])