test_actor_base_device_info.py 3.6 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 typing
  21. import unittest.mock
  22. import bluepy.btle
  23. import pytest
  24. # pylint: disable=import-private-name; internal
  25. from switchbot_mqtt._actors import _ButtonAutomator, _CurtainMotor
  26. from switchbot_mqtt._actors.base import _MQTTControlledActor
  27. # pylint: disable=protected-access
  28. _LE_ON_PERMISSION_DENIED_ERROR = bluepy.btle.BTLEManagementError(
  29. "Failed to execute management command 'le on'",
  30. {
  31. "rsp": ["mgmt"],
  32. "code": ["mgmterr"],
  33. "estat": [20],
  34. "emsg": ["Permission Denied"],
  35. },
  36. )
  37. @pytest.mark.parametrize("actor_class", [_CurtainMotor, _ButtonAutomator])
  38. def test__update_device_info_le_on_permission_denied_log(
  39. actor_class: typing.Type[_MQTTControlledActor],
  40. ) -> None: # pySwitchbot>=v0.10.0
  41. actor = actor_class(mac_address="dummy", retry_count=0, password=None)
  42. with unittest.mock.patch(
  43. "bluepy.btle.Scanner.scan",
  44. side_effect=_LE_ON_PERMISSION_DENIED_ERROR,
  45. ), pytest.raises(
  46. PermissionError, match=r"^bluepy-helper failed to enable low energy mode "
  47. ) as exc_info:
  48. actor._update_device_info()
  49. assert "sudo setcap cap_net_admin+ep /" in exc_info.exconly()
  50. assert exc_info.value.__cause__ == _LE_ON_PERMISSION_DENIED_ERROR
  51. @pytest.mark.parametrize("actor_class", [_CurtainMotor, _ButtonAutomator])
  52. def test__update_device_info_le_on_permission_denied_exc(
  53. actor_class: typing.Type[_MQTTControlledActor],
  54. ) -> None: # pySwitchbot<v0.10.1
  55. actor = actor_class(mac_address="dummy", retry_count=21, password=None)
  56. with unittest.mock.patch.object(
  57. actor._get_device(),
  58. "update",
  59. side_effect=_LE_ON_PERMISSION_DENIED_ERROR,
  60. ) as update_mock, pytest.raises(
  61. PermissionError, match=r"^bluepy-helper failed to enable low energy mode "
  62. ) as exc_info:
  63. actor._update_device_info()
  64. update_mock.assert_called_once_with()
  65. bluepy_helper_path_match = re.search(
  66. r"sudo setcap cap_net_admin\+ep (\S+/bluepy-helper)\b",
  67. exc_info.exconly(),
  68. )
  69. assert bluepy_helper_path_match is not None
  70. assert os.path.isfile(bluepy_helper_path_match.group(1))
  71. assert exc_info.value.__cause__ == _LE_ON_PERMISSION_DENIED_ERROR
  72. @pytest.mark.parametrize("actor_class", [_CurtainMotor, _ButtonAutomator])
  73. def test__update_device_info_other_error(
  74. actor_class: typing.Type[_MQTTControlledActor],
  75. ) -> None:
  76. actor = actor_class(mac_address="dummy", retry_count=21, password=None)
  77. side_effect = bluepy.btle.BTLEManagementError("test")
  78. with unittest.mock.patch.object(
  79. actor._get_device(), "update", side_effect=side_effect
  80. ) as update_mock, pytest.raises(type(side_effect)) as exc_info:
  81. actor._update_device_info()
  82. update_mock.assert_called_once_with()
  83. assert exc_info.value == side_effect