Skip to content

Commit

Permalink
initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
netmanchris committed Aug 17, 2018
0 parents commit 0306a18
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyuhooair/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

14 changes: 14 additions & 0 deletions pyuhooair/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

import hashlib



class UhooAuth():

def __init__(self, email, password):
self.email = email
salt1 = b'154f7c2e9bd94d5d90aa382ae199206a4048c9ce931c78680b0942355ca918b1'
salt2 = b'@uhooinc.com'
new_hash = hashlib.sha256()
new_hash.update(salt1+bytes(password, 'utf-8')+salt2)
self.hexdigest = new_hash.hexdigest()
45 changes: 45 additions & 0 deletions pyuhooair/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import json, requests


def get_all_devices(auth):
url = 'https://api.uhooinc.com/v1/getdevicelist'
data = {'username' : auth.email, 'password' : auth.hexdigest}
r = requests.post(url, data=data)
device_list = json.loads(r.text)
return device_list

def get_current_data(auth, device_name=None, serial=None,):
url = 'https://api.uhooinc.com/v1/getlatestdata'
device_list = get_all_devices(auth)
if serial is None:
for dev in device_list:
if dev['deviceName'] == device_name:
serial = dev['serialNumber']
else:
serial = serial
data = {'username': auth.email, 'password': auth.hexdigest, 'serialNumber': serial}
r = requests.post(url, data=data)
current_data = json.loads(r.text)
return current_data

def get_hourly_data(auth, device_name):
url = 'https://api.uhooinc.com/v1/gethourlydata'
device_list = get_all_devices(auth)
for dev in device_list:
if dev['deviceName'] == device_name:
serial = dev['serialNumber']
data = {'username': auth.email, 'password': auth.hexdigest, 'serialNumber': serial}
r = requests.post(url, data=data)
current_data = json.loads(r.text)
return current_data

def get_daily_data(auth, device_name):
url = 'https://api.uhooinc.com/v1/getdailydata'
device_list = get_all_devices(auth)
for dev in device_list:
if dev['deviceName'] == device_name:
serial = dev['serialNumber']
data = {'username': auth.email, 'password': auth.hexdigest, 'serialNumber': serial}
r = requests.post(url, data=data)
current_data = json.loads(r.text)
return current_data
74 changes: 74 additions & 0 deletions pyuhooair/objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

#!/usr/bin/env python3
# coding=utf-8
# author: @netmanchris
# -*- coding: utf-8 -*-

from pyuhooair.auth import *
from pyuhooair.data import get_current_data, get_all_devices
import datetime


class UhooDev:
def __init__(self, device_name: str, auth: UhooAuth, cache_time: float = 15):
"""
Initialise AwairDev object.
:param device_name: The name of the device a can be found in the Awair app. Careful, case sensitive.
:param auth: The authentication object as created by the UhooAuth function.
:param cache_time: The time in minutes that the state values should be cached. When this time has expired, new values
will be requested. Keep in mind that the API has daily limits so setting this too low might
cause problems.
"""
self._auth = auth
self._cache_time = cache_time
self._last_update = datetime.datetime.now() # records the last update

self._device_name = device_name

# Get device type and ID from name
devices = get_all_devices(self._auth)
self._serial = next((item for item in devices if item["deviceName"] == device_name),
False)['serialNumber'] # get the device type
# Initiate data fields
self._data = {}
self._last_update = None
self.refresh()

def get_state(self, indicator: str) -> float:
"""
Function to get the state of a specific indicator.
The values are cached, in accordance with the cache time that is set for the object.
:param indicator: A string containing one of the values from: score, temp, humid, co2, voc or dust.
:return: The value of the specific indicator.
"""
now = datetime.datetime.now()
delta_min = (now - self._last_update).total_seconds() / 60
if delta_min > self._cache_time:
self.refresh()
return(self._data[indicator])

def refresh(self):
"""
Function to refresh the state of the objects.
The values are cached internally for the period equal to the cache
time value. The refresh function refreshed these values, independent of the time that has past since the last
refresh.
"""
current_data = get_current_data(self._auth, serial=self._serial)
self._data['CO'] = current_data['CO']
self._data['air_pressure'] = current_data['Air Pressure']
self._data['humidity'] = current_data['Relative Humidity']
self._data['co2'] = current_data['CO2']
self._data['voc'] = current_data['TVOC']
self._data['dust'] = current_data['PM2.5']
self._data['timestamp'] = current_data['Timestamp']
self._data['ozone'] = current_data['Ozone']
self._data['NO2'] = current_data['NO2']
self._data['DateTime'] = current_data['DateTime']
self._last_update = datetime.datetime.now() # records the time of the last update


0 comments on commit 0306a18

Please sign in to comment.