GadgetReactor / pyHS100

Python Library to control TPLink Switch (HS100 / HS110)
Other
409 stars 128 forks source link

when plug hasn't had lat/long setup, exception is thrown #54

Closed whorfin closed 7 years ago

whorfin commented 7 years ago

When lat/long have not been set up, sysinfo shows latitude_i and longitude_i set to 0, and there is no entry for latitude or longitude

Traceback (most recent call last):
  File "/usr/local/bin/pyhs100", line 11, in <module>
    sys.exit(cli())
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args[1:], **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pyHS100/cli.py", line 70, in state
    click.echo("Location:     %s" % plug.location)
  File "/usr/local/lib/python2.7/dist-packages/pyHS100/pyHS100.py", line 251, in location
    return {"latitude": info["latitude"],
KeyError: u'latitude'
jeffsf commented 7 years ago

Can not reproduce

>>> tpl = SmartPlug(my_ip_address_as_string)
>>> tpl.location
{'latitude': 0, 'longitude': 0}

Confirmed that device is returning "latitude":0,"longitude":0 in its response, so this result is "correct" from the device's perspective,

Line 251 is in the middle of comments and not executable. Given that and that the current code can't be run with Python older than 3.4, this appears to be an issue with an older version of the code base.

whorfin commented 7 years ago

Error was from version installed via pip Latest version from this github repo is broken the same way, with different line numbers, as can be seen by inspecting the source.

Traceback (most recent call last):
  File "./pyhs100-v3", line 11, in <module>
    sys.exit(cli())
  File "/usr/local/lib/python3.5/dist-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.5/dist-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.5/dist-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.5/dist-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args[1:], **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/whorfin/pyHS100-master/pyHS100/cli.py", line 81, in state
    click.echo("Location:     %s" % dev.location)
  File "/home/whorfin/pyHS100-master/pyHS100/pyHS100.py", line 294, in location
    return {"latitude": info["latitude"],
KeyError: 'latitude'

If a device does not have lat/long set, "latitude" is not set, "latitude_i" is.

== System info ==
{'active_mode': 'none',
 'alias': 'redacted',
 'dev_name': 'Smart Wi-Fi Plug Mini',
 'deviceId': 'redacted',
 'feature': 'TIM',
 'fwId': '00000000000000000000000000000000',
 'hwId': 'redacted',
 'hw_ver': '1.0',
 'icon_hash': '',
 'latitude_i': 0,
 'led_off': 0,
 'longitude_i': 0,
 'mac': 'redacted',
 'model': 'HS105(US)',
 'oemId': 'redacted',
 'on_time': 1731,
 'relay_state': 1,
 'rssi': -30,
 'sw_ver': '1.0.6 Build 160722 Rel.081616',
 'type': 'IOT.SMARTPLUGSWITCH',
 'updating': 0}
mlebrun commented 7 years ago

Also getting the same:


pi@hassbian:~ $ pyhs100 --ip 192.168.1.221 state
== Office Light - HS105(US) ==
Device state: ON
LED state:    True
Time:         2017-04-24 22:59:54
On since:     2017-04-25 02:59:42.533478
Hardware:     1.0
Software:     1.0.6 Build 160722 Rel.081616
MAC (rssi):   50:C7:BF:xx:xx:xx (xx)
Traceback (most recent call last):
  File "/usr/local/bin/pyhs100", line 11, in <module>
    sys.exit(cli())
  File "/usr/local/lib/python3.4/dist-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.4/dist-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.4/dist-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.4/dist-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args[1:], **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/pyHS100/cli.py", line 70, in state
    click.echo("Location:     %s" % plug.location)
  File "/usr/local/lib/python3.4/dist-packages/pyHS100/pyHS100.py", line 251, in location
    return {"latitude": info["latitude"],
KeyError: 'latitude'
jeffsf commented 7 years ago

Interesting -- that's not what I'm seeing on a HS110

Received: {"system":{"get_sysinfo":{"err_code":0,"sw_ver":"1.1.2 Build 161014 Rel.164223","hw_ver":"1.0","type":"IOT.SMARTPLUGSWITCH","model":"HS110(US)","mac":"<redacted>","deviceId":"<redacted>","hwId":"<redacted>","fwId":"<redacted>","oemId":"<redacted>","alias":"TP-LINK_Smart Plug_<redacted>","dev_name":"Wi-Fi Smart Plug With Energy Monitoring","icon_hash":"","relay_state":0,"on_time":0,"active_mode":"none","feature":"TIM:ENE","updating":0,"rssi":-75,"led_off":0,"latitude":0,"longitude":0}}}

(Using https://github.com/softScheck/tplink-smartplug as a somewhat independent source)

mlebrun commented 7 years ago

Maybe it's the HS105 only?

whorfin commented 7 years ago

@mlebrun - using that same code on an HS105 i see:

Received:  {"system":{"get_sysinfo":{"sw_ver":"1.0.6 Build 160722 Rel.081616","hw_ver":"1.0","type":"IOT.SMARTPLUGSWITCH","model":"HS105(US)","mac":"redacted","dev_name":"Smart Wi-Fi Plug Mini","alias":"WiFiPlug-03","relay_state":1,"on_time":633,"active_mode":"none","feature":"TIM","updating":0,"icon_hash":"","rssi":-29,"led_off":0,"longitude_i":0,"latitude_i":0,"hwId":"redacted","fwId":"00000000000000000000000000000000","deviceId":"redacted","oemId":"redacted","err_code":0}}}

So indeed, perhaps the right response is to to use one of latitude/longitude or latitude_i/longitude_i; i've not set a HS105 up w/ lat/long all the way, so i don't know if the _i goes away or not... That all-zeros fwId is also interesting.

mlebrun commented 7 years ago

I tried setting it up via the Kasa app, but didn't seem to make a difference, and interesting. I copied the python script from that repo to my machine and have been trying to hit the plug but haven't been getting responses:

 |2.1.2| be-dev40 in ~/Desktop
○ → ./tplink-smartplug.py -t 192.168.1.221 -c info
Sent:      {"system":{"get_sysinfo":{}}}
Received:  

 |2.1.2| be-dev40 in ~/Desktop
○ → ./tplink-smartplug.py -t 192.168.1.221 -j '{"system":{"set_dev_location":{"longitude":-71.1199,"latitude":42.0564}}}'
Sent:      {"system":{"set_dev_location":{"longitude":-71.1199,"latitude":42.0564}}}
Received:  

 |2.1.2| be-dev40 in ~/Desktop
○ → ./tplink-smartplug.py -t 192.168.1.221 -j '{"system":{"get_sysinfo":null}}'
Sent:      {"system":{"get_sysinfo":null}}
Received:  

 |2.1.2| be-dev40 in ~/Desktop
○ → ./tplink-smartplug.py -t 192.168.1.221 -j '{"system":{"get_dev_icon":null}}'
Sent:      {"system":{"get_dev_icon":null}}
Received:  

 |2.1.2| be-dev40 in ~/Desktop
○ → ./tplink-smartplug.py -t 192.168.1.221 -j '{"system":{"set_relay_state":{"state":0}}}'
Sent:      {"system":{"set_relay_state":{"state":0}}}
Received:  

 |2.1.2| be-dev40 in ~/Desktop
○ → ./tplink-smartplug.py -t 192.168.1.221 -j '{"system":{"set_relay_state":{"state":0}}}'
Sent:      {"system":{"set_relay_state":{"state":0}}}
Received:  

Heading to bed, though. I'll catch up in the morning.

whorfin commented 7 years ago

@mlebrun The script doesn't work on HS105 without some hacking; i had to use the "lightbulb" codepath [datagram, no header] w/ the Plug command table. [lightbulb code path is from a not-merged Pull request]

kirichkov commented 7 years ago

@mlebrun is it possible your plug has crashed? Some of us have experienced crashes of the plug so please check whether the Kasa app can still find the plug.

Can any of you provide a fix for the HS105 issue? All of the core maintainers have either HS100 or HS110 so we can neither develop nor test the contributed code whether it actually works with HS105.

mlebrun commented 7 years ago

@kirichkov I don't think it's crashed because it's still able to be controlled via the app and via the pyhs100 on command so I think it's just a few things that don't work (mainly just anything querying location). But yea, I'm at work now and will try to provide a PR tonight. Python isn't my daily language, but I've dabbled a little so be gentle with feedback 😅

jeffsf commented 7 years ago

I opened up my HS105 this morning and can confirm that, as delivered, it uses the _i fields. Having problems getting/setting the WiFi network at the moment, so more will likely have to wait. >>> tp105.sys_info {'sw_ver': '1.0.6 Build 160722 Rel.081616', 'hw_ver': '1.0', 'type': 'IOT.SMARTPLUGSWITCH', 'model': 'HS105(US)', 'mac': '50:C7:BF:xx:xx:xx', 'dev_name': 'Smart Wi-Fi Plug Mini', 'alias': 'TP-LINK_Smart Plug_CF0B', 'relay_state': 0, 'on_time': 0, 'active_mode': 'none', 'feature': 'TIM', 'updating': 0, 'icon_hash': '', 'rssi': 33, 'led_off': 0, 'longitude_i': 0, 'latitude_i': 0, 'hwId': '<redacted>', 'fwId': '00000000000000000000000000000000', 'deviceId': '<redacted>', 'oemId': '<redacted>'}

kirichkov commented 7 years ago

@mlebrun Have no worries, it's not mine either. :-)

rytilahti commented 7 years ago

Well, maybe this could be patched so, that if longitude or latitude returns a keyerror, those variants with i would be tried? The relevant piece of code is here https://github.com/GadgetReactor/pyHS100/blob/master/pyHS100/pyHS100.py#L286

When creating a PR please also add an example dummy to fakes.py and make a test-case for this, thanks!

mlebrun commented 7 years ago

Open a pull request without appropriate tests? I would never 😉

mlebrun commented 7 years ago

https://github.com/GadgetReactor/pyHS100/pull/56

rytilahti commented 7 years ago

This is closed with #56, thanks @mlebrun