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."""
|
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",
|
2024-06-21 18:25:55 +00:00
|
|
|
name="Auto off in",
|
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-07-30 18:23:07 +00:00
|
|
|
unit_getter=lambda: "min", # ha-friendly unit, see UnitOfTime.MINUTES
|
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-06-25 16:30:36 +00:00
|
|
|
type=Feature.Type.Sensor,
|
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"])
|
2024-06-10 14:47:00 +00:00
|
|
|
|
|
|
|
async def _check_supported(self):
|
|
|
|
"""Additional check to see if the module is supported by the device.
|
|
|
|
|
|
|
|
Parent devices that report components of children such as P300 will not have
|
|
|
|
the auto_off_status is sysinfo.
|
|
|
|
"""
|
|
|
|
return "auto_off_status" in self._device.sys_info
|