module: power

Add separate output_id configuration attribute for TPLink Smartplug device.

Signed-off-by:  Matthew Humphrey <mhumphrey@gmail.com>
This commit is contained in:
Matthew Humphrey 2022-02-02 07:45:47 -08:00 committed by Eric Callahan
parent 10ac044485
commit aca7cb78b9
2 changed files with 19 additions and 10 deletions

View File

@ -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:

View File

@ -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: