-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from konnected-io/async
Async
- Loading branch information
Showing
4 changed files
with
54 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,39 @@ | ||
# Python library for interacting with Konnected devices | ||
# learn more at konnected.io | ||
|
||
import requests | ||
import asyncio | ||
import aiohttp | ||
import json | ||
import os | ||
|
||
from requests.exceptions import RequestException; | ||
|
||
__author__ = 'Nate Clark, Konnected Inc <[email protected]>' | ||
|
||
|
||
class Client(object): | ||
|
||
def __init__(self, host, port): | ||
def __init__(self, host, port, websession): | ||
self.host = host | ||
self.port = port | ||
self.websession = websession | ||
self.base_url = 'http://' + host + ':' + port | ||
|
||
def get_device(self, pin=None): | ||
async def get_device(self, pin=None): | ||
""" Query the status of a specific pin (or all configured pins if pin is ommitted) """ | ||
url = self.base_url + '/device' | ||
try: | ||
r = requests.get(url, params={'pin': pin}, timeout=10) | ||
return r.json() | ||
except RequestException as err: | ||
async with self.websession.get(url, params={'pin': pin}, timeout=10) as resp: | ||
return await resp.json() | ||
except (aiohttp.ClientError, asyncio.TimeoutError) as err: | ||
raise Client.ClientError(err) | ||
|
||
def get_status(self): | ||
async def get_status(self): | ||
""" Query the device status. Returns JSON of the device internal state """ | ||
url = self.base_url + '/status' | ||
try: | ||
r = requests.get(url, timeout=10) | ||
return r.json() | ||
except RequestException as err: | ||
async with self.websession.get(url, timeout=10) as resp: | ||
return await resp.json() | ||
except (aiohttp.ClientError, asyncio.TimeoutError) as err: | ||
raise Client.ClientError(err) | ||
|
||
def put_device(self, pin, state, momentary=None, times=None, pause=None): | ||
async def put_device(self, pin, state, momentary=None, times=None, pause=None): | ||
""" Actuate a device pin """ | ||
url = self.base_url + '/device' | ||
|
||
|
@@ -54,12 +52,12 @@ def put_device(self, pin, state, momentary=None, times=None, pause=None): | |
payload["pause"] = pause | ||
|
||
try: | ||
r = requests.put(url, json=payload, timeout=10) | ||
return r.json() | ||
except RequestException as err: | ||
async with self.websession.put(url, json=payload, timeout=10) as resp: | ||
return await resp.json() | ||
except (aiohttp.ClientError, asyncio.TimeoutError) as err: | ||
raise Client.ClientError(err) | ||
|
||
def put_settings(self, sensors=[], actuators=[], auth_token=None, | ||
async def put_settings(self, sensors=[], actuators=[], auth_token=None, | ||
endpoint=None, blink=None, discovery=None, | ||
dht_sensors=[], ds18b20_sensors=[]): | ||
""" Sync settings to the Konnected device """ | ||
|
@@ -81,9 +79,9 @@ def put_settings(self, sensors=[], actuators=[], auth_token=None, | |
payload['discovery'] = discovery | ||
|
||
try: | ||
r = requests.put(url, json=payload, timeout=10) | ||
return r.ok | ||
except RequestException as err: | ||
async with self.websession.put(url, json=payload, timeout=10) as resp: | ||
return (resp.status >= 200 and resp.status < 300) | ||
except (aiohttp.ClientError, asyncio.TimeoutError) as err: | ||
raise Client.ClientError(err) | ||
|
||
class ClientError(Exception): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
requests | ||
aiohttp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,15 +40,15 @@ | |
|
||
setup( | ||
name='konnected', | ||
version='0.1.5', | ||
version='1.0.0', | ||
packages=['konnected'], | ||
url='https://github.com/konnected-io/konnected-py', | ||
license=license, | ||
description='A Python library for interacting with Konnected home automation controllers (see https://konnected.io)', | ||
description='A async Python library for interacting with Konnected home automation controllers (see https://konnected.io)', | ||
long_description=long_description, | ||
author='Nate Clark, Konnected Inc', | ||
author_email='[email protected]', | ||
install_requires=['requests>=2.0'], | ||
install_requires=['aiohttp>=3.6.1'], | ||
project_urls={ | ||
'Homepage': 'https://github.com/konnected-io/konnected-py', | ||
'Website & Online Store': 'https://konnected.io', | ||
|