144 lines
5.6 KiB
C++
144 lines
5.6 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<Light>::instance(new Light{ "Led", "led",
|
|
[](const char* msg) {
|
|
turnLed(strcmp("ON", msg) == 0);
|
|
}
|
|
}).restoreStateFromCommand().build();
|
|
|
|
auto brightnessMqtt = Builder<Number>::instance(new Number{ "Brightness", "brightness",
|
|
[](const char* msg) {
|
|
Display::brightness = String{ msg }.toInt();
|
|
}
|
|
}).withMin(Display::Brightness::MIN).withMax(Display::Brightness::MAX).withStep(1).restoreStateFromCommand().build();
|
|
|
|
auto hourFormatMqtt = Builder<Switch>::instance(new Switch{ "Format 24h", "format_24h",
|
|
[](const char* msg) {
|
|
Display::hourFormat24 = (strcmp("ON", msg) == 0);
|
|
}
|
|
}).restoreStateFromCommand().build();
|
|
Number* displayTimerMqtt = Builder<Number>::instance(new Number{ "Timer duration", "timer_duration",
|
|
[](const char* msg) {
|
|
auto value = String{ msg }.toInt();
|
|
Display::Timer::timer = value;
|
|
displayTimerMqtt->updateState(value);
|
|
}
|
|
}).withMin(0).withMax(90).withStep(5).restoreStateFromCommand().build();
|
|
Sensor* timerRemainingMqtt = Builder<Sensor>::instance(new Sensor("Timer remaining", "timer_remaining"))
|
|
.withUnitMeasure("min").withPrecision(0).build();
|
|
Switch* timerMqtt = Builder<Switch>::instance(new Switch{"Timer", "timer",
|
|
[](const char* msg) {
|
|
if (strcmp("ON", msg) == 0) {
|
|
Display::Timer::start();
|
|
timerMqtt->updateState(true);
|
|
} else {
|
|
Display::Timer::stop();
|
|
timerMqtt->updateState(false);
|
|
}
|
|
}
|
|
}).restoreStateFromCommand().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{"Display remaining timer", "display_remaining_timer",
|
|
[](const char* msg) {
|
|
if (strcmp("PRESS", msg) == 0 && !Display::Timer::tDisplayTimer.isEnabled()) {
|
|
Display::Timer::tDisplayTimer.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)
|
|
.addSecondary(timerMqtt)
|
|
.addSecondary(timerRemainingMqtt)
|
|
.addConfiguration(brightnessMqtt)
|
|
.addConfiguration(hourFormatMqtt)
|
|
.addConfiguration(displayTimerMqtt)
|
|
.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::hourFormat24.changedCallback = []{
|
|
hourFormatMqtt->updateState(Display::hourFormat24);
|
|
};
|
|
Display::brightness.changedCallback = []{
|
|
brightnessMqtt->updateState(Display::brightness);
|
|
};
|
|
Display::Timer::remainingCallback = [](int8 current){
|
|
timerRemainingMqtt->updateState(to_string(current).c_str());
|
|
};
|
|
}
|
|
} |