test_switchbot_curtain_motor_device_info.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. def test__update_position_le_on_permission_denied():
  26. actor = switchbot_mqtt._CurtainMotor(
  27. mac_address="dummy", retry_count=21, password=None
  28. )
  29. with unittest.mock.patch(
  30. "switchbot.SwitchbotCurtain.update",
  31. side_effect=bluepy.btle.BTLEManagementError(
  32. "Failed to execute management command 'le on'",
  33. {
  34. "rsp": ["mgmt"],
  35. "code": ["mgmterr"],
  36. "estat": [20],
  37. "emsg": ["Permission Denied"],
  38. },
  39. ),
  40. ) as update_mock, unittest.mock.patch.object(
  41. actor, "_report_position"
  42. ) as report_position_mock, pytest.raises(
  43. PermissionError
  44. ) as exc_info:
  45. actor._update_position(mqtt_client="client")
  46. update_mock.assert_called_once_with()
  47. report_position_mock.assert_not_called()
  48. assert os.path.isfile(
  49. re.search(
  50. r"sudo setcap cap_net_admin\+ep (\S+/bluepy-helper)\b",
  51. exc_info.exconly(),
  52. ).group(1)
  53. )
  54. assert isinstance(exc_info.value.__cause__, bluepy.btle.BTLEManagementError)
  55. def test__update_position_other_error():
  56. actor = switchbot_mqtt._CurtainMotor(
  57. mac_address="dummy", retry_count=21, password=None
  58. )
  59. side_effect = bluepy.btle.BTLEManagementError("test")
  60. with unittest.mock.patch(
  61. "switchbot.SwitchbotCurtain.update", side_effect=side_effect
  62. ) as update_mock, unittest.mock.patch.object(
  63. actor, "_report_position"
  64. ) as report_position_mock, pytest.raises(
  65. type(side_effect)
  66. ) as exc_info:
  67. actor._update_position(mqtt_client="client")
  68. update_mock.assert_called_once_with()
  69. report_position_mock.assert_not_called()
  70. assert exc_info.value == side_effect