diff --git a/pytedee/pytedee/Lock.py b/pytedee/pytedee/Lock.py index f923797..9a0ffee 100644 --- a/pytedee/pytedee/Lock.py +++ b/pytedee/pytedee/Lock.py @@ -31,6 +31,7 @@ def __init__(self, name, id): self._state = 0 self._battery_level = None self._is_connected = False + self._is_charging = False def get_name(self): return self._name @@ -39,10 +40,10 @@ def get_id(self): return self._id def is_state_locked(self): - return self._status == 6 + return self._state == 6 def is_state_unlocked(self): - return self._status == 2 + return self._state == 2 def get_state(self): return self._state @@ -61,3 +62,9 @@ def set_connected(self, connected): def is_connected(self): return self._is_connected + + def get_is_charging(self): + return self._is_charging + + def set_is_charging(self, isCharging): + self._is_charging = isCharging diff --git a/pytedee/pytedee/TedeeClient.py b/pytedee/pytedee/TedeeClient.py index 21ab8f3..c7e4f68 100644 --- a/pytedee/pytedee/TedeeClient.py +++ b/pytedee/pytedee/TedeeClient.py @@ -71,13 +71,26 @@ def get_devices(self): timeout=self._timeout) _LOGGER.debug("Locks %s", r.json()) result = r.json()["result"] - for x in result: + + for x in result: id = x["id"] name = x["name"] + isConnected = x["isConnected"] + state = x["lockProperties"]["state"] + batteryLevel = x["lockProperties"]["batteryLevel"] + isCharging = x["lockProperties"]["isCharging"] + + lock = Lock(name, id) + lock.set_connected(isConnected) + lock.set_state(state) + lock.set_battery_level(batteryLevel) + lock.set_is_charging(isCharging) + self._lock_id = id '''store the found lock in _sensor_list and get the battery_level''' - self._sensor_list.append(Lock(name, id)) - self.get_battery(id) + + self._sensor_list.append(lock) + if self._lock_id == None: raise TedeeClientException("No lock found") @@ -87,17 +100,10 @@ def get_locks(self): def unlock(self, id): '''Unlock method''' - for lock in self._sensor_list: - if id == lock.get_id(): - payload = {"deviceId":id} - break - if payload == None: - raise TedeeClientException("This Id not found") + lock = self.find_lock(id) + payload = {"deviceId":id} - if (self.is_token_valid() == False): - self.get_token() - - if (self.is_token_valid() == True): + if (self.ensure_token_is_valid() == True): r = requests.post(api_url_open, headers=self._api_header, data=json.dumps(payload), timeout=self._timeout) @@ -108,21 +114,13 @@ def unlock(self, id): t = Timer(2, self.get_state) t.start() - def lock(self, id): ''''Lock method''' - for lock in self._sensor_list: - if id == lock.get_id(): - payload = {"deviceId":id} - break - if payload == None: - raise TedeeClientException("This Id not found") + lock = self.find_lock(id) + payload = {"deviceId":id} - if (self.is_token_valid() == False): - self.get_token() - - if (self.is_token_valid() == True): + if (self.ensure_token_is_valid() == True): r = requests.post(api_url_close, headers=self._api_header, data=json.dumps(payload), timeout=self._timeout) @@ -136,17 +134,10 @@ def lock(self, id): def open(self, id): '''Open the door latch''' - for lock in self._sensor_list: - if id == lock.get_id(): - payload = {"deviceId":id} - break - if payload == None: - raise TedeeClientException("This Id not found") + lock = self.find_lock(id) + payload = {"deviceId":id} - if (self.is_token_valid() == False): - self.get_token() - - if (self.is_token_valid() == True): + if (self.ensure_token_is_valid() == True): r = requests.post(api_url_pull, headers=self._api_header, data=json.dumps(payload), timeout=self._timeout) @@ -159,30 +150,17 @@ def open(self, id): t.start() def is_unlocked(self, id): - for lock in self._sensor_list: - if id == lock.get_id(): - payload = {"deviceId":id} - break - if payload == None: - raise TedeeClientException("This Id not found") + lock = self.find_lock(id) return lock.get_state() == 2 def is_locked(self, id): - for lock in self._sensor_list: - if id == lock.get_id(): - payload = {"deviceId":id} - break - if payload == None: - raise TedeeClientException("This Id not found") + lock = self.find_lock(id) return lock.get_state() == 6 def get_battery(self, id): - for lock in self._sensor_list: - if id == lock.get_id(): - payload = {"deviceId":id} - break - if payload == None: - raise TedeeClientException("This Id not found") + lock = self.find_lock(id) + payload = {"deviceId":id} + api_url_battery = "https://api.tedee.com/api/v1.15/my/device/" + str(id) + "/battery" r = requests.get(api_url_battery, headers=self._api_header, timeout=self._timeout) @@ -223,6 +201,17 @@ def get_state(self): def is_token_valid(self): return self._token_valid_until > datetime.datetime.now() + + def ensure_token_is_valid(self): + if (self.is_token_valid() == False): + self.get_token() + return self.is_token_valid() == True + + def find_lock(self, id): + for lock in self._sensor_list: + if id == lock.get_id(): + return lock + raise TedeeClientException("This Id not found") def update(self, id): if (self.is_token_valid() == False):