2024-02-19 20:11:11 +00:00
|
|
|
"""Implementation of auto off module."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-02-19 20:11:11 +00:00
|
|
|
from datetime import datetime, timedelta
|
2024-04-17 13:39:24 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2024-02-19 20:11:11 +00:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
def __init__(self, device: SmartDevice, module: str):
|
2024-02-19 20:11:11 +00:00
|
|
|
super().__init__(device, module)
|
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
device,
|
|
|
|
"Auto off enabled",
|
|
|
|
container=self,
|
|
|
|
attribute_getter="enabled",
|
|
|
|
attribute_setter="set_enabled",
|
2024-04-24 16:38:52 +00:00
|
|
|
type=Feature.Type.Switch,
|
2024-02-19 20:11:11 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
device,
|
|
|
|
"Auto off minutes",
|
|
|
|
container=self,
|
|
|
|
attribute_getter="delay",
|
|
|
|
attribute_setter="set_delay",
|
2024-04-24 16:38:52 +00:00
|
|
|
type=Feature.Type.Number,
|
2024-02-19 20:11:11 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
device, "Auto off at", container=self, attribute_getter="auto_off_at"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
def query(self) -> dict:
|
2024-02-19 20:11:11 +00:00
|
|
|
"""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
|
2024-04-17 13:39:24 +00:00
|
|
|
def auto_off_at(self) -> datetime | None:
|
2024-02-19 20:11:11 +00:00
|
|
|
"""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"])
|