test_switchbot_curtain_motor_position.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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) 2022 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 _pytest.logging
  21. import pytest
  22. from paho.mqtt.client import MQTTMessage
  23. from switchbot_mqtt._actors import _CurtainMotor
  24. from switchbot_mqtt._actors._base import _MQTTCallbackUserdata
  25. # pylint: disable=protected-access
  26. @pytest.mark.parametrize(
  27. ("topic", "payload", "expected_mac_address", "expected_position_percent"),
  28. [
  29. (
  30. b"homeassistant/cover/switchbot-curtain/aa:bb:cc:dd:ee:ff/position/set-percent",
  31. b"42",
  32. "aa:bb:cc:dd:ee:ff",
  33. 42,
  34. ),
  35. (
  36. b"homeassistant/cover/switchbot-curtain/11:22:33:44:55:66/position/set-percent",
  37. b"0",
  38. "11:22:33:44:55:66",
  39. 0,
  40. ),
  41. (
  42. b"homeassistant/cover/switchbot-curtain/11:22:33:44:55:66/position/set-percent",
  43. b"100",
  44. "11:22:33:44:55:66",
  45. 100,
  46. ),
  47. ],
  48. )
  49. @pytest.mark.parametrize("retry_count", (3, 42))
  50. def test__mqtt_set_position_callback(
  51. caplog: _pytest.logging.LogCaptureFixture,
  52. topic: bytes,
  53. payload: bytes,
  54. expected_mac_address: str,
  55. retry_count: int,
  56. expected_position_percent: int,
  57. ) -> None:
  58. callback_userdata = _MQTTCallbackUserdata(
  59. retry_count=retry_count,
  60. device_passwords={},
  61. fetch_device_info=False,
  62. )
  63. message = MQTTMessage(topic=topic)
  64. message.payload = payload
  65. with unittest.mock.patch(
  66. "switchbot.SwitchbotCurtain"
  67. ) as device_init_mock, caplog.at_level(logging.DEBUG):
  68. _CurtainMotor._mqtt_set_position_callback(
  69. mqtt_client="client dummy", userdata=callback_userdata, message=message
  70. )
  71. device_init_mock.assert_called_once_with(
  72. mac=expected_mac_address,
  73. password=None,
  74. retry_count=retry_count,
  75. reverse_mode=True,
  76. )
  77. device_init_mock().set_position.assert_called_once_with(expected_position_percent)
  78. assert caplog.record_tuples == [
  79. (
  80. "switchbot_mqtt._actors",
  81. logging.DEBUG,
  82. f"received topic=homeassistant/cover/switchbot-curtain/{expected_mac_address}"
  83. f"/position/set-percent payload=b'{expected_position_percent}'",
  84. ),
  85. (
  86. "switchbot_mqtt._actors",
  87. logging.INFO,
  88. f"set position of switchbot curtain {expected_mac_address}"
  89. f" to {expected_position_percent}%",
  90. ),
  91. ]
  92. def test__mqtt_set_position_callback_ignore_retained(
  93. caplog: _pytest.logging.LogCaptureFixture,
  94. ) -> None:
  95. callback_userdata = _MQTTCallbackUserdata(
  96. retry_count=3,
  97. device_passwords={},
  98. fetch_device_info=False,
  99. )
  100. message = MQTTMessage(
  101. topic=b"homeassistant/cover/switchbot-curtain/aa:bb:cc:dd:ee:ff/position/set-percent"
  102. )
  103. message.payload = b"42"
  104. message.retain = True
  105. with unittest.mock.patch(
  106. "switchbot.SwitchbotCurtain"
  107. ) as device_init_mock, caplog.at_level(logging.INFO):
  108. _CurtainMotor._mqtt_set_position_callback(
  109. mqtt_client="client dummy", userdata=callback_userdata, message=message
  110. )
  111. device_init_mock.assert_not_called()
  112. assert caplog.record_tuples == [
  113. (
  114. "switchbot_mqtt._actors",
  115. logging.INFO,
  116. "ignoring retained message on topic"
  117. " homeassistant/cover/switchbot-curtain/aa:bb:cc:dd:ee:ff/position/set-percent",
  118. ),
  119. ]
  120. def test__mqtt_set_position_callback_unexpected_topic(
  121. caplog: _pytest.logging.LogCaptureFixture,
  122. ) -> None:
  123. callback_userdata = _MQTTCallbackUserdata(
  124. retry_count=3,
  125. device_passwords={},
  126. fetch_device_info=False,
  127. )
  128. message = MQTTMessage(topic=b"switchbot-curtain/aa:bb:cc:dd:ee:ff/position/set")
  129. message.payload = b"42"
  130. with unittest.mock.patch(
  131. "switchbot.SwitchbotCurtain"
  132. ) as device_init_mock, caplog.at_level(logging.INFO):
  133. _CurtainMotor._mqtt_set_position_callback(
  134. mqtt_client="client dummy", userdata=callback_userdata, message=message
  135. )
  136. device_init_mock.assert_not_called()
  137. assert caplog.record_tuples == [
  138. (
  139. "switchbot_mqtt._actors._base",
  140. logging.WARN,
  141. "unexpected topic switchbot-curtain/aa:bb:cc:dd:ee:ff/position/set",
  142. ),
  143. ]
  144. def test__mqtt_set_position_callback_invalid_mac_address(
  145. caplog: _pytest.logging.LogCaptureFixture,
  146. ) -> None:
  147. callback_userdata = _MQTTCallbackUserdata(
  148. retry_count=3,
  149. device_passwords={},
  150. fetch_device_info=False,
  151. )
  152. message = MQTTMessage(
  153. topic=b"homeassistant/cover/switchbot-curtain/aa:bb:cc:dd:ee/position/set-percent"
  154. )
  155. message.payload = b"42"
  156. with unittest.mock.patch(
  157. "switchbot.SwitchbotCurtain"
  158. ) as device_init_mock, caplog.at_level(logging.INFO):
  159. _CurtainMotor._mqtt_set_position_callback(
  160. mqtt_client="client dummy", userdata=callback_userdata, message=message
  161. )
  162. device_init_mock.assert_not_called()
  163. assert caplog.record_tuples == [
  164. (
  165. "switchbot_mqtt._actors._base",
  166. logging.WARN,
  167. "invalid mac address aa:bb:cc:dd:ee",
  168. ),
  169. ]
  170. @pytest.mark.parametrize("payload", [b"-1", b"123"])
  171. def test__mqtt_set_position_callback_invalid_position(
  172. caplog: _pytest.logging.LogCaptureFixture,
  173. payload: bytes,
  174. ) -> None:
  175. callback_userdata = _MQTTCallbackUserdata(
  176. retry_count=3,
  177. device_passwords={},
  178. fetch_device_info=False,
  179. )
  180. message = MQTTMessage(
  181. topic=b"homeassistant/cover/switchbot-curtain/aa:bb:cc:dd:ee:ff/position/set-percent"
  182. )
  183. message.payload = payload
  184. with unittest.mock.patch(
  185. "switchbot.SwitchbotCurtain"
  186. ) as device_init_mock, caplog.at_level(logging.INFO):
  187. _CurtainMotor._mqtt_set_position_callback(
  188. mqtt_client="client dummy", userdata=callback_userdata, message=message
  189. )
  190. device_init_mock.assert_called_once()
  191. device_init_mock().set_position.assert_not_called()
  192. assert caplog.record_tuples == [
  193. (
  194. "switchbot_mqtt._actors",
  195. logging.WARN,
  196. f"invalid position {payload.decode()}%, ignoring message",
  197. ),
  198. ]
  199. def test__mqtt_set_position_callback_command_failed(
  200. caplog: _pytest.logging.LogCaptureFixture,
  201. ) -> None:
  202. callback_userdata = _MQTTCallbackUserdata(
  203. retry_count=3,
  204. device_passwords={},
  205. fetch_device_info=False,
  206. )
  207. message = MQTTMessage(
  208. topic=b"homeassistant/cover/switchbot-curtain/aa:bb:cc:dd:ee:ff/position/set-percent"
  209. )
  210. message.payload = b"21"
  211. with unittest.mock.patch(
  212. "switchbot.SwitchbotCurtain"
  213. ) as device_init_mock, caplog.at_level(logging.INFO):
  214. device_init_mock().set_position.return_value = False
  215. device_init_mock.reset_mock()
  216. _CurtainMotor._mqtt_set_position_callback(
  217. mqtt_client="client dummy", userdata=callback_userdata, message=message
  218. )
  219. device_init_mock.assert_called_once()
  220. device_init_mock().set_position.assert_called_with(21)
  221. assert caplog.record_tuples == [
  222. (
  223. "switchbot_mqtt._actors",
  224. logging.ERROR,
  225. "failed to set position of switchbot curtain aa:bb:cc:dd:ee:ff",
  226. ),
  227. ]