|
17 | 17 | import org.openhab.core.binding.AbstractActiveBinding;
|
18 | 18 | import org.openhab.core.items.ItemNotFoundException;
|
19 | 19 | 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.*; |
24 | 21 | import org.openhab.core.types.Command;
|
25 | 22 | import org.openhab.core.types.State;
|
26 | 23 | import org.osgi.framework.BundleContext;
|
@@ -242,6 +239,10 @@ private void processOtherCommands(JsonObject jobject) {
|
242 | 239 | logger.debug("XiaomiGateway: processing color event");
|
243 | 240 | processColorEvent(itemName, jobject);
|
244 | 241 | }
|
| 242 | + if (type.endsWith(".brightness") && getItemSid(type).equals(sid)) { |
| 243 | + logger.debug("XiaomiGateway: processing brightness event"); |
| 244 | + processColorEvent(itemName, jobject); |
| 245 | + } |
245 | 246 | if (type.endsWith(".virtual_switch") && isButtonEvent(jobject, "click")) {
|
246 | 247 | logger.debug("XiaomiGateway: processing virtual switch click event");
|
247 | 248 | processVirtualSwitchEvent(itemName);
|
@@ -280,17 +281,22 @@ private void processColorEvent(String itemName, JsonObject jobject) {
|
280 | 281 | JsonObject jo = parser.parse(data).getAsJsonObject();
|
281 | 282 | if (jo == null || jo.get("rgb") == null)
|
282 | 283 | return;
|
283 |
| - long rgb = jo.get("rgb").getAsLong(); |
| 284 | + rgb = jo.get("rgb").getAsLong(); |
284 | 285 | State oldValue = itemRegistry.getItem(itemName).getState();
|
285 | 286 | State newValue = oldValue;
|
286 | 287 | if (oldValue instanceof OnOffType) {
|
287 | 288 | newValue = rgb > 0 ? OnOffType.ON : OnOffType.OFF;
|
288 |
| - } else { |
| 289 | + } else if (oldValue instanceof HSBType) { |
289 | 290 | //HSBType
|
290 | 291 | long br = rgb / 65536 / 256;
|
291 | 292 | Color color = new Color((int) (rgb - (br * 65536 * 256)));
|
292 | 293 | newValue = new HSBType(color);
|
| 294 | + } else { |
| 295 | + //Percent Type |
| 296 | + long br = rgb / 65536 / 256; |
| 297 | + newValue = new PercentType((int) br); |
293 | 298 | }
|
| 299 | + |
294 | 300 | if (!newValue.equals(oldValue))
|
295 | 301 | eventPublisher.postUpdate(itemName, newValue);
|
296 | 302 | } catch (Exception ex) {
|
@@ -670,23 +676,33 @@ protected void internalReceiveCommand(String itemName, Command command) {
|
670 | 676 | // BindingProviders provide a binding for the given 'itemName'.
|
671 | 677 | logger.debug("internalReceiveCommand({},{}) is called!", itemName, command);
|
672 | 678 | 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"); |
675 | 681 | return;
|
676 | 682 | }
|
677 |
| - if (!(itemType.contains("channel") || itemType.endsWith(".color"))) { |
| 683 | + if (!(itemType.contains("channel") || itemType.endsWith(".color") || itemType.endsWith(".brightness"))) { |
678 | 684 | //only channel items
|
679 | 685 | return;
|
680 | 686 | }
|
681 | 687 |
|
682 |
| - if (itemType.endsWith(".color") && sid.equals(getItemSid(itemType))) { |
| 688 | + if ((itemType.endsWith(".color") || itemType.endsWith(".brightness")) && sid.equals(getItemSid(itemType))) { |
683 | 689 | if (command instanceof OnOffType) {
|
684 | 690 | changeGatewayColor(command.equals(OnOffType.OFF) ? 0 : startColor);
|
685 |
| - } else { |
| 691 | + } else if (command instanceof HSBType) { |
686 | 692 | HSBType hsb = (HSBType) command;
|
687 | 693 | long color = getRGBColor(hsb);
|
688 | 694 | 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); |
689 | 704 | }
|
| 705 | + |
690 | 706 | return;
|
691 | 707 | }
|
692 | 708 |
|
|
0 commit comments