Skip to content

Commit 2f261f1

Browse files
committed
Added support for changing brightness
1 parent b23871c commit 2f261f1

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This binding brings Xiaomi Gateway Smart Home devices (Aqara) integration with OpenHAB1.x
44
Currently only one gateway is supported and only getting sub device state/reading events confirmed to be working.
5-
Controlling of gateway light works too including color change within OpenHAB.
5+
Controlling of gateway light works too including color and brightness change within OpenHAB.
66
Unfortunately I have no switch to test controlling its state using write command, but write key generation based on gateway token seems to be implemented OK.
77

88
Based on info found here: https://github.com/louisZL/lumi-gateway-local-api
@@ -55,6 +55,7 @@ Number RoomTemperature "Temperature [%.1f °C]" <temperature> { xiaomigateway=
5555
Number RoomHumidity "Humidity [%.1f %%]" <humidity> { xiaomigateway="158d0001182814.humidity" }
5656
Switch XiaomiGatewayLight "Gateway light" { xiaomigateway="f1b5299a55e5.color" }
5757
Color XiaomiGatewayLightColor "Gateway light color" { xiaomigateway="f1b5299a55e5.color" }
58+
Dimmer XiaomiGatewayBrightness "Gateway brightness" { xiaomigateway="f1b5299a55e5.brightness" }
5859
```
5960
not tested, but should work - ___send ON command to these items to fire an event___
6061
```

src/main/java/org/openhab/binding/xiaomigateway/internal/XiaomiGatewayBinding.java

+27-11
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
import org.openhab.core.binding.AbstractActiveBinding;
1818
import org.openhab.core.items.ItemNotFoundException;
1919
import org.openhab.core.items.ItemRegistry;
20-
import org.openhab.core.library.types.DecimalType;
21-
import org.openhab.core.library.types.HSBType;
22-
import org.openhab.core.library.types.OnOffType;
23-
import org.openhab.core.library.types.OpenClosedType;
20+
import org.openhab.core.library.types.*;
2421
import org.openhab.core.types.Command;
2522
import org.openhab.core.types.State;
2623
import org.osgi.framework.BundleContext;
@@ -242,6 +239,10 @@ private void processOtherCommands(JsonObject jobject) {
242239
logger.debug("XiaomiGateway: processing color event");
243240
processColorEvent(itemName, jobject);
244241
}
242+
if (type.endsWith(".brightness") && getItemSid(type).equals(sid)) {
243+
logger.debug("XiaomiGateway: processing brightness event");
244+
processColorEvent(itemName, jobject);
245+
}
245246
if (type.endsWith(".virtual_switch") && isButtonEvent(jobject, "click")) {
246247
logger.debug("XiaomiGateway: processing virtual switch click event");
247248
processVirtualSwitchEvent(itemName);
@@ -280,17 +281,22 @@ private void processColorEvent(String itemName, JsonObject jobject) {
280281
JsonObject jo = parser.parse(data).getAsJsonObject();
281282
if (jo == null || jo.get("rgb") == null)
282283
return;
283-
long rgb = jo.get("rgb").getAsLong();
284+
rgb = jo.get("rgb").getAsLong();
284285
State oldValue = itemRegistry.getItem(itemName).getState();
285286
State newValue = oldValue;
286287
if (oldValue instanceof OnOffType) {
287288
newValue = rgb > 0 ? OnOffType.ON : OnOffType.OFF;
288-
} else {
289+
} else if (oldValue instanceof HSBType) {
289290
//HSBType
290291
long br = rgb / 65536 / 256;
291292
Color color = new Color((int) (rgb - (br * 65536 * 256)));
292293
newValue = new HSBType(color);
294+
} else {
295+
//Percent Type
296+
long br = rgb / 65536 / 256;
297+
newValue = new PercentType((int) br);
293298
}
299+
294300
if (!newValue.equals(oldValue))
295301
eventPublisher.postUpdate(itemName, newValue);
296302
} catch (Exception ex) {
@@ -670,23 +676,33 @@ protected void internalReceiveCommand(String itemName, Command command) {
670676
// BindingProviders provide a binding for the given 'itemName'.
671677
logger.debug("internalReceiveCommand({},{}) is called!", itemName, command);
672678
String itemType = getItemType(itemName);
673-
if (!(command instanceof OnOffType || command instanceof HSBType)) {
674-
logger.error("Only OnOff/HSB command types currently supported");
679+
if (!(command instanceof PercentType || command instanceof OnOffType || command instanceof HSBType)) {
680+
logger.error("Only OnOff/HSB/Percent command types currently supported");
675681
return;
676682
}
677-
if (!(itemType.contains("channel") || itemType.endsWith(".color"))) {
683+
if (!(itemType.contains("channel") || itemType.endsWith(".color") || itemType.endsWith(".brightness"))) {
678684
//only channel items
679685
return;
680686
}
681687

682-
if (itemType.endsWith(".color") && sid.equals(getItemSid(itemType))) {
688+
if ((itemType.endsWith(".color") || itemType.endsWith(".brightness")) && sid.equals(getItemSid(itemType))) {
683689
if (command instanceof OnOffType) {
684690
changeGatewayColor(command.equals(OnOffType.OFF) ? 0 : startColor);
685-
} else {
691+
} else if (command instanceof HSBType) {
686692
HSBType hsb = (HSBType) command;
687693
long color = getRGBColor(hsb);
688694
changeGatewayColor(color);
695+
} else {
696+
if( rgb == 0 )
697+
return;
698+
699+
//Percent type
700+
PercentType brightness = (PercentType) command;
701+
long currentBrightness = (rgb / 65536 / 256);
702+
long color = rgb - (currentBrightness * 65536 * 256) + brightness.longValue() * 65536 * 256;
703+
changeGatewayColor(color);
689704
}
705+
690706
return;
691707
}
692708

src/main/java/org/openhab/binding/xiaomigateway/internal/XiaomiGatewayGenericBindingProvider.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010

1111
import org.openhab.binding.xiaomigateway.XiaomiGatewayBindingProvider;
1212
import org.openhab.core.items.Item;
13-
import org.openhab.core.library.items.ColorItem;
14-
import org.openhab.core.library.items.ContactItem;
15-
import org.openhab.core.library.items.NumberItem;
16-
import org.openhab.core.library.items.SwitchItem;
13+
import org.openhab.core.library.items.*;
1714
import org.openhab.model.item.binding.AbstractGenericBindingProvider;
1815
import org.openhab.model.item.binding.BindingConfigParseException;
1916

@@ -38,10 +35,10 @@ public String getBindingType() {
3835
*/
3936
@Override
4037
public void validateItemType(Item item, String bindingConfig) throws BindingConfigParseException {
41-
if (!(item instanceof ColorItem || item instanceof SwitchItem || item instanceof ContactItem || item instanceof NumberItem)) {
38+
if (!(item instanceof DimmerItem || item instanceof ColorItem || item instanceof SwitchItem || item instanceof ContactItem || item instanceof NumberItem)) {
4239
throw new BindingConfigParseException("item '" + item.getName()
4340
+ "' is of type '" + item.getClass().getSimpleName()
44-
+ "', only Switch- Contact- and NumberItems are allowed - please check your *.items configuration");
41+
+ "', only Dimmer- Color- Switch- Contact- and NumberItems are allowed - please check your *.items configuration");
4542
}
4643
}
4744

0 commit comments

Comments
 (0)