diff --git a/.gitignore b/.gitignore index d7ee03c..2cb2faa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,3 @@ -.pio -.vscode/.browse.c_cpp.db* -.vscode/c_cpp_properties.json -.vscode/launch.json -.vscode/ipch +.pio/ +.vscode/ credentials.h diff --git a/include/ha.h b/include/ha.h index 9f3a5fa..3b46d0d 100644 --- a/include/ha.h +++ b/include/ha.h @@ -4,62 +4,86 @@ namespace Ha { - void buildDeviceConfig(JsonDocument& jsonDoc) { - JsonObject device = jsonDoc.createNestedObject("device"); - device["name"] = "ESP Clock"; - device["suggested_area"] = "Living room"; - JsonArray connections = device.createNestedArray("connections"); - JsonArray mac = connections.createNestedArray(); - mac.add("mac"); - mac.add(WiFi.macAddress()); - } + struct Component { + const char* name; + const char* uniqueId; + const char* configTopic; - void buildCommandConfig(char(&output)[JSON_SIZE], const char* name, const char* uniqueId, const char* commandTopic) { - StaticJsonDocument jsonDoc; - buildDeviceConfig(jsonDoc); - jsonDoc["name"] = name; - jsonDoc["unique_id"] = uniqueId; - jsonDoc["command_topic"] = commandTopic; - serializeJson(jsonDoc, output); - } + Component(const char* name, const char* uniqueId, const char* configTopic) { + this->name = name; + this->uniqueId = uniqueId; + this->configTopic = configTopic; + } - struct SensorConfig { - const char* deviceClass; - const char* unitMeasure; - const char* valueTemplate; - - void buildConfig(char(&output)[JSON_SIZE], const char* name, const char* uniqueId, const char* stateTopic) { - StaticJsonDocument jsonDoc; + virtual void buildConfig(JsonDocument& jsonDoc) { buildDeviceConfig(jsonDoc); jsonDoc["name"] = name; jsonDoc["unique_id"] = uniqueId; - jsonDoc["state_topic"] = stateTopic; - jsonDoc["device_class"] = deviceClass; - jsonDoc["unit_of_measurement"] = unitMeasure; - jsonDoc["value_template"] = valueTemplate; - serializeJson(jsonDoc, output); + } + + void buildDeviceConfig(JsonDocument& jsonDoc) { + JsonObject device = jsonDoc.createNestedObject("device"); + device["name"] = "ESP Clock"; + device["suggested_area"] = "Living room"; + JsonArray connections = device.createNestedArray("connections"); + JsonArray mac = connections.createNestedArray(); + mac.add("mac"); + mac.add(WiFi.macAddress()); + } + }; + + struct Command : Component { + const char* commandTopic; + + Command(const char* name, const char* uniqueId, const char* configTopic, const char* commandTopic) : Component(name, uniqueId, configTopic) { + this->commandTopic = commandTopic; + } + + void buildConfig(JsonDocument& jsonDoc) { + Component::buildConfig(jsonDoc); + jsonDoc["command_topic"] = commandTopic; } }; - struct TemperatureConfig : SensorConfig { - TemperatureConfig() { + struct Sensor : Component { + const char* deviceClass; + const char* unitMeasure; + const char* valueTemplate; + const char* stateTopic; + + Sensor(const char* name, const char* uniqueId, const char* configTopic, const char* stateTopic) : Component(name, uniqueId, configTopic) { + this->stateTopic = stateTopic; + } + + void buildConfig(JsonDocument& jsonDoc) { + 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* uniqueId, const char* configTopic, const char* stateTopic) : Sensor(name, uniqueId, configTopic, stateTopic) { deviceClass = "temperature"; unitMeasure = "°C"; valueTemplate = "{{ value_json.temperature }}"; } }; - struct PressureConfig : SensorConfig { - PressureConfig() { + struct PressureSensor : Sensor { + PressureSensor(const char* name, const char* uniqueId, const char* configTopic, const char* stateTopic) : Sensor(name, uniqueId, configTopic, stateTopic) { deviceClass = "pressure"; unitMeasure = "hPa"; valueTemplate = "{{ value_json.pressure }}"; } }; - struct AltitudeConfig : SensorConfig { - AltitudeConfig() { + struct AltitudeSensor : Sensor { + AltitudeSensor(const char* name, const char* uniqueId, const char* configTopic, const char* stateTopic) : Sensor(name, uniqueId, configTopic, stateTopic) { deviceClass = "distance"; unitMeasure = "m"; valueTemplate = "{{ value_json.altitude }}"; diff --git a/include/mqtt.h b/include/mqtt.h index 5d2c8f4..6ef9cd6 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -49,49 +49,29 @@ namespace Mqtt { client.disconnect(); } - void publishTempConfig() { - char message[JSON_SIZE]; - Ha::TemperatureConfig{}.buildConfig(message, "Living room Temperature", "living_room_temperature", bmpTopic); - client.publish("homeassistant/sensor/esp_clock/temperature/config", 0, true, message); - } + Ha::Component components[] = { + Ha::TemperatureSensor{"Living room Temperature", "living_room_temperature", "homeassistant/sensor/esp_clock/temperature/config", bmpTopic}, + Ha::PressureSensor{"Living room Pressure", "living_room_pressure", "homeassistant/sensor/esp_clock/pressure/config", bmpTopic}, + Ha::AltitudeSensor{"Living room Altitude", "living_room_altitude", "homeassistant/sensor/esp_clock/altitude/config", bmpTopic}, + Ha::Command{"Restart", "esp_clock_restart", "homeassistant/button/esp_clock/restart/config", restartTopic}, + Ha::Command{"ESP Clock Led", "esp_clock_led", "homeassistant/switch/esp_clock/led/config", ledTopic}, + Ha::Command{"ESP Clock Format 24h", "esp_clock_format_24h", "homeassistant/switch/esp_clock/hour_format/config", hourFormatTopic} + }; - void publishPressureConfig() { - char message[JSON_SIZE]; - Ha::PressureConfig{}.buildConfig(message, "Living room Pressure", "living_room_pressure", bmpTopic); - client.publish("homeassistant/sensor/esp_clock/pressure/config", 0, true, message); - } + void publishComponentConfig(Ha::Component& component) { + StaticJsonDocument jsonDoc; + component.buildConfig(jsonDoc); - void publishAltitudeConfig() { char message[JSON_SIZE]; - Ha::AltitudeConfig{}.buildConfig(message, "Living room Altitude", "living_room_altitude", bmpTopic); - client.publish("homeassistant/sensor/esp_clock/altitude/config", 0, true, message); - } + serializeJson(jsonDoc, message); - void publishRestartConfig() { - char message[JSON_SIZE]; - Ha::buildCommandConfig(message, "Restart", "esp_clock_restart", restartTopic); - client.publish("homeassistant/button/esp_clock/restart/config", 0, true, message); - } - - void publishLedConfig() { - char message[JSON_SIZE]; - Ha::buildCommandConfig(message, "ESP Clock Led", "esp_clock_led", ledTopic); - client.publish("homeassistant/switch/esp_clock/led/config", 0, true, message); - } - - void publishHourFormatConfig() { - char message[JSON_SIZE]; - Ha::buildCommandConfig(message, "ESP Clock Format 24h", "esp_clock_format_24h", hourFormatTopic); - client.publish("homeassistant/switch/esp_clock/hour_format/config", 0, true, message); + client.publish(component.configTopic, 0, true, message); } void publishConfig() { - publishTempConfig(); - publishPressureConfig(); - publishAltitudeConfig(); - publishRestartConfig(); - publishLedConfig(); - publishHourFormatConfig(); + for (Ha::Component cmp : components) { + publishComponentConfig(cmp); + } ts.deleteTask(tPublishConfig); }