2021-11-07 01:41:12 +00:00
|
|
|
"""Base implementation for all rule-based modules."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-11-07 01:41:12 +00:00
|
|
|
import logging
|
|
|
|
from enum import Enum
|
|
|
|
from typing import Dict, List, Optional
|
|
|
|
|
2024-05-01 13:59:35 +00:00
|
|
|
from pydantic.v1 import BaseModel
|
2021-11-07 01:41:12 +00:00
|
|
|
|
2024-02-19 17:01:31 +00:00
|
|
|
from ..iotmodule import IotModule, merge
|
2021-11-07 01:41:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Action(Enum):
|
|
|
|
"""Action to perform."""
|
|
|
|
|
|
|
|
Disabled = -1
|
|
|
|
TurnOff = 0
|
|
|
|
TurnOn = 1
|
|
|
|
Unknown = 2
|
|
|
|
|
|
|
|
|
|
|
|
class TimeOption(Enum):
|
|
|
|
"""Time when the action is executed."""
|
|
|
|
|
|
|
|
Disabled = -1
|
|
|
|
Enabled = 0
|
|
|
|
AtSunrise = 1
|
|
|
|
AtSunset = 2
|
|
|
|
|
|
|
|
|
|
|
|
class Rule(BaseModel):
|
|
|
|
"""Representation of a rule."""
|
|
|
|
|
|
|
|
id: str
|
|
|
|
name: str
|
|
|
|
enable: bool
|
2024-04-17 13:39:24 +00:00
|
|
|
wday: List[int] # noqa: UP006
|
2021-11-07 01:41:12 +00:00
|
|
|
repeat: bool
|
|
|
|
|
|
|
|
# start action
|
2024-04-17 13:39:24 +00:00
|
|
|
sact: Optional[Action] # noqa: UP007
|
2021-11-07 01:41:12 +00:00
|
|
|
stime_opt: TimeOption
|
|
|
|
smin: int
|
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
eact: Optional[Action] # noqa: UP007
|
2021-11-07 01:41:12 +00:00
|
|
|
etime_opt: TimeOption
|
|
|
|
emin: int
|
|
|
|
|
|
|
|
# Only on bulbs
|
2024-04-17 13:39:24 +00:00
|
|
|
s_light: Optional[Dict] # noqa: UP006,UP007
|
2021-11-07 01:41:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
class RuleModule(IotModule):
|
2021-11-07 01:41:12 +00:00
|
|
|
"""Base class for rule-based modules, such as countdown and antitheft."""
|
|
|
|
|
|
|
|
def query(self):
|
|
|
|
"""Prepare the query for rules."""
|
|
|
|
q = self.query_for_command("get_rules")
|
|
|
|
return merge(q, self.query_for_command("get_next_action"))
|
|
|
|
|
|
|
|
@property
|
2024-04-17 13:39:24 +00:00
|
|
|
def rules(self) -> list[Rule]:
|
2021-11-07 01:41:12 +00:00
|
|
|
"""Return the list of rules for the service."""
|
|
|
|
try:
|
|
|
|
return [
|
|
|
|
Rule.parse_obj(rule) for rule in self.data["get_rules"]["rule_list"]
|
|
|
|
]
|
|
|
|
except Exception as ex:
|
|
|
|
_LOGGER.error("Unable to read rule list: %s (data: %s)", ex, self.data)
|
|
|
|
return []
|
|
|
|
|
|
|
|
async def set_enabled(self, state: bool):
|
|
|
|
"""Enable or disable the service."""
|
|
|
|
return await self.call("set_overall_enable", state)
|
|
|
|
|
|
|
|
async def delete_rule(self, rule: Rule):
|
|
|
|
"""Delete the given rule."""
|
|
|
|
return await self.call("delete_rule", {"id": rule.id})
|
|
|
|
|
|
|
|
async def delete_all_rules(self):
|
|
|
|
"""Delete all rules."""
|
|
|
|
return await self.call("delete_all_rules")
|