snOOrz / homebridge-aqara

HomeBridge plugin for Aqara gateway and devices
539 stars 74 forks source link

Does the Magic Cube work? #5

Open jjensn opened 7 years ago

jjensn commented 7 years ago

http://xiaomi-mi.com/mi-smart-home/xiaomi-mi-smart-home-cube-white/

Will this sensor be supported?

snOOrz commented 7 years ago

@jared-jensen sorry I don't have magic cube. @WeiranWang can you help?

cocacola89 commented 7 years ago

@jared-jensen, @snOOrz I do own a Magic Cube and a couple of Xiaomi Wireless Switches (http://xiaomi-mi.com/mi-smart-home/xiaomi-mi-wireless-switch/). Even though they are returned in response to get_id_list and readrequests - they have nothing in the data field.

That's how it looks (I've replaced parts of sids with **\ for privacy):

{"cmd":"read_ack","model":"plug","sid":"158d0000fc****","short_id":39147,"data":"{\"status\":\"off\"}"}
{"cmd":"read_ack","model":"plug","sid":"158d0000fc****","short_id":47486,"data":"{\"status\":\"on\"}"}
{"cmd":"read_ack","model":"sensor_ht","sid":"158d000118****","short_id":18844,"data":"{\"temperature\":\"2842\",\"humidity\":\"2652\"}"}

{"cmd":"read_ack","model":"switch","sid":"158d00011****","short_id":2714,"data":"{}"}
{"cmd":"read_ack","model":"switch","sid":"158d0000fd****","short_id":10651,"data":"{}"}
{"cmd":"read_ack","model":"","sid":"158d00011****","short_id":41818,"data":"{}"}

I guess three bottom ones are the switches and the magic cube (with empty model field). That means that the gateway returns those devices, but as long as they are stateless - they don't have any data associated. That's why wall switches are supported (they do have state returned in data field), and these devices are not.

I guess in order to support those switches and a magic cube we need some kind of event-based API, but the API I've seen in the source code is poll-based (currently this plugin just polls bridge from time to time and returns current state from data field).

@snOOrz, @WeiranWang I would REALLY like to use those devices in Homebridge. Is there any way I could help you guys with it? Is there any description for Xiaomi local network protocol (even in Chinese, I would still like to read it and try to use)? Any way to subscribe to events from switches?

Rob3E3 commented 7 years ago

There may be some hope for this yet. I have a wireless switch and a cube. Using the script here: https://github.com/jon1012/mihome I got data for the switch. The cube remains silent using that script. I didn't even get a string with empty model and data. But the switch did return some data, so maybe that will be helpful going forward. {u'model': u'switch', u'cmd': u'report', u'data': u'{"status":"double_click"}', u'short_id': 8603, u'sid': u'158d0001256666'} {u'model': u'switch', u'cmd': u'report', u'data': u'{"status":"long_click_press"}', u'short_id': 8603, u'sid': u'158d0001256666'} {u'model': u'switch', u'cmd': u'report', u'data': u'{"status":"long_click_release"}', u'short_id': 8603, u'sid': u'158d0001256666'} {u'model': u'switch', u'cmd': u'report', u'data': u'{"status":"double_click"}', u'short_id': 8603, u'sid': u'158d0001256666'} {u'model': u'switch', u'cmd': u'report', u'data': u'{"status":"long_click_press"}', u'short_id': 8603, u'sid': u'158d0001256666'} {u'model': u'switch', u'cmd': u'report', u'data': u'{"status":"long_click_release"}', u'short_id': 8603, u'sid': u'158d0001256666'} {u'model': u'switch', u'cmd': u'report', u'data': u'{"status":"click"}', u'short_id': 8603, u'sid': u'158d0001256666'}

illxi commented 7 years ago

@cocacola89 just in case you haven't seen this. It is the official api document. http://www.lumiunited.com/downloads/zigbee%20sdk%20Message%20format%20V1.0.4.doc

cocacola89 commented 7 years ago

@illxi, @Rob3E3 thanks a lot for sharing, really appreciate this! Funny thing, just 3 days ago I accidentally came across https://github.com/louisZL/lumi-gateway-local-api which has just the same API described. I even managed to hook my Philips Hue lights to Xiaomi switches and it works like a peach. Finally, I can control both lights connected to Xiaomi plugs and Hue lights from the same switch. Sweet!

If you decide to follow this route, there is a bit of a problem here though. This API is based on UDP multicast and you can't run two processes listening to the required address and port on the same machine (at least I couldn't manage to do so). That means that if you're using homebridge-aqara, you can't run another Node.js script or any other app receiving the same events. Right now homebridge-aqara does not support any kind of external extensibility model, so you can't simply hook to it with your code. Luckily, JavaScript allows you to do some prototype-magic to intercept homebridge calls to homebridge-aqara and hook to the parsers array.

