163 lines
5.9 KiB
C++
163 lines
5.9 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>(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>(SENSOR_ID).withValueTemplate("{{ value_json.humidity }}").withPrecision(1).build())
|
|
.addSecondary(Builder<PressureSensor>(SENSOR_ID).withValueTemplate("{{ value_json.pressure }}").withPrecision(1).build())
|
|
.build();
|
|
|
|
auto ledMqtt = Builder<Light>(new Light{ "Led", "led",
|
|
[](const char* msg) {
|
|
turnLed(strcmp("ON", msg) == 0);
|
|
}
|
|
}).restoreStateFromTopic().build();
|
|
|
|
auto brightnessMqtt = Builder<Number>(new Number{ "Brightness", "brightness",
|
|
[](const char* msg) {
|
|
Display::brightness = String{ msg }.toInt();
|
|
}
|
|
})
|
|
.withMin(Display::Brightness::MIN).withMax(Display::Brightness::MAX).withStep(1)
|
|
.withIcon("mdi:brightness-7")
|
|
.restoreStateFromTopic()
|
|
.build();
|
|
|
|
auto hourFormatMqtt = Builder<Switch>(new Switch{ "Format 24h", "format_24h",
|
|
[](const char* msg) {
|
|
Display::hourFormat24 = (strcmp("ON", msg) == 0);
|
|
}
|
|
}).withIcon("mdi:hours-24").restoreStateFromTopic().build();
|
|
Number* displayTimerMqtt = Builder<Number>(new Number{ "Timer duration", "timer_duration",
|
|
[](const char* msg) {
|
|
auto value = String{ msg }.toInt();
|
|
timer = value;
|
|
displayTimerMqtt->updateState(value);
|
|
if (Display::tTimer.isEnabled()) {
|
|
Display::tTimer.cancel();
|
|
Display::tTimer.enable();
|
|
}
|
|
}
|
|
})
|
|
.withMin(0).withMax(90).withStep(5)
|
|
.withDeviceClass("duration")
|
|
.withUnitMeasure("min")
|
|
.withIcon("mdi:timer-edit-outline")
|
|
.restoreStateFromTopic()
|
|
.build();
|
|
Sensor* timerRemainingMqtt = Builder<Sensor>(new Sensor("Timer remaining", "timer_remaining"))
|
|
.withUnitMeasure("min").withPrecision(0).withIcon("mdi:timer-sand").build();
|
|
Switch* timerMqtt = Builder<Switch>(new Switch{"Timer", "timer",
|
|
[](const char* msg) {
|
|
if (strcmp("ON", msg) == 0) {
|
|
Display::tTimer.restart();
|
|
timerMqtt->updateState(true);
|
|
} else {
|
|
Display::tTimer.cancel();
|
|
timerMqtt->updateState(false);
|
|
}
|
|
}
|
|
}).withIcon("mdi:timer-play-outline").restoreStateFromTopic().build();
|
|
|
|
auto button =
|
|
HaESP::restartButton()
|
|
.asDevice(espClockDevice)
|
|
.addSecondary(
|
|
Builder<Button>(new Button{"Display time", "display_time",
|
|
[](const char* msg) {
|
|
if (strcmp("PRESS", msg) == 0) {
|
|
Display::displayTask.activate(Display::tDisplayTime);
|
|
};
|
|
}
|
|
}).build()
|
|
)
|
|
.addSecondary(
|
|
Builder<Button>(new Button{"Display sensor data", "display_sensor_data",
|
|
[](const char* msg) {
|
|
if (strcmp("PRESS", msg) == 0) {
|
|
Bme::data.readAll();
|
|
Display::displayTask.activate(Display::tDisplaySensor);
|
|
};
|
|
}
|
|
}).build()
|
|
)
|
|
.addSecondary(
|
|
Builder<Button>(new Button{"Display date", "display_date",
|
|
[](const char* msg) {
|
|
if (strcmp("PRESS", msg) == 0) {
|
|
Display::displayTask.activate(Display::tDisplayDate);
|
|
};
|
|
}
|
|
}).build()
|
|
)
|
|
.addSecondary(
|
|
Builder<Button>(new Button{"Display remaining timer", "display_remaining_timer",
|
|
[](const char* msg) {
|
|
if (strcmp("PRESS", msg) == 0) {
|
|
Display::displayTask.activate(Display::tDisplayTimer);
|
|
}
|
|
}
|
|
}).build()
|
|
)
|
|
.addSecondary(
|
|
Builder<Button>(new Button{"Update time", "update_time",
|
|
[](const char* msg) {
|
|
if (strcmp("PRESS", msg) == 0) Ntp::tUpdateTime.restart();
|
|
}
|
|
}).withIcon("mdi:update").build()
|
|
)
|
|
.addSecondary(ledMqtt)
|
|
.addSecondary(timerMqtt)
|
|
.addSecondary(timerRemainingMqtt)
|
|
.addConfiguration(brightnessMqtt)
|
|
.addConfiguration(hourFormatMqtt)
|
|
.addConfiguration(displayTimerMqtt)
|
|
.addPreconfigured(HaESP::heapStats)
|
|
.addPreconfigured(HaESP::restartInfo)
|
|
.addPreconfigured(HaESP::wifiInfo)
|
|
.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.registerCallback([]{
|
|
hourFormatMqtt->updateState(Display::hourFormat24);
|
|
});
|
|
Display::brightness.registerCallback([]{
|
|
brightnessMqtt->updateState(Display::brightness);
|
|
});
|
|
timer.registerCallback([](int8 current){
|
|
timerRemainingMqtt->updateState(to_string(current).c_str());
|
|
});
|
|
}
|
|
} |