test_actor_base.py 3.2 KB

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