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"]
self._write()
class Device(object):
"""docs at http://developer.withings.com/oauth2/#tag/user%2Fpaths%2Fhttps%3A~1~1wbsapi.withings.net~1v2~1user%3Faction%3Dgetdevice%2Fget"""
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"]
break
def battery_level(self):
if self.battery >= 75:
return "high"
elif self.battery >=30:
return "medium"
else:
return "low"
class API(object):
base_api = "https://wbsapi.withings.net"
pathv2 = "/v2/measure"
pathv1 = "/measure"
user = "/v2/user"
def __init__(self, token):
self.token = token
self.get_devices()
@property
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