1
0

test_switchbot_curtain_motor_position.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. mqtt_topic_prefix="",
  128. )
  129. message = MQTTMessage(topic=b"switchbot-curtain/aa:bb:cc:dd:ee:ff/position/set")
  130. message.payload = b"42"
  131. with unittest.mock.patch(
  132. "switchbot.SwitchbotCurtain"
  133. ) as device_init_mock, caplog.at_level(logging.INFO):
  134. _CurtainMotor._mqtt_set_position_callback(
  135. mqtt_client="client dummy", userdata=callback_userdata, message=message
  136. )
  137. device_init_mock.assert_not_called()
  138. assert caplog.record_tuples == [
  139. (
  140. "switchbot_mqtt._actors.base",
  141. logging.WARN,
  142. "unexpected topic switchbot-curtain/aa:bb:cc:dd:ee:ff/position/set",
  143. ),
  144. ]
  145. def test__mqtt_set_position_callback_invalid_mac_address(
  146. caplog: _pytest.logging.LogCaptureFixture,
  147. ) -> None:
  148. callback_userdata = _MQTTCallbackUserdata(
  149. retry_count=3,
  150. device_passwords={},
  151. fetch_device_info=False,
  152. )
  153. message = MQTTMessage(
  154. topic=b"homeassistant/cover/switchbot-curtain/aa:bb:cc:dd:ee/position/set-percent"
  155. )
  156. message.payload = b"42"
  157. with unittest.mock.patch(
  158. "switchbot.SwitchbotCurtain"
  159. ) as device_init_mock, caplog.at_level(logging.INFO):
  160. _CurtainMotor._mqtt_set_position_callback(
  161. mqtt_client="client dummy", userdata=callback_userdata, message=message
  162. )
  163. device_init_mock.assert_not_called()
  164. assert caplog.record_tuples == [
  165. (
  166. "switchbot_mqtt._actors.base",
  167. logging.WARN,
  168. "invalid mac address aa:bb:cc:dd:ee",
  169. ),
  170. ]
  171. @pytest.mark.parametrize("payload", [b"-1", b"123"])
  172. def test__mqtt_set_position_callback_invalid_position(
  173. caplog: _pytest.logging.LogCaptureFixture,
  174. payload: bytes,
  175. ) -> None:
  176. callback_userdata = _MQTTCallbackUserdata(
  177. retry_count=3,
  178. device_passwords={},
  179. fetch_device_info=False,
  180. )
  181. message = MQTTMessage(
  182. topic=b"homeassistant/cover/switchbot-curtain/aa:bb:cc:dd:ee:ff/position/set-percent"
  183. )
  184. message.payload = payload
  185. with unittest.mock.patch(
  186. "switchbot.SwitchbotCurtain"
  187. ) as device_init_mock, caplog.at_level(logging.INFO):
  188. _CurtainMotor._mqtt_set_position_callback(
  189. mqtt_client="client dummy", userdata=callback_userdata, message=message
  190. )
  191. device_init_mock.assert_called_once()
  192. device_init_mock().set_position.assert_not_called()
  193. assert caplog.record_tuples == [
  194. (
  195. "switchbot_mqtt._actors",
  196. logging.WARN,
  197. f"invalid position {payload.decode()}%, ignoring message",
  198. ),
  199. ]
  200. def test__mqtt_set_position_callback_command_failed(
  201. caplog: _pytest.logging.LogCaptureFixture,
  202. ) -> None:
  203. callback_userdata = _MQTTCallbackUserdata(
  204. retry_count=3,
  205. device_passwords={},
  206. fetch_device_info=False,
  207. )
  208. message = MQTTMessage(
  209. topic=b"homeassistant/cover/switchbot-curtain/aa:bb:cc:dd:ee:ff/position/set-percent"
  210. )
  211. message.payload = b"21"
  212. with unittest.mock.patch(
  213. "switchbot.SwitchbotCurtain"
  214. ) as device_init_mock, caplog.at_level(logging.INFO):
  215. device_init_mock().set_position.return_value = False
  216. device_init_mock.reset_mock()
  217. _CurtainMotor._mqtt_set_position_callback(
  218. mqtt_client="client dummy", userdata=callback_userdata, message=message
  219. )
  220. device_init_mock.assert_called_once()
  221. device_init_mock().set_position.assert_called_with(21)
  222. assert caplog.record_tuples == [
  223. (
  224. "switchbot_mqtt._actors",
  225. logging.ERROR,
  226. "failed to set position of switchbot curtain aa:bb:cc:dd:ee:ff",
  227. ),
  228. ]