Skip to content

Commit

Permalink
Merge pull request #5 from don41382/master
Browse files Browse the repository at this point in the history
changed underlying bluetooth lib to bluepy (see issue #4)
  • Loading branch information
Betree authored Jun 22, 2016
2 parents 42bc803 + b49b6e0 commit e7159e1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ If you run into problems during devices listing or connect, try to follow this p

### Using it as an API

>>> from magicbluelib import MagicBlue
>>> from magicbluelib import magicblue

>>> bulb_mac_address = 'XX:XX:XX:XX:XX:XX'
>>> bulb = MagicBlue(bulb_mac_address)
Expand Down
28 changes: 15 additions & 13 deletions magicblue/magicbluelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# ========================================================================================
import logging
import random
from gattlib import GATTRequester
import bluepy.btle

__all__ = ['MagicBlue']

Expand All @@ -27,15 +27,14 @@ def __init__(self, mac_address):
self.mac_address = mac_address
self._connection = None

def connect(self, bluetooth_adapter='hci0'):
def connect(self, bluetooth_adapter_nr=0):
"""
Connect to device
:param bluetooth_adapter: bluetooth adapter name as shown by "hciconfig" command. Default : "hci0"
:param bluetooth_adapter: bluetooth adapter name as shown by "hciconfig" command. Default : 0 for (hci0)
:return: True if connection succeed, False otherwise
"""
try:
self._connection = GATTRequester(self.mac_address, False, bluetooth_adapter)
self._connection.connect(True, "random")
self._connection = bluepy.btle.Peripheral(self.mac_address, bluepy.btle.ADDR_TYPE_RANDOM, bluetooth_adapter_nr)
except RuntimeError as e:
logger.error('Connection failed : {}'.format(e))
return False
Expand All @@ -49,9 +48,9 @@ def disconnect(self):

def is_connected(self):
"""
:return: True if connection succeed, False otherwise
:return: currently not supported anymore
"""
return self._connection.is_connected()
raise Exception("is_connected is not supported by bluepy library")

def set_warm_light(self, intensity=1.0):
"""
Expand All @@ -60,14 +59,14 @@ def set_warm_light(self, intensity=1.0):
:param intensity: the intensity between 0.0 and 1.0
"""
self._connection.write_by_handle(HANDLE_CHANGE_COLOR, bytes(bytearray([MAGIC_CHANGE_COLOR, 0, 0, 0, int(intensity * 255), 0x0f, 0xaa, 0x09])))
self._connection.writeCharacteristic(HANDLE_CHANGE_COLOR, bytes(bytearray([MAGIC_CHANGE_COLOR, 0, 0, 0, int(intensity * 255), 0x0f, 0xaa, 0x09])))

def set_color(self, rgb_color):
"""
Change bulb's color
:param rgb_color: color as a list of 3 values between 0 and 255
"""
self._connection.write_by_handle(HANDLE_CHANGE_COLOR, bytes(bytearray([MAGIC_CHANGE_COLOR] + list(rgb_color) + [0x00, 0xf0, 0xaa])))
self._connection.writeCharacteristic(HANDLE_CHANGE_COLOR, bytes(bytearray([MAGIC_CHANGE_COLOR] + list(rgb_color) + [0x00, 0xf0, 0xaa])))

def set_random_color(self):
"""
Expand All @@ -77,13 +76,16 @@ def set_random_color(self):

def turn_off(self):
"""
Turn off the light by setting color to black (rgb(0,0,0))
Turn off the light
"""
self.set_color([0, 0, 0])
self._connection.writeCharacteristic(HANDLE_CHANGE_COLOR,b'\xCC\x24\x33')

def turn_on(self, brightness=1.0):
def turn_on(self, brightness=None):
"""
Set white color on the light
:param brightness: a float value between 0.0 and 1.0 defining the brightness
"""
self.set_color([int(255 * brightness) for i in range(3)])
if brightness is None:
self._connection.writeCharacteristic(HANDLE_CHANGE_COLOR,b'\xCC\x23\x33')
else:
self.set_color([int(255 * brightness) for i in range(3)])
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
packages=['magicblue'],
install_requires=[
'pybluez',
'gattlib',
'bluepy',
'webcolors'
],
include_package_data=True,
Expand Down

0 comments on commit e7159e1

Please sign in to comment.