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
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
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
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?
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.
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
Good luck getting yours working.
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)
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 :
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
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
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.
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.