HomeAssistantDiscoveryClient.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. JsonObject deviceMetadata = config.createNestedObject(F("device"));
  36. deviceMetadata[F("manufacturer")] = F("esp8266_milight_hub");
  37. deviceMetadata[F("sw_version")] = QUOTE(MILIGHT_HUB_VERSION);
  38. JsonArray identifiers = deviceMetadata.createNestedArray(F("identifiers"));
  39. identifiers.add(ESP.getChipId());
  40. bulbId.serialize(identifiers);
  41. // HomeAssistant only supports simple client availability
  42. if (settings.mqttClientStatusTopic.length() > 0 && settings.simpleMqttClientStatus) {
  43. config[F("availability_topic")] = settings.mqttClientStatusTopic;
  44. config[F("payload_available")] = F("connected");
  45. config[F("payload_not_available")] = F("disconnected");
  46. }
  47. // Configure supported commands based on the bulb type
  48. // All supported bulbs support brightness and night mode
  49. config[GroupStateFieldNames::BRIGHTNESS] = true;
  50. config[GroupStateFieldNames::EFFECT] = true;
  51. JsonArray effects = config.createNestedArray(F("effect_list"));
  52. effects.add(MiLightCommandNames::NIGHT_MODE);
  53. // These bulbs support RGB color
  54. switch (bulbId.deviceType) {
  55. case REMOTE_TYPE_FUT089:
  56. case REMOTE_TYPE_RGB:
  57. case REMOTE_TYPE_RGB_CCT:
  58. case REMOTE_TYPE_RGBW:
  59. config[F("rgb")] = true;
  60. break;
  61. default:
  62. break; //nothing
  63. }
  64. // These bulbs support adjustable white values
  65. switch (bulbId.deviceType) {
  66. case REMOTE_TYPE_CCT:
  67. case REMOTE_TYPE_FUT089:
  68. case REMOTE_TYPE_FUT091:
  69. case REMOTE_TYPE_RGB_CCT:
  70. config[GroupStateFieldNames::COLOR_TEMP] = true;
  71. break;
  72. default:
  73. break; //nothing
  74. }
  75. // These bulbs support switching between rgb/white, and have a "white_mode" command
  76. switch (bulbId.deviceType) {
  77. case REMOTE_TYPE_FUT089:
  78. case REMOTE_TYPE_RGB_CCT:
  79. case REMOTE_TYPE_RGBW:
  80. effects.add("white_mode");
  81. break;
  82. default:
  83. break; //nothing
  84. }
  85. String message;
  86. serializeJson(config, message);
  87. #ifdef MQTT_DEBUG
  88. Serial.printf_P(PSTR("HomeAssistantDiscoveryClient: adding discoverable device: %s...\n"), alias);
  89. Serial.printf_P(PSTR(" topic: %s\nconfig: %s\n"), topic.c_str(), message.c_str());
  90. #endif
  91. mqttClient->send(topic.c_str(), message.c_str(), true);
  92. }
  93. // Topic syntax:
  94. // <discovery_prefix>/<component>/[<node_id>/]<object_id>/config
  95. //
  96. // source: https://www.home-assistant.io/docs/mqtt/discovery/
  97. String HomeAssistantDiscoveryClient::buildTopic(const BulbId& bulbId) {
  98. String topic = settings.homeAssistantDiscoveryPrefix;
  99. // Don't require the user to entier a "/" (or break things if they do)
  100. if (! topic.endsWith("/")) {
  101. topic += "/";
  102. }
  103. topic += "light/";
  104. // Use a static ID that doesn't depend on configuration.
  105. topic += "milight_hub_" + String(ESP.getChipId());
  106. // make the object ID based on the actual parameters rather than the alias.
  107. topic += "/";
  108. topic += MiLightRemoteTypeHelpers::remoteTypeToString(bulbId.deviceType);
  109. topic += "_";
  110. topic += bulbId.getHexDeviceId();
  111. topic += "_";
  112. topic += bulbId.groupId;
  113. topic += "/config";
  114. return topic;
  115. }
  116. String HomeAssistantDiscoveryClient::bindTopicVariables(const String& topic, const char* alias, const BulbId& bulbId) {
  117. String boundTopic = topic;
  118. String hexDeviceId = bulbId.getHexDeviceId();
  119. boundTopic.replace(":device_alias", alias);
  120. boundTopic.replace(":device_id", hexDeviceId);
  121. boundTopic.replace(":hex_device_id", hexDeviceId);
  122. boundTopic.replace(":dec_device_id", String(bulbId.deviceId));
  123. boundTopic.replace(":device_type", MiLightRemoteTypeHelpers::remoteTypeToString(bulbId.deviceType));
  124. boundTopic.replace(":group_id", String(bulbId.groupId));
  125. return boundTopic;
  126. }