Skip to content

Commit

Permalink
Merge pull request openhab#4 from dimalo/mihome-pullme
Browse files Browse the repository at this point in the history
Several Improvements
  • Loading branch information
pboos authored Apr 2, 2017
2 parents 0b7fdd5 + 87e5ab2 commit 92c604b
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<description>Multifunctional controller equipped with an accelerometer and a gyroscope. Triggers the following actions: move, rotate, rotate, flip 90, flip 180, tap twice, shake air, free fall, alert.</description>

<channels>
<channel id="action" typeId="action"/>
<channel id="action" typeId="action"></channel>
<channel typeId="rotation" id="rotation"></channel>
</channels>

<config-description>
Expand Down Expand Up @@ -42,4 +43,14 @@
</options>
</event>
</channel-type>
<channel-type id="rotationAngle">
<item-type>Number</item-type>
<label>Cube rotation angle in degrees</label>
<state readOnly="true" step="1" pattern="%d°"></state>
</channel-type>
<channel-type id="rotationTime">
<item-type>Number</item-type>
<label>Cube rotation time in milliseconds</label>
<state readOnly="false" step="1" pattern="%d ms"></state>
</channel-type>
</thing:thing-descriptions>
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@
<description>An identifier for the item connected.</description>
<required>true</required>
</parameter>
<parameter name="noMotionAfter" type="integer" readOnly="false">
<limitToOptions>true</limitToOptions>
<options>
<option value="120">120</option>
<option value="180">180</option>
<option value="300">300</option>
<option value="600">600</option>
<option value="1200">1200</option>
<option value="1800">1800</option>
</options>
<description>Time after which the motion sensor tells openhab, that there is no motion anymore.</description>
<unitLabel>Seconds</unitLabel>
<required>true</required>
<default>120</default>
<label>No motion detected after</label>
</parameter>
</config-description>
</thing-type>

Expand Down
219 changes: 144 additions & 75 deletions addons/binding/org.openhab.binding.mihome/README.md
Original file line number Diff line number Diff line change
@@ -1,117 +1,186 @@
---
layout: documentation
---

{% include base.html %}

# Xiaomi Mi Smart Home Binding

The binding allows your openHAB to communicate with Xiaomi Smart Home Suite.
This binding allows your openHAB to communicate with the Xiaomi Smart Home Suite.

