Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Aritzherrero4/python-trionesControl
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.0.0
Choose a base ref
...
head repository: Aritzherrero4/python-trionesControl
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 8 commits
  • 3 files changed
  • 2 contributors

Commits on Apr 8, 2021

  1. Copy the full SHA
    8bc4e00 View commit details
  2. Merge pull request #2 from mtttz/master

    Replace writes to handle with writes to UUID
    Aritzherrero4 authored Apr 8, 2021
    Copy the full SHA
    fba8e86 View commit details
  3. Bump version to v1.1.0

    Changelog:
    + Add compatibility with more lights by using UUID instead of handles. #1
    Aritzherrero4 committed Apr 8, 2021
    Copy the full SHA
    6f7904d View commit details

Commits on Apr 10, 2021

  1. Copy the full SHA
    964dde1 View commit details
  2. Copy the full SHA
    f98bffa View commit details
  3. Merge pull request #4 from mtttz/multiple_devices

    add reset_on_start parameter to connect function and pass it through to pygatt
    Aritzherrero4 authored Apr 10, 2021
    Copy the full SHA
    3cbda6b View commit details
  4. Merge pull request #6 from mtttz/wait_for_response

    add parameter wait_for_response to all write functions
    Aritzherrero4 authored Apr 10, 2021
    Copy the full SHA
    a887957 View commit details
  5. Bump version to v1.2.0

    Changelog:
    + Update readme
    + Add multidevice support. #3
    + Improve the speed of fast succeding messages by not waiting for replies. #5
    Aritzherrero4 committed Apr 10, 2021
    Copy the full SHA
    b609031 View commit details
Showing with 63 additions and 14 deletions.
  1. +5 −1 README.md
  2. +1 −1 setup.py
  3. +57 −12 trionesControl/trionesControl.py
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ This will install all the dependencies used by this package and ``pexpect``, an
### Connexion handling

* ``connect(MAC)``: Connect to the device with the mac address specified.

* ``connect(MAC, False)``: Connect to the device with the mac address specified and dont reset previous connections for using multiple devices at once.
* ``disconnect(device)``: Disconnects from the specified device.

### LED Control
@@ -38,6 +38,10 @@ This will install all the dependencies used by this package and ``pexpect``, an

* ``setBuiltIn(mode: int, speed: int, device)``: Activates the selected predefined built-in mode at the selected speed (0-255). The built modes go from 37 to 56.

All the functions do not wait for any response from the device by default. This can be overriden by adding an aditional argument set to True.

* ``powerOn(device, True)``: Powers on the device, the LEDs will turn on and waits for a reponse.

## Example use

