HomeAssistantDiscoveryClient.cpp 4.7 KB

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