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-05-24 17:39:10 +00:00
|
|
|
import logging
|
2024-02-19 20:11:11 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
from ...feature import Feature
|
|
|
|
from ..smartmodule import SmartModule
|
|
|
|
|
2024-05-24 17:39:10 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2024-02-19 20:11:11 +00:00
|
|
|
|
|
|
|
|
2024-05-11 18:28:18 +00:00
|
|
|
class AutoOff(SmartModule):
|
2024-02-19 20:11:11 +00:00
|
|
|
"""Implementation of auto off module."""
|
|
|
|
|
|
|
|
REQUIRED_COMPONENT = "auto_off"
|
|
|
|
QUERY_GETTER_NAME = "get_auto_off_config"
|
|
|
|
|
2024-05-24 17:39:10 +00:00
|
|
|
def _initialize_features(self):
|
|
|
|
"""Initialize features after the initial update."""
|
|
|
|
if not isinstance(self.data, dict):
|
|
|
|
_LOGGER.warning(
|
|
|
|
"No data available for module, skipping %s: %s", self, self.data
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2024-02-19 20:11:11 +00:00
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
2024-05-24 17:39:10 +00:00
|
|
|
self._device,
|
2024-05-07 09:13:35 +00:00
|
|
|
id="auto_off_enabled",
|
|
|
|
name="Auto off enabled",
|
2024-02-19 20:11:11 +00:00
|
|
|
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(
|
2024-05-24 17:39:10 +00:00
|
|
|
self._device,
|
2024-05-07 09:13:35 +00:00
|
|
|
id="auto_off_minutes",
|
|
|
|
name="Auto off minutes",
|
2024-02-19 20:11:11 +00:00
|
|
|
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(
|
2024-05-24 17:39:10 +00:00
|
|
|
self._device,
|
2024-05-07 09:13:35 +00:00
|
|
|
id="auto_off_at",
|
|
|
|
name="Auto off at",
|
|
|
|
container=self,
|
|
|
|
attribute_getter="auto_off_at",
|
|
|
|
category=Feature.Category.Info,
|
2024-02-19 20:11:11 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
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"]
|
|
|
|
|
2024-05-02 13:32:06 +00:00
|
|
|
async def set_enabled(self, enable: bool):
|
2024-02-19 20:11:11 +00:00
|
|
|
"""Enable/disable auto off."""
|
2024-05-02 13:32:06 +00:00
|
|
|
return await self.call(
|
2024-02-19 20:11:11 +00:00
|
|
|
"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"]
|
|
|
|
|
2024-05-02 13:32:06 +00:00
|
|
|
async def set_delay(self, delay: int):
|
2024-02-19 20:11:11 +00:00
|
|
|
"""Set time until auto off."""
|
2024-05-02 13:32:06 +00:00
|
|
|
return await self.call(
|
2024-02-19 20:11:11 +00:00
|
|
|
"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"])
|