|
@@ -28,6 +28,72 @@ import switchbot_mqtt
|
|
|
|
|
|
|
|
|
|
|
|
+@pytest.mark.parametrize(
|
|
|
+ "mac_address",
|
|
|
+ ("aa:bb:cc:dd:ee:ff", "aa:bb:cc:dd:ee:gg"),
|
|
|
+)
|
|
|
+@pytest.mark.parametrize(
|
|
|
+ ("position", "expected_payload"), [(0, b"0"), (100, b"100"), (42, b"42")]
|
|
|
+)
|
|
|
+def test__report_position(
|
|
|
+ caplog, mac_address: str, position: int, expected_payload: bytes
|
|
|
+):
|
|
|
+ with unittest.mock.patch(
|
|
|
+ "switchbot.SwitchbotCurtain.__init__", return_value=None
|
|
|
+ ) as device_init_mock, caplog.at_level(logging.DEBUG):
|
|
|
+ actor = switchbot_mqtt._CurtainMotor(
|
|
|
+ mac_address=mac_address, retry_count=7, password=None
|
|
|
+ )
|
|
|
+ device_init_mock.assert_called_once_with(
|
|
|
+ mac=mac_address,
|
|
|
+ retry_count=7,
|
|
|
+ password=None,
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ reverse_mode=True,
|
|
|
+ )
|
|
|
+ with unittest.mock.patch.object(
|
|
|
+ actor, "_mqtt_publish"
|
|
|
+ ) as publish_mock, unittest.mock.patch(
|
|
|
+ "switchbot.SwitchbotCurtain.get_position", return_value=position
|
|
|
+ ):
|
|
|
+ actor._report_position(mqtt_client="dummy")
|
|
|
+ publish_mock.assert_called_once_with(
|
|
|
+ topic_levels=[
|
|
|
+ "homeassistant",
|
|
|
+ "cover",
|
|
|
+ "switchbot-curtain",
|
|
|
+ switchbot_mqtt._MQTTTopicPlaceholder.MAC_ADDRESS,
|
|
|
+ "position",
|
|
|
+ ],
|
|
|
+ payload=expected_payload,
|
|
|
+ mqtt_client="dummy",
|
|
|
+ )
|
|
|
+ assert not caplog.record_tuples
|
|
|
+
|
|
|
+
|
|
|
+@pytest.mark.parametrize("position", ("", 'lambda: print("")'))
|
|
|
+def test__report_position_invalid(caplog, position):
|
|
|
+ with unittest.mock.patch(
|
|
|
+ "switchbot.SwitchbotCurtain.__init__", return_value=None
|
|
|
+ ), caplog.at_level(logging.DEBUG):
|
|
|
+ actor = switchbot_mqtt._CurtainMotor(
|
|
|
+ mac_address="aa:bb:cc:dd:ee:ff", retry_count=3, password=None
|
|
|
+ )
|
|
|
+ with unittest.mock.patch.object(
|
|
|
+ actor, "_mqtt_publish"
|
|
|
+ ) as publish_mock, unittest.mock.patch(
|
|
|
+ "switchbot.SwitchbotCurtain.get_position", return_value=position
|
|
|
+ ), pytest.raises(
|
|
|
+ ValueError
|
|
|
+ ):
|
|
|
+ actor._report_position(mqtt_client="dummy")
|
|
|
+ publish_mock.assert_not_called()
|
|
|
+
|
|
|
+
|
|
|
@pytest.mark.parametrize("mac_address", ["aa:bb:cc:dd:ee:ff", "aa:bb:cc:11:22:33"])
|
|
|
@pytest.mark.parametrize("password", ["pa$$word", None])
|
|
|
@pytest.mark.parametrize("retry_count", (2, 3))
|
|
@@ -70,9 +136,7 @@ def test_execute_command(
|
|
|
mqtt_client="dummy", mqtt_message_payload=message_payload
|
|
|
)
|
|
|
device_init_mock.assert_called_once_with(
|
|
|
- mac=mac_address,
|
|
|
- password=password,
|
|
|
- retry_count=retry_count,
|
|
|
+ mac=mac_address, password=password, retry_count=retry_count, reverse_mode=True
|
|
|
)
|
|
|
action_mock.assert_called_once_with()
|
|
|
if command_successful:
|
|
@@ -125,7 +189,7 @@ def test_execute_command_invalid_payload(
|
|
|
mqtt_client="dummy", mqtt_message_payload=message_payload
|
|
|
)
|
|
|
device_mock.assert_called_once_with(
|
|
|
- mac=mac_address, password=password, retry_count=7
|
|
|
+ mac=mac_address, password=password, retry_count=7, reverse_mode=True
|
|
|
)
|
|
|
assert not device_mock().mock_calls
|
|
|
report_mock.assert_not_called()
|