1
0

test_utils.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 pytest
  20. # pylint: disable=import-private-name; internal
  21. from switchbot_mqtt._utils import (
  22. _mac_address_valid,
  23. _MQTTTopicLevel,
  24. _MQTTTopicPlaceholder,
  25. _parse_mqtt_topic,
  26. )
  27. @pytest.mark.parametrize(
  28. ("mac_address", "valid"),
  29. [
  30. ("aa:bb:cc:dd:ee:ff", True),
  31. ("AA:BB:CC:DD:EE:FF", True),
  32. ("AA:12:34:45:67:89", True),
  33. ("aabbccddeeff", False), # not supported by PySwitchbot
  34. ("aa:bb:cc:dd:ee:gg", False),
  35. ],
  36. )
  37. def test__mac_address_valid(mac_address: str, valid: bool) -> None:
  38. # pylint: disable=protected-access
  39. assert _mac_address_valid(mac_address) == valid
  40. @pytest.mark.parametrize(
  41. ("expected_prefix", "expected_levels", "topic", "expected_attrs"),
  42. [
  43. (
  44. "",
  45. ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
  46. "switchbot/aa:bb:cc:dd:ee:ff/set",
  47. {_MQTTTopicPlaceholder.MAC_ADDRESS: "aa:bb:cc:dd:ee:ff"},
  48. ),
  49. (
  50. "",
  51. ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
  52. "switchbot//set",
  53. {_MQTTTopicPlaceholder.MAC_ADDRESS: ""},
  54. ),
  55. (
  56. "",
  57. ["prefix", _MQTTTopicPlaceholder.MAC_ADDRESS],
  58. "prefix/aa:bb:cc:dd:ee:f1",
  59. {_MQTTTopicPlaceholder.MAC_ADDRESS: "aa:bb:cc:dd:ee:f1"},
  60. ),
  61. (
  62. "",
  63. [_MQTTTopicPlaceholder.MAC_ADDRESS],
  64. "00:11:22:33:44:55",
  65. {_MQTTTopicPlaceholder.MAC_ADDRESS: "00:11:22:33:44:55"},
  66. ),
  67. (
  68. "prefix/",
  69. [_MQTTTopicPlaceholder.MAC_ADDRESS],
  70. "prefix/aa:bb:cc:dd:ee:f2",
  71. {_MQTTTopicPlaceholder.MAC_ADDRESS: "aa:bb:cc:dd:ee:f2"},
  72. ),
  73. (
  74. "prefix-",
  75. ["test", _MQTTTopicPlaceholder.MAC_ADDRESS, "42"],
  76. "prefix-test/aa:bb:cc:dd:ee:f3/42",
  77. {_MQTTTopicPlaceholder.MAC_ADDRESS: "aa:bb:cc:dd:ee:f3"},
  78. ),
  79. ],
  80. )
  81. def test__parse_mqtt_topic(
  82. expected_prefix: str,
  83. expected_levels: typing.List[_MQTTTopicLevel],
  84. topic: str,
  85. expected_attrs: typing.Dict[_MQTTTopicPlaceholder, str],
  86. ) -> None:
  87. assert (
  88. _parse_mqtt_topic(
  89. topic=topic,
  90. expected_prefix=expected_prefix,
  91. expected_levels=expected_levels,
  92. )
  93. == expected_attrs
  94. )
  95. def test__parse_mqtt_topic_unexpected_prefix() -> None:
  96. with pytest.raises(
  97. ValueError,
  98. match=r"^expected topic prefix abcdefg/, got topic abcdef/aa:bb:cc:dd:ee:ff$",
  99. ):
  100. _parse_mqtt_topic(
  101. topic="abcdef/aa:bb:cc:dd:ee:ff",
  102. expected_prefix="abcdefg/",
  103. expected_levels=[_MQTTTopicPlaceholder.MAC_ADDRESS],
  104. )
  105. @pytest.mark.parametrize(
  106. ("expected_prefix", "expected_levels", "topic"),
  107. [
  108. (
  109. "",
  110. ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
  111. "switchbot/aa:bb:cc:dd:ee:ff",
  112. ),
  113. (
  114. "",
  115. ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
  116. "switchbot/aa:bb:cc:dd:ee:ff/change",
  117. ),
  118. (
  119. "prfx",
  120. ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
  121. "prfx/switchbot/aa:bb:cc:dd:ee:ff/set/suffix",
  122. ),
  123. ],
  124. )
  125. def test__parse_mqtt_topic_fail(
  126. expected_prefix: str, expected_levels: typing.List[_MQTTTopicLevel], topic: str
  127. ) -> None:
  128. with pytest.raises(ValueError):
  129. _parse_mqtt_topic(
  130. topic=topic,
  131. expected_prefix=expected_prefix,
  132. expected_levels=expected_levels,
  133. )