discovery_spec.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. require 'api_client'
  2. RSpec.describe 'MQTT Discovery' do
  3. before(:all) do
  4. @client = ApiClient.new(ENV.fetch('ESPMH_HOSTNAME'), ENV.fetch('ESPMH_TEST_DEVICE_ID_BASE'))
  5. @client.upload_json('/settings', 'settings.json')
  6. @test_id = 1
  7. @topic_prefix = mqtt_topic_prefix()
  8. @discovery_prefix = "#{@topic_prefix}discovery/"
  9. @mqtt_client = create_mqtt_client()
  10. end
  11. after(:all) do
  12. # Clean up any leftover cruft
  13. @mqtt_client.on_message("#{@discovery_prefix}#", 1, false) do |topic, message|
  14. if message.length > 0
  15. @mqtt_client.publish(topic, '', true)
  16. end
  17. false
  18. end
  19. @mqtt_client.wait_for_listeners
  20. end
  21. before(:each) do
  22. mqtt_params = mqtt_parameters()
  23. @client.put(
  24. '/settings',
  25. mqtt_params
  26. )
  27. @id_params = {
  28. id: @client.generate_id,
  29. type: 'rgb_cct',
  30. group_id: 1
  31. }
  32. @discovery_suffix = "#{@id_params[:type]}_#{sprintf("0x%04x", @id_params[:id])}_#{@id_params[:group_id]}/config"
  33. @test_discovery_prefix = "#{@discovery_prefix}#{@id_params[:id]}/"
  34. end
  35. context 'when not configured' do
  36. it 'should behave appropriately when MQTT is not configured' do
  37. @client.patch_settings(mqtt_server: '', home_assistant_discovery_prefix: '')
  38. expect { @client.get('/settings') }.to_not raise_error
  39. end
  40. it 'should behave appropriately when MQTT is configured, but discovery is not' do
  41. @client.patch_settings(mqtt_parameters().merge(home_assistant_discovery_prefix: ''))
  42. expect { @client.get('/settings') }.to_not raise_error
  43. end
  44. end
  45. context 'discovery topics' do
  46. it 'should send discovery messages' do
  47. saw_message = false
  48. @mqtt_client.on_message("#{@test_discovery_prefix}light/+/#{@discovery_suffix}") do |topic, message|
  49. saw_message = true
  50. end
  51. @client.patch_settings(
  52. home_assistant_discovery_prefix: @test_discovery_prefix,
  53. group_id_aliases: {
  54. 'test_group' => [@id_params[:type], @id_params[:id], @id_params[:group_id]]
  55. }
  56. )
  57. @mqtt_client.wait_for_listeners
  58. expect(saw_message).to be(true)
  59. end
  60. it 'config should have expected keys' do
  61. saw_message = false
  62. config = nil
  63. @mqtt_client.on_message("#{@test_discovery_prefix}light/+/#{@discovery_suffix}") do |topic, message|
  64. config = JSON.parse(message)
  65. saw_message = true
  66. end
  67. @client.patch_settings(
  68. home_assistant_discovery_prefix: @test_discovery_prefix,
  69. group_id_aliases: {
  70. 'test_group' => [@id_params[:type], @id_params[:id], @id_params[:group_id]]
  71. }
  72. )
  73. @mqtt_client.wait_for_listeners
  74. expect(saw_message).to be(true)
  75. expected_keys = %w(
  76. schema
  77. name
  78. command_topic
  79. state_topic
  80. brightness
  81. rgb
  82. color_temp
  83. effect
  84. effect_list
  85. )
  86. expect(config.keys).to include(*expected_keys)
  87. end
  88. it 'should remove discoverable devices when alias is removed' do
  89. seen_config = false
  90. seen_blank_message = false
  91. @mqtt_client.on_message("#{@test_discovery_prefix}light/+/#{@discovery_suffix}") do |topic, message|
  92. seen_config = seen_config || message.length > 0
  93. seen_blank_message = seen_blank_message || message.length == 0
  94. seen_config && seen_blank_message
  95. end
  96. # This should create the device
  97. @client.patch_settings(
  98. home_assistant_discovery_prefix: @test_discovery_prefix,
  99. group_id_aliases: {
  100. 'test_group' => [@id_params[:type], @id_params[:id], @id_params[:group_id]]
  101. }
  102. )
  103. # This should clear it
  104. @client.patch_settings(
  105. group_id_aliases: { }
  106. )
  107. @mqtt_client.wait_for_listeners
  108. expect(seen_config).to be(true)
  109. expect(seen_blank_message).to be(true), "should see deletion message"
  110. end
  111. it 'should configure devices with an availability topic if client status is configured' do
  112. expected_keys = %w(
  113. availability_topic
  114. payload_available
  115. payload_not_available
  116. )
  117. config = nil
  118. @mqtt_client.on_message("#{@test_discovery_prefix}light/+/#{@discovery_suffix}") do |topic, message|
  119. config = JSON.parse(message)
  120. (expected_keys - config.keys).empty?
  121. end
  122. # This should create the device
  123. @client.patch_settings(
  124. home_assistant_discovery_prefix: @test_discovery_prefix,
  125. group_id_aliases: {
  126. 'test_group' => [@id_params[:type], @id_params[:id], @id_params[:group_id]]
  127. },
  128. mqtt_client_status_topic: "#{@topic_prefix}status",
  129. simple_mqtt_client_status: true
  130. )
  131. @mqtt_client.wait_for_listeners
  132. expect(config.keys).to include(*expected_keys)
  133. end
  134. end
  135. end