Skip to content

fix(meter-pro): validate display setting input types - #622

Merged
zerzhang merged 4 commits into
sblibs:mainfrom
bvuillemin:fix/meter-pro-settings-input-validation
Nov 19, 2026
Merged

zerzhang merged 4 commits into
sblibs:mainfrom
bvuillemin:fix/meter-pro-settings-input-validation

Conversation

@bvuillemin

@bvuillemin bvuillemin commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #621, addressing the non-blocking points left in its last automated review.

  • Enum setters accept raw ints. set_display_zone, set_zone_icons and set_date_format accept the enum or its integer value. These setters and set_weather share the same enum validation. An invalid type or unknown value raises SwitchbotOperationError.
  • Bool setters are strict. set_battery_icon, set_temperature_unit and set_button_functions reject non-bool values ("false", 0.0, 1…) before anything is sent, so a config value can no longer silently send the opposite setting.
  • Zone icon readback. Unset (0x00) and keep (0x0F) return None. Other unknown icon codes are returned as raw integers for diagnostics.
  • Calibration readback. Keep the low-nibble parsing from feat: add Meter Pro display and calibration settings #621 and validate the tenths digit against 0–9. 0x15 reads as .5. Low-nibble values 0xA–0xF are rejected.
  • Constants. COMMAND_GET_WEATHER/COMMAND_SET_WEATHER are grouped with the other opcodes again.

No wire format changes.

Tests

  • 2,403 tests passed locally. Simulated replies cover the 0x0F keep value and calibration responses with a nonzero upper nibble.
  • Display and calibration readback succeeded on both Meter Pro models through ESPHome.
  • Ruff lint and format checks passed locally.

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

Enum setters now accept the enum or its integer value and raise
SwitchbotOperationError for anything else, bool setters reject
non-bool values, unknown zone icons are kept as raw ints, the
calibration tenths byte is range-checked in full, and the weather
opcodes are grouped again.

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.

@zerzhang

Copy link
Copy Markdown
Collaborator

Thanks for following up on #621. I merged the latest main to resolve the conflict and kept the input validation changes. I made two adjustments to readback:

  • Calibration: I kept the original low-nibble parsing from feat: add Meter Pro display and calibration settings #621 and the 0–9 range check I added there. The protocol defines the tenths digit in bits 3–0, so 0x15 reads as .5. Low-nibble values 0xA–0xF are still rejected.
  • Zone icons: 0x0F means “keep the current icon,” so it reads back as None, which both icon setters accept. Other unknown icon values are still returned as raw integers for diagnostics.

I verified normal display and calibration readback on both Meter Pro models through ESPHome. The two edge cases are covered by regression tests with simulated responses.

@bluetoothbot

Copy link
Copy Markdown
Collaborator

@zerzhang: Thanks for following up on #621. I merged the latest main to resolve the conflict and kept the inp...

Both adjustments check out. Low nibble matches protocol, line 491. 0x0F → None round-trips with setters. Please update PR description too. It still claims whole-byte rejection.

@bluetoothbot

bluetoothbot commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Previous review — superseded by a newer review below.

bluetoothbot
bluetoothbot previously approved these changes Nov 19, 2026

@bluetoothbot bluetoothbot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Tip

No blocking issues found — ready to merge.

@bluetoothbot

Copy link
Copy Markdown
Collaborator

PR Review — fix(meter-pro): validate display setting input types

Merge-ready. The input validation is correct and tested on every setter, and the wire format is unchanged.

What works well:

  • Last review's duplicate-helper finding is fixed: _encode_weather_icon is gone and set_weather now uses _coerce_enum.
  • _coerce_enum rejects bool, so True can't pass as 1. _check_bool stops "false", 0.0 and 1 before anything is sent.
  • Every rejection case in the parametrized test also checks assert_not_called().
  • Zone icon readback is now based on nibbles. Unset (0) and keep (0xF) return None, and unknown codes are returned as raw ints, the same way sources already are. The f1/2f tests cover holding one zone while setting the other.
  • Calibration only masks the low nibble for tenths, so 0x15 reads as .5, and 0xA–0xF are rejected. Both cases are tested.
  • The PR description now matches the code: it describes nibble-based icon handling and the low-nibble calibration check. The weather opcodes are grouped with the other command constants.

Remaining nit (non-blocking):

  • Bool and enum validation still use two different error-raising patterns.

✅ Resolved since last review (2)

Previously-flagged issues verified fixed
  • switchbot/devices/meter_pro.py:137 _encode_weather_icon now duplicates _coerce_enum
  • switchbot/devices/meter_pro.py:708 Bool and enum validation use two different patterns

🟢 Suggestions

1. Bool and enum validation still raise errors in two different ways
switchbot/devices/meter_pro.py:700-704

This point is still open from the last review. Enum inputs go through the module-level _coerce_enum, which raises TypeError/ValueError. Each caller then wraps that in its own try/except and re-raises it as SwitchbotOperationError. There are now four of these blocks: set_display_zone, set_zone_icons, set_date_format and set_weather. Bool inputs go through the instance method _check_bool, which raises SwitchbotOperationError directly.

Both helpers check input at the same boundary and end in the same exception type. A reader still has to learn two patterns, though, and each new enum setter has to copy the try/except.

This doesn't need to block the merge. If you want to unify them, one option is a _coerce_enum method on the device that raises SwitchbotOperationError with self.name. Most of the per-setter try/except (TypeError, ValueError) wrappers would then go away; set_weather would keep its wrapper for the temperature and humidity encoders. That makes it match _check_bool.

def _check_bool(self, value: object, name: str) -> None:
    if not isinstance(value, bool):
        raise SwitchbotOperationError(
            f"{self.name}: {name} must be a boolean, got {value!r}"
        )

Checklist

  • Input validation at boundaries
  • No hardcoded secrets
  • New branches covered by tests
  • No duplicated helpers
  • Consistent validation patterns — suggestion #1
  • Backward-compatible public API
  • PR description matches diff

Automated review by Kōan (Claude) HEAD=14f8271 28s

@bluetoothbot bluetoothbot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Tip

No blocking issues found — ready to merge.

@zerzhang
zerzhang merged commit a0e68a6 into sblibs:main Nov 19, 2026
11 checks passed
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.

3 participants