power.py add parameter timer

Add a timer parameter to shelly and tasmota integration.

Use case: 3D printer mains power is connected to one of these devices. Just turning off the smart switch will just kill power to the RaspberryPI. Adding a timer will allow the PI to shutdown cleanly before killing the power

Signed-off-by: Dominik Weis fsironman@gmail.com
This commit is contained in:
fsironman 2021-04-06 23:58:51 +02:00 committed by GitHub
parent 6cc11276c8
commit b111e3d6c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 8 deletions

View File

@ -219,6 +219,7 @@ port:
address:
password:
output_id:
timer:
# The above options are used for "tasmota" devices. The
# address should be a valid ip or hostname for the tasmota device.
# Provide a password if configured in Tasmota (default is empty).
@ -231,12 +232,16 @@ address:
user:
password:
output_id:
timer:
# The above options are used for "shelly" devices. The
# address should be a valid ip or hostname for the Shelly device.
# Provide a user and password if configured in Shelly (default is empty).
# If password is set but user is empty the default user "admin" will be used
# Provided an output_id (relay id) if the Shelly device supports
# more than one (default is 0).
# When timer option is used to delay the turn off make sure to set
# the state to "on" in action call_remote_method.
# So we send a command to turn it on for x sec when its already on then it turns off.
address:
device:
user:

View File

@ -420,10 +420,13 @@ class Tasmota(PowerDevice):
self.addr = config.get("address")
self.output_id = config.getint("output_id", 1)
self.password = config.get("password", "")
self.timer = config.get("timer","")
async def _send_tasmota_command(self, command, password=None):
if command in ["on", "off"]:
out_cmd = f"Power{self.output_id}%20{command}"
if self.timer != "" and command == "off":
out_cmd = f"Backlog%20Delay%20{self.timer}0%3B%20{out_cmd}"
elif command == "info":
out_cmd = f"Power{self.output_id}"
else:
@ -471,13 +474,14 @@ class Tasmota(PowerDevice):
async def set_power(self, state):
try:
res = await self._send_tasmota_command(state)
try:
state = res[f"POWER{self.output_id}"].lower()
except KeyError as e:
if self.output_id == 1 :
state = res[f"POWER"].lower()
else:
raise KeyError(e)
if self.timer == "" or state != "off":
try:
state = res[f"POWER{self.output_id}"].lower()
except KeyError as e:
if self.output_id == 1 :
state = res[f"POWER"].lower()
else:
raise KeyError(e)
except Exception:
self.state = "error"
msg = f"Error Setting Device Status: {self.name} to {state}"
@ -494,10 +498,14 @@ class Shelly(PowerDevice):
self.output_id = config.getint("output_id", 0)
self.user = config.get("user", "admin")
self.password = config.get("password", "")
self.timer = config.get("timer","")
async def _send_shelly_command(self, command):
if command in ["on", "off"]:
out_cmd = f"relay/{self.output_id}?turn={command}"
if self.timer != "":
out_cmd = f"relay/{self.output_id}?turn={command}&timer={self.timer}"
else:
out_cmd = f"relay/{self.output_id}?turn={command}"
elif command == "info":
out_cmd = f"relay/{self.output_id}"
else: