-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor for command pattern to become I/O independent (#34)
* First draft asyncio support/v2 * Import fixes, fix parsing, rebase against upstream tinydtls branch, monkey patch DTLSSecurityStore. * Various fixes, added sync and async examples (including observation), added requirements for deps. * Bug fixes, update imports/exports, fix examples. * Fix imports * Delete old file * Update tests to use commands, rather than API integration. * Linting issues * Fix imports, fix compatibility with Python 3.4, update examples * Clean up the code a little for efficiency. * Bug fixes, unpack lists. * Fix packaging * Remove async_timeout, tune retries * Bug fixes * Bug fixes * Revert protocol caching * Address some comments * Update async example * Fetch loop when None passed in to api_factory * Fix test * Move test to match file it tests * Lint * Update documentation, disable debug output * Fix transport imports * Revert back to making requests in sequence Only three requests would be handled by the gateway at the same time. * Simplify install-aiocoap.sh
- Loading branch information
Showing
28 changed files
with
778 additions
and
303 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,10 @@ pip-selfcheck.json | |
# Python stuff | ||
*.py[cod] | ||
|
||
# IDE stuff | ||
.idea/ | ||
*.iml | ||
|
||
# Python venv stuff | ||
bin | ||
lib | ||
|
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
This is an example of how the pytradfri-library can be used async. | ||
To run the script, do the following: | ||
$ pip3 install pytradfri | ||
$ Download this file (example_sync.py) | ||
$ python3 test_pytradfri.py <IP> <KEY> | ||
Where <IP> is the address to your IKEA gateway and | ||
<KEY> is found on the back of your IKEA gateway. | ||
""" | ||
|
||
import asyncio | ||
import logging | ||
import sys | ||
|
||
from pytradfri import Gateway | ||
from pytradfri.api.aiocoap_api import api_factory | ||
|
||
root = logging.getLogger() | ||
root.setLevel(logging.INFO) | ||
|
||
try: | ||
# pylint: disable=ungrouped-imports | ||
from asyncio import ensure_future | ||
except ImportError: | ||
# Python 3.4.3 and earlier has this as async | ||
# pylint: disable=unused-import | ||
from asyncio import async | ||
ensure_future = async | ||
|
||
|
||
@asyncio.coroutine | ||
def run(): | ||
# Assign configuration variables. | ||
# The configuration check takes care they are present. | ||
api = yield from api_factory(sys.argv[1], sys.argv[2]) | ||
|
||
gateway = Gateway() | ||
|
||
devices_command = gateway.get_devices() | ||
devices_commands = yield from api(devices_command) | ||
devices = yield from api(*devices_commands) | ||
|
||
lights = [dev for dev in devices if dev.has_light_control] | ||
|
||
tasks_command = gateway.get_smart_tasks() | ||
tasks = yield from api(tasks_command) | ||
|
||
# Print all lights | ||
print(lights) | ||
|
||
# Lights can be accessed by its index, so lights[1] is the second light | ||
light = lights[0] | ||
|
||
def observe_callback(updated_device): | ||
light = updated_device.light_control.lights[0] | ||
print("Received message for: %s" % light) | ||
|
||
def observe_err_callback(err): | ||
print('observe error:', err) | ||
|
||
observe_command = light.observe(observe_callback, observe_err_callback, | ||
duration=120) | ||
# Start observation as a second task on the loop. | ||
observe_future = ensure_future(api(observe_command)) | ||
# Yield to allow observing to start. | ||
yield from asyncio.sleep(0) | ||
|
||
# Example 1: checks state of the light 2 (true=on) | ||
print("Is on:", light.light_control.lights[0].state) | ||
|
||
# Example 2: get dimmer level of light 2 | ||
print("Dimmer:", light.light_control.lights[0].dimmer) | ||
|
||
# Example 3: What is the name of light 2 | ||
print("Name:", light.name) | ||
|
||
# Example 4: Set the light level of light 2 | ||
dim_command = light.light_control.set_dimmer(255) | ||
yield from api(dim_command) | ||
|
||
# Example 5: Change color of light 2 | ||
# f5faf6 = cold | f1e0b5 = normal | efd275 = warm | ||
color_command = light.light_control.set_hex_color('efd275') | ||
yield from api(color_command) | ||
|
||
# Example 6: Return the transition time (in minutes) for task#1 | ||
if tasks: | ||
print(tasks[0].task_control.tasks[0].transition_time) | ||
|
||
# Example 7: Set the dimmer stop value to 30 for light#1 in task#1 | ||
dim_command_2 = tasks[0].start_action.devices[0].item_controller\ | ||
.set_dimmer(30) | ||
yield from api(dim_command_2) | ||
|
||
print("Waiting for observation to end (2 mins)") | ||
print("Try altering the light (%s) in the app, and watch the events!" % | ||
light.name) | ||
yield from observe_future | ||
|
||
|
||
asyncio.get_event_loop().run_until_complete(run()) |
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
This is an example of how the pytradfri-library can be used. | ||
To run the script, do the following: | ||
$ pip3 install pytradfri | ||
$ Download this file (example_sync.py) | ||
$ python3 test_pytradfri.py <IP> <KEY> | ||
Where <IP> is the address to your IKEA gateway and | ||
<KEY> is found on the back of your IKEA gateway. | ||
""" | ||
|
||
import sys | ||
import threading | ||
|
||
import time | ||
|
||
from pytradfri import Gateway | ||
from pytradfri.api.libcoap_api import api_factory | ||
|
||
|
||
def observe(api, device): | ||
def callback(updated_device): | ||
light = updated_device.light_control.lights[0] | ||
print("Received message for: %s" % light) | ||
|
||
def err_callback(err): | ||
print(err) | ||
|
||
def worker(): | ||
api(device.observe(callback, err_callback, duration=120)) | ||
|
||
threading.Thread(target=worker, daemon=True).start() | ||
print('Sleeping to start observation task') | ||
time.sleep(1) | ||
|
||
|
||
def run(): | ||
# Assign configuration variables. | ||
# The configuration check takes care they are present. | ||
api = api_factory(sys.argv[1], sys.argv[2]) | ||
|
||
gateway = Gateway() | ||
|
||
devices_command = gateway.get_devices() | ||
devices_commands = api(devices_command) | ||
devices = api(*devices_commands) | ||
|
||
lights = [dev for dev in devices if dev.has_light_control] | ||
|
||
tasks_command = gateway.get_smart_tasks() | ||
tasks = api(tasks_command) | ||
|
||
# Print all lights | ||
print(lights) | ||
|
||
# Lights can be accessed by its index, so lights[1] is the second light | ||
light = lights[0] | ||
|
||
observe(api, light) | ||
|
||
# Example 1: checks state of the light 2 (true=on) | ||
print(light.light_control.lights[0].state) | ||
|
||
# Example 2: get dimmer level of light 2 | ||
print(light.light_control.lights[0].dimmer) | ||
|
||
# Example 3: What is the name of light 2 | ||
print(light.name) | ||
|
||
# Example 4: Set the light level of light 2 | ||
dim_command = light.light_control.set_dimmer(255) | ||
api(dim_command) | ||
|
||
# Example 5: Change color of light 2 | ||
# f5faf6 = cold | f1e0b5 = normal | efd275 = warm | ||
color_command = light.light_control.set_hex_color('efd275') | ||
api(color_command) | ||
|
||
# Example 6: Return the transition time (in minutes) for task#1 | ||
if tasks: | ||
print(tasks[0].task_control.tasks[0].transition_time) | ||
|
||
# Example 7: Set the dimmer stop value to 30 for light#1 in task#1 | ||
dim_command_2 = tasks[0].start_action.devices[0].item_controller\ | ||
.set_dimmer(30) | ||
api(dim_command_2) | ||
|
||
print("Sleeping for 2 min to receive the rest of the observation events") | ||
print("Try altering the light (%s) in the app, and watch the events!" % | ||
light.name) | ||
time.sleep(120) | ||
|
||
|
||
run() |
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,10 +1,8 @@ | ||
"""Implement an API wrapper around Ikea Tradfri.""" | ||
|
||
from .api import retry_timeout | ||
from .coap_cli import api_factory as cli_api_factory | ||
from .error import ( | ||
PyTradFriError, RequestError, ClientError, ServerError, RequestTimeout) | ||
from .gateway import Gateway | ||
|
||
__all__ = ['Gateway', 'cli_api_factory', 'PyTradFriError', 'RequestError', | ||
'ClientError', 'ServerError', 'RequestTimeout', 'retry_timeout'] | ||
__all__ = ['Gateway', 'PyTradFriError', 'RequestError', 'ClientError', | ||
'ServerError', 'RequestTimeout'] |
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
Oops, something went wrong.