test_switchbot_curtain_motor_device_info.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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) 2021 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 os
  19. import re
  20. import unittest.mock
  21. import bluepy.btle
  22. import pytest
  23. import switchbot_mqtt
  24. # pylint: disable=protected-access
  25. _LE_ON_PERMISSION_DENIED_ERROR = bluepy.btle.BTLEManagementError(
  26. "Failed to execute management command 'le on'",
  27. {
  28. "rsp": ["mgmt"],
  29. "code": ["mgmterr"],
  30. "estat": [20],
  31. "emsg": ["Permission Denied"],
  32. },
  33. )
  34. def test__update_position_le_on_permission_denied_log(): # pySwitchbot>=v0.10.0
  35. actor = switchbot_mqtt._CurtainMotor(
  36. mac_address="dummy", retry_count=21, password=None
  37. )
  38. with unittest.mock.patch(
  39. "bluepy.btle.Scanner.scan",
  40. side_effect=_LE_ON_PERMISSION_DENIED_ERROR,
  41. ), unittest.mock.patch.object(
  42. actor, "_report_position"
  43. ) as report_position_mock, pytest.raises(
  44. PermissionError
  45. ) as exc_info:
  46. actor._update_position(mqtt_client="client")
  47. report_position_mock.assert_not_called()
  48. assert "sudo setcap cap_net_admin+ep /" in exc_info.exconly()
  49. assert exc_info.value.__cause__ == _LE_ON_PERMISSION_DENIED_ERROR
  50. def test__update_position_le_on_permission_denied_exc(): # pySwitchbot<v0.10.1
  51. actor = switchbot_mqtt._CurtainMotor(
  52. mac_address="dummy", retry_count=21, password=None
  53. )
  54. with unittest.mock.patch(
  55. "switchbot.SwitchbotCurtain.update",
  56. side_effect=_LE_ON_PERMISSION_DENIED_ERROR,
  57. ) as update_mock, unittest.mock.patch.object(
  58. actor, "_report_position"
  59. ) as report_position_mock, pytest.raises(
  60. PermissionError
  61. ) as exc_info:
  62. actor._update_position(mqtt_client="client")
  63. update_mock.assert_called_once_with()
  64. report_position_mock.assert_not_called()
  65. assert os.path.isfile(
  66. re.search(
  67. r"sudo setcap cap_net_admin\+ep (\S+/bluepy-helper)\b",
  68. exc_info.exconly(),
  69. ).group(1)
  70. )
  71. assert exc_info.value.__cause__ == _LE_ON_PERMISSION_DENIED_ERROR
  72. def test__update_position_other_error():
  73. actor = switchbot_mqtt._CurtainMotor(
  74. mac_address="dummy", retry_count=21, password=None
  75. )
  76. side_effect = bluepy.btle.BTLEManagementError("test")
  77. with unittest.mock.patch(
  78. "switchbot.SwitchbotCurtain.update", side_effect=side_effect
  79. ) as update_mock, unittest.mock.patch.object(
  80. actor, "_report_position"
  81. ) as report_position_mock, pytest.raises(
  82. type(side_effect)
  83. ) as exc_info:
  84. actor._update_position(mqtt_client="client")
  85. update_mock.assert_called_once_with()
  86. report_position_mock.assert_not_called()
  87. assert exc_info.value == side_effect