109 lines
4.0 KiB
C++
109 lines
4.0 KiB
C++
#pragma once
|
|
|
|
#define SENSOR_ID "bme280"
|
|
|
|
#include "ha.h"
|
|
#include "esp.h"
|
|
#include "display.h"
|
|
|
|
using namespace Ha;
|
|
|
|
namespace Devices {
|
|
|
|
auto espClockDevice = &DeviceConfig::create(MAIN_DEVICE_ID)
|
|
.withName("ESP Clock")
|
|
.withManufacturer("Espressif")
|
|
.withModel("Esp8266 D1 Mini")
|
|
.withArea("Living room");
|
|
|
|
Sensor* sensor = Builder<TemperatureSensor>::instance(SENSOR_ID)
|
|
.withValueTemplate("{{ value_json.temperature }}")
|
|
.withPrecision(1)
|
|
.asDevice(&DeviceConfig::create("esp-clock-living-room")
|
|
.withName("Living room")
|
|
.withModel("BPE280")
|
|
.withArea("Living room")
|
|
.withParent(espClockDevice)
|
|
)
|
|
.addSecondary(Builder<HumiditySensor>::instance(SENSOR_ID).withValueTemplate("{{ value_json.humidity }}").withPrecision(1).build())
|
|
.addSecondary(Builder<PressureSensor>::instance(SENSOR_ID).withValueTemplate("{{ value_json.pressure }}").withPrecision(1).build())
|
|
.build();
|
|
|
|
auto ledMqtt = Builder<Switch>::instance(new Switch{ "Led", "led",
|
|
[](const char* msg) {
|
|
turnLed(strcmp("ON", msg) == 0);
|
|
}
|
|
}).withStateTopic().restoreFromState().build();
|
|
|
|
auto brightnessMqtt = Builder<Number>::instance(new Number{ "Brightness", "brightness",
|
|
[](const char* msg) {
|
|
Display::Brightness::set(String{ msg }.toInt());
|
|
}
|
|
}).withMin(BRIGHTNESS_MIN).withMax(BRIGHTNESS_MAX).withStep(BRIGHTNESS_STEP).withStateTopic().restoreFromState().build();
|
|
|
|
auto hourFormatMqtt = Builder<Switch>::instance(new Switch{ "Format 24h", "format_24h",
|
|
[](const char* msg) {
|
|
strcmp("ON", msg) == 0 ? Display::changeHourFormat24(true) : Display::changeHourFormat24(false);
|
|
}
|
|
}).withStateTopic().restoreFromState().build();
|
|
|
|
auto button =
|
|
Builder<Button>::instance(new Button{"Restart", "restart",
|
|
[](const char* msg) {
|
|
if (strcmp("PRESS", msg) == 0) ESP.restart();
|
|
}
|
|
})
|
|
.asDevice(espClockDevice)
|
|
.addSecondary(
|
|
Builder<Button>::instance(new Button{"Display sensor data", "display_sensor_data",
|
|
[](const char* msg) {
|
|
if (strcmp("PRESS", msg) == 0 && !Display::tDisplaySensor.isEnabled()) {
|
|
Bme::data.readAll();
|
|
Display::tDisplaySensor.setIterations(DISPLAY_SENSOR_ITERATIONS);
|
|
Display::tDisplaySensor.restart();
|
|
};
|
|
}
|
|
}).build()
|
|
)
|
|
.addSecondary(
|
|
Builder<Button>::instance(new Button{"Display date", "display_date",
|
|
[](const char* msg) {
|
|
if (strcmp("PRESS", msg) == 0 && !Display::tDisplayDate.isEnabled()) {
|
|
Display::tDisplayDate.restart();
|
|
};
|
|
}
|
|
}).build()
|
|
)
|
|
.addSecondary(
|
|
Builder<Button>::instance(new Button{"Update time", "update_time",
|
|
[](const char* msg) {
|
|
if (strcmp("PRESS", msg) == 0) Ntp::tUpdateTime.restart();
|
|
}
|
|
}).build()
|
|
)
|
|
.addSecondary(ledMqtt)
|
|
.addConfiguration(brightnessMqtt)
|
|
.addConfiguration(hourFormatMqtt)
|
|
.addPreconfigured(HaESP::heapStats)
|
|
.addPreconfigured(HaESP::restartInfo)
|
|
.build();
|
|
|
|
void publishBme280() {
|
|
StaticJsonDocument<255> jsonDoc;
|
|
jsonDoc["temperature"] = Bme::data.temp;
|
|
jsonDoc["humidity"] = Bme::data.humidity;
|
|
jsonDoc["pressure"] = Bme::data.pressure;
|
|
char message[255];
|
|
serializeJson(jsonDoc, message);
|
|
sensor->updateState(message);
|
|
}
|
|
|
|
void setup() {
|
|
Display::hourFormatChangedCallback = []{
|
|
hourFormatMqtt->updateState(Display::hourFormat24);
|
|
};
|
|
Display::Brightness::brightnessChangedCallback = []{
|
|
brightnessMqtt->updateState(Display::Brightness::current);
|
|
};
|
|
}
|
|
} |