Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hue] Fix edge cases for broken lights (API v2) #15999

Merged
merged 2 commits into from
Dec 6, 2023

Conversation

andrewfg
Copy link
Contributor

@andrewfg andrewfg commented Dec 3, 2023

Resolves #15995

Signed-off-by: Andrew Fiddian-Green software@whitebear.ch

@andrewfg andrewfg added the enhancement An enhancement or new feature for an existing add-on label Dec 3, 2023
@andrewfg andrewfg self-assigned this Dec 3, 2023
@andrewfg andrewfg requested a review from cweitkamp as a code owner December 3, 2023 12:41
@andrewfg
Copy link
Contributor Author

andrewfg commented Dec 3, 2023

@jlaur here is the promised PR

It probably makes most sense if you review the test cases first, to make sure that they do address all of your actual edge cases.

Note: we had discussed adding a patch method to the thing handler for the case of things that don't report a minimum dimming level. However this is not feasible since such a patch would also add a a minimum dimming level to an On/Off only lamp, which would cause a brightness channel to wrongly be created when such a thing is discovered.

@andrewfg andrewfg requested a review from jlaur December 3, 2023 12:49
@jlaur
Copy link
Contributor

jlaur commented Dec 3, 2023

This looks like exactly the same fix as provided in 52ea4f2 except that you use minimum dimming level rather than 100. Correct?

@andrewfg
Copy link
Contributor Author

andrewfg commented Dec 3, 2023

This looks like exactly the same fix as provided in 52ea4f2 except that you use minimum dimming level rather than 100. Correct?

Yes indeed. So it matches the Hue app.

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
@andrewfg andrewfg force-pushed the hue-fix-light-edge-cases branch from fa5f1a7 to 5f58831 Compare December 5, 2023 18:04
@andrewfg andrewfg added bug An unexpected problem or unintended behavior of an add-on and removed enhancement An enhancement or new feature for an existing add-on labels Dec 5, 2023
Copy link
Contributor

@jlaur jlaur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewfg - the fix looks good to me. Besides the change of 100 to minimum dimming in comparison with my original fix, any brightness outside of the [minimum dimming : 100] interval will now be corrected to minimum dimming. Just to be sure, is that intentional? It does make some sense, just double-checking.

Regarding the test coverage, can I kindly ask you to bring back the original unit tests provided in 52ea4f2? They are more concise and readable without the distraction of deserialization which is already covered elsewhere. They also use the constraint-based assertion model for readability. I have adapted them for testing the new version of the fix:

/**
 * Copyright (c) 2010-2023 Contributors to the openHAB project
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package org.openhab.binding.hue.internal.clip2;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

import java.math.BigDecimal;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.openhab.binding.hue.internal.api.dto.clip2.Dimming;
import org.openhab.binding.hue.internal.api.dto.clip2.OnState;
import org.openhab.binding.hue.internal.api.dto.clip2.Resource;
import org.openhab.binding.hue.internal.api.dto.clip2.enums.ResourceType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.types.UnDefType;

/**
 * Tests for {@link Resource}.
 * 
 * @author Jacob Laursen - Initial contribution
 */
@NonNullByDefault
public class ResourceTest {

    @Test
    void getBrightnessStateWhenDimmingMissingReturnNull() {
        assertThat(createLightResource(true, null).getBrightnessState(), is(equalTo(UnDefType.NULL)));
    }

    @Test
    void getBrightnessStateWhenOnAndDimming75ReturnBrightness75() {
        assertThat(createLightResource(true, 75.0).getBrightnessState(), is(equalTo(new PercentType(75))));
    }

    @Test
    void getBrightnessStateWhenOnAndDimming125ReturnBrightness100() {
        assertThat(createLightResource(true, 125.0).getBrightnessState(), is(equalTo(new PercentType(100))));
    }

    @Test
    void getBrightnessStateWhenOffAndDimming100ReturnBrightness0() {
        assertThat(createLightResource(false, 100.0).getBrightnessState(), is(equalTo(new PercentType(0))));
    }