In order to connect the Gateway, you need to install MiHome app
In order to connect the Gateway, you need to install the MiHome app
from the [Google Play](https://play.google.com/store/apps/details?id=com.xiaomi.smarthome) or [AppStore](https://itunes.apple.com/app/mi-home-xiaomi-for-your-smarthome/id957323480).

## How to set up

1. Install the binding
2. Setup Gateway to be discoverable
1. Add Gateway 2 to your WiFi Network
2. Install Mi Home app from [Google Play](https://play.google.com/store/apps/details?id=com.xiaomi.smarthome) or [AppStore](https://itunes.apple.com/app/mi-home-xiaomi-for-your-smarthome/id957323480)
3. Set your region to Mainland China under Settings -> Locale (seems to be required)
4. Update gateway (maybe multiple times)
5. Enable developer mode:<br />
1. Select your Gateway in Mi Home
2. Go to the "..." menu and click "About"
3. Tap the version number "Version : 2.XX" at the bottom of the screen repeatedly until you enable developer mode
4. you should now have 2 extra options listed in Chinese.
5. Choose the first new option (fourth position in the menu or the longer text in Chinese)
6. Tap the toggle switch to enable LAN functions. Note down the developer key (something like: 91bg8zfkf9vd6uw7).
7. Make sure you hit the OK button (to the right of the cancel button) to save your changes.
4. In openHAB you should now be able to discover the Xiaomi Gateway
5. Enter the previously noted developer key in openHAB Paper UI -> Configuration -> Things -> Xiaomi Gateway -> Edit -> Developer Key. Save.
(This is required if you want to be able to send controls to the devices like the light of the gateway.)
6. Your sensors should be getting discovered by openHAB as you add and use them.
## Setup

* Install the binding
* Setup Gateway to be discoverable

1. Add Gateway 2 to your WiFi Network
1. Install Mi Home app from [Google Play](https://play.google.com/store/apps/details?id=com.xiaomi.smarthome) or [AppStore](https://itunes.apple.com/app/mi-home-xiaomi-for-your-smarthome/id957323480) (your phone may need to be changed to English language first)
1. Set your region to Mainland China under Settings -> Locale (seems to be required)
1. Update gateway (maybe multiple times)
1. Enable developer mode:

1. Select your Gateway in Mi Home
1. Go to the "..." menu and click "About"
1. Tap the version number "Version : 2.XX" at the bottom of the screen repeatedly until you enable developer mode
1. You should now have 2 extra options listed in Chinese
1. Choose the first new option (fourth position in the menu or the longer text in Chinese)
1. Tap the toggle switch to enable LAN functions. Note down the developer key (something like: 91bg8zfkf9vd6uw7)
1. Make sure you hit the OK button (to the right of the cancel button) to save your changes

* In openHAB you should now be able to discover the Xiaomi Gateway
* From now on you don't really need the app anymore - only if you're keen on updates or you want to add devices (see below), which also can be done without the app
* Enter the previously noted developer key in openHAB Paper UI -> Configuration -> Things -> Xiaomi Gateway -> Edit -> Developer Key. Save
(This is required if you want to be able to send controls to the devices like the light of the gateway)
* Your sensors should be getting discovered by openHAB as you add and use them

## Connecting sub-devices (sensors) to the Gateway

There are two ways of connecting Xiaomi devices to the gateway:

1. Online - within the MiHome App
2. Offline - manual
* Online - within the MiHome App
* Offline - manual

1. Click 3 times on the Gateway's button
1. Gateway will flash in blue and you will hear female voice in Chinese
1. Place the needle into the sensor and hold it for at least 3 seconds
1. You'll hear confirmation message in Chinese
1. The device appears in openHAB thing Inbox

* If you don't want to hear the chinese voice every time, you can disable it by setting the volume to minimum in the MiHome App (same for the blinking light)
* The devices don't need an internet connection to be working after you have set up the developer mode BUT you won't be able to connect to them via App anymore

* Click 3 times on the Gateway's button
* Gateway will flash in blue and you will hear female voice in Chinese
* Place the needle into the sensor and hold it for at least 3 seconds
* You'll hear confirmation message in Chinese

## Important information

The binding requires port `9898` to not be used by any other service on the system.

## Full Example

xiaomi.items:
**xiaomi.items:**

```
TODO
// Xiaomi Gateway
Dimmer Gateway_Brightness <dimmablelight> { channel="mihome:gateway:<ID>:brightness" }
Color Gateway_Color <rgb> { channel="mihome:gateway:<ID>:color" }
Dimmer Gateway_ColorTemperature <heating> { channel="mihome:gateway:<ID>:colorTemperature" }
// Xiaomi Temperature and Humidity Sensor
Number HT_Temperature <temperature> { channel="mihome:sensor_ht:<ID>:temperature" }
Number HT_Humidity <humidity> { channel="mihome:sensor_ht:<ID>:humidity" }
// NOTE: Battery level will be sent when battery is low
Number HT_Battery <battery> { channel="mihome:sensor_ht:<ID>:voltage" }
// Xiaomi Motion Sensor
Switch MotionSensor_MotionStatus <motion> { channel="mihome:sensor_motion:<ID>:motion" }
DateTime MotionSensor_LastMotion "[%1$tY-%1$tm-%1$td %1$tH:%1$tM]" <clock-on> { channel="mihome:sensor_motion:<ID>:lastMotion" }
// NOTE: Battery level will be sent when battery is low
Number MotionSensor_Battery <battery> { channel="mihome:sensor_motion:<ID>:voltage" }
// Xiaomi Plug
Switch Plug_Switch <switch> { channel="mihome:sensor_plug:<ID>:powerOn" }
Number Plug_Voltage <poweroutlet> { channel="mihome:sensor_plug:<ID>:loadVoltage" }
Number Plug_Power <energy> { channel="mihome:sensor_plug:<ID>:loadPower" }
Number Plug_Consumption <line-incline> { channel="mihome:sensor_plug:<ID>:powerConsumed" }
// Xiaomi Window Switch
Contact WindowSwitch_Status <window> { channel="mihome:sensor_magnet:<ID>:isOpen" }
// NOTE: Battery level will be sent when battery is low
Number WindowSwitch_Battery <battery> { channel="mihome:sensor_magnet:<ID>:voltage" }
// Xiaomi Cube - see "xiaomi.rules" for action triggers
Number Cube_RotationAngle { channel="mihome:sensor_cube:<ID>:rotationAngle" }
Number Cube_RotationTime { channel="mihome:sensor_cube:<ID>:rotationTime" }
// NOTE: Battery level will be sent when battery is low
Number Cube_Battery <battery> { channel="mihome:sensor_cube:<ID>:voltage" }
// Xiaomi Switch - see "xiaomi.rules" for action triggers
// NOTE: Battery level will be sent when battery is low
Number Switch_Battery <battery> { channel="mihome:sensor_switch:<ID>:voltage" }
```

xiaomi.rules:
**xiaomi.rules:**

```
rule "Xiaomi Switch single click"
rule "Xiaomi Switch"
when
Channel "mihome:sensor_switch:<id>:button" triggered CLICK
Channel "mihome:sensor_switch:<ID>:button" triggered
then
if (Vacuum_Dock.state == ON) {
sendCommand(Vacuum_Dock, OFF)
} else {
sendCommand(Vacuum_Dock, ON)
var actionName = receivedEvent.getEvent()
switch(actionName) {
case "CLICK": {
<ACTION>
}
case "DOUBLE_CLICK": {
<ACTION>
}
case "LONG_CLICK_PRESS": {
<ACTION>
}
case "LONG_CLICK_RELEASE": {
<ACTION>
}
}
end
rule "Xiaomi Switch double click"
rule "Xiaomi Cube"
when
Channel "mihome:sensor_switch:<id>:button" triggered DOUBLE_CLICK
Channel 'mihome:sensor_cube:<ID>:action' triggered
then
if (Gateway_Light.state > 0) {
sendCommand(Gateway_Light, OFF)
} else {
sendCommand(Gateway_Light, ON)
var actionName = receivedEvent.getEvent()
switch(actionName) {
case "MOVE": {
<ACTION>
}
case "ROTATE_RIGHT": {
<ACTION>
}
case "ROTATE_LEFT": {
<ACTION>
}
case "FLIP90": {
<ACTION>
}
case "FLIP180": {
<ACTION>
}
case "TAP_TWICE": {
<ACTION>
}
case "SHAKE_AIR": {
<ACTION>
}
case "FREE_FALL": {
<ACTION>
}
case "ALERT": {
<ACTION>
}
}
end
rule "Bedroom light control with cube"
rule "Xiaomi Motion Sensor"
when
Channel 'mihome:sensor_cube:<id>:action' triggered
Item MotionSensor_MotionStatus changed
then
if (receivedEvent.event == "SHAKE_AIR") {
if (LEDBulb_Brightness.state == 0) {
sendCommand(LEDBulb_Brightness, ON)
sendCommand(LEDBulb_ColorTemperature, 50)
if (MotionSensor_MotionStatus.state == ON) {
<ACTION>
} else {
sendCommand(LEDBulb_Brightness, OFF)
<ACTION>
}
} else if (receivedEvent.event == "FLIP90") {
sendCommand(LEDBulb_Brightness, ON)
sendCommand(LEDBulb_ColorTemperature, 10)
} else if (receivedEvent.event == "FLIP180") {
sendCommand(LEDBulb_Brightness, ON)
sendCommand(LEDBulb_Color, new HSBType(new DecimalType(90),new PercentType(100),new PercentType(100)))
}
end
rule "Xiaomi Window Switch"
when
Item WindowSwitch_Status changed
then
if (WindowSwitch_Status.state == OPEN) {
<ACTION>
} else {
<ACTION>
}
end
rule "Hallway night light"
// This rule is applicable for every battery powered sensor device
rule "Xiaomi Motion Sensor Low Battery"
when
Item MotionSensor_MotionStatus changed from OFF to ON or
Item MotionSensor_MotionStatus changed from ON to OFF
Channel "mihome:sensor_motion:<ID>:batteryLevel" triggered LOW
then
if (MotionSensor_MotionStatus.state == ON) {
sendCommand(Gateway_Brightness, new PercentType(2))
Thread::sleep(100)
sendCommand(Gateway_Color, HSBType.fromRGB(i255, 241, 224))
} else {
sendCommand(Gateway_Brightness, new PercentType(0))
}
<ACTION>
end
```
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class XiaomiGatewayBindingConstants {
// List of all Channel ids
public static final String CHANNEL_TEMPERATURE = "temperature";
public static final String CHANNEL_VOLTAGE = "voltage";
public static final String CHANNEL_BATTERY_LOW = "batteryLevel";
public static final String CHANNEL_HUMIDITY = "humidity";
public static final String CHANNEL_MOTION = "motion";
public static final String CHANNEL_LAST_MOTION = "lastMotion";
Expand All @@ -39,6 +40,10 @@ public class XiaomiGatewayBindingConstants {
public static final String CHANNEL_LOAD_VOLTAGE = "loadVoltage";
public static final String CHANNEL_LOAD_POWER = "loadPower";
public static final String CHANNEL_POWER_CONSUMED = "powerConsumed";
public static final String CHANNEL_CUBE_ACTION = "action";
public static final String CHANNEL_CUBE_ROTATION_ANGLE = "rotationAngle";
public static final String CHANNEL_CUBE_ROTATION_TIME = "rotationTime";

// gateway light
public static final String CHANNEL_BRIGHTNESS = "brightness";
public static final String CHANNEL_COLOR = "color";
Expand All @@ -53,5 +58,6 @@ public class XiaomiGatewayBindingConstants {

// Item config properties
public static final String ITEM_ID = "itemId";
public static final String NO_MOTION = "noMotionAfter";

}
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ private void updateGatewayStatus() {
updateStatus(isGatewayOnline() ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
}

void updateDeviceStatus(String sid) {
if (sid != null) {
lastOnlineMap.put(sid, System.currentTimeMillis());
}
}

private boolean isGatewayOnline() {
return hasItemActivity(getGatewaySid(), ONLINE_TIMEOUT);
}
Expand Down
Loading

0 comments on commit 92c604b

Please sign in to comment.