From 28366008572e615e43da43e30de50d4ddb095ae8 Mon Sep 17 00:00:00 2001 From: crashmaxx <34484735+crashmaxx@users.noreply.github.com> Date: Tue, 12 Oct 2021 06:35:32 -0400 Subject: [PATCH] power: add timer option to tplink-smartplug devices Signed-off-by: Andrew Stowell crashmaxx@gmail.com --- docs/configuration.md | 3 +++ moonraker/components/power.py | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 72ecc88..c00437c 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -231,9 +231,12 @@ initial_state: off # state. Default is off. address: port: +timer: # The above options are used for "tplink_smartplug" devices. The # address should be a valid ip or hostname for the tplink device. # The port should be the port the device is configured to use. +# The timer option is used to delay the turn off for x sec when +# its already on. # "Power Strips" can be controlled by including the socket index # in the ip address. For example, to control socket index 1: # 192.168.1.127/1 diff --git a/moonraker/components/power.py b/moonraker/components/power.py index f9dee07..52e5c89 100644 --- a/moonraker/components/power.py +++ b/moonraker/components/power.py @@ -530,6 +530,7 @@ class TPLinkSmartPlug(PowerDevice): START_KEY = 0xAB def __init__(self, config: ConfigHelper) -> None: super().__init__(config) + self.timer = config.get("timer", "") self.request_mutex = asyncio.Lock() self.addr: List[str] = config.get("address").split('/') self.port = config.getint("port", 9999) @@ -551,6 +552,14 @@ class TPLinkSmartPlug(PowerDevice): } elif command == "info": out_cmd = {'system': {'get_sysinfo': {}}} + elif command == "clear_rules": + out_cmd = {'count_down': {'delete_all_rules': None}} + elif command == "count_off": + out_cmd = { + 'count_down': {'add_rule': + {'enable': 1, 'delay': int(self.timer), + 'act': 0, 'name': 'turn off'}} + } else: raise self.server.error(f"Invalid tplink command: {command}") s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -622,8 +631,13 @@ class TPLinkSmartPlug(PowerDevice): async with self.request_mutex: err: int try: - res = await self._send_tplink_command(state) - err = res['system']['set_relay_state']['err_code'] + if self.timer != "" and state == "off": + await self._send_tplink_command("clear_rules") + res = await self._send_tplink_command("count_off") + err = res['count_down']['add_rule']['err_code'] + else: + res = await self._send_tplink_command(state) + err = res['system']['set_relay_state']['err_code'] except Exception: err = 1 logging.exception(f"Power Toggle Error: {self.name}")