Merge branch 'rc-devices'

This commit is contained in:
Nicu Hodos 2025-10-08 16:19:33 +02:00
commit 04b2d1c306
3 changed files with 90 additions and 76 deletions

View File

@ -1,20 +1,9 @@
#pragma once
#define MAIN_DEVICE_ID "rc-gateway"
#include "esp.h"
#include "ha.h"
#include "rc_devices.h"
using namespace Ha;
typedef unordered_multimap<unsigned long, Ha::Switch*> mapswitches;
mapswitches onSwitches;
mapswitches offSwitches;
unordered_map<string, Ha::Switch*> p1Switches;
auto gatewayDevice = &DeviceConfig::create(MAIN_DEVICE_ID).withName("RC Gateway").withManufacturer("Adafruit").withModel("Huzzah Esp8266");
auto roomSensor = Builder<TemperatureSensor>::instance(TEMP_SENSOR)
.asDevice(&DeviceConfig::create(TEMP_SENSOR)
.withName("Servers room")
@ -23,8 +12,7 @@ auto roomSensor = Builder<TemperatureSensor>::instance(TEMP_SENSOR)
.withArea("Basement")
.withParent(gatewayDevice))
.withValueTemplate("{{ value_json.sensor.temperature }}")
.addDiagnostic(new VoltageSensor{TEMP_SENSOR, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
.addDiagnostic(new BatterySensor{TEMP_SENSOR, "Battery level", "{{ ((value_json.sensor.diagnostic.voltage|float-2.5)|round(2)*100/2)|int }}"})
.addPreconfigured(batterySensors<TemperatureSensor>(TEMP_SENSOR, BATTERY_PERCENTAGE_TEMPLATE(2.5, 2)))
.build();
auto tankSensor = Builder<Sensor>::instance(new Sensor{ "Level", OIL_SENSOR })
@ -46,8 +34,7 @@ auto tankSensor = Builder<Sensor>::instance(new Sensor{ "Level", OIL_SENSOR })
.withValueTemplate("{{ value_json.sensor.value }}")
.build()
)
.addDiagnostic(new VoltageSensor{OIL_SENSOR, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
.addDiagnostic(new BatterySensor{OIL_SENSOR, "Battery level", "{{ ((value_json.sensor.diagnostic.voltage|float-4.0)|round(2)*100/2.4)|int }}"})
.addPreconfigured(batterySensors<Sensor>(OIL_SENSOR, BATTERY_PERCENTAGE_TEMPLATE(4, 2.4)))
.build();
auto presenceTracker = Builder<BinarySensor>::instance(PRESENCE_SENSOR)
@ -57,69 +44,11 @@ auto presenceTracker = Builder<BinarySensor>::instance(PRESENCE_SENSOR)
.withModel("AtTiny85")
.withParent(gatewayDevice))
.withValueTemplate("{{ value_json.sensor.state }}")
.addDiagnostic(new VoltageSensor{PRESENCE_SENSOR, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
.addDiagnostic(new BatterySensor{PRESENCE_SENSOR, "Battery level", "{{ ((value_json.sensor.diagnostic.voltage|float-2.7)|round(2)*100/0.6)|int }}"})
.addPreconfigured(batterySensors<BinarySensor>(PRESENCE_SENSOR, BATTERY_PERCENTAGE_TEMPLATE(2.7, 0.6)))
.withOffDelaySeconds(5*60)
.withDeviceClass("presence")
.build();
struct PollinSwitch : Switch {
constexpr static const char* man = "Pollin";
const char* group;
unsigned char channel;
PollinSwitch(const char* group, const unsigned char channel, const char* name = nullptr, const char* area = nullptr)
: Switch(nullptr, [group, channel]{
// copy id from string into a new pointer, to avoid memory leaks
return (new string{Protocol_1::buildId(group, channel)})->c_str();
}()), group(group), channel(channel) {
if (!name) name = (new string{string(man).append(" ").append(id)})->c_str();
mainDevice = &DeviceConfig::create(id).withName(name).withManufacturer(man).withArea(area).withParent(gatewayDevice);
withStateTopic();
deviceClass = "outlet";
p1Switches.insert({ string(id), this });
}
void onCommand(const char* msg) override {
strcmp("ON", msg) == 0 ? mySwitch.switchOn(group, channel) : mySwitch.switchOff(group, channel);
publisher(stateTopic, msg);
}
};
struct EasyHomeSwitch : Switch {
unsigned long on[8] = { 4326554, 4537114, 4767530, 4972714 };
unsigned long off[8] = { 4483146, 4626810, 4661562, 4819642 };
EasyHomeSwitch(const char remotePosition, unsigned long on[4], unsigned long off[4], const char* name = nullptr, const char* area = nullptr)
: Switch(nullptr, [remotePosition] {
auto uId = new string("easy_home_");
(*uId) += tolower(remotePosition);
return uId->c_str();
}()) {
memcpy(&this->on[4], on, 4 * sizeof(unsigned long));
memcpy(&this->off[4], off, 4 * sizeof(unsigned long));
if (!name) {
auto n = new string("Easy Home ");
(*n) += remotePosition;
name = n->c_str();
}
mainDevice = &DeviceConfig::create(id).withName(name).withManufacturer("Intertek").withModel("Easy Home").withArea(area).withParent(gatewayDevice);
withStateTopic();
deviceClass = "outlet";
for (int i = 0; i < 8; i++) {
onSwitches.insert({ this->on[i], this });
offSwitches.insert({ this->off[i], this });
}
}
void onCommand(const char* msg) override {
mySwitch.setProtocol(4);
strcmp("ON", msg) == 0 ? mySwitch.send(on[4], 24) : mySwitch.send(off[4], 24);
publisher(stateTopic, msg);
}
};
Command* commands[] = {
HaESP::restartButton()
.asDevice(gatewayDevice)

85
include/rc_devices.h Normal file
View File

@ -0,0 +1,85 @@
#pragma once
#define MAIN_DEVICE_ID "rc-gateway"
#define BATTERY_PERCENTAGE_TEMPLATE(min, diff) "{{ ((value_json.sensor.diagnostic.voltage|float-" #min ")|round(2)*100/" #diff ")|int }}"
#include "esp.h"
#include "ha.h"
using namespace Ha;
typedef unordered_multimap<unsigned long, Ha::Switch*> mapswitches;
mapswitches onSwitches;
mapswitches offSwitches;
unordered_map<string, Ha::Switch*> p1Switches;
auto gatewayDevice = &DeviceConfig::create(MAIN_DEVICE_ID).withName("RC Gateway").withManufacturer("Adafruit").withModel("Huzzah Esp8266");
struct PollinSwitch : Switch {
constexpr static const char* man = "Pollin";
const char* group;
unsigned char channel;
PollinSwitch(const char* group, const unsigned char channel, const char* name = nullptr, const char* area = nullptr)
: Switch(nullptr, [group, channel]{
// copy id from string into a new pointer, to avoid memory leaks
return (new string{Protocol_1::buildId(group, channel)})->c_str();
}()), group(group), channel(channel) {
if (!name) name = (new string{string(man).append(" ").append(id)})->c_str();
mainDevice = &DeviceConfig::create(id).withName(name).withManufacturer(man).withArea(area).withParent(gatewayDevice);
withStateTopic();
deviceClass = "outlet";
p1Switches.insert({ string(id), this });
}
void onCommand(const char* msg) override {
strcmp("ON", msg) == 0 ? mySwitch.switchOn(group, channel) : mySwitch.switchOff(group, channel);
publisher(stateTopic, msg);
}
};
struct EasyHomeSwitch : Switch {
unsigned long on[8] = { 4326554, 4537114, 4767530, 4972714 };
unsigned long off[8] = { 4483146, 4626810, 4661562, 4819642 };
EasyHomeSwitch(const char remotePosition, unsigned long on[4], unsigned long off[4], const char* name = nullptr, const char* area = nullptr)
: Switch(nullptr, [remotePosition] {
auto uId = new string("easy_home_");
(*uId) += tolower(remotePosition);
return uId->c_str();
}()) {
memcpy(&this->on[4], on, 4 * sizeof(unsigned long));
memcpy(&this->off[4], off, 4 * sizeof(unsigned long));
if (!name) {
auto n = new string("Easy Home ");
(*n) += remotePosition;
name = n->c_str();
}
mainDevice = &DeviceConfig::create(id).withName(name).withManufacturer("Intertek").withModel("Easy Home").withArea(area).withParent(gatewayDevice);
withStateTopic();
deviceClass = "outlet";
for (int i = 0; i < 8; i++) {
onSwitches.insert({ this->on[i], this });
offSwitches.insert({ this->off[i], this });
}
}
void onCommand(const char* msg) override {
mySwitch.setProtocol(4);
strcmp("ON", msg) == 0 ? mySwitch.send(on[4], 24) : mySwitch.send(off[4], 24);
publisher(stateTopic, msg);
}
};
template <class T>
auto batterySensors(const char* id, const char* batterySensorTemplate) {
return [id, batterySensorTemplate](Builder<T>& builder) -> Builder<T>& {
builder
.addDiagnostic(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
.addDiagnostic(new BatterySensor{id, "Battery level", batterySensorTemplate})
.build();
return builder;
};
}

View File

@ -33,7 +33,7 @@ framework = arduino
lib_deps =
${env.lib_deps}
arkhipenko/TaskScheduler@^3.8.5
https://git.hodos.ro/libraries/ha-mqtt.git@^1.12.0
https://git.hodos.ro/libraries/ha-mqtt.git@^1.13.0
https://git.hodos.ro/libraries/wifi.git@^2.0.0
esphome/ESPAsyncWebServer-esphome@^3.4.0
upload_port = 192.168.6.161