Skip to content

refactor(meter-pro): share input validation between setters - #624

Open
bvuillemin wants to merge 1 commit into
sblibs:mainfrom
bvuillemin:meter-pro-shared-input-validation
Open

bvuillemin wants to merge 1 commit into
sblibs:mainfrom
bvuillemin:meter-pro-shared-input-validation

Conversation

@bvuillemin

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #622, addressing the last non-blocking point of its automated review: bool and enum inputs were validated in two different ways.

  • Both kinds of input now go through module-level helpers that raise TypeError/ValueError: the existing _coerce_enum and a new _require_bool.
  • A single _invalid_input() context manager on SwitchbotMeterPro turns those errors into SwitchbotOperationError, with the original error as __cause__.
  • It replaces _check_bool and the repeated try/except (TypeError, ValueError) blocks in set_display_zone, set_zone_icons, set_date_format, set_battery_icon, set_temperature_unit, set_button_functions, set_calibration, set_weather, and the comfort and temperature/humidity alarm setters.

No behaviour or wire format changes. The only visible difference is that the comfort setter's custom error message now includes the rejected value, like the other bool checks.

Tests

  • test_setters_reject_wrong_types also checks that the error keeps the original TypeError/ValueError as its cause.
  • pytest: 2403 passed; switchbot/devices/meter_pro.py stays at 100 % coverage.
  • ruff check, ruff format --check and codespell are clean; mypy reports no new errors.

I prepared this with an AI coding assistant; I'm the person responsible for this PR and will follow up on review.

🤖 Generated with Claude Code

Bool and enum checks now both raise TypeError/ValueError from module
helpers, and a single _invalid_input() context manager turns them into
SwitchbotOperationError. It replaces _check_bool and the repeated
try/except blocks in the display, weather, calibration and alarm setters.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
switchbot/devices/meter_pro.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant