Refactor some zwave code and add test coverage
Proposed change
Slowly working on improving test coverage. You'll notice that I made some changes to the code of the modules. These changes should be no-op from a user perspective as described below. Note that this should probably be merged before https://github.com/home-assistant/core/pull/92223 and https://github.com/home-assistant/core/pull/90248 so I can rebase those to accommodate the logic changes in the sensor platform.
device_action:
- The if statement logic was wrong so corrected (we want to skip an entity if it doesn't have a state so it is disabled or gone, or it's state is unknown)
- Combined statements
fan:
- Rather than guarding for no target value in each service, we guard during initialization since a fan without a target value is not controllable.
- Removed
speed_countproperty. It is unused inZwaveFanbecausepercentage_stepis hardcoded to 1 (speed_countis used inFanEntityto calculate percentage_step) - Refactored
ValueMappingZwaveFan.percentage_to_zwave_speedto accomplish the same thing while reducing code branches
light:
- fixed turn_on logic for
ZwaveBlackIsOffLightto only set the colors to turn on the light if colors are sent in the service call.
sensor:
- The classes were built for specific use cases but there was a way to combine them into a single base sensor class so we can eliminate the number of different class types
- Removed dupe/redundant logic across classes
- Meter sensor state attributes were unnecessarily guarded
- If endpoint is missing for meter sensor, default to 0
trigger:
- Currently all trigger platforms have
async_validate_trigger_configso there is no need for conditional logic, we can add it back in later if/when needed
Type of change
- [ ] Dependency upgrade
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New integration (thank you!)
- [ ] New feature (which adds functionality to an existing integration)
- [ ] Deprecation (breaking change to happen in the future)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [x] Code quality improvements to existing code or addition of tests
Additional information
- This PR fixes or closes issue: fixes #
- This PR is related to issue:
- Link to documentation pull request:
Checklist
- [ ] The code change is tested and works locally.
- [ ] Local tests pass. Your PR cannot be merged unless tests pass
- [ ] There is no commented out code in this PR.
- [ ] I have followed the development checklist
- [ ] I have followed the perfect PR recommendations
- [ ] The code has been formatted using Black (
black --fast homeassistant tests) - [ ] Tests have been added to verify that the new code works.
If user exposed functionality or configuration variables are added/changed:
- [ ] Documentation added/updated for www.home-assistant.io
If the code communicates with devices, web services, or third-party tools:
- [ ] The manifest file has all fields filled out correctly.
Updated and included derived files by running:python3 -m script.hassfest. - [ ] New or updated dependencies have been added to
requirements_all.txt.
Updated by runningpython3 -m script.gen_requirements_all. - [ ] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
- [ ] Untested files have been added to
.coveragerc.
To help with the load of incoming pull requests:
- [ ] I have reviewed two other open pull requests in this repository.
Hey there @home-assistant/z-wave, mind taking a look at this pull request as it has been labeled with an integration (zwave_js) you are listed as a code owner for? Thanks!
Code owner commands
Code owners of zwave_js can trigger bot actions by commenting:
-
@home-assistant closeCloses the pull request. -
@home-assistant rename Awesome new titleRenames the pull request. -
@home-assistant reopenReopen the pull request. -
@home-assistant unassign zwave_jsRemoves the current integration label and assignees on the pull request, add the integration domain after the command.
even though this fails on patch coverage, this improves coverage in the component.
With this PR:
---------- coverage: platform darwin, python 3.11.3-final-0 ----------
Name Stmts Miss Cover Missing
TOTAL 5081 36 99%
Without this PR:
---------- coverage: platform darwin, python 3.11.3-final-0 ----------
Name Stmts Miss Cover Missing
TOTAL 5135 87 98%
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks :+1:
opened separate PRs