Skip to content

Commit

Permalink
Revert normal getProperty and introduce cached service
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyfx1 committed Mar 22, 2020
1 parent 5289f10 commit 99379e8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
24 changes: 24 additions & 0 deletions PyViCare/PyViCareCachedService.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from datetime import datetime

from PyViCare.PyViCareService import apiURLBase, ViCareService

class ViCareCachedService(ViCareService):

def __init__(self, username, password, cacheDuration, token_file=None,circuit=0):
ViCareService.__init__(self, username, password, token_file, circuit)
self.cacheDuration = cacheDuration
self.cache = None
self.cacheTime = None

def __setCache(self):
url = apiURLBase + '/operational-data/installations/' + str(self.id) + '/gateways/' + str(self.serial) + '/devices/0/features/'
self.cache = self.get(url)
self.cacheTime = datetime.now()

def getProperty(self,property_name):
if self.cache is None or self.cacheTime is None or (datetime.now() - self.cacheTime).seconds > self.cacheDuration:
self.__setCache()

entities = self.cache["entities"]
feature = next((f for f in entities if f["class"][0] == property_name and f["class"][1] == "feature"), {})
return feature
10 changes: 5 additions & 5 deletions PyViCare/PyViCareDevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Device:
"""

# TODO cirtcuit management should move at method level
def __init__(self, username, password,token_file=None,circuit=0):
def __init__(self, username, password,token_file=None,circuit=0,service=None):
"""Init function. Create the necessary oAuth2 sessions
Parameters
----------
Expand All @@ -37,7 +37,10 @@ def __init__(self, username, password,token_file=None,circuit=0):
-------
"""

self.service = ViCareService(username, password, token_file, circuit)
if service is None:
self.service = ViCareService(username, password, token_file, circuit)
else:
self.service = service

""" Set the active mode
Parameters
Expand Down Expand Up @@ -119,9 +122,6 @@ def deactivateProgram(self,program):
def deactivateComfort(self):
return self.deactivateProgram("comfort")

def fetchAndCacheFeatures(self):
self.service.fetchAndCacheFeatures()

def getMonthSinceLastService(self):
try:
return self.service.getProperty("heating.service.timeBased")["properties"]["activeMonthSinceLastService"]["value"]
Expand Down
11 changes: 5 additions & 6 deletions PyViCare/PyViCareService.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,14 @@ def _getInstallations(self):
def getInstallations(self):
return self.installations

def fetchAndCacheFeatures(self):
url = apiURLBase + '/operational-data/installations/' + str(self.id) + '/gateways/' + str(self.serial) + '/devices/0/features/'
self.j = self.__get(url)
def get(self, url):
return self.__get(url)

#TODO should move to device after refactoring
def getProperty(self,property_name):
entities = self.j["entities"]
feature = next((f for f in entities if f["class"][0] == property_name and f["class"][1] == "feature"), {})
return feature
url = apiURLBase + '/operational-data/installations/' + str(self.id) + '/gateways/' + str(self.serial) + '/devices/0/features/' + property_name
j=self.__get(url)
return j

def setProperty(self,property_name,action,data):
url = apiURLBase + '/operational-data/v1/installations/' + str(self.id) + '/gateways/' + str(self.serial) + '/devices/0/features/' + property_name +"/"+action
Expand Down
2 changes: 1 addition & 1 deletion PyViCare/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__all__ = ['PyViCareService''PyViCareDevice','PyViCareGazBoiler','PyViCareHeatPump']
__all__ = ['PyViCareService''PyViCareDevice','PyViCareGazBoiler','PyViCareHeatPump','PyViCareCachedService']

0 comments on commit 99379e8

Please sign in to comment.