Merge branch 'ha_components'

This commit is contained in:
Nicu Hodos 2023-07-08 22:06:51 +02:00
commit 9767db6cce
3 changed files with 149 additions and 138 deletions

7
.gitignore vendored
View File

@ -1,6 +1,3 @@
.pio .pio/
.vscode/.browse.c_cpp.db* .vscode/
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
credentials.h credentials.h

View File

@ -1,50 +1,30 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#define JSON_SIZE 512 #define JSON_SIZE 512
#define TOPIC_SIZE 255
namespace Ha { namespace Ha {
struct SensorConfig { struct Component {
const char* name; const char* name;
const char* uniqueId; const char* id;
const char* stateTopic; char configTopic[TOPIC_SIZE];
const char* deviceClass;
const char* unitMeasure;
const char* valueTemplate;
};
struct TemperatureConfig : SensorConfig { Component(const char* name, const char* id, const char* type) {
TemperatureConfig(const char* name, const char* uniqueId, const char* stateTopic) {
this->name = name; this->name = name;
this->uniqueId = uniqueId; this->id = id;
this->stateTopic = stateTopic; sprintf(configTopic, "homeassistant/%s/esp_clock/%s/config", type, id);
this->deviceClass = "temperature";
this->unitMeasure = "°C";
this->valueTemplate = "{{ value_json.temperature }}";
} }
};
struct PressureConfig : SensorConfig { virtual void buildUniqueId(char* uniqueId) = 0;
PressureConfig(const char* name, const char* uniqueId, const char* stateTopic) {
this->name = name;
this->uniqueId = uniqueId;
this->stateTopic = stateTopic;
this->deviceClass = "pressure";
this->unitMeasure = "hPa";
this->valueTemplate = "{{ value_json.pressure }}";
}
};
struct AltitudeConfig : SensorConfig { virtual void buildConfig(JsonDocument& jsonDoc) {
AltitudeConfig(const char* name, const char* uniqueId, const char* stateTopic) { buildDeviceConfig(jsonDoc);
this->name = name; jsonDoc["name"] = name;
this->uniqueId = uniqueId; char uniqueId[50];
this->stateTopic = stateTopic; buildUniqueId(uniqueId);
this->deviceClass = "distance"; jsonDoc["unique_id"] = uniqueId;
this->unitMeasure = "m";
this->valueTemplate = "{{ value_json.altitude }}";
} }
};
void buildDeviceConfig(JsonDocument& jsonDoc) { void buildDeviceConfig(JsonDocument& jsonDoc) {
JsonObject device = jsonDoc.createNestedObject("device"); JsonObject device = jsonDoc.createNestedObject("device");
@ -55,43 +35,90 @@ namespace Ha {
mac.add("mac"); mac.add("mac");
mac.add(WiFi.macAddress()); mac.add(WiFi.macAddress());
} }
};
void buildSensorConfig(char(&output)[JSON_SIZE], SensorConfig config) { typedef void (*onMessage)(const char* msg);
StaticJsonDocument<JSON_SIZE> jsonDoc; struct Command : Component {
buildDeviceConfig(jsonDoc); char commandTopic[TOPIC_SIZE];
jsonDoc["device_class"] = config.deviceClass; onMessage f;
jsonDoc["name"] = config.name;
jsonDoc["unique_id"] = config.uniqueId; Command(const char* name, const char* id, const char* type, onMessage f) : Component(name, id, type) {
jsonDoc["unit_of_measurement"] = config.unitMeasure; this->f = f;
jsonDoc["state_topic"] = config.stateTopic;
jsonDoc["value_template"] = config.valueTemplate;
serializeJson(jsonDoc, output);
} }
void buildRestartConfig(char(&output)[JSON_SIZE], const char* topic) { void buildUniqueId(char* uniqueId) override {
StaticJsonDocument<JSON_SIZE> jsonDoc; sprintf(uniqueId, "esp_clock_%s", id);
buildDeviceConfig(jsonDoc);
jsonDoc["name"] = "Restart";
jsonDoc["unique_id"] = "esp_clock_restart";
jsonDoc["command_topic"] = topic;
serializeJson(jsonDoc, output);
} }
void buildLedConfig(char(&output)[JSON_SIZE], const char* topic) { void buildConfig(JsonDocument& jsonDoc) override {
StaticJsonDocument<JSON_SIZE> jsonDoc; Component::buildConfig(jsonDoc);
buildDeviceConfig(jsonDoc); jsonDoc["command_topic"] = commandTopic;
jsonDoc["name"] = "ESP Clock Led";
jsonDoc["unique_id"] = "esp_clock_led";
jsonDoc["command_topic"] = topic;
serializeJson(jsonDoc, output);
} }
void buildHourFormatConfig(char(&output)[JSON_SIZE], const char* topic) { };
StaticJsonDocument<JSON_SIZE> jsonDoc;
buildDeviceConfig(jsonDoc); struct Button : Command {
jsonDoc["name"] = "ESP Clock Format 24h"; static constexpr const char* type = "button";
jsonDoc["unique_id"] = "esp_clock_format_24h";
jsonDoc["command_topic"] = topic; Button(const char* name, const char* id, onMessage f) : Command(name, id, type, f) {
serializeJson(jsonDoc, output); sprintf(commandTopic, "homeassistant/%s/esp_clock/%s", type, id);
} }
};
struct Switch : Command {
static constexpr const char* type = "switch";
Switch(const char* name, const char* id, onMessage f) : Command(name, id, type, f) {
sprintf(commandTopic, "homeassistant/%s/esp_clock/%s/set", type, id);
}
};
struct Sensor : Component {
const char* deviceClass;
const char* unitMeasure;
const char* valueTemplate;
static constexpr const char* stateTopic = "homeassistant/sensor/esp_clock/state";
Sensor(const char* name, const char* id) : Component(name, id, "sensor") {
}
void buildUniqueId(char* uniqueId) override {
sprintf(uniqueId, "living_room_%s", id);
}
void buildConfig(JsonDocument& jsonDoc) override {
Component::buildConfig(jsonDoc);
jsonDoc["device_class"] = deviceClass;
jsonDoc["unit_of_measurement"] = unitMeasure;
jsonDoc["value_template"] = valueTemplate;
jsonDoc["state_topic"] = stateTopic;
}
};
struct TemperatureSensor : Sensor {
TemperatureSensor(const char* name, const char* id) : Sensor(name, id) {
deviceClass = "temperature";
unitMeasure = "°C";
valueTemplate = "{{ value_json.temperature }}";
}
};
struct PressureSensor : Sensor {
PressureSensor(const char* name, const char* id) : Sensor(name, id) {
deviceClass = "pressure";
unitMeasure = "hPa";
valueTemplate = "{{ value_json.pressure }}";
}
};
struct AltitudeSensor : Sensor {
AltitudeSensor(const char* name, const char* id) : Sensor(name, id) {
deviceClass = "distance";
unitMeasure = "m";
valueTemplate = "{{ value_json.altitude }}";
}
};
} }

View File

@ -34,10 +34,6 @@ namespace Mqtt {
} }
} commands; } commands;
const char* bmpTopic = "homeassistant/sensor/esp_clock/state";
const char* restartTopic = "homeassistant/button/esp_clock/restart";
const char* ledTopic = "homeassistant/switch/esp_clock/led/set";
const char* hourFormatTopic = "homeassistant/switch/esp_clock/hour_format/set";
const char* espClockTopic = "homeassistant/+/esp_clock/#"; const char* espClockTopic = "homeassistant/+/esp_clock/#";
void connect() { void connect() {
@ -49,49 +45,53 @@ namespace Mqtt {
client.disconnect(); client.disconnect();
} }
void publishTempConfig() { Ha::Sensor sensors[] = {
char message[JSON_SIZE]; Ha::TemperatureSensor{"Living room Temperature", "temperature"},
Ha::buildSensorConfig(message, Ha::TemperatureConfig{ "Living room Temperature", "living_room_temperature", bmpTopic }); Ha::PressureSensor{"Living room Pressure", "pressure"},
client.publish("homeassistant/sensor/esp_clock/temperature/config", 0, true, message); Ha::AltitudeSensor{"Living room Altitude", "altitude"}
} };
void publishPressureConfig() { Ha::Command switches[] = {
char message[JSON_SIZE]; Ha::Button{"ESP Clock Restart", "restart",
Ha::buildSensorConfig(message, Ha::PressureConfig{ "Living room Pressure", "living_room_pressure", bmpTopic }); [](const char* msg) {
client.publish("homeassistant/sensor/esp_clock/pressure/config", 0, true, message); if (String { "PRESS" }.equals(msg)) ESP.restart();
} }
},
void publishAltitudeConfig() { Ha::Switch{"ESP Clock Led", "led",
char message[JSON_SIZE]; [](const char* msg) {
Ha::buildSensorConfig(message, Ha::AltitudeConfig{ "Living room Altitude", "living_room_altitude", bmpTopic }); String{ "ON" }.equals(msg) ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH);
client.publish("homeassistant/sensor/esp_clock/altitude/config", 0, true, message);
} }
},
void publishRestartConfig() { Ha::Switch{"ESP Clock Format 24h", "format_24h",
char message[JSON_SIZE]; [](const char* msg) {
Ha::buildRestartConfig(message, restartTopic); if (String{ "ON" }.equals(msg)) {
client.publish("homeassistant/button/esp_clock/restart/config", 0, true, message); Display::hourFormat24 = true;
Display::drawTime();
} else {
Display::hourFormat24 = false;
Display::drawTime();
} }
void publishLedConfig() {
char message[JSON_SIZE];
Ha::buildLedConfig(message, ledTopic);
client.publish("homeassistant/switch/esp_clock/led/config", 0, true, message);
} }
}
};
void publishComponentConfig(Ha::Component& component) {
StaticJsonDocument<JSON_SIZE> jsonDoc;
component.buildConfig(jsonDoc);
void publishHourFormatConfig() {
char message[JSON_SIZE]; char message[JSON_SIZE];
Ha::buildHourFormatConfig(message, hourFormatTopic); serializeJson(jsonDoc, message);
client.publish("homeassistant/switch/esp_clock/hour_format/config", 0, true, message);
client.publish(component.configTopic, 0, true, message);
} }
void publishConfig() { void publishConfig() {
publishTempConfig(); for (Ha::Component& cmp : sensors) {
publishPressureConfig(); publishComponentConfig(cmp);
publishAltitudeConfig(); }
publishRestartConfig(); for (Ha::Component& cmp : switches) {
publishLedConfig(); publishComponentConfig(cmp);
publishHourFormatConfig(); }
ts.deleteTask(tPublishConfig); ts.deleteTask(tPublishConfig);
} }
@ -103,7 +103,7 @@ namespace Mqtt {
jsonDoc["altitude"] = Bmp::data.altitude; jsonDoc["altitude"] = Bmp::data.altitude;
char message[255]; char message[255];
serializeJson(jsonDoc, message); serializeJson(jsonDoc, message);
client.publish(bmpTopic, 0, true, message); client.publish(Ha::Sensor::stateTopic, 0, true, message);
} }
void publishCommand() { void publishCommand() {
@ -122,23 +122,10 @@ namespace Mqtt {
char msg[len + 1]; char msg[len + 1];
memcpy(msg, payload, len); memcpy(msg, payload, len);
msg[len] = 0; msg[len] = 0;
if (String{ restartTopic }.equals(topic) && String { "PRESS" }.equals(msg)) { for (Ha::Command cmd : switches) {
ESP.restart(); if (String{ cmd.commandTopic }.equals(topic)) {
} cmd.f(msg);
if (String{ ledTopic }.equals(topic)) { return;
if (String{ "ON" }.equals(msg)) {
digitalWrite(LED_BUILTIN, LOW);
} else {
digitalWrite(LED_BUILTIN, HIGH);
}
}
if (String{ hourFormatTopic }.equals(topic)) {
if (String{ "ON" }.equals(msg)) {
Display::hourFormat24 = true;
Display::drawTime();
} else {
Display::hourFormat24 = false;
Display::drawTime();
} }
} }
} }