power: Fix Shelly behavior when a timer is configured (#149)

* power: Fix Shelly behavior when a timer is configured

Signed-off-by:  Maciej Rutkowski <dev@nomadic.works>
This commit is contained in:
Maciej Rutkowski 2021-04-22 14:47:40 +02:00 committed by GitHub
parent 46dea00b2f
commit b9487276e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 6 deletions

View File

@ -97,10 +97,10 @@ class PrinterPower:
ep = web_request.get_endpoint() ep = web_request.get_endpoint()
if not args: if not args:
raise self.server.error("No arguments provided") raise self.server.error("No arguments provided")
requsted_devs = {k: self.devices.get(k, None) for k in args} requested_devs = {k: self.devices.get(k, None) for k in args}
result = {} result = {}
req = ep.split("/")[-1] req = ep.split("/")[-1]
for name, device in requsted_devs.items(): for name, device in requested_devs.items():
if device is not None: if device is not None:
result[name] = await self._process_request(device, req) result[name] = await self._process_request(device, req)
else: else:
@ -501,9 +501,11 @@ class Shelly(PowerDevice):
self.timer = config.get("timer","") self.timer = config.get("timer","")
async def _send_shelly_command(self, command): async def _send_shelly_command(self, command):
if command in ["on", "off"]: if command == "on":
out_cmd = f"relay/{self.output_id}?turn={command}"
elif command == "off":
if self.timer != "": if self.timer != "":
out_cmd = f"relay/{self.output_id}?turn={command}&timer={self.timer}" out_cmd = f"relay/{self.output_id}?turn=on&timer={self.timer}"
else: else:
out_cmd = f"relay/{self.output_id}?turn={command}" out_cmd = f"relay/{self.output_id}?turn={command}"
elif command == "info": elif command == "info":
@ -539,23 +541,25 @@ class Shelly(PowerDevice):
try: try:
res = await self._send_shelly_command("info") res = await self._send_shelly_command("info")
state = res[f"ison"] state = res[f"ison"]
timer_remaining = res[f"timer_remaining"] if self.timer != "" else 0
except Exception: except Exception:
self.state = "error" self.state = "error"
msg = f"Error Refeshing Device Status: {self.name}" msg = f"Error Refeshing Device Status: {self.name}"
logging.exception(msg) logging.exception(msg)
raise self.server.error(msg) from None raise self.server.error(msg) from None
self.state = "on" if state else "off" self.state = "on" if state and timer_remaining == 0 else "off"
async def set_power(self, state): async def set_power(self, state):
try: try:
res = await self._send_shelly_command(state) res = await self._send_shelly_command(state)
state = res[f"ison"] state = res[f"ison"]
timer_remaining = res[f"timer_remaining"] if self.timer != "" else 0
except Exception: except Exception:
self.state = "error" self.state = "error"
msg = f"Error Setting Device Status: {self.name} to {state}" msg = f"Error Setting Device Status: {self.name} to {state}"
logging.exception(msg) logging.exception(msg)
raise self.server.error(msg) from None raise self.server.error(msg) from None
self.state = "on" if state else "off" self.state = "on" if state and timer_remaining == 0 else "off"
class HomeSeer(PowerDevice): class HomeSeer(PowerDevice):
def __init__(self, config): def __init__(self, config):