Betree / magicblue

💡 Unofficial Python API to control Magic Blue bulbs over Bluetooth
MIT License
100 stars 23 forks source link

Enhancing magicblue, moving to pygatt from bluepy? #17

Closed StevenLooman closed 7 years ago

StevenLooman commented 7 years ago

Hi there,

First of all, great work with with magicblue! I was trying to integrate it with HomeAssistant, per your request, but ran into a few problems.

Wanting to know more about this bulb, I started digging a bit more, such as looking at traffic dumps: found on the research-branch and this article.

After learning more about BTLE and playing around with bluepy and pygatt, I am now able to do things, such as read the state of the lamp (color, brightness, on/off, etc.) Also, I think I am able to remove the need to specify the bulb-version (although I am not sure, I only have V10 bulbs.)

The downside is, though, that I was not able to read the state of the bulb using bluepy. I only got it to work with pygatt. The mechanism works by sending a command and then receiving a notification with data. While pygatt nicely handles the notification by calling a provided callback, I see no callbacks being called in bluepy.

I would like to improve magicblue, and using pygatt instead of bluepy would be the first move. Are you willing to merge a pull request which does this? The PR will include other finding/enhancements as well, of course. I will try to keep the existing API in tact/compatible.

Betree commented 7 years ago

Hello Steven,

The bulb version is required because versions >= 9 use a public address type whereas versions < 9 use a random address type. So except if we bruteforce the connection, we won't be able to connect to read bulb's version without specifying it. Edit: We may be able to detect the address type. Have to dig it.

BluePy has caused me some trouble in the past, with breaking updates and difficulties to install on a lot of platforms. If PyGatt provides the same functionalities and allow us to implement more, I would definitely be willing to merge such pull requests.

One thing that worries me though, PyGatt specifies it runs on Python 2.7. If it's not compatible with python 3, that would be a breaking change. Also I think that HomeAssistant runs on python3.

StevenLooman commented 7 years ago

Hello Benjamin,

Thank you for your response. You are right about the public/random address issue. It slipped my mind, but I will see what I can do about it.

I cannot guarantee that PyGatt will not give issues though, like BluePy might give. The issues for Bluepy probably arise from compiling the helper binary. I think this is something we will have to find out. You can try to install PyGatt yourself and see if you have any problems during installation.

PyGatt also runs on Python 3, which I'm currently doing. HomeAssistant itself runs on Python3, indeed.

I hope to come up with something soon.

Betree commented 7 years ago

For BluePy it indeed comes from the compilation of the helper binary. From what I've tested it needs to be installed as root to compile this helper properly. If you run pip install bluepy without sudo (for example to install it in a virtual env) it will not be compiled, resulting in the error #11.

Glad to hear PyGatt also works with Python 3 :ok_hand:

StevenLooman commented 7 years ago

Closing the defect. Please use the pull request for comments.