mjg59 / python-qube

Python module for controlling Qube Bluetooth smartbulbs
MIT License
8 stars 2 forks source link

Helpful updates #1

Open jimpastos opened 7 years ago

jimpastos commented 7 years ago

Hi, Great work with this project! I've based a custom Qube driver for my home automation project (pimatic, nodejs based) on your work here and I thought I'd contribute my findings. I managed to get everything working with multiple bulbs and working flawlessly with HomeKit :)

You can change the turn on/set rgbw state byte (2nd byte) to 0x11 (instead of 0x01) to enable gamma correction. (this is what the android app does)

In addition, the first byte of set_rgbw can be set to transition the colour instead of a rather crude change. Setting a value of (0x53) followed by duration makes a much cleaner change.

Here is an example (trial an error, hoping its correct) [0] =0x53 <- MASK_AFTER_STATE & MASK_RATE_OF_CHANGE & MASK_COLOR1 & MASK_STATE [1] = 0x11 <- LED_STATE_ON & LED_STATE_OPTION_USE_GAMMA_CORRECTION [2] = red [3] = green [4] = blue [5] = white [6] = 0 // Duration start [7] = 0 // Duration [8] = 0x02 // Duration [9] = 0x26 // Duration end -> 550ms [10] = 0x01 // after state (AFTER_STATE_PERSIST)

From the android APK classes I can see the following flags. I haven't really tested them all but good to know public static final int AFTER_STATE_PERSIST = 1; public static final int AFTER_STATE_REVERT = 0; public static final int AFTER_STATE_TURN_OFF = 2; public static final int AFTER_STATE_TURN_ON = 3; public static final int LED_STATE_CANDLE = 2; public static final int LED_STATE_FLICKER = 4; public static final int LED_STATE_GLOW = 3; public static final int LED_STATE_OFF = 0; public static final int LED_STATE_ON = 1; public static final int LED_STATE_OPTION_OVERWRITE_DEFAULT_START_COLOR = 32; public static final int LED_STATE_OPTION_USE_GAMMA_CORRECTION = 16; public static final int LED_STATE_TRI_FLICKER = 6; public static final int LED_STATE_TRI_GLOW = 5; private static final int MASK_AFTER_STATE = 64; private static final int MASK_COLOR1 = 2; private static final int MASK_COLOR2 = 4; private static final int MASK_COLOR3 = 8; private static final int MASK_DURATION = 32; private static final int MASK_RATE_OF_CHANGE = 16; private static final int MASK_REVERT_RATE = 128; private static final int MASK_STATE = 1;

VanCoding commented 7 years ago

Do you guys know if there's an API documentation for the bulbs? Or did you reverse-engineer all this stuff?

I'm interested in controlling the bulbs over WiFi & Node.js. But apart from this repo, I haven't found that's helpful.

jimpastos commented 7 years ago

Im not sure if you can connect to the bulb directly with wifi. There is no documentation that I know of at present.

The android app seems to use the Qube AWS services when on Wifi, or use bluetooth LE when not connected.

VanCoding commented 7 years ago

Oh, I hope there is a way to use the bulb without their servers, otherwise the bulb would be useless if they go out of business and shut down their servers :S

I've sent them an email. Maybe they answer..

VanCoding commented 7 years ago

I had an email conversation with them.

There is indeed currently no TCP or UDP API that could be used. So the only way to control the bulb over WiFi is the app, which makes it useless for my purposes.

But they are still working on the bulbs and are even able to patch their firmware to add new features. So it's still possible that they will add such an API.

They are focusing on IFTTT and Amazon Echo first, though, so it won't happen soon.

jimpastos commented 7 years ago

Yeap got a response from them too. They don't allow wifi access to the bulb at the moment. We can figure out their entire Cloud API hosted on amazon by looking at their android app, but thats slow because it requires the bulbs and our automation systems to have an internet connection.

I've got my nodejs driver pretty stable and integrated with a pimatic plugin + homekit using BLE. I haven't tested thoroughly with disconnects/reconnects but overall it works great and its fast.

Maybe I'll clean it up and release it as a separate project, but im hoping Qube releases a properly documented (local) API. I have a feeling they will only release a Cloud API though.

VanCoding commented 7 years ago

I have the same feeling. And it would be pretty sad...

I've found out, that the bulbs, when connected to a wi-fi, establish a websocket connection to ws://sock.qube-smarthome.com:3002, and then agree on an encryption mechanism in plain text. After that, all messages are encrypted.

I've set up a local DNS server, that resolves sock.qube-smarthome.com to my local computer, which ran a websocket server that proxied the messages, to find this out.

I think it won't be possible to imitate an official qube-server. We'd most likely need their key for that. I'm not sure, though. But yeah, all I'm going to do now is wait & hope they'll release a local API..

RobAWarner commented 7 years ago

@VanCoding just FYI, me and a friend managed to man-in-the-middle the app and create a very basic Python library to control them, but it still goes via the Qube servers until they release an official API, but it works. https://github.com/Azelphur/qube-api

mjg59 commented 7 years ago

@jimpastos Did you figure out what gamma correction actually does? When I was testing I seemed to get better whites with it turned off, but it wasn't clear to me what exactly it was doing.

jimpastos commented 7 years ago

@mjg59 Im not exactly sure, but there were a few articles describing why its done on RGB LEDs to make colors look more realistic.