Skip to content

Commit

Permalink
Merge pull request #2 from konradsikorski/use-lock-live-properties
Browse files Browse the repository at this point in the history
Use lock live properties
  • Loading branch information
joerg65 authored Nov 28, 2020
2 parents 02b7a39 + 8034943 commit 0ae88d9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 54 deletions.
11 changes: 9 additions & 2 deletions pytedee/pytedee/Lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
93 changes: 41 additions & 52 deletions pytedee/pytedee/TedeeClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 0ae88d9

Please sign in to comment.