HomeAssistantDiscoveryClient.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <HomeAssistantDiscoveryClient.h>
  2. HomeAssistantDiscoveryClient::HomeAssistantDiscoveryClient(Settings& settings, MqttClient* mqttClient)
  3. : settings(settings)
  4. , mqttClient(mqttClient)
  5. { }
  6. void HomeAssistantDiscoveryClient::sendDiscoverableDevices(const std::map<String, BulbId>& aliases) {
  7. #ifdef MQTT_DEBUG
  8. Serial.println(F("HomeAssistantDiscoveryClient: Sending discoverable devices..."));
  9. #endif
  10. for (auto itr = aliases.begin(); itr != aliases.end(); ++itr) {
  11. addConfig(itr->first.c_str(), itr->second);
  12. }
  13. }
  14. void HomeAssistantDiscoveryClient::removeOldDevices(const std::map<uint32_t, BulbId>& aliases) {
  15. #ifdef MQTT_DEBUG
  16. Serial.println(F("HomeAssistantDiscoveryClient: Removing discoverable devices..."));
  17. #endif
  18. for (auto itr = aliases.begin(); itr != aliases.end(); ++itr) {
  19. removeConfig(itr->second);
  20. }
  21. }
  22. void HomeAssistantDiscoveryClient::removeConfig(const BulbId& bulbId) {
  23. // Remove by publishing an empty message
  24. String topic = buildTopic(bulbId);
  25. mqttClient->send(topic.c_str(), "", true);
  26. }
  27. void HomeAssistantDiscoveryClient::addConfig(const char* alias, const BulbId& bulbId) {
  28. String topic = buildTopic(bulbId);
  29. DynamicJsonDocument config(1024);
  30. config[F("schema")] = F("json");
  31. config[F("name")] = alias;
  32. config[F("command_topic")] = bindTopicVariables(settings.mqttTopicPattern, alias, bulbId);
  33. config[F("state_topic")] = bindTopicVariables(settings.mqttStateTopicPattern, alias, bulbId);
  34. // HomeAssistant only supports simple client availability
  35. if (settings.mqttClientStatusTopic.length() > 0 && settings.simpleMqttClientStatus) {
  36. config[F("availability_topic")] = settings.mqttClientStatusTopic;
  37. config[F("payload_available")] = F("connected");
  38. config[F("payload_not_available")] = F("disconnected");
  39. }
  40. // Configure supported commands based on the bulb type
  41. // All supported bulbs support brightness and night mode
  42. config[F("brightness")] = true;
  43. config[F("effect")] = true;
  44. JsonArray effects = config.createNestedArray(F("effect_list"));
  45. effects.add(F("night_mode"));
  46. // These bulbs support RGB color
  47. switch (bulbId.deviceType) {
  48. case REMOTE_TYPE_FUT089:
  49. case REMOTE_TYPE_RGB:
  50. case REMOTE_TYPE_RGB_CCT:
  51. case REMOTE_TYPE_RGBW:
  52. config[F("rgb")] = true;
  53. break;
  54. default:
  55. break; //nothing
  56. }
  57. // These bulbs support adjustable white values
  58. switch (bulbId.deviceType) {
  59. case REMOTE_TYPE_CCT:
  60. case REMOTE_TYPE_FUT089:
  61. case REMOTE_TYPE_FUT091:
  62. case REMOTE_TYPE_RGB_CCT:
  63. config[F("color_temp")] = true;
  64. break;
  65. default:
  66. break; //nothing
  67. }
  68. // These bulbs support switching between rgb/white, and have a "white_mode" command
  69. switch (bulbId.deviceType) {
  70. case REMOTE_TYPE_FUT089:
  71. case REMOTE_TYPE_RGB_CCT:
  72. case REMOTE_TYPE_RGBW:
  73. effects.add(F("white_mode"));
  74. break;
  75. default:
  76. break; //nothing
  77. }
  78. String message;
  79. serializeJson(config, message);
  80. #ifdef MQTT_DEBUG
  81. Serial.printf_P(PSTR("HomeAssistantDiscoveryClient: adding discoverable device: %s...\n"), alias);
  82. Serial.printf_P(PSTR(" topic: %s\nconfig: %s\n"), topic.c_str(), message.c_str());
  83. #endif
  84. mqttClient->send(topic.c_str(), message.c_str(), true);
  85. }
  86. // Topic syntax:
  87. // <discovery_prefix>/<component>/[<node_id>/]<object_id>/config
  88. //
  89. // source: https://www.home-assistant.io/docs/mqtt/discovery/
  90. String HomeAssistantDiscoveryClient::buildTopic(const BulbId& bulbId) {
  91. String topic = settings.homeAssistantDiscoveryPrefix;
  92. // Don't require the user to entier a "/" (or break things if they do)
  93. if (! topic.endsWith("/")) {
  94. topic += "/";
  95. }
  96. topic += "light/";
  97. // Use a static ID that doesn't depend on configuration.
  98. topic += "milight_hub_" + String(ESP.getChipId());
  99. // make the object ID based on the actual parameters rather than the alias.
  100. topic += "/";
  101. topic += MiLightRemoteTypeHelpers::remoteTypeToString(bulbId.deviceType);
  102. topic += "_0x";
  103. topic += String(bulbId.deviceId, HEX);
  104. topic += "_";
  105. topic += bulbId.groupId;
  106. topic += "/config";
  107. return topic;
  108. }
  109. String HomeAssistantDiscoveryClient::bindTopicVariables(const String& topic, const char* alias, const BulbId& bulbId) {
  110. String boundTopic = topic;
  111. boundTopic.replace(":device_alias", alias);
  112. boundTopic.replace(":device_id", String("0x") + String(bulbId.deviceId, HEX));
  113. boundTopic.replace(":hex_device_id", String("0x") + String(bulbId.deviceId, HEX));
  114. boundTopic.replace(":dec_device_id", String(bulbId.deviceId));
  115. boundTopic.replace(":device_type", MiLightRemoteTypeHelpers::remoteTypeToString(bulbId.deviceType));
  116. boundTopic.replace(":group_id", String(bulbId.groupId));
  117. return boundTopic;
  118. }