If I have some time off work, I will try to suggest a pull request to allow hooking to this events in an easier way.

Rob3E3 commented 7 years ago

@cocacola89 What Xiaomi switches are you using? I implemented the script linked in my last reply, and I now get mqtt data for my Wireless switch. I use Home Assistant, and now the switch can be used as a trigger in HA, which is great. But my Magic Cube is not generating any mqtt data, so right now it can only be used for items connected to my Xiaomi Gateway.

cocacola89 commented 7 years ago

@Rob3E3, I'm using Xiaomi Wireless ZigBee Switches like this one. I also own a Magic Cube and confirm that right now it is not generating any events and can only be used for automations inside MiHome.

I'm currently using homebridge with homebridge-aqara to control Xiaomi Plugs and get temperature from sensors using iOS 10 built-in home app. I really like this setup because all devices supported by homebridge-aqara can appear in iOS Control Center and be used really fast without any external app.

But if you check the code of homebridge-aqara you'll see that at this moment Wireless Switches are not supported and those events are just ignored:

this.parsers = {
    'sensor_ht' : new TemperatureAndHumidityParser(this),
    'motion' : new MotionParser(this),
    'magnet' : new ContactParser(this),
    'ctrl_neutral1' : new LightSwitchParser(this),
    'ctrl_neutral2' : new DuplexLightSwitchParser(this),
    'plug' : new PlugSwitchParser(this)
  };
var model = json['model'];
if (model in this.parsers) {
  this.parsers[model].parse(json, rinfo);

From what I found about Apple HomeKit, the Apple-way of supporting Xiaomi Switches in HomeKit is exposing them via Homebridge as stateless programmable switches, like in homebridge-amazondash plugin, which can be used later for HomeKit automations.

@Rob3E3, I guess you've done the same thing for Home Assistant. That's an interesting approach, considering that Home Assistant can be bridged with HomeBridge via homebridge-homeassistant.

I'm personally not a Python-guy, so I'm trying to achieve a single-homebridge solution in JavaScript. Generally, it's not that hard - I managed to expose Wireless Switches from homebridge-aqara as stateless programmable switches, but that made no use for me as I don't own ATV4 or iPad with iOS 10 to run HomeKit automations. Another thing, I don't like the complexity of Apple-way solution as it involves a whole lot of communications:

**Xiaomi Switch** 
(1) -- via ZigBee --> 
**Xiaomi Gateway** 
(2) -- via UDP -->
**HomeBridge**
(3) -- via MDNS -->
ATV4 or iPad
(4) -- via MDNS -->
**HomeBridge**
(5) -- via HTTP REST -->
**Philips Hue Bridge**
(6) -- via ZigBee -->
**Philips Hue Lightbulb**

So, what I've done is I've made a homebridge plugin that hooks to homebgidge-aqara parsers array and registers a new parser for switch. That parser uses directly Philips Hue API via node-hue-api to control Hue lights.

This solution is not that configurable, it's almost hardcoded, but on the other hand it involves 4 vs. 6 communications and does not require ATV4/iPad/Home Assistant:

**Xiaomi Switch** 
(1) -- via ZigBee --> 
**Xiaomi Gateway** 
(2) -- via UDP -->
**HomeBridge**
(3) -- via HTTP REST -->
**Philips Hue Bridge**
(4) -- via ZigBee -->
**Philips Hue Lightbulb**

I'll work on making this solution more pluggable/configurable and post an update if it turns into something.

Rob3E3 commented 7 years ago

@cocacola89 Yep, I'm using Home Assistant, so I use the Home-Assistant-Homebridge to make things available in iOS. I'm mostly interested in the homebridge-aqara in hopes that in might help expose more Xiaomi Gateway items to Home Assistant. Getting them into iOS Home is an added bonus. I don't use Homekit for any kind of automations. That's what Home Assistant is for. I just use it for being able to operate some lights and switches via Siri. Having the cube and the wireless switch visible to Homekit doesn't really help me (although I can see where it would if I were using Homekit for automations), but having the Xiaomi network data exposed is very helpful, which is why I'm keeping an eye on this project.

cocacola89 commented 7 years ago

@Rob3E3 thanks again for sharing! I'll definitely give Home Assistant a try as I've been looking for a solution that has some non-iOS bound interface and Home Assistant does offer a pretty good looking web-interface.

And I guess we'll all be waiting for a Magic Cube support in the next firmware updates.