Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Auto auto-off module for smartdevice #760

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 5 commits into from
Feb 19, 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
3 changes: 2 additions & 1 deletion kasa/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ async def set_value(self, value):
"""Set the value."""
if self.attribute_setter is None:
raise ValueError("Tried to set read-only feature.")
return await getattr(self.device, self.attribute_setter)(value)
container = self.container if self.container is not None else self.device
return await getattr(container, self.attribute_setter)(value)
2 changes: 2 additions & 0 deletions kasa/smart/modules/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Modules for SMART devices."""
from .autooffmodule import AutoOffModule
from .childdevicemodule import ChildDeviceModule
from .cloudmodule import CloudModule
from .devicemodule import DeviceModule
Expand All @@ -12,6 +13,7 @@
"EnergyModule",
"DeviceModule",
"ChildDeviceModule",
"AutoOffModule",
"LedModule",
"CloudModule",
"LightTransitionModule",
Expand Down
84 changes: 84 additions & 0 deletions kasa/smart/modules/autooffmodule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""Implementation of auto off module."""
from datetime import datetime, timedelta
from typing import TYPE_CHECKING, Dict, Optional

from ...feature import Feature
from ..smartmodule import SmartModule

if TYPE_CHECKING:
from ..smartdevice import SmartDevice


class AutoOffModule(SmartModule):
"""Implementation of auto off module."""

REQUIRED_COMPONENT = "auto_off"
QUERY_GETTER_NAME = "get_auto_off_config"

def __init__(self, device: "SmartDevice", module: str):
super().__init__(device, module)
self._add_feature(
Feature(
device,
"Auto off enabled",
container=self,
attribute_getter="enabled",
attribute_setter="set_enabled",
)
)
self._add_feature(
Feature(
device,
"Auto off minutes",
container=self,
attribute_getter="delay",
attribute_setter="set_delay",
)
)
self._add_feature(
Feature(
device, "Auto off at", container=self, attribute_getter="auto_off_at"
)
)

def query(self) -> Dict:
"""Query to execute during the update cycle."""
return {self.QUERY_GETTER_NAME: {"start_index": 0}}

@property
def enabled(self) -> bool:
"""Return True if enabled."""
return self.data["enable"]

def set_enabled(self, enable: bool):
"""Enable/disable auto off."""
return self.call(
"set_auto_off_config",
{"enable": enable, "delay_min": self.data["delay_min"]},
)

@property
def delay(self) -> int:
"""Return time until auto off."""
return self.data["delay_min"]

def set_delay(self, delay: int):
"""Set time until auto off."""
return self.call(
"set_auto_off_config", {"delay_min": delay, "enable": self.data["enable"]}
)

@property
def is_timer_active(self) -> bool:
"""Return True is auto-off timer is active."""
return self._device.sys_info["auto_off_status"] == "on"

@property
def auto_off_at(self) -> Optional[datetime]:
"""Return when the device will be turned off automatically."""
if not self.is_timer_active:
return None

sysinfo = self._device.sys_info

return self._device.time + timedelta(seconds=sysinfo["auto_off_remain_time"])
1 change: 1 addition & 0 deletions kasa/smart/smartdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ..feature import Feature, FeatureType
from ..smartprotocol import SmartProtocol
from .modules import ( # noqa: F401
AutoOffModule,
ChildDeviceModule,
CloudModule,
DeviceModule,
Expand Down
1 change: 1 addition & 0 deletions kasa/tests/fakeprotocol_smart.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def credentials_hash(self):

FIXTURE_MISSING_MAP = {
"get_wireless_scan_info": ("wireless", {"ap_list": [], "wep_supported": False}),
"get_auto_off_config": ("auto_off", {'delay_min': 10, 'enable': False}),
"get_led_info": (
"led",
{
Expand Down