|
@@ -20,7 +20,11 @@ def test__mqtt_on_message_address(topic, address):
|
|
|
message = MQTTMessage(topic=topic)
|
|
|
message.payload = b"ON"
|
|
|
with unittest.mock.patch("intertechno_cc1101.RemoteControl") as remote_control_mock:
|
|
|
- intertechno_cc1101_mqtt._mqtt_on_message("dummy", {}, message)
|
|
|
+ intertechno_cc1101_mqtt._mqtt_on_message(
|
|
|
+ "dummy",
|
|
|
+ intertechno_cc1101_mqtt._MQTTEventUserData(aliases={}, power_setting=0xC6),
|
|
|
+ message,
|
|
|
+ )
|
|
|
remote_control_mock.assert_called_once_with(address=address)
|
|
|
|
|
|
|
|
@@ -36,7 +40,13 @@ def test__mqtt_on_message_invalid_address(caplog, topic, address_str):
|
|
|
message.payload = b"ON"
|
|
|
with unittest.mock.patch("intertechno_cc1101.RemoteControl") as remote_control_mock:
|
|
|
with caplog.at_level(logging.WARNING):
|
|
|
- intertechno_cc1101_mqtt._mqtt_on_message("dummy", {}, message)
|
|
|
+ intertechno_cc1101_mqtt._mqtt_on_message(
|
|
|
+ "dummy",
|
|
|
+ intertechno_cc1101_mqtt._MQTTEventUserData(
|
|
|
+ aliases={}, power_setting=0xC6
|
|
|
+ ),
|
|
|
+ message,
|
|
|
+ )
|
|
|
remote_control_mock.assert_not_called()
|
|
|
assert caplog.record_tuples == [
|
|
|
(
|
|
@@ -58,22 +68,30 @@ def test__mqtt_on_message_invalid_address(caplog, topic, address_str):
|
|
|
((b"ON", True), (b"On", True), (b"on", True), (b"OFF", False), (b"off", False)),
|
|
|
)
|
|
|
@pytest.mark.parametrize("retain", [True, False])
|
|
|
+@pytest.mark.parametrize("power_setting", [0xC6, 0xC0])
|
|
|
def test__mqtt_on_message_button_index_action(
|
|
|
- topic, button_index, payload, turn_on, retain
|
|
|
+ topic, button_index, payload, turn_on, retain, power_setting
|
|
|
):
|
|
|
+
|
|
|
message = MQTTMessage(topic=topic)
|
|
|
message.payload = payload
|
|
|
message.retain = retain
|
|
|
with unittest.mock.patch("intertechno_cc1101.RemoteControl") as remote_control_mock:
|
|
|
- intertechno_cc1101_mqtt._mqtt_on_message("dummy", {}, message)
|
|
|
+ intertechno_cc1101_mqtt._mqtt_on_message(
|
|
|
+ "dummy",
|
|
|
+ intertechno_cc1101_mqtt._MQTTEventUserData(
|
|
|
+ aliases={}, power_setting=power_setting
|
|
|
+ ),
|
|
|
+ message,
|
|
|
+ )
|
|
|
if turn_on:
|
|
|
remote_control_mock().turn_on.assert_called_once_with(
|
|
|
- button_index=button_index, power_setting=0xC6
|
|
|
+ button_index=button_index, power_setting=power_setting
|
|
|
)
|
|
|
remote_control_mock().turn_off.assert_not_called()
|
|
|
else:
|
|
|
remote_control_mock().turn_off.assert_called_once_with(
|
|
|
- button_index=button_index, power_setting=0xC6
|
|
|
+ button_index=button_index, power_setting=power_setting
|
|
|
)
|
|
|
remote_control_mock().turn_on.assert_not_called()
|
|
|
|
|
@@ -90,7 +108,13 @@ def test__mqtt_on_message_invalid_button_index(caplog, topic, button_index_str):
|
|
|
message.payload = b"ON"
|
|
|
with unittest.mock.patch("intertechno_cc1101.RemoteControl") as remote_control_mock:
|
|
|
with caplog.at_level(logging.WARNING):
|
|
|
- intertechno_cc1101_mqtt._mqtt_on_message("dummy", {}, message)
|
|
|
+ intertechno_cc1101_mqtt._mqtt_on_message(
|
|
|
+ "dummy",
|
|
|
+ intertechno_cc1101_mqtt._MQTTEventUserData(
|
|
|
+ aliases={}, power_setting=0xC6
|
|
|
+ ),
|
|
|
+ message,
|
|
|
+ )
|
|
|
remote_control_mock().turn_on.assert_not_called()
|
|
|
remote_control_mock().turn_off.assert_not_called()
|
|
|
assert caplog.record_tuples == [
|
|
@@ -117,10 +141,13 @@ def test__mqtt_on_message_alias(topic, address, button_index):
|
|
|
with unittest.mock.patch("intertechno_cc1101.RemoteControl") as remote_control_mock:
|
|
|
intertechno_cc1101_mqtt._mqtt_on_message(
|
|
|
"dummy",
|
|
|
- {
|
|
|
- "some-name": {"address": 21, "button-index": 0},
|
|
|
- "another-name": {"address": "42", "button-index": "7"},
|
|
|
- },
|
|
|
+ intertechno_cc1101_mqtt._MQTTEventUserData(
|
|
|
+ aliases={
|
|
|
+ "some-name": {"address": 21, "button-index": 0},
|
|
|
+ "another-name": {"address": "42", "button-index": "7"},
|
|
|
+ },
|
|
|
+ power_setting=0xC6,
|
|
|
+ ),
|
|
|
message,
|
|
|
)
|
|
|
remote_control_mock.assert_called_once_with(address=address)
|
|
@@ -142,7 +169,12 @@ def test__mqtt_on_message_undefined_alias(caplog, topic, alias):
|
|
|
with unittest.mock.patch("intertechno_cc1101.RemoteControl") as remote_control_mock:
|
|
|
with caplog.at_level(logging.WARNING):
|
|
|
intertechno_cc1101_mqtt._mqtt_on_message(
|
|
|
- "dummy", {"some-name": {"address": 21, "button-index": 0}}, message
|
|
|
+ "dummy",
|
|
|
+ intertechno_cc1101_mqtt._MQTTEventUserData(
|
|
|
+ aliases={"some-name": {"address": 21, "button-index": 0}},
|
|
|
+ power_setting=0xC6,
|
|
|
+ ),
|
|
|
+ message,
|
|
|
)
|
|
|
remote_control_mock.assert_not_called()
|
|
|
assert caplog.record_tuples == [
|
|
@@ -167,7 +199,13 @@ def test__mqtt_on_message_alias_missing_attrs(caplog, aliases):
|
|
|
message.payload = b"ON"
|
|
|
with unittest.mock.patch("intertechno_cc1101.RemoteControl") as remote_control_mock:
|
|
|
with caplog.at_level(logging.WARNING):
|
|
|
- intertechno_cc1101_mqtt._mqtt_on_message("dummy", aliases, message)
|
|
|
+ intertechno_cc1101_mqtt._mqtt_on_message(
|
|
|
+ "dummy",
|
|
|
+ intertechno_cc1101_mqtt._MQTTEventUserData(
|
|
|
+ aliases=aliases, power_setting=0xC6
|
|
|
+ ),
|
|
|
+ message,
|
|
|
+ )
|
|
|
remote_control_mock.assert_not_called()
|
|
|
assert caplog.record_tuples == [
|
|
|
(
|
|
@@ -185,7 +223,11 @@ def test__mqtt_on_message_remote_init_failed(caplog, topic):
|
|
|
message = MQTTMessage(topic=topic)
|
|
|
message.payload = b"ON"
|
|
|
with caplog.at_level(logging.WARNING):
|
|
|
- intertechno_cc1101_mqtt._mqtt_on_message("dummy", {}, message)
|
|
|
+ intertechno_cc1101_mqtt._mqtt_on_message(
|
|
|
+ "dummy",
|
|
|
+ intertechno_cc1101_mqtt._MQTTEventUserData(aliases={}, power_setting=0xC6),
|
|
|
+ message,
|
|
|
+ )
|
|
|
assert len(caplog.records) == 1
|
|
|
assert caplog.records[0].levelno == logging.WARNING
|
|
|
assert (
|
|
@@ -202,7 +244,11 @@ def test__mqtt_on_message_transmission_failed(caplog):
|
|
|
"cc1101.CC1101.__enter__",
|
|
|
side_effect=FileNotFoundError("[Errno 2] No such file or directory"),
|
|
|
), caplog.at_level(logging.ERROR):
|
|
|
- intertechno_cc1101_mqtt._mqtt_on_message("dummy", {}, message)
|
|
|
+ intertechno_cc1101_mqtt._mqtt_on_message(
|
|
|
+ "dummy",
|
|
|
+ intertechno_cc1101_mqtt._MQTTEventUserData(aliases={}, power_setting=0xC6),
|
|
|
+ message,
|
|
|
+ )
|
|
|
assert len(caplog.records) == 1
|
|
|
assert caplog.records[0].levelno == logging.ERROR
|
|
|
assert caplog.records[0].message == "failed to send signal"
|
|
@@ -214,7 +260,11 @@ def test__mqtt_on_message_invalid_payload(caplog, payload):
|
|
|
message = MQTTMessage(topic=b"intertechno-cc1101/1234/7/set")
|
|
|
message.payload = payload
|
|
|
with unittest.mock.patch("intertechno_cc1101.RemoteControl") as remote_control_mock:
|
|
|
- intertechno_cc1101_mqtt._mqtt_on_message("dummy", {}, message)
|
|
|
+ intertechno_cc1101_mqtt._mqtt_on_message(
|
|
|
+ "dummy",
|
|
|
+ intertechno_cc1101_mqtt._MQTTEventUserData(aliases={}, power_setting=0xC6),
|
|
|
+ message,
|
|
|
+ )
|
|
|
remote_control_mock().turn_off.assert_not_called()
|
|
|
remote_control_mock().turn_on.assert_not_called()
|
|
|
assert caplog.record_tuples == [
|