Initial steps to remove caching (#26)

This commit removes caching of sysinfo to avoid
inconsistent states as described in issue #14.

Each an every access for properties will cause a request
to be made to the device. To avoid this, user of the library
may want to access sys_info() directly instead of using the helpers.

Currently sys_info() returns raw json object where-as helpers do
parse information for easier consumption; current state is just to
provide a PoC how it looks compared to having an active update()
for fetching the info.
This commit is contained in:
Teemu R 2017-01-11 09:17:48 +01:00 committed by GadgetReactor
parent 76cc597c2a
commit 609b323a45

View File

@ -80,17 +80,6 @@ class SmartPlug(object):
if not protocol: if not protocol:
protocol = TPLinkSmartHomeProtocol() protocol = TPLinkSmartHomeProtocol()
self.protocol = protocol self.protocol = protocol
self._sys_info = None
def _fetch_sysinfo(self):
"""
Fetches the system information from the device.
This should be called when the state of the plug is changed.
:raises: SmartPlugException: on error
"""
self._sys_info = self.get_sysinfo()
def _query_helper(self, target, cmd, arg={}): def _query_helper(self, target, cmd, arg={}):
""" """
@ -126,10 +115,8 @@ class SmartPlug(object):
@property @property
def sys_info(self): def sys_info(self):
if not self._sys_info: # TODO use volyptuous
self._fetch_sysinfo() return self.get_sysinfo()
return self._sys_info
@property @property
def state(self): def state(self):
@ -173,8 +160,6 @@ class SmartPlug(object):
else: else:
raise ValueError("State %s is not valid.", value) raise ValueError("State %s is not valid.", value)
self._fetch_sysinfo()
def get_sysinfo(self): def get_sysinfo(self):
""" """
Retrieve system information. Retrieve system information.
@ -212,8 +197,6 @@ class SmartPlug(object):
""" """
self._query_helper("system", "set_relay_state", {"state": 1}) self._query_helper("system", "set_relay_state", {"state": 1})
self._fetch_sysinfo()
def turn_off(self): def turn_off(self):
""" """
Turn the switch off. Turn the switch off.
@ -222,8 +205,6 @@ class SmartPlug(object):
""" """
self._query_helper("system", "set_relay_state", {"state": 0}) self._query_helper("system", "set_relay_state", {"state": 0})
self._fetch_sysinfo()
@property @property
def has_emeter(self): def has_emeter(self):
""" """
@ -307,8 +288,6 @@ class SmartPlug(object):
self._query_helper("emeter", "erase_emeter_stat", None) self._query_helper("emeter", "erase_emeter_stat", None)
self._fetch_sysinfo()
# As query_helper raises exception in case of failure, we have succeeded when we are this far. # As query_helper raises exception in case of failure, we have succeeded when we are this far.
return True return True
@ -334,7 +313,12 @@ class SmartPlug(object):
:return: (alias, model, list of supported features) :return: (alias, model, list of supported features)
:rtype: tuple :rtype: tuple
""" """
return self.alias, self.model, self.features
info = self.sys_info
# TODO sysinfo parsing should happen in sys_info
# to avoid calling fetch here twice..
return info["alias"], info["model"], self.features
@property @property
def model(self): def model(self):
@ -384,8 +368,6 @@ class SmartPlug(object):
""" """
self._query_helper("system", "set_dev_alias", {"alias": alias}) self._query_helper("system", "set_dev_alias", {"alias": alias})
self._fetch_sysinfo()
@property @property
def led(self): def led(self):
""" """
@ -406,8 +388,6 @@ class SmartPlug(object):
""" """
self._query_helper("system", "set_led_off", {"off": int(not state)}) self._query_helper("system", "set_led_off", {"off": int(not state)})
self._fetch_sysinfo()
@property @property
def icon(self): def icon(self):
""" """
@ -499,7 +479,8 @@ class SmartPlug(object):
:rtype: dict :rtype: dict
""" """
keys = ["sw_ver", "hw_ver", "mac", "hwId", "fwId", "oemId", "dev_name"] keys = ["sw_ver", "hw_ver", "mac", "hwId", "fwId", "oemId", "dev_name"]
return {key: self.sys_info[key] for key in keys} info = self.sys_info
return {key: info[key] for key in keys}
@property @property
def on_since(self): def on_since(self):
@ -520,9 +501,9 @@ class SmartPlug(object):
:return: latitude and longitude :return: latitude and longitude
:rtype: dict :rtype: dict
""" """
info = self.sys_info
return {"latitude": self.sys_info["latitude"], return {"latitude": info["latitude"],
"longitude": self.sys_info["longitude"]} "longitude": info["longitude"]}
@property @property
def rssi(self): def rssi(self):
@ -553,8 +534,3 @@ class SmartPlug(object):
:raises SmartPlugException: on error :raises SmartPlugException: on error
""" """
self._query_helper("system", "set_mac_addr", {"mac": mac}) self._query_helper("system", "set_mac_addr", {"mac": mac})
self._fetch_sysinfo()