Extract Home Assistant discovery logic
This commit is contained in:
84
src/main.cpp
84
src/main.cpp
@@ -5,6 +5,7 @@
|
||||
#include <RFPowerView.h>
|
||||
#include "Shade.h"
|
||||
#include "ShadeRepository.h"
|
||||
#include "HADiscovery.h"
|
||||
#include "secrets.h"
|
||||
|
||||
#define SER_BAUDRATE (115200)
|
||||
@@ -30,6 +31,10 @@ EspMQTTClient client(
|
||||
|
||||
ShadeRepository shadeRepository = ShadeRepository();
|
||||
|
||||
HADiscovery haDiscovery("hotdog/", [] (const char* topic, const char* message) {
|
||||
client.publish(topic, message);
|
||||
});
|
||||
|
||||
#define MAX_FETCH_COUNT (20)
|
||||
auto timer = Timer<10, millis, uint16_t>();
|
||||
|
||||
@@ -377,85 +382,8 @@ void publishBattery(const String& shadeName, const uint8_t battery) {
|
||||
client.publish("hotdog/" + shadeName + "/battery", String(battery), true);
|
||||
}
|
||||
|
||||
char jsonBuffer[1024];
|
||||
|
||||
void addDeviceObject(JsonDocument &doc, const Shade& shade) {
|
||||
String deviceID = "hotdog-" + String(shade.ID, HEX);
|
||||
|
||||
JsonObject device = doc["device"].to<JsonObject>();
|
||||
device["name"] = shade.friendlyName;
|
||||
JsonArray identifiers = device["identifiers"].to<JsonArray>();
|
||||
identifiers.add(deviceID);
|
||||
device["manufacturer"] = "Hunter Douglas";
|
||||
// TODO: Add fields like sw_version and model
|
||||
}
|
||||
|
||||
void publishCoverDiscoveryTopic(const Shade& shade) {
|
||||
String objectID = String(shade.ID, HEX);
|
||||
String entityID = "cover-" + objectID;
|
||||
|
||||
JsonDocument doc;
|
||||
|
||||
doc["name"] = nullptr;
|
||||
doc["unique_id"] = entityID;
|
||||
doc["availability_topic"] = "hotdog/availability";
|
||||
doc["state_topic"] = "hotdog/" + shade.key + "/state";
|
||||
doc["command_topic"] = "hotdog/" + shade.key + "/command";
|
||||
doc["position_topic"] = "hotdog/" + shade.key + "/position";
|
||||
doc["set_position_topic"] = "hotdog/" + shade.key + "/set_position";
|
||||
doc["position_open"] = 100;
|
||||
doc["position_closed"] = 0;
|
||||
doc["optimistic"] = false;
|
||||
|
||||
addDeviceObject(doc, shade);
|
||||
|
||||
serializeJson(doc, jsonBuffer);
|
||||
|
||||
client.publish("homeassistant/cover/" + objectID + "/config", jsonBuffer);
|
||||
}
|
||||
|
||||
void publishBatteryDiscoveryTopic(const Shade& shade) {
|
||||
String objectID = String(shade.ID, HEX);
|
||||
String entityID = "battery-" + objectID;
|
||||
|
||||
JsonDocument doc;
|
||||
|
||||
doc["name"] = "Battery";
|
||||
doc["unique_id"] = entityID;
|
||||
doc["availability_topic"] = "hotdog/availability";
|
||||
doc["device_class"] = "battery";
|
||||
doc["state_topic"] = "hotdog/" + shade.key + "/battery";
|
||||
|
||||
addDeviceObject(doc, shade);
|
||||
|
||||
serializeJson(doc, jsonBuffer);
|
||||
|
||||
client.publish("homeassistant/sensor/" + objectID + "/config", jsonBuffer);
|
||||
}
|
||||
|
||||
void publishRefreshButtonDiscoveryTopic(const Shade& shade) {
|
||||
String objectID = String(shade.ID, HEX);
|
||||
String entityID = "refresh-button-" + objectID;
|
||||
|
||||
JsonDocument doc;
|
||||
|
||||
doc["name"] = "Refresh";
|
||||
doc["unique_id"] = entityID;
|
||||
doc["availability_topic"] = "hotdog/availability";
|
||||
doc["command_topic"] = "hotdog/" + shade.key + "/command";
|
||||
doc["payload_press"] = "REFRESH";
|
||||
|
||||
addDeviceObject(doc, shade);
|
||||
|
||||
serializeJson(doc, jsonBuffer);
|
||||
|
||||
client.publish("homeassistant/button/" + objectID + "/config", jsonBuffer);
|
||||
}
|
||||
|
||||
void publishDiscoveryTopics() {
|
||||
for (auto shade = shadeRepository.begin(); shade != shadeRepository.end(); shade++) {
|
||||
publishCoverDiscoveryTopic(*shade);
|
||||
publishBatteryDiscoveryTopic(*shade);
|
||||
publishRefreshButtonDiscoveryTopic(*shade);
|
||||
haDiscovery.publish(*shade);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user