fix: normalize range validation for light, cover, and valve domains#84
Merged
Conversation
) Introduce a shared validate_range() helper in domains/_utils.py and apply it consistently to the four action methods that previously had inconsistent or missing input validation: - light.set_brightness: now validates 0-255 (was int() coerce only) - light.set_rgb: now validates each component 0-255 (was no validation) - cover.set_position: now validates 0-100 (docstring said clamped but only coerced with int()) - valve.set_position: now validates 0-100 (was no range check at all); validation fires before the feature-support guard so callers receive an immediate ValueError for clearly invalid input even on binary valves fan.set_percentage, humidifier.set_humidity, and media_player.set_volume already validated correctly and are unchanged. Tests added: below-range, above-range, and boundary values for all four fixed methods (4 new test functions, 311 tests total pass).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #75
Issue validity
Valid and actionable. Four public action methods had inconsistent or missing numeric range validation, making the client's behaviour unpredictable and pushing input checking onto callers.
light.set_brightnessint()coerce only, no range checkValueErroroutside 0–255light.set_rgbValueErrorfor any component outside 0–255cover.set_positionint()coerce only (docstring said "clamped")ValueErroroutside 0–100valve.set_positionValueErroroutside 0–100; validation runs before feature-support guardfan.set_percentage,humidifier.set_humidity, andmedia_player.set_volumealready validated correctly and are unchanged.Fix
Added
src/haclient/domains/_utils.pywith a single shared helper:Coerces to
int, checks the closed interval, and raisesValueErrorwith a consistent message ("{name} must be between {lo} and {hi}, got {value}").Applied to
light.set_brightness,light.set_rgb(all three components),cover.set_position, andvalve.set_position.Docstrings updated with explicit
Raisessections where missing.Tests
Four new test functions added to
tests/test_domains.py:test_light_set_brightness_validation— below-range, above-range, boundary (0, 255)test_light_set_rgb_validation— below/above range for each of r, g, b; boundaries (0,0,0) and (255,255,255)test_cover_set_position_validation— below-range, above-range, boundary (0, 100)test_valve_set_position_validation— range validation fires even when feature is absent; boundaries dispatched when feature is presentChecks run
pytest: 311 passed, coverage 97.17% (threshold 95%)ruff check+ruff format --check: all passedmypy src: no issues