From 93d24281c689e4691579dc3210af2857c26ba7b8 Mon Sep 17 00:00:00 2001 From: Matt LeBrun Date: Wed, 26 Apr 2017 12:43:50 -0400 Subject: [PATCH] Add check to ensure devices with lat/lon with `_i` suffix are supported (#54) (#56) * Add check to ensure devices with lat/lon with `_i` suffix are supported (#54) * Add .gitignore for posterity --- .gitignore | 11 +++++++++++ pyHS100/pyHS100.py | 15 +++++++++++++-- pyHS100/tests/fakes.py | 22 ++++++++++++++++++++++ pyHS100/tests/test_pyHS100.py | 7 ++++++- 4 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..cdae3264 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +# Compiled python modules. +__pycache__/ + +# Setuptools distribution folder. +/dist/ + +# Python egg metadata, regenerated from source files by setuptools. +/*.egg-info +/*.egg + +/build diff --git a/pyHS100/pyHS100.py b/pyHS100/pyHS100.py index 8336e9c5..d215b10e 100644 --- a/pyHS100/pyHS100.py +++ b/pyHS100/pyHS100.py @@ -291,8 +291,19 @@ class SmartDevice(object): :rtype: dict """ info = self.sys_info - return {"latitude": info["latitude"], - "longitude": info["longitude"]} + loc = {"latitude": None, + "longitude": None} + + if "latitude" in info and "longitude" in info: + loc["latitude"] = info["latitude"] + loc["longitude"] = info["longitude"] + elif "latitude_i" in info and "longitude_i" in info: + loc["latitude"] = info["latitude_i"] + loc["longitude"] = info["longitude_i"] + else: + _LOGGER.warning("Unsupported device location.") + + return loc @property def rssi(self): diff --git a/pyHS100/tests/fakes.py b/pyHS100/tests/fakes.py index 29ec353a..f2f28be1 100644 --- a/pyHS100/tests/fakes.py +++ b/pyHS100/tests/fakes.py @@ -45,6 +45,28 @@ emeter_units_support = {"get_realtime": get_realtime_units, "get_monthstat": get_monthstat_units, "get_daystat": get_daystat_units,} +sysinfo_hs105 = {'system': {'get_sysinfo': + {'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': -12.2, + 'latitude_i': 12.2, + 'hwId': '60FF6B258734EA6880E186F8C96DDC61', + 'fwId': '00000000000000000000000000000000', + 'deviceId': '800654F32938FCBA8F7327887A38647617', + 'oemId': 'FFF22CFF774A0B89F7624BFC6F50D5DE'}}} + sysinfo_hs110 = {'system': {'get_sysinfo': {'active_mode': 'schedule', 'alias': 'Mobile Plug', diff --git a/pyHS100/tests/test_pyHS100.py b/pyHS100/tests/test_pyHS100.py index 28986c96..87314d27 100644 --- a/pyHS100/tests/test_pyHS100.py +++ b/pyHS100/tests/test_pyHS100.py @@ -5,7 +5,7 @@ import datetime import re from .. import SmartPlug, SmartPlugException -from .fakes import FakeTransportProtocol, sysinfo_hs110 +from .fakes import FakeTransportProtocol, sysinfo_hs110, sysinfo_hs105 PLUG_IP = '192.168.250.186' SKIP_STATE_TESTS = False @@ -221,6 +221,11 @@ class TestSmartPlug(TestCase): def test_location(self): self.sysinfo_schema(self.plug.location) + def test_location_i(self): + plug_i = SmartPlug(PLUG_IP, + protocol=FakeTransportProtocol(sysinfo_hs105)) + self.sysinfo_schema(plug_i.location) + def test_rssi(self): self.sysinfo_schema({'rssi': self.plug.rssi}) # wrapping for vol