From 960ec6a34678c535c89c636c294083a14d85c89f Mon Sep 17 00:00:00 2001 From: Teemu R Date: Sat, 16 Mar 2019 21:32:59 +0100 Subject: [PATCH] Smartstrip: return on_since state information only when the socket is on (#161) * Smartstrip: return on_since state information only when the socket is on Fixes #160 * add proper cli printout for hs300 child sockets * iterate over range, not an integer --- pyHS100/cli.py | 9 +++++++++ pyHS100/smartdevice.py | 1 + pyHS100/smartstrip.py | 6 +++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pyHS100/cli.py b/pyHS100/cli.py index 640397e3..dfcacc3b 100755 --- a/pyHS100/cli.py +++ b/pyHS100/cli.py @@ -129,6 +129,15 @@ def state(ctx, dev): click.echo(click.style("Device state: %s" % "ON" if dev.is_on else "OFF", fg="green" if dev.is_on else "red")) + if dev.num_children > 0: + is_on = dev.is_on() + aliases = dev.get_alias() + for child in range(dev.num_children): + click.echo( + click.style(" * %s state: %s" % + (aliases[child], + "ON" if is_on[child] else "OFF"), + fg="green" if is_on[child] else "red")) click.echo("Host/IP: %s" % dev.host) for k, v in dev.state_information.items(): click.echo("%s: %s" % (k, v)) diff --git a/pyHS100/smartdevice.py b/pyHS100/smartdevice.py index 90464873..c95b4af3 100755 --- a/pyHS100/smartdevice.py +++ b/pyHS100/smartdevice.py @@ -88,6 +88,7 @@ class SmartDevice(object): self.protocol = protocol self.emeter_type = "emeter" # type: str self.context = context + self.num_children = 0 def _query_helper(self, target: str, diff --git a/pyHS100/smartstrip.py b/pyHS100/smartstrip.py index 633d4838..e37e48ab 100755 --- a/pyHS100/smartstrip.py +++ b/pyHS100/smartstrip.py @@ -204,8 +204,12 @@ class SmartStrip(SmartPlug): """ state = {'LED state': self.led} on_since = self.on_since() + is_on = self.is_on() for plug_index in range(self.num_children): - state['Plug %d on since' % (plug_index + 1)] = on_since[plug_index] + plug_number = plug_index + 1 + if is_on[plug_index]: + state['Plug %d on since' % plug_number] = on_since[plug_index] + return state def get_emeter_realtime(self, *, index: int = -1) -> Optional[Any]: