1
0

test_utils.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 pytest
  19. from switchbot_mqtt._utils import (
  20. _mac_address_valid,
  21. _MQTTTopicPlaceholder,
  22. _parse_mqtt_topic,
  23. )
  24. @pytest.mark.parametrize(
  25. ("mac_address", "valid"),
  26. [
  27. ("aa:bb:cc:dd:ee:ff", True),
  28. ("AA:BB:CC:DD:EE:FF", True),
  29. ("AA:12:34:45:67:89", True),
  30. ("aabbccddeeff", False), # not supported by PySwitchbot
  31. ("aa:bb:cc:dd:ee:gg", False),
  32. ],
  33. )
  34. def test__mac_address_valid(mac_address, valid):
  35. # pylint: disable=protected-access
  36. assert _mac_address_valid(mac_address) == valid
  37. @pytest.mark.parametrize(
  38. ("expected_levels", "topic", "expected_attrs"),
  39. [
  40. (
  41. ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
  42. "switchbot/aa:bb:cc:dd:ee:ff/set",
  43. {_MQTTTopicPlaceholder.MAC_ADDRESS: "aa:bb:cc:dd:ee:ff"},
  44. ),
  45. (
  46. ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
  47. "switchbot//set",
  48. {_MQTTTopicPlaceholder.MAC_ADDRESS: ""},
  49. ),
  50. (
  51. ["prefix", _MQTTTopicPlaceholder.MAC_ADDRESS],
  52. "prefix/aa:bb:cc:dd:ee:ff",
  53. {_MQTTTopicPlaceholder.MAC_ADDRESS: "aa:bb:cc:dd:ee:ff"},
  54. ),
  55. (
  56. [_MQTTTopicPlaceholder.MAC_ADDRESS],
  57. "00:11:22:33:44:55",
  58. {_MQTTTopicPlaceholder.MAC_ADDRESS: "00:11:22:33:44:55"},
  59. ),
  60. ],
  61. )
  62. def test__parse_mqtt_topic(expected_levels, topic, expected_attrs):
  63. assert (
  64. _parse_mqtt_topic(topic=topic, expected_levels=expected_levels)
  65. == expected_attrs
  66. )
  67. @pytest.mark.parametrize(
  68. ("expected_levels", "topic"),
  69. [
  70. (
  71. ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
  72. "switchbot/aa:bb:cc:dd:ee:ff",
  73. ),
  74. (
  75. ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
  76. "switchbot/aa:bb:cc:dd:ee:ff/change",
  77. ),
  78. (
  79. ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
  80. "switchbot/aa:bb:cc:dd:ee:ff/set/suffix",
  81. ),
  82. ],
  83. )
  84. def test__parse_mqtt_topic_fail(expected_levels, topic):
  85. with pytest.raises(ValueError):
  86. _parse_mqtt_topic(topic=topic, expected_levels=expected_levels)