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

Prevent device value less than 1% showing as 100% in HomeKit #674

Conversation

jtheller
Copy link

Update `transformValueFromMqtt` to return minimum at 1 percent value.
@sonarcloud
Copy link

sonarcloud bot commented Apr 28, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

71.4% 71.4% Coverage
0.0% 0.0% Duplication

@github-actions
Copy link
Contributor

🆗 Published SonarCloud and code coverage data.

@codecov
Copy link

codecov bot commented Apr 28, 2023

Codecov Report

Attention: Patch coverage is 75.00000% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 52.88%. Comparing base (24efcc7) to head (9ef66d3).
Report is 124 commits behind head on master.

Files Patch % Lines
src/converters/monitor.ts 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #674      +/-   ##
==========================================
+ Coverage   52.86%   52.88%   +0.01%     
==========================================
  Files          40       40              
  Lines        2457     2460       +3     
  Branches      616      617       +1     
==========================================
+ Hits         1299     1301       +2     
- Misses        618      619       +1     
  Partials      540      540              
Flag Coverage Δ
tests 52.88% <75.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@itavero
Copy link
Owner

itavero commented Apr 28, 2023

When I have time, I'll see if I can also come up with an automated test that verifies this fix.

return out_minimum + percentage * (out_maximum - out_minimum);
const result = out_minimum + percentage * (out_maximum - out_minimum);
if (result < 1 && result > 0) {
return Math.ceil(result);
Copy link
Owner

Choose a reason for hiding this comment

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

Shouldn't we simply just return 1 here, as we already know the outcome of Math.ceil in this case because of the if condition.

Suggested change
return Math.ceil(result);
return 1;

Copy link
Owner

Choose a reason for hiding this comment

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

Oh sorry, I didn't check properly. This shouldn't be a fixed value. I think we definitely need a test to figure this one out.

I would expect the condition to some how relate to the out_minimum boundary (and perhaps we need the same for the out_maximum).

Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't be a fixed value.

Your initial thought was correct, returning a fixed value of 1 probably is the right approach here. 😉
Otherwise, a Zigbee brightness level of 1 (out of 254) would result in about 0.39%, which HomeKit subsequently rounds down to 0%, an implied invalid value when a light is still powered on.

@burmistrzak
Copy link
Contributor

@jtheller Having the same problem #673 and can confirm that the mapping/translation of brightness ranges is definitely not ideal in its current state. As I also mentioned in my mail to @itavero, the official Philips Hue bridge has the exact same issue.

According to the HAP specification, Brightness has a default minimum step of 1, meaning that any float values (e.g. 1.3) are automatically rounded. However, anything below 1 isn't really a valid value, because 0 means power off. So a bulb can't simultaneously be On and at 0% brightness.

NumericCharacteristicMonitor is a generic monitor, i.e. it does not exclusively translate brightness levels, so we have to be careful to only return 1 when the characteristic being translated is Brightness. Otherwise we would probably break all sorts of other characteristics, e.g. battery percentage.

@@ -145,6 +145,10 @@ export class NumericCharacteristicMonitor extends BaseCharacteristicMonitor {
}
const percentage = (input - this.input_min) / (this.input_max - this.input_min);

return out_minimum + percentage * (out_maximum - out_minimum);
const result = out_minimum + percentage * (out_maximum - out_minimum);
if (result < 1 && result > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (result < 1 && result > 0) {
if ((result < 1 && result > 0) && (actualCharacteristic.UUID === '00000008-0000-1000-8000-0026BB765291')) {

Checking the UUID of the actual characteristic we're dealing with, should prevent us from accidentally breaking other numeric characteristics like, e.g. TargetPosition, and thus would only treat Brightness differently.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can confirm this works quite well, albeit looking not very pretty. 😅

All characteristics, except Brightness, are unaffected by this change.
The actual implementation still needs a bit of work, but it's a good starting point.

Copy link

stale bot commented Apr 22, 2024

It appears this PR hasn't had any activity for a long time. Please check if it is still applicable and if it can be merged without any issues. If there isn't any activity in the next three weeks, this PR will be closed automatically.

@itavero
Copy link
Owner

itavero commented May 21, 2024

Replaced by #863

@itavero itavero closed this May 21, 2024
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.

None yet

4 participants