test_switchbot_curtain_motor.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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", ["pa$$word", None])
  27. @pytest.mark.parametrize("retry_count", (2, 3))
  28. @pytest.mark.parametrize(
  29. ("message_payload", "action_name"),
  30. [
  31. (b"open", "switchbot.SwitchbotCurtain.open"),
  32. (b"OPEN", "switchbot.SwitchbotCurtain.open"),
  33. (b"Open", "switchbot.SwitchbotCurtain.open"),
  34. (b"close", "switchbot.SwitchbotCurtain.close"),
  35. (b"CLOSE", "switchbot.SwitchbotCurtain.close"),
  36. (b"Close", "switchbot.SwitchbotCurtain.close"),
  37. (b"stop", "switchbot.SwitchbotCurtain.stop"),
  38. (b"STOP", "switchbot.SwitchbotCurtain.stop"),
  39. (b"Stop", "switchbot.SwitchbotCurtain.stop"),
  40. ],
  41. )
  42. @pytest.mark.parametrize("command_successful", [True, False])
  43. def test_execute_command(
  44. caplog,
  45. mac_address,
  46. password,
  47. retry_count,
  48. message_payload,
  49. action_name,
  50. command_successful,
  51. ):
  52. with unittest.mock.patch(
  53. "switchbot.SwitchbotCurtain.__init__", return_value=None
  54. ) as device_init_mock, caplog.at_level(logging.INFO):
  55. actor = switchbot_mqtt._CurtainMotor(
  56. mac_address=mac_address, retry_count=retry_count, password=password
  57. )
  58. with unittest.mock.patch.object(
  59. actor, "report_state"
  60. ) as report_mock, unittest.mock.patch(
  61. action_name, return_value=command_successful
  62. ) as action_mock:
  63. actor.execute_command(
  64. mqtt_client="dummy", mqtt_message_payload=message_payload
  65. )
  66. device_init_mock.assert_called_once_with(
  67. mac=mac_address,
  68. password=password,
  69. retry_count=retry_count,
  70. )
  71. action_mock.assert_called_once_with()
  72. if command_successful:
  73. assert caplog.record_tuples == [
  74. (
  75. "switchbot_mqtt",
  76. logging.INFO,
  77. "switchbot curtain {} {}".format(
  78. mac_address,
  79. {b"open": "opening", b"close": "closing", b"stop": "stopped"}[
  80. message_payload.lower()
  81. ],
  82. ),
  83. )
  84. ]
  85. report_mock.assert_called_once_with(
  86. mqtt_client="dummy",
  87. # https://www.home-assistant.io/integrations/cover.mqtt/#state_opening
  88. state={b"open": b"opening", b"close": b"closing", b"stop": b""}[
  89. message_payload.lower()
  90. ],
  91. )
  92. else:
  93. assert caplog.record_tuples == [
  94. (
  95. "switchbot_mqtt",
  96. logging.ERROR,
  97. "failed to {} switchbot curtain {}".format(
  98. message_payload.decode().lower(), mac_address
  99. ),
  100. )
  101. ]
  102. report_mock.assert_not_called()
  103. @pytest.mark.parametrize("mac_address", ["aa:bb:cc:dd:ee:ff"])
  104. @pytest.mark.parametrize("password", ["secret"])
  105. @pytest.mark.parametrize("message_payload", [b"OEFFNEN", b""])
  106. def test_execute_command_invalid_payload(
  107. caplog, mac_address, password, message_payload
  108. ):
  109. with unittest.mock.patch(
  110. "switchbot.SwitchbotCurtain"
  111. ) as device_mock, caplog.at_level(logging.INFO):
  112. actor = switchbot_mqtt._CurtainMotor(
  113. mac_address=mac_address, retry_count=7, password=password
  114. )
  115. with unittest.mock.patch.object(actor, "report_state") as report_mock:
  116. actor.execute_command(
  117. mqtt_client="dummy", mqtt_message_payload=message_payload
  118. )
  119. device_mock.assert_called_once_with(
  120. mac=mac_address, password=password, retry_count=7
  121. )
  122. assert not device_mock().mock_calls # no methods called
  123. report_mock.assert_not_called()
  124. assert caplog.record_tuples == [
  125. (
  126. "switchbot_mqtt",
  127. logging.WARNING,
  128. "unexpected payload {!r} (expected 'OPEN', 'CLOSE', or 'STOP')".format(
  129. message_payload
  130. ),
  131. )
  132. ]
  133. @pytest.mark.parametrize("mac_address", ["aa:bb:cc:dd:ee:ff"])
  134. @pytest.mark.parametrize("message_payload", [b"OPEN", b"CLOSE", b"STOP"])
  135. def test_execute_command_bluetooth_error(caplog, mac_address, message_payload):
  136. """
  137. paho.mqtt.python>=1.5.1 no longer implicitly suppresses exceptions in callbacks.
  138. verify pySwitchbot catches exceptions raised in bluetooth stack.
  139. https://github.com/Danielhiversen/pySwitchbot/blob/0.8.0/switchbot/__init__.py#L48
  140. https://github.com/Danielhiversen/pySwitchbot/blob/0.8.0/switchbot/__init__.py#L94
  141. """
  142. with unittest.mock.patch(
  143. "bluepy.btle.Peripheral",
  144. side_effect=bluepy.btle.BTLEDisconnectError(
  145. "Failed to connect to peripheral {}, addr type: random".format(mac_address)
  146. ),
  147. ), caplog.at_level(logging.ERROR):
  148. switchbot_mqtt._CurtainMotor(
  149. mac_address=mac_address, retry_count=10, password="secret"
  150. ).execute_command(mqtt_client="dummy", mqtt_message_payload=message_payload)
  151. assert caplog.record_tuples == [
  152. (
  153. "switchbot",
  154. logging.ERROR,
  155. "Switchbot communication failed. Stopping trying.",
  156. ),
  157. (
  158. "switchbot_mqtt",
  159. logging.ERROR,
  160. "failed to {} switchbot curtain {}".format(
  161. message_payload.decode().lower(), mac_address
  162. ),
  163. ),
  164. ]