openhab / openhab-addons

Add-ons for openHAB
https://www.openhab.org/
Eclipse Public License 2.0
1.86k stars 3.58k forks source link

[avmfritz] FRITZ!DECT 500 exposes Switch and Dimmer as single channel #12508

Closed sommer closed 2 weeks ago

sommer commented 2 years ago

From what I understand, the FRITZ!DECT 500 Color LED lightbulb has two separately controllable parameters:

  1. its brightness/color and
  2. whether it is on or off.

Changing either is done using separate commands sent to the AHA HTTP interface.

As of version 3.2.0, the openhab-addons expose both as a single channel and, depending on which command is sent to the channel (on/off vs. a new brightness) either parameter is changed.

This does not mesh very well with the way OpenHAB seems to think (a light that is "off" has zero brightness; a light that has nonzero brightness is "on").

Expected Behavior

From my limited understanding, I can think of two ways to work around this:

  1. expose both parameters (on/off and brightness) as separate channels
  2. control both parameters via single channel, but make sure that OpenHAB assumptions hold:
    1. If somebody switches the physical light "off", report zero brightness so that OpenHAB knows the light is off
    2. If somebody turns up the brightness in OpenHAB, make sure the physical light is also turned "on"

Current Behavior

OpenHAB UIs show lights that have been switched "off" as "on" (because they still have nonzero brightness). Dimming a light to 0% in OpenHAB UIs does not switch the light "off" but only reduces its brightness to zero, confusing other apps controlling the same bulb.

Possible Solution

To illustrate the second option (single channel control of bulb), the following (crude) code seems to achieve this:

diff --git a/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/handler/AVMFritzBaseThingHandler.java b/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/handler/AVMFritzBaseThingHandler.java
index e9942d1224..f78af39cec 100644
--- a/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/handler/AVMFritzBaseThingHandler.java
+++ b/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/handler/AVMFritzBaseThingHandler.java
@@ -157,7 +157,8 @@ public abstract class AVMFritzBaseThingHandler extends BaseThingHandler implemen
                 if (deviceModel.isHANFUNBlinds()) {
                     updateLevelControl(deviceModel.getLevelControlModel());
                 } else if (deviceModel.isColorLight()) {
-                    updateColorLight(deviceModel.getColorControlModel(), deviceModel.getLevelControlModel());
+                    updateColorLight(deviceModel.getColorControlModel(), deviceModel.getLevelControlModel(),
+                            deviceModel.getSimpleOnOffUnit());
                 } else if (deviceModel.isDimmableLight() && !deviceModel.isHANFUNBlinds()) {
                     updateDimmableLight(deviceModel.getLevelControlModel());
                 } else if (deviceModel.isHANFUNUnit() && deviceModel.isHANFUNOnOff()) {
@@ -203,11 +204,24 @@ public abstract class AVMFritzBaseThingHandler extends BaseThingHandler implemen
     }

     private void updateColorLight(@Nullable ColorControlModel colorControlModel,
-            @Nullable LevelControlModel levelControlModel) {
+            @Nullable LevelControlModel levelControlModel, @Nullable SimpleOnOffModel simpleOnOffUnit) {
         if (colorControlModel != null && levelControlModel != null) {
             DecimalType hue = new DecimalType(colorControlModel.hue);
             PercentType saturation = ColorControlModel.toPercent(colorControlModel.saturation);
             PercentType brightness = new PercentType(levelControlModel.getLevelPercentage());
+
+            if (simpleOnOffUnit.state == false) {
+                if (brightness.toBigDecimal().compareTo(BigDecimal.ZERO) > 0) {
+                    logger.debug("device is off, but brightness is nonzero - deliberately misreporting as zero");
+                    brightness = new PercentType(0);
+                }
+            } else {
+                if (brightness.toBigDecimal().compareTo(BigDecimal.ZERO) <= 0) {
+                    logger.debug("device is on, but brightness is zero - deliberately misreporting as nonzero");
+                    brightness = new PercentType(1);
+                }
+            }
+
             updateThingChannelState(CHANNEL_COLOR, new HSBType(hue, saturation, brightness));
         }
     }
@@ -420,6 +476,13 @@ public abstract class AVMFritzBaseThingHandler extends BaseThingHandler implemen
                 }
                 if (brightness != null) {
                     fritzBox.setLevelPercentage(ain, brightness);
+                    if (brightness.compareTo(BigDecimal.ZERO) > 0) {
+                        logger.debug("brightness is nonzero - also setting on");
+                        fritzBox.setSwitch(ain, true);
+                    } else {
+                        logger.debug("brightness is zero - also setting off");
+                        fritzBox.setSwitch(ain, false);
+                    }
                 }
                 break;
             case CHANNEL_SETTEMP:

Steps to Reproduce (for Bugs)

  1. Add bulb as avmfritz:HAN_FUN_COLOR_BULB, make sure to use AIN that ends in -1 (the one without seems to be a dummy device, as also evidenced by its functionbitmask=1 (no functions outside of, well, being a HAN-FUN device)
  2. Connect Switch and Dimmer items to its color channel
  3. Switch bulb off. OpenHAB still shows light as "on"

Context

  1. While all of this can be worked around with clever rules, I still think it makes sense to either address the behavior in code or in the documentation

Your Environment

kojid0 commented 1 year ago

Any update on this issue as it already drafts a solution?

vich-667 commented 3 weeks ago

I did some fix of the on off behavior with this PR https://github.com/openhab/openhab-addons/pull/14373. Since this change you can control the the brightness with a percentage value of 0 or via a switch. If the light blub is conctoled via fritz box gui or app its also showed in openhab in a way that it brightness is 0 when its off. I reccomend to cloase this bug.

lsiepel commented 2 weeks ago

(Fixed with openhab 4.0 or later)