discovery_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. end
  7. before(:each) do
  8. mqtt_params = mqtt_parameters()
  9. @topic_prefix = mqtt_topic_prefix()
  10. @discovery_prefix = "#{@topic_prefix}/discovery"
  11. @client.put(
  12. '/settings',
  13. mqtt_params
  14. )
  15. @id_params = {
  16. id: @client.generate_id,
  17. type: 'rgb_cct',
  18. group_id: 1
  19. }
  20. @discovery_suffix = "#{@id_params[:type]}_#{sprintf("%x", @id_params[:id])}_#{@id_params[:group_id]}"
  21. @mqtt_client = create_mqtt_client()
  22. end
  23. context 'when not configured' do
  24. it 'should behave appropriately when MQTT is not configured' do
  25. @client.patch_settings(mqtt_server: '', home_assistant_discovery_prefix: '')
  26. expect { @client.get('/settings') }.to_not raise_error
  27. end
  28. it 'should behave appropriately when MQTT is configured, but discovery is not' do
  29. @client.patch_settings(mqtt_parameters().merge(home_assistant_discovery_prefix: ''))
  30. expect { @client.get('/settings') }.to_not raise_error
  31. end
  32. end
  33. context 'discovery topics' do
  34. it 'should send discovery messages' do
  35. saw_message = false
  36. @mqtt_client.on_message("#{@discovery_prefix}/light/+/#{@discovery_suffix}") do |topic, message|
  37. saw_message = true
  38. end
  39. @client.patch_settings(
  40. home_assistant_discovery_prefix: @discovery_prefix,
  41. group_id_aliases: {
  42. 'test_group' => [@id_params[:type], @id_params[:id], @id_params[:group_id]]
  43. }
  44. )
  45. expect(saw_message).to be_true
  46. end
  47. it 'config should have expected keys' do
  48. saw_message = false
  49. config = nil
  50. @mqtt_client.on_message("#{@discovery_prefix}/light/+/#{@discovery_suffix}") do |topic, message|
  51. config = JSON.parse(message)
  52. saw_message = true
  53. end
  54. @client.patch_settings(
  55. home_assistant_discovery_prefix: @discovery_prefix,
  56. group_id_aliases: {
  57. 'test_group' => [@id_params[:type], @id_params[:id], @id_params[:group_id]]
  58. }
  59. )
  60. expect(saw_message).to be_true
  61. expected_keys = %w(
  62. schema
  63. name
  64. command_topic
  65. state_topic
  66. availability_topic
  67. payload_available
  68. payload_not_available
  69. brightness
  70. rgb
  71. color_temp
  72. effect
  73. effect_list
  74. )
  75. expect(config.keys).to include(*expected_keys)
  76. end
  77. it 'should remove discoverable devices when alias is removed' do
  78. seen_config = false
  79. seen_blank_message = false
  80. @mqtt_client.on_message("#{@discovery_prefix}/light/+/#{@discovery_suffix}") do |topic, message|
  81. seen_config = message.length > 0
  82. seen_blank_message = message.empty?
  83. seen_config && seen_blank_message
  84. end
  85. # This should create the device
  86. @client.patch_settings(
  87. home_assistant_discovery_prefix: @discovery_prefix,
  88. group_id_aliases: {
  89. 'test_group' => [@id_params[:type], @id_params[:id], @id_params[:group_id]]
  90. }
  91. )
  92. # This should clear it
  93. @client.patch_settings(
  94. group_id_aliases: { }
  95. )
  96. end
  97. end
  98. end