Skip to content

Commit

Permalink
Merge pull request #38 from reinder83/thermostat-states-api
Browse files Browse the repository at this point in the history
Fix thermostat states
  • Loading branch information
costastf authored Jun 10, 2019
2 parents 35a7fa0 + 504be81 commit 8294d7b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
1 change: 1 addition & 0 deletions toonapilib/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


STATE_CACHING_SECONDS = 300
THERMOSTAT_STATE_CACHING_SECONDS = 3600

STATES = {0: 'Comfort',
1: 'Home',
Expand Down
41 changes: 29 additions & 12 deletions toonapilib/toonapilib.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
import coloredlogs
from cachetools import TTLCache, cached

from .configuration import STATES, STATE_CACHING_SECONDS, BURNER_STATES, PROGRAM_STATES
from .configuration import STATES, STATE_CACHING_SECONDS, THERMOSTAT_STATE_CACHING_SECONDS, BURNER_STATES, \
PROGRAM_STATES
from .helpers import (Agreement,
Light,
PowerUsage,
Expand Down Expand Up @@ -76,6 +77,8 @@
LOGGER.addHandler(logging.NullHandler())

STATE_CACHE = TTLCache(maxsize=1, ttl=STATE_CACHING_SECONDS)
THERMOSTAT_STATE_CACHE = TTLCache(maxsize=1, ttl=THERMOSTAT_STATE_CACHING_SECONDS)

EXPIRED_TOKEN_FAULT_STRING = 'Access Token expired'


Expand Down Expand Up @@ -237,7 +240,7 @@ def _reset(self):
@property
@cached(STATE_CACHE)
def status(self):
"""The status of toon, cached for 30 seconds"""
"""The status of toon, cached for 300 seconds"""
url = ('{base_url}/toon/v3/'
'{agreement_id}/status').format(base_url=self._base_url,
agreement_id=self.agreement.id)
Expand All @@ -253,6 +256,30 @@ def status(self):
raise IncompleteStatus
return data

@property
@cached(THERMOSTAT_STATE_CACHE)
def thermostat_states(self):
"""The thermostat states of toon, cached for 1 hour"""
url = ('{base_url}/toon/v3/'
'{agreement_id}/thermostat/states').format(base_url=self._base_url,
agreement_id=self.agreement.id)
response = requests.get(url, headers=self._headers)
if response.status_code == 202:
self._logger.debug('Response accepted but no data yet, '
'trying one more time...')
response = requests.get(url, headers=self._headers)
try:
states = response.json().get('state', [])
except ValueError:
self._logger.debug('No json on response :%s', response.text)
raise IncompleteStatus

return [ThermostatState(STATES[state.get('id')],
state.get('id'),
state.get('tempValue'),
state.get('dhw'))
for state in states]

def _monkey_patch_requests(self):
self.original_request = requests.get # pylint: disable=attribute-defined-outside-init
requests.get = self._patched_request
Expand Down Expand Up @@ -418,16 +445,6 @@ def thermostat_info(self):
info.get('programState'),
info.get('realSetpoint'))

@property
def thermostat_states(self):
""":return: A list of thermostatstate object modeled as named tuples"""
states = self._get_status_value('thermostatStates').get('state', [])
return [ThermostatState(STATES[state.get('id')],
state.get('id'),
state.get('tempValue'),
state.get('dhw'))
for state in states]

def get_thermostat_state_by_name(self, name):
"""Retrieves a thermostat state object by its assigned name
Expand Down

0 comments on commit 8294d7b

Please sign in to comment.