The unittest code available in [tests/test.py](https://github.com/Aritzherrero4/python-trionesControl/blob/master/tests/test.py) can be used as a sample to use the available functions of the package. You can test your bulb / LED strip by using the following code too.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@

setuptools.setup(
name="trionesControl",
version="1.0.0",
version="1.2.0",
author="Aritz Herrero",
description="Simple python package to control smart lights using the Triones porotocol",
long_description=long_description,
69 changes: 57 additions & 12 deletions trionesControl/trionesControl.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import pygatt
import logging
import pygatt.exceptions

MAIN_CHARACTERISTIC_UUID = "0000ffd9-0000-1000-8000-00805f9b34fb"

log = logging.getLogger(__name__)
def connect(MAC):

def connect(MAC, reset_on_start=True):
"""
Create and start a new backend adapter and connect it to a device.
When connecting to multiple devices at the same time make sure to set reset_on_start
to False after the first connection is made, otherwise all connections made before are
invalidated.
:param string MAC: MAC address of the device to connect to.
:param bool reset_on_start: Perhaps due to a bug in gatttol or pygatt,
but if the bluez backend isn't restarted, it can sometimes lock up
the computer when trying to make a connection to HCI device.
"""
try:
adapter = pygatt.GATTToolBackend()
adapter.start()
adapter.start(reset_on_start=reset_on_start)
device = adapter.connect(MAC)
except pygatt.exceptions.NotConnectedError:
raise pygatt.exceptions.NotConnectedError("Device nor connected!")
@@ -18,21 +34,39 @@ def disconnect(device):
except pygatt.exceptions.NotConnectedError:
raise pygatt.exceptions.NotConnectedError("Device nor connected!")
log.info("Device disconnected")
def powerOn(device):

def powerOn(device, wait_for_response=False):
"""
:param bool wait_for_response: wait for response after writing. A GATT "command"
is used when not waiting for a response. The remote host will not
acknowledge the write.
"""

try:
device.char_write_handle(0x0009, b'\xcc\x23\x33')
device.char_write(MAIN_CHARACTERISTIC_UUID, b'\xcc\x23\x33', wait_for_response=wait_for_response)
except pygatt.exceptions.NotConnectedError:
raise pygatt.exceptions.NotConnectedError("Device nor connected!")
log.info("Device powered on")

def powerOff(device):
def powerOff(device, wait_for_response=False):
"""
:param bool wait_for_response: wait for response after writing. A GATT "command"
is used when not waiting for a response. The remote host will not
acknowledge the write.
"""

try:
device.char_write_handle(0x0009, b'\xcc\x24\x33')
device.char_write(MAIN_CHARACTERISTIC_UUID, b'\xcc\x24\x33', wait_for_response=wait_for_response)
except pygatt.exceptions.NotConnectedError:
raise pygatt.exceptions.NotConnectedError("Device nor connected!")
log.info("Device powered off")

def setRGB(r: int, g: int, b: int, device):
def setRGB(r: int, g: int, b: int, device, wait_for_response=False):
"""
:param bool wait_for_response: wait for response after writing. A GATT "command"
is used when not waiting for a response. The remote host will not
acknowledge the write.
"""
# Values for color should be between 0 and 255
if r > 255: r = 255
if r < 0: r= 0
@@ -50,12 +84,17 @@ def setRGB(r: int, g: int, b: int, device):
payload.append(0xF0)
payload.append(0xAA)
try:
device.char_write_handle(0x0009, payload)
device.char_write(MAIN_CHARACTERISTIC_UUID, payload, wait_for_response=wait_for_response)
except pygatt.exceptions.NotConnectedError:
raise pygatt.exceptions.NotConnectedError("Device nor connected!")
log.info("RGB set -- R: %d, G: %d, B: %d", r, g, b)

def setWhite(intensity: int, device):
def setWhite(intensity: int, device, wait_for_response=False):
"""
:param bool wait_for_response: wait for response after writing. A GATT "command"
is used when not waiting for a response. The remote host will not
acknowledge the write.
"""
# Intensity value shoud be between 0 and 255
if (intensity > 255): intensity = 255
if (intensity < 0): intensity = 0
@@ -68,12 +107,18 @@ def setWhite(intensity: int, device):
payload.append(0x0F)
payload.append(0xAA)
try:
device.char_write_handle(0x0009, payload)
device.char_write(MAIN_CHARACTERISTIC_UUID, payload, wait_for_response=wait_for_response)
except pygatt.exceptions.NotConnectedError:
raise pygatt.exceptions.NotConnectedError("Device nor connected!")
log.info("White color set -- Intensity: %d", intensity)

def setBuiltIn(mode: int, speed: int, device):
def setBuiltIn(mode: int, speed: int, device, wait_for_response=False):
"""
:param bool wait_for_response: wait for response after writing. A GATT "command"
is used when not waiting for a response. The remote host will not
acknowledge the write.
"""

if mode<37 | mode > 56:
raise pygatt.exceptions.BLEError("Invalid Mode")
if speed<1: speed =1
@@ -84,7 +129,7 @@ def setBuiltIn(mode: int, speed: int, device):
payload.append(speed)
payload.append(0x44)
try:
device.char_write_handle(0x0009, payload)
device.char_write(MAIN_CHARACTERISTIC_UUID, payload, wait_for_response=wait_for_response)
except pygatt.exceptions.NotConnectedError:
raise pygatt.exceptions.NotConnectedError("Device nor connected!")
log.info("Default mode %d set -- Speed %d", mode, speed)