test_actor_base_device_info.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. @pytest.mark.parametrize(
  35. "actor_class", [switchbot_mqtt._CurtainMotor, switchbot_mqtt._ButtonAutomator]
  36. )
  37. def test__update_device_info_le_on_permission_denied_log(
  38. actor_class,
  39. ): # pySwitchbot>=v0.10.0
  40. actor = actor_class(mac_address="dummy", retry_count=0, password=None)
  41. with unittest.mock.patch(
  42. "bluepy.btle.Scanner.scan",
  43. side_effect=_LE_ON_PERMISSION_DENIED_ERROR,
  44. ), pytest.raises(
  45. PermissionError, match=r"^bluepy-helper failed to enable low energy mode "
  46. ) as exc_info:
  47. actor._update_device_info()
  48. assert "sudo setcap cap_net_admin+ep /" in exc_info.exconly()
  49. assert exc_info.value.__cause__ == _LE_ON_PERMISSION_DENIED_ERROR
  50. @pytest.mark.parametrize(
  51. "actor_class", [switchbot_mqtt._CurtainMotor, switchbot_mqtt._ButtonAutomator]
  52. )
  53. def test__update_device_info_le_on_permission_denied_exc(
  54. actor_class,
  55. ): # pySwitchbot<v0.10.1
  56. actor = actor_class(mac_address="dummy", retry_count=21, password=None)
  57. with unittest.mock.patch.object(
  58. actor._get_device(),
  59. "update",
  60. side_effect=_LE_ON_PERMISSION_DENIED_ERROR,
  61. ) as update_mock, pytest.raises(
  62. PermissionError, match=r"^bluepy-helper failed to enable low energy mode "
  63. ) as exc_info:
  64. actor._update_device_info()
  65. update_mock.assert_called_once_with()
  66. assert os.path.isfile(
  67. re.search(
  68. r"sudo setcap cap_net_admin\+ep (\S+/bluepy-helper)\b",
  69. exc_info.exconly(),
  70. ).group(1)
  71. )
  72. assert exc_info.value.__cause__ == _LE_ON_PERMISSION_DENIED_ERROR
  73. @pytest.mark.parametrize(
  74. "actor_class", [switchbot_mqtt._CurtainMotor, switchbot_mqtt._ButtonAutomator]
  75. )
  76. def test__update_device_info_other_error(actor_class):
  77. actor = actor_class(mac_address="dummy", retry_count=21, password=None)
  78. side_effect = bluepy.btle.BTLEManagementError("test")
  79. with unittest.mock.patch.object(
  80. actor._get_device(), "update", side_effect=side_effect
  81. ) as update_mock, pytest.raises(type(side_effect)) as exc_info:
  82. actor._update_device_info()
  83. update_mock.assert_called_once_with()
  84. assert exc_info.value == side_effect