Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SwitchbotRelaySwitch get_basic_info method #283

Merged
merged 4 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions switchbot/devices/relay_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
COMMAND_TURN_ON = f"{COMMAND_HEADER}0f70010100"
COMMAND_TOGGLE = f"{COMMAND_HEADER}0f70010200"
COMMAND_GET_VOLTAGE_AND_CURRENT = f"{COMMAND_HEADER}0f7106000000"
COMMAND_GET_SWITCH_STATE = f"{COMMAND_HEADER}0f7101000000"
PASSIVE_POLL_INTERVAL = 10 * 60


Expand Down Expand Up @@ -87,6 +88,15 @@ async def get_voltage_and_current(self) -> dict[str, Any] | None:
}
return None

async def get_basic_info(self) -> dict[str, Any] | None:
"""Get the current state of the switch."""
result = await self._send_command(COMMAND_GET_SWITCH_STATE)
if self._check_command_result(result, 0, {1}):
return {
"is_on": result[1] & 0x01 != 0,
}
return None

def poll_needed(self, seconds_since_last_poll: float | None) -> bool:
"""Return if device needs polling."""
if self._force_next_update:
Expand Down
16 changes: 15 additions & 1 deletion tests/test_relay_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,22 @@ async def test_turn_on():


@pytest.mark.asyncio
async def test_trun_off():
async def test_turn_off():
relay_switch_device = create_device_for_command_testing()
relay_switch_device._send_command = AsyncMock(return_value=b"\x01")
await relay_switch_device.turn_off()
assert relay_switch_device.is_on() is False


@pytest.mark.asyncio
async def test_get_basic_info():
relay_switch_device = create_device_for_command_testing()
relay_switch_device._send_command = AsyncMock(return_value=b"\x01\x01")
info = await relay_switch_device.get_basic_info()
assert info["is_on"] is True
relay_switch_device._send_command = AsyncMock(return_value=b"\x01\x00")
info = await relay_switch_device.get_basic_info()
assert info["is_on"] is False
relay_switch_device._send_command = AsyncMock(return_value=b"\x00\x00")
info = await relay_switch_device.get_basic_info()
assert info is None
Loading