Magic Blue Bulb Bluetooth 4.0

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.

Hi,

Thanks for creating this :slight_smile:

I’ve been trying to test it but I get the following errors when trying to switch a bulb on/off

Traceback (most recent call last):
File “/usr/lib/python3.4/asyncio/tasks.py”, line 233, in _step
result = coro.throw(exc)
File “/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/core.py”, line 1015, in _event_to_service_call
yield from service_handler.func(service_call)
File “/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/light/init.py”, line 246, in async_handle_light_service
yield from light.async_turn_on(**params)
File “/usr/lib/python3.4/asyncio/futures.py”, line 388, in iter
yield self # This tells Task to wait for completion.
File “/usr/lib/python3.4/asyncio/tasks.py”, line 286, in _wakeup
value = future.result()
File “/usr/lib/python3.4/asyncio/futures.py”, line 277, in result
raise self._exception
File “/usr/lib/python3.4/concurrent/futures/thread.py”, line 54, in run
result = self.fn(*self.args, **self.kwargs)
File “/home/homeassistant/.homeassistant/custom_components/light/magicbluelight.py”, line 84, in turn_on
_LOGGER.error(‘Could not connect to the MagicBlue %s’, bulb_mac_address)
NameError: name ‘bulb_mac_address’ is not defined
17-04-25 21:19:52 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/home/homeassistant/.homeassistant/custom_components/light/magicbluelight.py”, line 82, in turn_on
self._light.connect()
File “/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/magicblue/magicbluelib.py”, line 49, in connect
bluetooth_adapter_nr)
File “/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/bluepy/btle.py”, line 318, in init
self.connect(deviceAddr, addrType, iface)
File “/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/bluepy/btle.py”, line 353, in connect
self._startHelper()
File “/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/bluepy/btle.py”, line 210, in _startHelper
universal_newlines=True)
File “/usr/lib/python3.4/subprocess.py”, line 859, in init
restore_signals, start_new_session)
File “/usr/lib/python3.4/subprocess.py”, line 1457, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: ‘/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/bluepy/bluepy-helper’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/usr/lib/python3.4/asyncio/tasks.py”, line 233, in _step
result = coro.throw(exc)
File “/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/core.py”, line 1015, in _event_to_service_call
yield from service_handler.func(service_call)
File “/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/light/init.py”, line 246, in async_handle_light_service
yield from light.async_turn_on(**params)
File “/usr/lib/python3.4/asyncio/futures.py”, line 388, in iter
yield self # This tells Task to wait for completion.
File “/usr/lib/python3.4/asyncio/tasks.py”, line 286, in _wakeup
value = future.result()
File “/usr/lib/python3.4/asyncio/futures.py”, line 277, in result
raise self._exception
File “/usr/lib/python3.4/concurrent/futures/thread.py”, line 54, in run
result = self.fn(*self.args, **self.kwargs)
File “/home/homeassistant/.homeassistant/custom_components/light/magicbluelight.py”, line 84, in turn_on
_LOGGER.error(‘Could not connect to the MagicBlue %s’, bulb_mac_address)
NameError: name ‘bulb_mac_address’ is not defined

if I use the following as a PI user I can turn them on/off and change the colour so they are working and I have the correct mac address.

sudo magicblueshell -c ‘set_color red’ -m F2:F1:09:E6:DE:78
sudo magicblueshell -c ‘set_color blue’ -m C1:40:5D:6E:A3:E8

My yaml config…

light:
  - platform: magicbluelight
    name: 'BedroomRedBot'
    address: C1:40:5D:6E:A3:E8
    version: 8
  - platform: magicbluelight
    name: 'PC'
    address: F2:F1:09:E6:DE:78
    version: 8

FileNotFoundError: [Errno 2] No such file or directory: '/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/bluepy/bluepy-helper'


I guess it’s because your HA is running in the virtualenv. But the ‘magicblue’ module is installed in your global environment. So it can’t find the ‘bluepy-helper’. Try to re-install ‘magicblue’ in your venv.

There seems to be a mistype on the component logger : bulbmac_address instead of bulb_mac_address

But for your particular bug, it looks like HomeAssistant doesn’t use your default python. Can you run python --version (and / or python3 --version if you have both installed) ?

Edit: To go with x.ma answer you should install magicblue + bluepy directly in HomeAssistant venv

It is weird to see “bulbmac_address” cause I just checked the code and there’s no bulbmac_address typo.

MagicBlue is installed in my venv…

