test_switchbot_button_automator.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. @pytest.mark.parametrize("mac_address", ["aa:bb:cc:dd:ee:ff", "aa:bb:cc:11:22:33"])
  25. @pytest.mark.parametrize(
  26. ("message_payload", "action_name"),
  27. [
  28. (b"on", "switchbot.Switchbot.turn_on"),
  29. (b"ON", "switchbot.Switchbot.turn_on"),
  30. (b"On", "switchbot.Switchbot.turn_on"),
  31. (b"off", "switchbot.Switchbot.turn_off"),
  32. (b"OFF", "switchbot.Switchbot.turn_off"),
  33. (b"Off", "switchbot.Switchbot.turn_off"),
  34. ],
  35. )
  36. @pytest.mark.parametrize("command_successful", [True, False])
  37. def test_execute_command(
  38. caplog, mac_address, message_payload, action_name, command_successful
  39. ):
  40. with unittest.mock.patch(
  41. "switchbot.Switchbot.__init__", return_value=None
  42. ) as device_init_mock, caplog.at_level(logging.INFO):
  43. actor = switchbot_mqtt._ButtonAutomator(mac_address=mac_address)
  44. with unittest.mock.patch.object(
  45. actor, "report_state"
  46. ) as report_mock, unittest.mock.patch(
  47. action_name, return_value=command_successful
  48. ) as action_mock:
  49. actor.execute_command(
  50. mqtt_client="dummy", mqtt_message_payload=message_payload
  51. )
  52. device_init_mock.assert_called_once_with(mac=mac_address)
  53. action_mock.assert_called_once_with()
  54. if command_successful:
  55. assert caplog.record_tuples == [
  56. (
  57. "switchbot_mqtt",
  58. logging.INFO,
  59. "switchbot {} turned {}".format(
  60. mac_address, message_payload.decode().lower()
  61. ),
  62. )
  63. ]
  64. report_mock.assert_called_once_with(
  65. mqtt_client="dummy", state=message_payload.upper()
  66. )
  67. else:
  68. assert caplog.record_tuples == [
  69. (
  70. "switchbot_mqtt",
  71. logging.ERROR,
  72. "failed to turn {} switchbot {}".format(
  73. message_payload.decode().lower(), mac_address
  74. ),
  75. )
  76. ]
  77. report_mock.assert_not_called()
  78. @pytest.mark.parametrize("mac_address", ["aa:bb:cc:dd:ee:ff"])
  79. @pytest.mark.parametrize("message_payload", [b"EIN", b""])
  80. def test_execute_command_invalid_payload(caplog, mac_address, message_payload):
  81. with unittest.mock.patch("switchbot.Switchbot") as device_mock, caplog.at_level(
  82. logging.INFO
  83. ):
  84. actor = switchbot_mqtt._ButtonAutomator(mac_address=mac_address)
  85. with unittest.mock.patch.object(actor, "report_state") as report_mock:
  86. actor.execute_command(
  87. mqtt_client="dummy", mqtt_message_payload=message_payload
  88. )
  89. device_mock.assert_called_once_with(mac=mac_address)
  90. assert not device_mock().mock_calls # no methods called
  91. report_mock.assert_not_called()
  92. assert caplog.record_tuples == [
  93. (
  94. "switchbot_mqtt",
  95. logging.WARNING,
  96. "unexpected payload {!r} (expected 'ON' or 'OFF')".format(message_payload),
  97. )
  98. ]
  99. @pytest.mark.parametrize("mac_address", ["aa:bb:cc:dd:ee:ff"])
  100. @pytest.mark.parametrize("message_payload", [b"ON", b"OFF"])
  101. def test_execute_command_bluetooth_error(caplog, mac_address, message_payload):
  102. """
  103. paho.mqtt.python>=1.5.1 no longer implicitly suppresses exceptions in callbacks.
  104. verify pySwitchbot catches exceptions raised in bluetooth stack.
  105. https://github.com/Danielhiversen/pySwitchbot/blob/0.8.0/switchbot/__init__.py#L48
  106. https://github.com/Danielhiversen/pySwitchbot/blob/0.8.0/switchbot/__init__.py#L94
  107. """
  108. with unittest.mock.patch(
  109. "bluepy.btle.Peripheral",
  110. side_effect=bluepy.btle.BTLEDisconnectError(
  111. "Failed to connect to peripheral {}, addr type: random".format(mac_address)
  112. ),
  113. ), caplog.at_level(logging.ERROR):
  114. switchbot_mqtt._ButtonAutomator(mac_address=mac_address).execute_command(
  115. mqtt_client="dummy", mqtt_message_payload=message_payload
  116. )
  117. assert caplog.record_tuples == [
  118. (
  119. "switchbot",
  120. logging.ERROR,
  121. "Switchbot communication failed. Stopping trying.",
  122. ),
  123. (
  124. "switchbot_mqtt",
  125. logging.ERROR,
  126. "failed to turn {} switchbot {}".format(
  127. message_payload.decode().lower(), mac_address
  128. ),
  129. ),
  130. ]