Magic Blue Bulb Bluetooth 4.0

Hey, I am the main developer of magicblue unofficial API.

I just found out about this post and the HomeAssistant project and it looks really cool :smile:

As you said the API is written in python 3 so it shouldn’t be complicated to integrate. Project can be used in command line as you’re doing now in your scripts but Python API is pretty straightforward too :

>>> from magicbluelib import magicblue

>>> bulb_mac_address = 'XX:XX:XX:XX:XX:XX'
>>> bulb = MagicBlue(bulb_mac_address)
>>> bulb.connect()
>>> bulb.set_color([255, 0, 0])         # Set red
>>> bulb.set_random_color()             # Set random
>>> bulb.turn_off()                     # Turn off the light
>>> bulb.turn_on()                      # Set white light

By looking at the HomeAssistant documentation example here you may just be able to include magiblue in REQUIREMENTS and use it directly. We may have to deploy it on https://pypi.python.org/ first (I’m not sure how they manage their dependencies).

However I don’t have the time to do it now but would be happy to help if someone wants to :wink:

Hey @Betree, I have a single bulb that i have bought in error so I am going to have a play with the AppDaemon in HomeAssistant to see if i can get some sort of control over it as a trial via a standard Python script, then i will look at a custom light platform

Edit: Will go through this: https://github.com/Betree/magicblue

Any tips?

Ok I went straight for the custom component but hit an issue.

I am getting

Command “python setup.py egg_info” failed with error code 1 in /tmp/pip-mleix1z3-build/

When i start HASS after adding the light to the config.

@Betree any ideas?

Also will i need the bulb to have just turned on in order to get a connection to it? Once connected will the bulb remember the source of the connection for the next time?

This command works fine when I run it directly on magicblue setup.py :

$ python setup.py egg_info                                                                                                                                                      
running egg_info
writing requirements to magicblue.egg-info/requires.txt
writing magicblue.egg-info/PKG-INFO
writing top-level names to magicblue.egg-info/top_level.txt
writing dependency_links to magicblue.egg-info/dependency_links.txt
writing entry points to magicblue.egg-info/entry_points.txt
reading manifest file 'magicblue.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'magicblue.egg-info/SOURCES.txt'

Do you have any more logs you could provide ? What is the content of the directory printed when it fails (/tmp/pip-mleix1z3-build/) ?

Hey @Betree, i tried removing homeassistant from the equation.

Turns out I’m not getting much luck even directly through the shell commands or gatttool.

The tool will just not connect to the bulb. Hcitool sees it and i get the Mac address but no commands work. Could it be a version thing?

Hi,

See my post here Xiaomi Mi Plants Monitor Flower at the bottom, for what I had to do to get BLE working.

In the bluemagic app what hardware does it say your bulb is? (just incase you have a newer version)

Thanks. Just took a look through.

Bulb hardware is v10. That was the easy bit.

Unfortunately after reinstalling my bluez version, following the steps, hciconfig no longer returns any results.

Bluetooth service is running, but there appears to be no devices anymore.

So i thinks this going to take a bit longer, last resort will be a reinstall of raspbian, but as that will knock out all my lighting in the house it is a definite last resort.

What does bluetoothctl come back with, can you see any devices using scan on?

OK, I see you have a newer version than magicblue supports, but if you specify version 9 it might work i think. Also wrt bluetooth … https://cdn-learn.adafruit.com/downloads/pdf/install-bluez-on-the-raspberry-pi.pdf this might help.

Right, i’ll give this a proper go tonight, currently working away. Before i left i did manage to do a quick run of bluetoothctl.

