test_actor_base_device_info.py 3.5 KB

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