Skip to content

add roller mode for switch2pm#457

Open
fankai777 wants to merge 10 commits intosblibs:masterfrom
fankai777:feat/2pm
Open

add roller mode for switch2pm#457
fankai777 wants to merge 10 commits intosblibs:masterfrom
fankai777:feat/2pm

Conversation

@fankai777
Copy link
Contributor

Summary

Add roller/cover mode support for SwitchbotRelaySwitch2PM and fix related test cases.

Changes

Features

  • SwitchbotRelaySwitch2PM now inherits from SwitchbotBaseCover, enabling cover/roller control support.
  • Added open(), close(), stop(), set_position() commands for roller mode.
  • Added position and mode properties to SwitchbotRelaySwitch2PM.
  • Added roller mode BLE commands: COMMAND_OPEN, COMMAND_CLOSE, COMMAND_POSITION, COMMAND_STOP.

Parser

  • process_relay_switch_2pm: Added mode, position, calibration fields to both channel 1 and channel 2 advertisement data.

Refactor

  • SwitchbotBaseCover.__init__: Changed reverse to a keyword-only argument, updated all subclass call sites (curtain.py, blind_tilt.py, roller_shade.py).
  • Fixed _parse_common_data in SwitchbotRelaySwitch: sequence_number now reads from raw_data[0] (was raw_data[1]).

Tests

  • Updated test_adv_parser.py: Added expected mode, position, calibration fields to SwitchbotRelaySwitch2PM test cases.
  • Updated test_base_cover.py: Fixed SwitchbotBaseCover instantiation to use new keyword argument signature.

@codecov
Copy link

codecov bot commented Mar 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
switchbot/adv_parsers/relay_switch.py 100.00% <ø> (ø)
switchbot/devices/base_cover.py 100.00% <100.00%> (ø)
switchbot/devices/blind_tilt.py 98.64% <100.00%> (ø)
switchbot/devices/curtain.py 98.66% <100.00%> (-0.02%) ⬇️
switchbot/devices/relay_switch.py 97.91% <100.00%> (-2.09%) ⬇️
switchbot/devices/roller_shade.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

fankai777 and others added 5 commits March 5, 2026 17:50
Small position values (< 16) produced odd-length hex strings causing
fromhex() ValueError. Format position as 02X to ensure even hex digits.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
… mode

- Format position as 2-digit hex (02X) to prevent odd-length hex string
  causing fromhex() ValueError on small position values
- Add _update_motion_direction override with HA semantics (new > prev = opening)
- Convert device positions (0=open, 100=closed) to HA positions before
  direction comparison in set_position
- Fix test expectations to match device/HA semantic mapping

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…h2PM

The method was defined twice; the first definition was dead code that
caused 8 lines of missing coverage reported by codecov.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds roller/cover mode support for SwitchbotRelaySwitch2PM by having it additionally inherit from SwitchbotBaseCover. It also refactors the SwitchbotBaseCover constructor to accept reverse as a keyword argument, updates the advertisement parser for 2PM to include mode, position, and calibration fields, and updates tests accordingly.

Changes:

  • SwitchbotRelaySwitch2PM now inherits from both SwitchbotRelaySwitch and SwitchbotBaseCover, gaining cover/roller commands (open, close, stop, set_position) and properties (position, mode).
  • SwitchbotBaseCover.__init__ changed reverse from a positional to a keyword argument; all subclass call sites updated.
  • Advertisement parser and _parse_common_data extended with mode, position, calibration fields for 2PM devices.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
switchbot/devices/base_cover.py Refactored constructor to accept reverse as keyword-only via kwargs.pop
switchbot/devices/relay_switch.py Added cover inheritance, roller commands, position/mode properties, motion direction logic, and refactored 2PM get_basic_info
switchbot/adv_parsers/relay_switch.py Added mode, position, calibration fields to 2PM advertisement parser
switchbot/devices/curtain.py Updated super().__init__ call for new keyword arg signature
switchbot/devices/blind_tilt.py Updated super().__init__ call for new keyword arg signature
switchbot/devices/roller_shade.py Updated super().__init__ call for new keyword arg signature
tests/test_relay_switch.py Added tests for cover operations, position, mode, and motion direction
tests/test_base_cover.py Updated SwitchbotBaseCover instantiation to match new keyword arg signature
tests/test_adv_parser.py Added expected mode, position, calibration fields to 2PM test cases

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

"sequence_number": mfr_data[6],
"isOn": bool(mfr_data[7] & 0b01000000),
"power": parse_power_data(mfr_data, 12),
"mode": mfr_data[9] & 0b11110000 >> 4,


class SwitchbotRelaySwitch2PM(SwitchbotRelaySwitch):
class SwitchbotRelaySwitch2PM(SwitchbotRelaySwitch, SwitchbotBaseCover):
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.

2 participants