No Default Controller Available(or similar error, sorry doing this from memory). Not had a chance to do anything further, a quick scan of blogs gives this [http://raspberrypi.stackexchange.com/questions/48220/raspberry-pi-3-bluetooth-no-default-controller-available] (http://raspberrypi.stackexchange.com/questions/48220/raspberry-pi-3-bluetooth-no-default-controller-available) sio i’ll give that a shot.

WRT the adafruit link, this was the article i used to do the setup, the bluetooth service runs fine, it is really odd.

I feel your pain, it’s taken me a good two weeks to get my ble working properly, as although I use a RPI3 I use an external dongle for bluetooth :stuck_out_tongue:
Good luck getting yours working.

Finally got it working… Had to go through this article. https://www.pi-supply.com/make/fix-raspberry-pi-3-bluetooth-issues/

Managed to control the bulb from the shell script (some odd errors but it worked).

Then tried to put a quick python script together, However the first line failed

pi@raspberrypi:~/magicblue $ python3 MagicBlue.py
Traceback (most recent call last):
  File "MagicBlue.py", line 1, in <module>
    from magicbluelib import magicblue
ImportError: No module named 'magicbluelib'

If i run via the python shell:

pi@raspberrypi:~/magicblue $ python
Python 2.7.9 (default, Sep 17 2016, 20:26:04)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from magicbluelib import magicblue
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named magicbluelib
>>>

(Full disclosure, i am a c# / javascript dev by trade, Python NOOB. So could well just be a stupid thing like paths)

magicbluelib.py should be in the magicblue dir after installing it, at least it is in mine.

Your feedback about V10 compatibility will be valuable, don’t hesitate to open an issue on Github to report bugs or installation specificities.

For your current problem, I assume you simply cloned the repo. The easiest way to use magicblue is to install it, either by calling python3 setup.py install in your cloned repo or directly with :

pip3 install git+https://github.com/Betree/pyMagicBlue.git

:warning: You may have to run previous commands as root

Of course you’ll need the dependencies but I think you already got that part :

sudo apt-get install libglib2.0-dev
sudo pip3 install bluepy

Then you’ll be able to run magicblueshell command or to import it with :

from magicblue import MagicBlue 

There was a glitch in README.md, I just fixed it with the previous line.

Thanks Guys.

Install was done following the readme, so i used the pip3 install command.

With the corrected import line, running from the Python shell as sudo i now get:

pi@raspberrypi:~ $ sudo python
Python 2.7.9 (default, Sep 17 2016, 20:26:04)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from magicblue import MagicBlue
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "magicblue/MagicBlue.py", line 1, in <module>
     from magicblue import MagicBlue
ImportError: cannot import name MagicBlue
>>>

If i go via a script (after making the mistake of using CAPS in my mac address) i can get control.

A quick test in Home Assistant was unsuccessful,

Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-0uc30b7c-build/

The path is a director where home assistant seems to unpack things

I wonder if have the wrong line for requirements:

REQUIREMENTS = [
    'https://github.com/Betree/magicblue'
    '/archive/0.2.2.zip'
    '#magicblue==0.2.2']

EDIT: Looking through the Home Assistant doc yes that bit is wrong. I’ll try installing it with a target to the deps folder tonight.

Are you sure your default python is python 3 ? On most debian-like systems python command is mapped to python2.7. You can run python --version to check that.

If this is your problem, you can usually run python 3 with python3 command. Ubuntu and some others also provide a pip3 package. Python 2 and Python 3 environments are totally separated (libraries you install for one version are only installed for this version).

Also, MagicBlue is only compatible with Python3 but it should be fine as HomeAssistant run on Python 3 too :slight_smile:

Yep Python 3 is there.

Got my python script working in isolation from Home Assistant so step 1 is done.

So i have now installed the magicblue library with a target of the deps folder (as per Home Assistant instructions) which has stopped the setup.py egg_info error. So another step forward!

However the start of the Home Assistant service now gets stuck and does not load any of my lights. I cannot see an errors but the load gets halted.

My theory is that the connection to the bulb is not going through, I think this may be sue to the need for the python script needing to run under sudo to access the bluetooth but Home Assistant not running it’s components in the same manner.

Any pointers welcome.

You’re right about the script needing to be run under sudo. I wish I could do otherwise but it’s a requirement from BluePy (and most other Bluetooth libs I’ve seen).

However there must be a solution to bypass that. Some says that by adding yourself to the lp group sudo is not necessary anymore (sudo adduser $USER lp then restart session).
Interactive shell (MagicBlueShell) does check for sudo but the lib that you import in your python does not so you can give that a try.

Hi guys.

I just created a component for MagicBlue. It’s on https://github.com/xiaohuim/homeassistant-magicblue

Right now it’s just has the On/Off and brightness control. I will add the colour control later when I have some time.

Please test it out or anyone wants to contribute is welcome.

Thanks,
Ma

That looks awesome :clap:

I can’t personally test it for now but I’ll put a link on magicblue README so people can find this easily.