Skip to content

Commit

Permalink
Merge pull request openviess#28 from crazyfx1/feat/batch
Browse files Browse the repository at this point in the history
Proposal for batching requests
  • Loading branch information
somm15 authored Mar 23, 2020
2 parents 5a030b5 + a7571ff commit e2a367e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 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
9 changes: 6 additions & 3 deletions PyViCare/PyViCareDevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import logging
from PyViCare.PyViCareService import ViCareService
from PyViCare.PyViCareCachedService import ViCareCachedService

logger = logging.getLogger('ViCare')
logger.addHandler(logging.NullHandler())
Expand All @@ -24,7 +25,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,cacheDuration=0):
"""Init function. Create the necessary oAuth2 sessions
Parameters
----------
Expand All @@ -37,7 +38,10 @@ def __init__(self, username, password,token_file=None,circuit=0):
-------
"""

self.service = ViCareService(username, password, token_file, circuit)
if cacheDuration == 0:
self.service = ViCareService(username, password, token_file, circuit)
else:
self.service = ViCareCachedService(username, password, cacheDuration, token_file, circuit)

""" Set the active mode
Parameters
Expand Down Expand Up @@ -173,7 +177,6 @@ def getHeatingCurveSlope(self):
except KeyError:
return "error"


def getActiveProgram(self):
try:
return self.service.getProperty("heating.circuits." + str(self.service.circuit) + ".operating.programs.active")["properties"]["value"]["value"]
Expand Down
3 changes: 3 additions & 0 deletions PyViCare/PyViCareService.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ def _getInstallations(self):
def getInstallations(self):
return self.installations

def get(self, url):
return self.__get(url)

#TODO should move to device after refactoring
def getProperty(self,property_name):
url = apiURLBase + '/operational-data/installations/' + str(self.id) + '/gateways/' + str(self.serial) + '/devices/0/features/' + property_name
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 e2a367e

Please sign in to comment.