Skip to content

Commit

Permalink
Merge pull request #23 from nikrolls/feature/automatically-switch-pro…
Browse files Browse the repository at this point in the history
…tocol-versions

Automatically switch protocol versions to find the best one
  • Loading branch information
nicole-ashley authored Jan 19, 2020
2 parents 93529ef + 4ac9a49 commit 21cc2bb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
25 changes: 22 additions & 3 deletions custom_components/goldair_climate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
from homeassistant.const import (CONF_NAME, CONF_HOST, TEMP_CELSIUS)
from homeassistant.helpers.discovery import load_platform

VERSION = '0.0.6'
VERSION = '0.0.7'

_LOGGER = logging.getLogger(__name__)

DOMAIN = 'goldair_climate'
DATA_GOLDAIR_CLIMATE = 'data_goldair_climate'

API_PROTOCOL_VERSIONS = [3.3, 3.1]

CONF_DEVICE_ID = 'device_id'
CONF_LOCAL_KEY = 'local_key'
CONF_TYPE = 'type'
Expand Down Expand Up @@ -83,7 +85,9 @@ def __init__(self, name, dev_id, address, local_key):
"""
import pytuya
self._name = name
self._api_protocol_version_index = None
self._api = pytuya.Device(dev_id, address, local_key, 'device')
self._rotate_api_protocol_version()

self._fixed_properties = {}
self._reset_cached_state()
Expand All @@ -97,7 +101,7 @@ def __init__(self, name, dev_id, address, local_key):
# onto the state while we wait for the board to update its switches.
self._FAKE_IT_TIL_YOU_MAKE_IT_TIMEOUT = 10
self._CACHE_TIMEOUT = 20
self._CONNECTION_ATTEMPTS = 2
self._CONNECTION_ATTEMPTS = 4
self._lock = Lock()

@property
Expand All @@ -118,7 +122,7 @@ def refresh(self):
cached_state = self._get_cached_state()
if now - cached_state['updated_at'] >= self._CACHE_TIMEOUT:
self._cached_state['updated_at'] = time()
self._retry_on_failed_connection(lambda: self._refresh_cached_state(), 'Failed to refresh device state.')
self._retry_on_failed_connection(lambda: self._refresh_cached_state(), 'Failed to refresh device state for {self.name}.')

def get_property(self, dps_id):
cached_state = self._get_cached_state()
Expand Down Expand Up @@ -208,6 +212,8 @@ def _retry_on_failed_connection(self, func, error_message):
if i + 1 == self._CONNECTION_ATTEMPTS:
self._reset_cached_state()
_LOGGER.error(error_message)
else:
self._rotate_api_protocol_version()

def _get_cached_state(self):
cached_state = self._cached_state.copy()
Expand All @@ -223,6 +229,19 @@ def _get_pending_updates(self):
if now - value['updated_at'] < self._FAKE_IT_TIL_YOU_MAKE_IT_TIMEOUT}
return self._pending_updates

def _rotate_api_protocol_version(self):
if self._api_protocol_version_index is None:
self._api_protocol_version_index = 0
else:
self._api_protocol_version_index += 1

if self._api_protocol_version_index >= len(API_PROTOCOL_VERSIONS):
self._api_protocol_version_index = 0

new_version = API_PROTOCOL_VERSIONS[self._api_protocol_version_index]
_LOGGER.info(f'Setting protocol version for {self.name} to {new_version}')
self._api.set_version(new_version)

@staticmethod
def get_key_for_value(obj, value):
keys = list(obj.keys())
Expand Down
2 changes: 1 addition & 1 deletion custom_updater.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"goldair_climate": {
"version": "0.0.6",
"version": "0.0.7",
"local_location": "/custom_components/goldair_climate/__init__.py",
"remote_location": "https://raw.githubusercontent.com/nikrolls/homeassistant-goldair-climate/master/custom_components/goldair_climate/__init__.py",
"visit_repo": "https://github.com/nikrolls/homeassistant-goldair-climate",
Expand Down

0 comments on commit 21cc2bb

Please sign in to comment.