(homeassistant_venv) homeassistant@raspberrypi3:/home/pi$ pip3 install bluepy
Collecting bluepy
Downloading bluepy-1.0.5.tar.gz (210kB)
100% |████████████████████████████████| 215kB 399kB/s
Building wheels for collected packages: bluepy
Running setup.py bdist_wheel for bluepy … done
Stored in directory: /home/homeassistant/.cache/pip/wheels/d1/31/8c/b38398d0e49570f1c74f746bf268fa8a02e0fd69dd15bdc2e3
Successfully built bluepy
Installing collected packages: bluepy
Successfully installed bluepy-1.0.5
(homeassistant_venv) homeassistant@raspberrypi3:/home/pi$ pip3 install git+https://github.com/Betree/pyMagicBlue.git
Collecting git+https://github.com/Betree/pyMagicBlue.git
Cloning https://github.com/Betree/pyMagicBlue.git to /tmp/pip-gg2n865n-build
Requirement already satisfied: bluepy in /srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages (from magicblue==0.2.3)
Collecting webcolors (from magicblue==0.2.3)
Downloading webcolors-1.7.tar.gz
Building wheels for collected packages: webcolors
Running setup.py bdist_wheel for webcolors … done
Stored in directory: /home/homeassistant/.cache/pip/wheels/4a/bb/ba/464319914f0bfb826beb0955bbb29d7188a18368ea226a758d
Successfully built webcolors
Installing collected packages: webcolors, magicblue
Running setup.py install for magicblue … done
Successfully installed magicblue-0.2.3 webcolors-1.7

My pythons are…
pi@raspberrypi3:~ $ python --version
Python 2.7.9
pi@raspberrypi3:~ $ python3 --version
Python 3.5.2

Can you cd <your_venv_python_lib_dir>/site-packages/bluepy/ and see if bluepy-helper exist?

And also try to run magicblueshell in the same venv as HA.

The bluez-helper.c file is there but I doubt it’s been compiled :stuck_out_tongue:
You can’t run magicblueshell from the venv as it needs root.

Someone had this issue in the past : https://github.com/Betree/magicblue/issues/11.

As stated on the issue, you can try to compile it yourself (replace the folder by the location of your bluepy installation) :

cd "/usr/local/lib/python3.4/dist-packages/bluepy-1.0.5-py3.4.egg/bluepy/"    
sudo make

This solutions was first found on https://installfights.blogspot.com/2016/10/how-to-control-magicblue-led-bulb-with.html

I found the install instructions on the bluepy Github page:

To install the source and build locally:

$ sudo apt-get install git build-essential libglib2.0-dev
$ git clone https://github.com/IanHarvey/bluepy.git
$ cd bluepy
$ python setup.py build
$ sudo python setup.py install

Besides, I think HA will need root permission to access the bluetooth device. If you can’t run magicblue in your venv manually, then HA can’t run it either.

1 Like

Trouble is you can’t use sudo in venv so it won’t make

Same for the above you can’t sudo anything (apt-get or python). I’ve already installed in my local env and it all works fine.

I installed HA in docker container and docker has this convenient argument docker run --privileged to allow the container to access to all devices. In my opinion, it’s better than virtualvenv to run HA so that also you can mass around and keeps your main system clean.

OK, I’ll give docker a try :slight_smile:

https://github.com/xiaohuim/homeassistant-magicblue.git

Update: Add color control

There is still some work that needs to be done. For example one issue is, when you change the color, the brightness will be set to 100%. Same thing the other way around.

@Betree I think magicblue need to have some small changes so that we can set the brightness while keeping the current color.

Edit: I just created an issue on Betree/magicblue.

You may be able to compile it without sudo but it will probably cause trouble at runtime as bluepy need to be root to access bluetooth devices (though you can also get rid of that by configuring your devices like in this post).

But as x.ma adviced, it’s probably easier to run home-assistant in a docker container :slight_smile:

I don’t understand this docker thing, well that’s not entirely true, I understand it as a concept but as a use case I’m completely at sea :stuck_out_tongue:

So far I’ve installed docker, and a container with HA in it, I think, well it did a lot of very slow downloading and expanding or somethings. However I don’t know where it is or if it’s running or how/where the yaml files go or how to even access/run HA. Also bear in mind that I have HA running via AIO and that can’t be disturbed as it’s basically running my life/home :stuck_out_tongue:

So any ideas where to go from here?

1 Like

Hi. Does someone managed to get these cheap Bluetoot LED bulbs to work with hass.io?

1 Like