test_actor_base.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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) 2020 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 unittest.mock
  19. import bleak.backends.device
  20. import paho.mqtt.client
  21. import pytest
  22. import switchbot
  23. import switchbot_mqtt._actors.base
  24. # pylint: disable=protected-access
  25. def test_abstract() -> None:
  26. with pytest.raises(TypeError, match=r"\babstract class\b"):
  27. # pylint: disable=abstract-class-instantiated
  28. switchbot_mqtt._actors.base._MQTTControlledActor( # type: ignore
  29. device=unittest.mock.Mock(), retry_count=21, password=None
  30. )
  31. @pytest.mark.asyncio
  32. async def test_execute_command_abstract() -> None:
  33. class _ActorMock(switchbot_mqtt._actors.base._MQTTControlledActor):
  34. # pylint: disable=duplicate-code
  35. def __init__(
  36. self,
  37. device: bleak.backends.device.BLEDevice,
  38. retry_count: int,
  39. password: str | None,
  40. ) -> None:
  41. super().__init__(device=device, retry_count=retry_count, password=password)
  42. async def execute_command(
  43. self,
  44. *,
  45. mqtt_message_payload: bytes,
  46. mqtt_client: paho.mqtt.client.Client,
  47. update_device_info: bool,
  48. mqtt_topic_prefix: str,
  49. ) -> None:
  50. assert 21
  51. await super().execute_command( # type: ignore
  52. mqtt_message_payload=mqtt_message_payload,
  53. mqtt_client=mqtt_client,
  54. update_device_info=update_device_info,
  55. mqtt_topic_prefix=mqtt_topic_prefix,
  56. )
  57. def _get_device(self) -> switchbot.SwitchbotDevice:
  58. assert 42
  59. return super()._get_device() # type: ignore
  60. with pytest.raises(TypeError) as exc_info:
  61. # pylint: disable=abstract-class-instantiated
  62. switchbot_mqtt._actors.base._MQTTControlledActor( # type: ignore
  63. device=unittest.mock.Mock(), retry_count=42, password=None
  64. )
  65. exc_info.match(
  66. r"^Can't instantiate abstract class _MQTTControlledActor"
  67. r" (with abstract methods __init__, _get_device, execute_command"
  68. r"|without an implementation for abstract methods '__init__'"
  69. r", '_get_device', 'execute_command')$"
  70. )
  71. actor = _ActorMock(device=unittest.mock.Mock(), retry_count=42, password=None)
  72. with pytest.raises(NotImplementedError):
  73. await actor.execute_command(
  74. mqtt_message_payload=b"dummy",
  75. mqtt_client="dummy",
  76. update_device_info=True,
  77. mqtt_topic_prefix="whatever",
  78. )
  79. with pytest.raises(NotImplementedError):
  80. actor._get_device()