    @Test
    void getBrightnessStateWhenOnStateMissingAndDimming0ReturnMinimumBrightness0() {
        assertThat(createLightResource(null, 0.0).getBrightnessState(),
                is(equalTo(new PercentType(new BigDecimal(Dimming.DEFAULT_MINIMUM_DIMMIMG_LEVEL)))));
    }

    @Test
    void getBrightnessStateWhenOnStateMissingAndDimming100ReturnBrightness100() {
        assertThat(createLightResource(null, 100.0).getBrightnessState(), is(equalTo(new PercentType(100))));
    }

    @Test
    void getBrightnessStateWhenOnStateMissingAndDimmingMinus1ReturnMinimumBrightness() {
        assertThat(createLightResource(null, -1.0).getBrightnessState(),
                is(equalTo(new PercentType(new BigDecimal(Dimming.DEFAULT_MINIMUM_DIMMIMG_LEVEL)))));
    }

    @Test
    void getBrightnessStateWhenOnAndDimmingMinus1ReturnMinimumBrightness() {
        assertThat(createLightResource(true, -1.0).getBrightnessState(),
                is(equalTo(new PercentType(new BigDecimal(Dimming.DEFAULT_MINIMUM_DIMMIMG_LEVEL)))));
    }

    @Test
    void getBrightnessStateWhenOnAndDimming0ReturnMinimumBrightness() {
        assertThat(createLightResource(true, 0.0).getBrightnessState(),
                is(equalTo(new PercentType(new BigDecimal(Dimming.DEFAULT_MINIMUM_DIMMIMG_LEVEL)))));
    }

    @Test
    void getBrightnessStateWhenOnAndDimming0ReturnCustomMinimumBrightness() {
        assertThat(createLightResource(true, 0.0, 2.0).getBrightnessState(), is(equalTo(new PercentType(2))));
    }

    private Resource createLightResource(@Nullable Boolean on, @Nullable Double brightness) {
        return createLightResource(on, brightness, null);
    }

    private Resource createLightResource(@Nullable Boolean on, @Nullable Double brightness,
            @Nullable Double minimumDimmingLevel) {
        Resource resource = new Resource(ResourceType.LIGHT);

        if (on != null) {
            OnState onState = new OnState();
            onState.setOn(on);
            resource.setOnState(onState);
        }

        if (brightness != null) {
            Dimming dimming = new Dimming();
            dimming.setBrightness(brightness);

            if (minimumDimmingLevel != null) {
                dimming.setMinimumDimmingLevel(minimumDimmingLevel);
            }

            resource.setDimming(dimming);
        }

        return resource;
    }
}

@andrewfg
Copy link
Contributor Author

andrewfg commented Dec 5, 2023

any brightness outside of the [minimum dimming : 100] interval will now be corrected to minimum dimming

Actually anything above 100 is reduced to 100

@andrewfg
Copy link
Contributor Author

andrewfg commented Dec 5, 2023

bring back the original unit tests

Ok. I will do that.

@jlaur
Copy link
Contributor

jlaur commented Dec 5, 2023

any brightness outside of the [minimum dimming : 100] interval will now be corrected to minimum dimming

Actually anything above 100 is reduced to 100

Yes, of course. 🙂 That was also the case previously, whereas now brightness 0 will be corrected to at least 0.5.

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
@andrewfg andrewfg requested a review from jlaur December 6, 2023 11:09
Copy link
Contributor

@jlaur jlaur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes. Two minor comments, otherwise LGTM.

@andrewfg andrewfg requested a review from jlaur December 6, 2023 11:46
Copy link
Contributor

@jlaur jlaur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jlaur jlaur merged commit 4476e6d into openhab:main Dec 6, 2023
3 checks passed
@jlaur jlaur added this to the 4.1 milestone Dec 6, 2023
@jlaur jlaur changed the title [hue] Fix edge cases for broken lights [hue] Fix edge cases for broken lights (API v2) Dec 6, 2023
austvik pushed a commit to austvik/openhab-addons that referenced this pull request Mar 27, 2024
Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
Signed-off-by: Jørgen Austvik <jaustvik@acm.org>
@andrewfg andrewfg deleted the hue-fix-light-edge-cases branch August 25, 2024 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An unexpected problem or unintended behavior of an add-on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[hue] Brightness handling is inconsistent for some devices
2 participants