test_homeassistant.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import json
  2. import logging
  3. import unittest.mock
  4. import pytest
  5. import intertechno_cc1101_mqtt
  6. import intertechno_cc1101_mqtt._homeassistant
  7. # pylint: disable=protected-access
  8. # https://github.com/fphammerle/wireless-sensor-mqtt/blob/v0.3.0/tests/test_homeassistant.py
  9. @pytest.mark.parametrize(
  10. ("object_id", "valid"),
  11. [
  12. ("raspberrypi", True),
  13. ("da-sh", True),
  14. ("under_score", True),
  15. ('" or ""="', False),
  16. ("", False),
  17. ],
  18. )
  19. def test_validate_object_id(object_id, valid):
  20. assert intertechno_cc1101_mqtt._homeassistant.validate_object_id(object_id) == valid
  21. @pytest.mark.parametrize(
  22. ("aliases", "discovery_topics", "unique_ids", "command_topics"),
  23. (
  24. ({}, [], [], []),
  25. (
  26. {
  27. "some-name": {"address": 12345678, "button-index": 0},
  28. "another-name": {"address": 12345678, "button-index": 1},
  29. },
  30. [
  31. "homeassistant/switch/some-name/config",
  32. "homeassistant/switch/another-name/config",
  33. ],
  34. [
  35. "intertechno-cc1101-mqtt/aliases/some-name",
  36. "intertechno-cc1101-mqtt/aliases/another-name",
  37. ],
  38. ["intertechno-cc1101/some-name/set", "intertechno-cc1101/another-name/set"],
  39. ),
  40. ),
  41. )
  42. def test__publish_homeassistant_discovery_configs(
  43. caplog, aliases, discovery_topics, unique_ids, command_topics
  44. ):
  45. mqtt_client = unittest.mock.MagicMock()
  46. with caplog.at_level(logging.DEBUG):
  47. intertechno_cc1101_mqtt._publish_homeassistant_discovery_configs(
  48. mqtt_client=mqtt_client, aliases=aliases
  49. )
  50. assert mqtt_client.publish.call_count == len(aliases)
  51. assert all(
  52. not call.args and set(call.kwargs.keys()) == {"topic", "payload", "retain"}
  53. for call in mqtt_client.publish.call_args_list
  54. )
  55. assert [
  56. call.kwargs["topic"] for call in mqtt_client.publish.call_args_list
  57. ] == discovery_topics
  58. assert all(
  59. call.kwargs["retain"] is True for call in mqtt_client.publish.call_args_list
  60. )
  61. configs = [
  62. json.loads(call.kwargs["payload"])
  63. for call in mqtt_client.publish.call_args_list
  64. ]
  65. assert all(
  66. set(c.keys())
  67. == {"unique_id", "command_topic", "payload_on", "payload_off", "retain", "name"}
  68. for c in configs
  69. )
  70. assert all(
  71. c["payload_on"] == "ON" and c["payload_off"] == "OFF" and c["retain"] == "true"
  72. for c in configs
  73. )
  74. assert [c["unique_id"] for c in configs] == unique_ids
  75. assert [c["command_topic"] for c in configs] == command_topics
  76. assert [c["name"] for c in configs] == list(aliases.keys())
  77. assert caplog.record_tuples == [
  78. (
  79. "intertechno_cc1101_mqtt",
  80. logging.DEBUG,
  81. "publishing home assistant discovery config on topic {}".format(topic),
  82. )
  83. for topic in discovery_topics
  84. ]
  85. def test__publish_homeassistant_discovery_configs_invalid_object_id(caplog):
  86. mqtt_client = unittest.mock.MagicMock()
  87. with caplog.at_level(logging.DEBUG):
  88. intertechno_cc1101_mqtt._publish_homeassistant_discovery_configs(
  89. mqtt_client=mqtt_client,
  90. aliases={
  91. "": {"address": 12345678, "button-index": 0},
  92. "invalid/alias": {"address": 12345678, "button-index": 0},
  93. "valid-alias": {"address": 12345678, "button-index": 0},
  94. },
  95. )
  96. assert mqtt_client.publish.call_count == 1
  97. assert (
  98. mqtt_client.publish.call_args.kwargs["topic"]
  99. == "homeassistant/switch/valid-alias/config"
  100. )
  101. assert caplog.record_tuples == [
  102. (
  103. "intertechno_cc1101_mqtt",
  104. logging.WARNING,
  105. "empty alias; skipping publishing of discovery config for home assistant",
  106. ),
  107. (
  108. "intertechno_cc1101_mqtt",
  109. logging.WARNING,
  110. "alias 'invalid/alias' contains characters unsupported by home assistant"
  111. " (supported characters: a-zA-Z0-9_-); skipping publishing of discovery config",
  112. ),
  113. (
  114. "intertechno_cc1101_mqtt",
  115. logging.DEBUG,
  116. "publishing home assistant discovery config"
  117. " on topic homeassistant/switch/valid-alias/config",
  118. ),
  119. ]