test_mqtt.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import unittest.mock
  2. import pytest
  3. from paho.mqtt.client import MQTTMessage
  4. import switchbot_mqtt
  5. # pylint: disable=protected-access
  6. @pytest.mark.parametrize("mqtt_host", ["mqtt-broker.local"])
  7. @pytest.mark.parametrize("mqtt_port", [1833])
  8. def test__run(mqtt_host, mqtt_port):
  9. with unittest.mock.patch(
  10. "paho.mqtt.client.Client"
  11. ) as mqtt_client_mock, unittest.mock.patch(
  12. "switchbot_mqtt._mqtt_on_message"
  13. ) as message_handler_mock:
  14. switchbot_mqtt._run(mqtt_host=mqtt_host, mqtt_port=mqtt_port)
  15. mqtt_client_mock.assert_called_once_with()
  16. mqtt_client_mock().connect.assert_called_once_with(host=mqtt_host, port=mqtt_port)
  17. mqtt_client_mock().socket().getpeername.return_value = (mqtt_host, mqtt_port)
  18. mqtt_client_mock().on_connect(mqtt_client_mock(), None, {}, 0)
  19. mqtt_client_mock().subscribe.assert_called_once_with(
  20. "homeassistant/switch/switchbot/+/set"
  21. )
  22. mqtt_client_mock().on_message(mqtt_client_mock(), None, "message")
  23. message_handler_mock.assert_called_once()
  24. mqtt_client_mock().loop_forever.assert_called_once_with()
  25. @pytest.mark.parametrize(
  26. ("topic", "payload", "expected_mac_address", "expected_action"),
  27. [
  28. (
  29. b"homeassistant/switch/switchbot/aa:bb:cc:dd:ee:ff/set",
  30. b"ON",
  31. "aa:bb:cc:dd:ee:ff",
  32. switchbot_mqtt._SwitchbotAction.ON,
  33. ),
  34. (
  35. b"homeassistant/switch/switchbot/aa:bb:cc:dd:ee:ff/set",
  36. b"OFF",
  37. "aa:bb:cc:dd:ee:ff",
  38. switchbot_mqtt._SwitchbotAction.OFF,
  39. ),
  40. (
  41. b"homeassistant/switch/switchbot/aa:bb:cc:dd:ee:ff/set",
  42. b"on",
  43. "aa:bb:cc:dd:ee:ff",
  44. switchbot_mqtt._SwitchbotAction.ON,
  45. ),
  46. (
  47. b"homeassistant/switch/switchbot/aa:bb:cc:dd:ee:ff/set",
  48. b"off",
  49. "aa:bb:cc:dd:ee:ff",
  50. switchbot_mqtt._SwitchbotAction.OFF,
  51. ),
  52. (
  53. b"homeassistant/switch/switchbot/aa:01:23:45:67:89/set",
  54. b"ON",
  55. "aa:01:23:45:67:89",
  56. switchbot_mqtt._SwitchbotAction.ON,
  57. ),
  58. ],
  59. )
  60. def test__mqtt_on_message(
  61. topic: bytes,
  62. payload: bytes,
  63. expected_mac_address: str,
  64. expected_action: switchbot_mqtt._SwitchbotAction,
  65. ):
  66. message = MQTTMessage(topic=topic)
  67. message.payload = payload
  68. with unittest.mock.patch("switchbot_mqtt._send_command") as send_command_mock:
  69. switchbot_mqtt._mqtt_on_message(None, None, message)
  70. send_command_mock.assert_called_once_with(
  71. switchbot_mac_address=expected_mac_address, action=expected_action
  72. )
  73. @pytest.mark.parametrize(
  74. ("topic", "payload"),
  75. [
  76. (b"homeassistant/switch/switchbot/aa:01:23:4E:RR:OR/set", b"ON"),
  77. (b"homeassistant/switch/switchbot/aa:bb:cc:dd:ee:ff", b"on"),
  78. (b"homeassistant/switch/switchbot/aa:bb:cc:dd:ee:ff/change", b"ON"),
  79. (b"homeassistant/switch/switchbot/aa:bb:cc:dd:ee:ff/set", b""),
  80. (b"homeassistant/switch/switchbot/aa:bb:cc:dd:ee:ff/set", b"EIN"),
  81. ],
  82. )
  83. def test__mqtt_on_message_ignored(
  84. topic: bytes, payload: bytes,
  85. ):
  86. message = MQTTMessage(topic=topic)
  87. message.payload = payload
  88. with unittest.mock.patch("switchbot_mqtt._send_command") as send_command_mock:
  89. switchbot_mqtt._mqtt_on_message(None, None, message)
  90. assert not send_command_mock.called
  91. @pytest.mark.parametrize(
  92. ("topic", "payload"),
  93. [(b"homeassistant/switch/switchbot/aa:bb:cc:dd:ee:ff/set", b"ON")],
  94. )
  95. def test__mqtt_on_message_ignored_retained(
  96. topic: bytes, payload: bytes,
  97. ):
  98. message = MQTTMessage(topic=topic)
  99. message.payload = payload
  100. message.retain = True
  101. with unittest.mock.patch("switchbot_mqtt._send_command") as send_command_mock:
  102. switchbot_mqtt._mqtt_on_message(None, None, message)
  103. assert not send_command_mock.called