test_utils.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_levels", "topic", "expected_attrs"),
  41. [
  42. (
  43. ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
  44. "switchbot/aa:bb:cc:dd:ee:ff/set",
  45. {_MQTTTopicPlaceholder.MAC_ADDRESS: "aa:bb:cc:dd:ee:ff"},
  46. ),
  47. (
  48. ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
  49. "switchbot//set",
  50. {_MQTTTopicPlaceholder.MAC_ADDRESS: ""},
  51. ),
  52. (
  53. ["prefix", _MQTTTopicPlaceholder.MAC_ADDRESS],
  54. "prefix/aa:bb:cc:dd:ee:ff",
  55. {_MQTTTopicPlaceholder.MAC_ADDRESS: "aa:bb:cc:dd:ee:ff"},
  56. ),
  57. (
  58. [_MQTTTopicPlaceholder.MAC_ADDRESS],
  59. "00:11:22:33:44:55",
  60. {_MQTTTopicPlaceholder.MAC_ADDRESS: "00:11:22:33:44:55"},
  61. ),
  62. ],
  63. )
  64. def test__parse_mqtt_topic(
  65. expected_levels: typing.List[_MQTTTopicLevel],
  66. topic: str,
  67. expected_attrs: typing.Dict[_MQTTTopicPlaceholder, str],
  68. ) -> None:
  69. assert (
  70. _parse_mqtt_topic(topic=topic, expected_levels=expected_levels)
  71. == expected_attrs
  72. )
  73. @pytest.mark.parametrize(
  74. ("expected_levels", "topic"),
  75. [
  76. (
  77. ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
  78. "switchbot/aa:bb:cc:dd:ee:ff",
  79. ),
  80. (
  81. ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
  82. "switchbot/aa:bb:cc:dd:ee:ff/change",
  83. ),
  84. (
  85. ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
  86. "switchbot/aa:bb:cc:dd:ee:ff/set/suffix",
  87. ),
  88. ],
  89. )
  90. def test__parse_mqtt_topic_fail(
  91. expected_levels: typing.List[_MQTTTopicLevel], topic: str
  92. ) -> None:
  93. with pytest.raises(ValueError):
  94. _parse_mqtt_topic(topic=topic, expected_levels=expected_levels)