test_switchbot_button_automator.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # switchbot-mqtt - MQTT client controlling SwitchBot button & curtain automators,
  2. # compatible with home-assistant.io's MQTT Switch & Cover platform
  3. #
  4. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. import logging
  19. import unittest.mock
  20. import bluepy.btle
  21. import pytest
  22. import switchbot_mqtt
  23. # pylint: disable=protected-access
  24. # pylint: disable=too-many-arguments; these are tests, no API
  25. @pytest.mark.parametrize("mac_address", ["aa:bb:cc:dd:ee:ff", "aa:bb:cc:11:22:33"])
  26. @pytest.mark.parametrize("password", (None, "secret"))
  27. @pytest.mark.parametrize("retry_count", (3, 21))
  28. @pytest.mark.parametrize(
  29. ("message_payload", "action_name"),
  30. [
  31. (b"on", "switchbot.Switchbot.turn_on"),
  32. (b"ON", "switchbot.Switchbot.turn_on"),
  33. (b"On", "switchbot.Switchbot.turn_on"),
  34. (b"off", "switchbot.Switchbot.turn_off"),
  35. (b"OFF", "switchbot.Switchbot.turn_off"),
  36. (b"Off", "switchbot.Switchbot.turn_off"),
  37. ],
  38. )
  39. @pytest.mark.parametrize("command_successful", [True, False])
  40. def test_execute_command(
  41. caplog,
  42. mac_address,
  43. password,
  44. retry_count,
  45. message_payload,
  46. action_name,
  47. command_successful,
  48. ):
  49. with unittest.mock.patch(
  50. "switchbot.Switchbot.__init__", return_value=None
  51. ) as device_init_mock, caplog.at_level(logging.INFO):
  52. actor = switchbot_mqtt._ButtonAutomator(
  53. mac_address=mac_address, retry_count=retry_count, password=password
  54. )
  55. with unittest.mock.patch.object(
  56. actor, "report_state"
  57. ) as report_mock, unittest.mock.patch(
  58. action_name, return_value=command_successful
  59. ) as action_mock:
  60. actor.execute_command(
  61. mqtt_client="dummy",
  62. mqtt_message_payload=message_payload,
  63. update_device_info=True,
  64. )
  65. device_init_mock.assert_called_once_with(
  66. mac=mac_address, password=password, retry_count=retry_count
  67. )
  68. action_mock.assert_called_once_with()
  69. if command_successful:
  70. assert caplog.record_tuples == [
  71. (
  72. "switchbot_mqtt",
  73. logging.INFO,
  74. f"switchbot {mac_address} turned {message_payload.decode().lower()}",
  75. )
  76. ]
  77. report_mock.assert_called_once_with(
  78. mqtt_client="dummy", state=message_payload.upper()
  79. )
  80. else:
  81. assert caplog.record_tuples == [
  82. (
  83. "switchbot_mqtt",
  84. logging.ERROR,
  85. f"failed to turn {message_payload.decode().lower()} switchbot {mac_address}",
  86. )
  87. ]
  88. report_mock.assert_not_called()
  89. @pytest.mark.parametrize("mac_address", ["aa:bb:cc:dd:ee:ff"])
  90. @pytest.mark.parametrize("message_payload", [b"EIN", b""])
  91. def test_execute_command_invalid_payload(caplog, mac_address, message_payload):
  92. with unittest.mock.patch("switchbot.Switchbot") as device_mock, caplog.at_level(
  93. logging.INFO
  94. ):
  95. actor = switchbot_mqtt._ButtonAutomator(
  96. mac_address=mac_address, retry_count=21, password=None
  97. )
  98. with unittest.mock.patch.object(actor, "report_state") as report_mock:
  99. actor.execute_command(
  100. mqtt_client="dummy",
  101. mqtt_message_payload=message_payload,
  102. update_device_info=True,
  103. )
  104. device_mock.assert_called_once_with(mac=mac_address, retry_count=21, password=None)
  105. assert not device_mock().mock_calls # no methods called
  106. report_mock.assert_not_called()
  107. assert caplog.record_tuples == [
  108. (
  109. "switchbot_mqtt",
  110. logging.WARNING,
  111. f"unexpected payload {message_payload!r} (expected 'ON' or 'OFF')",
  112. )
  113. ]
  114. @pytest.mark.parametrize("mac_address", ["aa:bb:cc:dd:ee:ff"])
  115. @pytest.mark.parametrize("message_payload", [b"ON", b"OFF"])
  116. def test_execute_command_bluetooth_error(caplog, mac_address, message_payload):
  117. """
  118. paho.mqtt.python>=1.5.1 no longer implicitly suppresses exceptions in callbacks.
  119. verify pySwitchbot catches exceptions raised in bluetooth stack.
  120. https://github.com/Danielhiversen/pySwitchbot/blob/0.8.0/switchbot/__init__.py#L48
  121. https://github.com/Danielhiversen/pySwitchbot/blob/0.8.0/switchbot/__init__.py#L94
  122. """
  123. with unittest.mock.patch(
  124. "bluepy.btle.Peripheral",
  125. side_effect=bluepy.btle.BTLEDisconnectError(
  126. f"Failed to connect to peripheral {mac_address}, addr type: random"
  127. ),
  128. ), caplog.at_level(logging.ERROR):
  129. switchbot_mqtt._ButtonAutomator(
  130. mac_address=mac_address, retry_count=3, password=None
  131. ).execute_command(
  132. mqtt_client="dummy",
  133. mqtt_message_payload=message_payload,
  134. update_device_info=True,
  135. )
  136. assert caplog.record_tuples == [
  137. (
  138. "switchbot",
  139. logging.ERROR,
  140. "Switchbot communication failed. Stopping trying.",
  141. ),
  142. (
  143. "switchbot_mqtt",
  144. logging.ERROR,
  145. f"failed to turn {message_payload.decode().lower()} switchbot {mac_address}",
  146. ),
  147. ]