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

Update Characteristic value when setProps called #902

Merged
merged 8 commits into from
Oct 14, 2022

Conversation

Shaquu
Copy link
Contributor

@Shaquu Shaquu commented Aug 11, 2021

Update Characteristic value when setProps called

♻️ Current situation

This PR is mainly about number values.
Currently, Characteristic default value is based upon minValue.
Once setProps is called and minValue is changed, then also value should be changed accordingly.

💡 Proposed solution

public setProps(props: Partial<CharacteristicProps>): Characteristic {
  //...

  if (this.props.format) {
    switch (this.props.format) {
      case Formats.BOOL:
      case Formats.STRING:
      case Formats.DATA:
      case Formats.TLV8:
      case Formats.DICTIONARY:
      case Formats.ARRAY:
        break;
      default:
        if (this.value) {
          const minValue = this.props.minValue ?? 0
          const maxValue = this.props.maxValue ?? minValue
  
          if (this.value < minValue) {
            this.updateValue(minValue)
          } else if (this.value > maxValue) {
            this.updateValue(maxValue)
          }
        }
    }
  }	 

  return this;
}

Problem that is solved

This PR will clamp Characteristic value, so there is no situation when allowed value range is 10-20 and value is 0.

Implications

updateValue is called, which means some actions might be triggered

Testing

Tested locally before and after with success.

Reviewer Nudging

Right at the top :)

Copy link
Member

@Supereg Supereg 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 PR. Great you caught that one 🚀

My comments are mainly to direct you to reuse some of the already existent logic covering cases like that. Additionally, solely checking for the minValue and maxValue properties might not be enough to fully address the issue.

src/lib/Characteristic.ts Outdated Show resolved Hide resolved
src/lib/Characteristic.ts Outdated Show resolved Hide resolved
@Shaquu
Copy link
Contributor Author

Shaquu commented Aug 12, 2021

@Supereg I will have a look at this soon, thanks for comments!

Shaquu added a commit to Shaquu/HAP-NodeJS that referenced this pull request Oct 5, 2021
@Shaquu Shaquu changed the base branch from beta to beta-0.10.0 October 5, 2021 00:14
@Shaquu
Copy link
Contributor Author

Shaquu commented Oct 7, 2021

Can you have a look @Supereg?
Also looks like there are some issues with tests on beta branch.

Copy link
Member

@Supereg Supereg left a comment

Choose a reason for hiding this comment

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

Hey sorry for the delay, I wasn't sure if a follow-up review was already requested, and then kinda forgot about this 😕

Thanks for incorporating my feedback. I had some concerns on how setProps is used internally, as it is partly called in yet uninitialized state. Happy to year your points in that discussion :)

In regard to the tests, quickly scanned over it. Seems like, due to the added validateUserInput call (probably also because it is called through the constructor) the expected test results do not align anymore. Specifically the expectations on how many calls were made to characteristicWarning are now a bit off. Would be great if you could investigate those cases, and adjust the values if it makes sense.

src/lib/Characteristic.ts Outdated Show resolved Hide resolved
src/lib/Characteristic.ts Outdated Show resolved Hide resolved
src/lib/Characteristic.ts Outdated Show resolved Hide resolved
@Shaquu Shaquu marked this pull request as draft December 28, 2021 01:47
@Supereg Supereg changed the base branch from beta-0.10.0 to master January 21, 2022 15:53
@coveralls
Copy link

coveralls commented Sep 16, 2022

Pull Request Test Coverage Report for Build 3247995066

  • 9 of 10 (90.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.06%) to 51.788%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/lib/Characteristic.ts 9 10 90.0%
Totals Coverage Status
Change from base Build 3095726760: 0.06%
Covered Lines: 5724
Relevant Lines: 10098

💛 - Coveralls

@Shaquu Shaquu marked this pull request as ready for review September 16, 2022 23:29
@Shaquu
Copy link
Contributor Author

Shaquu commented Sep 19, 2022

@Supereg can I ask for hints or comments?

@Shaquu
Copy link
Contributor Author

Shaquu commented Sep 25, 2022

@Supereg bump

@Shaquu Shaquu requested a review from Supereg October 1, 2022 19:38
@Supereg Supereg changed the base branch from master to beta-0.11.0 October 14, 2022 06:53
Copy link
Member

@Supereg Supereg left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for the contribution 🚀

@Supereg Supereg enabled auto-merge (squash) October 14, 2022 06:55
@Supereg Supereg merged commit 15d1359 into homebridge:beta-0.11.0 Oct 14, 2022
@ebaauw
Copy link
Contributor

ebaauw commented Oct 14, 2022

Did you guys check this change for Programmable Switch Event and other eventOnly characteristics, if there are any? I fear this might cause a ghost button action.

I now get the “characteristic was supplied an illegal value” on restart, when having changed the supported button actions, and the last button action before the restart is no longer supported. I thought about changing the characteristic value when changing the properties, but don’t know how to do that without generating a ghost button action.

@Supereg
Copy link
Member

Supereg commented Oct 14, 2022

My intention was to add some test cases in a follow up PR. Thanks for the input 👍

I now get the “characteristic was supplied an illegal value” on restart, when having changed the supported button actions, and the last button action before the restart is no longer supported.

You are justing calling setProps on a ProgrammabelSwitchEvent characteristic? Or am I misunderstanding something?

@ebaauw
Copy link
Contributor

ebaauw commented Oct 14, 2022

Yes. Based on the config.json settings, a button might support Double Press and/or Long Press in addition to Single Press. E.g. the doublePressTimeout and longPressTimeout settings in Homebridge RPi, or the hueDimmerRepeat setting in Homebridge Hue.

On restart, Homebridge restores the Stateless Programmable Switch from cache, including the last button action as the value of Programmable Switch Event. My plugin sets the characteristic properties based on the changed config.json settings. When the last button action from before the change is now no longer supported, Homebridge complains that the (restored) value is out of bound.

I think for eventOnly characteristics, there should be no check on setProps(), only on setValue(), updateValue(), and equivalents. HomeKit won't ever read the characteristic, so it won't see the out of bound value anyways.

@Supereg Supereg added the fix label Nov 9, 2022
Supereg added a commit that referenced this pull request Nov 18, 2022
Co-authored-by: Andi <mail@anderl-bauer.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants