From aca7cb78b98b12c693ae5b6567925b131bd516e0 Mon Sep 17 00:00:00 2001 From: Matthew Humphrey Date: Wed, 2 Feb 2022 07:45:47 -0800 Subject: [PATCH] module: power Add separate output_id configuration attribute for TPLink Smartplug device. Signed-off-by: Matthew Humphrey --- docs/configuration.md | 9 +++++---- moonraker/components/power.py | 20 ++++++++++++++------ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 3e2b09e..64f76f7 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -527,14 +527,15 @@ The following options are availble for `tplink_smartplug` device types: # moonraker.conf address: -# A valid ip address or hostname for the tplink device. "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 +# A valid ip address or hostname for the tplink device. For example: +# 192.168.1.127 # This parameter must be provided. port: # The port to connect to. Default is 9999. # +output_id: +# For power strips, the socket index to use. Default is 0 which indicates the +# device is not a power strip. ``` Example: diff --git a/moonraker/components/power.py b/moonraker/components/power.py index cd0cbb5..dcc1f2a 100644 --- a/moonraker/components/power.py +++ b/moonraker/components/power.py @@ -672,7 +672,15 @@ class TPLinkSmartPlug(PowerDevice): super().__init__(config) self.timer = config.get("timer", "") self.request_mutex = asyncio.Lock() - self.addr: List[str] = config.get("address").split('/') + addr_and_output_id = config.get("address").split('/') + self.addr = addr_and_output_id[0] + if (len(addr_and_output_id) > 1): + self.server.add_warning( + f"Power Device {self.name}: Including the output id in the" + " address is deprecated, use the 'output_id' option") + self.output_id: Optional[int] = int(addr_and_output_id[1]) + else: + self.output_id = config.getint("output_id", None) self.port = config.getint("port", 9999) async def _send_tplink_command(self, @@ -684,11 +692,11 @@ class TPLinkSmartPlug(PowerDevice): 'system': {'set_relay_state': {'state': int(command == "on")}} } # TPLink device controls multiple devices - if len(self.addr) == 2: + if self.output_id is not None: sysinfo = await self._send_tplink_command("info") dev_id = sysinfo["system"]["get_sysinfo"]["deviceId"] out_cmd["context"] = { - 'child_ids': [f"{dev_id}{int(self.addr[1]):02}"] + 'child_ids': [f"{dev_id}{self.output_id:02}"] } elif command == "info": out_cmd = {'system': {'get_sysinfo': {}}} @@ -703,7 +711,7 @@ class TPLinkSmartPlug(PowerDevice): else: raise self.server.error(f"Invalid tplink command: {command}") reader, writer = await asyncio.open_connection( - self.addr[0], self.port, family=socket.AF_INET) + self.addr, self.port, family=socket.AF_INET) try: writer.write(self._encrypt(out_cmd)) await writer.drain() @@ -755,11 +763,11 @@ class TPLinkSmartPlug(PowerDevice): try: state: str res = await self._send_tplink_command("info") - if len(self.addr) == 2: + if self.output_id is not None: # TPLink device controls multiple devices children: Dict[int, Any] children = res['system']['get_sysinfo']['children'] - state = children[int(self.addr[1])]['state'] + state = children[self.output_id]['state'] else: state = res['system']['get_sysinfo']['relay_state'] except Exception: