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

Changing z-wave brightness calculation to respect 0x01 and 0x02 byte values #16420

Merged
merged 4 commits into from
Sep 13, 2018

Conversation

Harvtronix
Copy link
Contributor

@Harvtronix Harvtronix commented Sep 4, 2018

Description:

Changing z-wave brightness calculation to respect 0x01 and 0x02 byte values

Related issue (if applicable): fixes #16406

Pull request in home-assistant.github.io with documentation (if applicable): n/a

Example entry for configuration.yaml (if applicable): n/a

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New or updated dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

@Harvtronix Harvtronix requested a review from a team as a code owner September 4, 2018 16:35
@homeassistant
Copy link
Contributor

Hi @Harvtronix,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@ghost ghost added the in progress label Sep 4, 2018
@@ -62,6 +62,15 @@ def brightness_state(value):
return round((value.data / 99) * 255), STATE_ON
return 0, STATE_OFF

def byte_to_zwave_brightness(value):

Choose a reason for hiding this comment

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

expected 2 blank lines, found 1

"""
if value > 0:
return max(1, int((value / 255) * 99))
return 0
Copy link
Member

Choose a reason for hiding this comment

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

If the value is 0, shouldn't we just return None (there is no brightness) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@balloob The brightness value as it was used in the original code was an integer in the range of 0 to 99, with 255 representing a special case. I wanted to stay as close to this behavior as possible to minimize the risk of regressing existing code.
I'm open to whatever changes you'd like. I was just going for "minimally invasive" :)

Copy link
Contributor

Choose a reason for hiding this comment

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

We should keep it as returning zero, since this is the brightness that we send to the ZWave device, not the brightness value for hass.

Copy link
Contributor

@emlove emlove left a comment

Choose a reason for hiding this comment

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

I'm 👍 with this change overall. Great catch, and thanks for adding tests!

You'll also need to fix the picky lint issues Travis called out:
https://travis-ci.org/home-assistant/home-assistant/jobs/424420886#L561-L562

"""
if value > 0:
return max(1, int((value / 255) * 99))
return 0
Copy link
Contributor

Choose a reason for hiding this comment

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

We should keep it as returning zero, since this is the brightness that we send to the ZWave device, not the brightness value for hass.

@Harvtronix
Copy link
Contributor Author

@armills Thanks! And thanks for pointing that out! Looks like some comment formatting issues. I'll search for a multiline comment and model after that. Strangely, the linter that was running locally after I imported the project into vscode wasn't reporting these same issues.

- Add additional unit test to increase code coverage
@ghost ghost assigned balloob Sep 13, 2018
device.turn_on(**{ATTR_BRIGHTNESS: 0})

assert device.is_on
assert device.brightness == 0
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@armills I thought about the second part of this test here that I added some more, and I'd like your opinion as to whether or not the result makes sense. Right now, calling turn on with a brightness val of 0, will result in device.is_on returning true... Is that the intended behavior? If not, I can add an else block to the turn_on method that sets the state to off in the case of 0 being passed in.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch. That really should be cleaned up.

If you don't mind we could also refactor it to make it a little simpler. self._state can just be a boolean. STATE_ON can be replaced with True, STATE_OFF can be replaced with False, and we can return it directly here: https://github.com/home-assistant/home-assistant/blob/a40a6e31ada250d48241698ed133a847f45508a0/homeassistant/components/light/zwave.py#L154 . Then in the turn_on method you can just assign self._state = brightness > 0.

@emlove emlove merged commit d076251 into home-assistant:dev Sep 13, 2018
@ghost ghost removed the in progress label Sep 13, 2018
@emlove
Copy link
Contributor

emlove commented Sep 13, 2018

I've merged this PR since the state problem existed already, and this is a solid bug fix. If you can help clean up that state problem you can open a new PR for that fix.

Thanks for the contribution! 🌟

@Harvtronix
Copy link
Contributor Author

@armills Sure thing! I should hopefully be able to work on that over the next couple of days.

@balloob balloob mentioned this pull request Sep 28, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Feb 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1% Z-Wave Light Brightness Not Respected
5 participants