test_utils.py 4.3 KB

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