Commit e1bf1a5c authored by Cédrick COPOL's avatar Cédrick COPOL

get devices

parent 754b1cdc
......@@ -149,3 +149,57 @@ class Token(object):
self.token = r.json()
self.userid = self.token["userid"]
class Device(object):
"""docs at"""
def __init__(self, dico):
# key is type, battery, model, device
self.type = dico["type"] # "Scale", "Babyphone", "Blood Pressure Monitor", "Activity Tracker", "Sleep Monitor", "Smart Connected Thermoter"
self.model = dico["model"]
self.ID = dico["deviceid"]
self.battery = dico["battery"]
self.timezone = dico["timezone"]
def update(self, devices):
"""Parcours tous les devices retourner par Withings, pour y trouver le bon appareil et
mettre les valeurs pouvant varier"""
for device in devices:
if device["deviceid"] == self.ID:
self.battery = device["battery"]
self.timezone = device["timezone"]
def battery_level(self):
if self.battery >= 75:
return "high"
elif self.battery >=30:
return "medium"
return "low"
class API(object):
base_api = ""
pathv2 = "/v2/measure"
pathv1 = "/measure"
user = "/v2/user"
def __init__(self, token):
self.token = token
def access_token(self):
return self.token.access
def get_devices(self):
"""Requests the devices of the user"""
headers = {"Authorization" : "Bearer " + self.access_token}
url = self.base_api + self.user
r = requests.get(url,{'action':'getdevice'}, headers=headers)
if r.status_code != 200:
raise Exception(f"Impossible to get devices. status: {r.status_code}. error: {r.json()['errors'][0]['message']}")
self.devices = [Device(device) for device in r.json()["body"]["devices"]]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment