From 264c1c1e80bad01bdaf35e1a330d7a70a0eef26b Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Fri, 3 Oct 2025 17:02:28 +0200 Subject: [PATCH 1/5] separate rc-gateway devices --- include/devices.h | 70 +----------------------------------------- include/rc_devices.h | 73 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 69 deletions(-) create mode 100644 include/rc_devices.h diff --git a/include/devices.h b/include/devices.h index 4f810d6..a4e1124 100644 --- a/include/devices.h +++ b/include/devices.h @@ -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 mapswitches; - -mapswitches onSwitches; -mapswitches offSwitches; -unordered_map p1Switches; - -auto gatewayDevice = &DeviceConfig::create(MAIN_DEVICE_ID).withName("RC Gateway").withManufacturer("Adafruit").withModel("Huzzah Esp8266"); - auto roomSensor = Builder::instance(TEMP_SENSOR) .asDevice(&DeviceConfig::create(TEMP_SENSOR) .withName("Servers room") @@ -63,63 +52,6 @@ auto presenceTracker = Builder::instance(PRESENCE_SENSOR) .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) diff --git a/include/rc_devices.h b/include/rc_devices.h new file mode 100644 index 0000000..f6da508 --- /dev/null +++ b/include/rc_devices.h @@ -0,0 +1,73 @@ +#pragma once + +#define MAIN_DEVICE_ID "rc-gateway" + +#include "esp.h" +#include "ha.h" + +using namespace Ha; + +typedef unordered_multimap mapswitches; + +mapswitches onSwitches; +mapswitches offSwitches; +unordered_map 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); + } +}; From 0a228df07df6277e5a54680d5bb3e54935372df8 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sun, 5 Oct 2025 09:49:22 +0200 Subject: [PATCH 2/5] create rc devices typical battery sensors --- include/devices.h | 12 ++++++------ include/rc_devices.h | 10 ++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/include/devices.h b/include/devices.h index a4e1124..fb7d813 100644 --- a/include/devices.h +++ b/include/devices.h @@ -12,8 +12,8 @@ auto roomSensor = Builder::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 }}"}) + .addDiagnostic(createVoltageSensor(TEMP_SENSOR)) + .addDiagnostic(createBatterySensor(TEMP_SENSOR, 2.5, 4.5)) .build(); auto tankSensor = Builder::instance(new Sensor{ "Level", OIL_SENSOR }) @@ -35,8 +35,8 @@ auto tankSensor = Builder::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 }}"}) + .addDiagnostic(createVoltageSensor(OIL_SENSOR)) + .addDiagnostic(createBatterySensor(OIL_SENSOR, 4.0, 6.4)) .build(); auto presenceTracker = Builder::instance(PRESENCE_SENSOR) @@ -46,8 +46,8 @@ auto presenceTracker = Builder::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 }}"}) + .addDiagnostic(createVoltageSensor(PRESENCE_SENSOR)) + .addDiagnostic(createBatterySensor(PRESENCE_SENSOR, 2.7, 3.3)) .withOffDelaySeconds(5*60) .withDeviceClass("presence") .build(); diff --git a/include/rc_devices.h b/include/rc_devices.h index f6da508..ebd37d0 100644 --- a/include/rc_devices.h +++ b/include/rc_devices.h @@ -71,3 +71,13 @@ struct EasyHomeSwitch : Switch { publisher(stateTopic, msg); } }; + +VoltageSensor* createVoltageSensor(const char* id) { + return new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"}; +} + +BatterySensor* createBatterySensor(const char* id, float min, float max) { + char value_json[128]; + snprintf(value_json, 128, "{{ ((value_json.sensor.diagnostic.voltage|float-%f)|round(2)*100/%f)|int }}", min, max - min); + return new BatterySensor{id, "Battery level", value_json}; +} From 57b9494c98b506954c1de76982e1f3976c9daea3 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sun, 5 Oct 2025 11:50:20 +0200 Subject: [PATCH 3/5] add helper methods for creating typical battery sensors for rc devices --- include/devices.h | 9 +++------ include/rc_devices.h | 12 ++++++++++-- platformio.ini | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/include/devices.h b/include/devices.h index fb7d813..3c7ad82 100644 --- a/include/devices.h +++ b/include/devices.h @@ -12,8 +12,7 @@ auto roomSensor = Builder::instance(TEMP_SENSOR) .withArea("Basement") .withParent(gatewayDevice)) .withValueTemplate("{{ value_json.sensor.temperature }}") - .addDiagnostic(createVoltageSensor(TEMP_SENSOR)) - .addDiagnostic(createBatterySensor(TEMP_SENSOR, 2.5, 4.5)) + .addPreconfigured(batterySensors(TEMP_SENSOR, 2.5, 4.5)) .build(); auto tankSensor = Builder::instance(new Sensor{ "Level", OIL_SENSOR }) @@ -35,8 +34,7 @@ auto tankSensor = Builder::instance(new Sensor{ "Level", OIL_SENSOR }) .withValueTemplate("{{ value_json.sensor.value }}") .build() ) - .addDiagnostic(createVoltageSensor(OIL_SENSOR)) - .addDiagnostic(createBatterySensor(OIL_SENSOR, 4.0, 6.4)) + .addPreconfigured(batterySensors(OIL_SENSOR, 4.0, 6.4)) .build(); auto presenceTracker = Builder::instance(PRESENCE_SENSOR) @@ -46,8 +44,7 @@ auto presenceTracker = Builder::instance(PRESENCE_SENSOR) .withModel("AtTiny85") .withParent(gatewayDevice)) .withValueTemplate("{{ value_json.sensor.state }}") - .addDiagnostic(createVoltageSensor(PRESENCE_SENSOR)) - .addDiagnostic(createBatterySensor(PRESENCE_SENSOR, 2.7, 3.3)) + .addPreconfigured(batterySensors(PRESENCE_SENSOR, 2.7, 3.3)) .withOffDelaySeconds(5*60) .withDeviceClass("presence") .build(); diff --git a/include/rc_devices.h b/include/rc_devices.h index ebd37d0..1b33534 100644 --- a/include/rc_devices.h +++ b/include/rc_devices.h @@ -77,7 +77,15 @@ VoltageSensor* createVoltageSensor(const char* id) { } BatterySensor* createBatterySensor(const char* id, float min, float max) { - char value_json[128]; - snprintf(value_json, 128, "{{ ((value_json.sensor.diagnostic.voltage|float-%f)|round(2)*100/%f)|int }}", min, max - min); + char* value_json = new char[128]; + snprintf(value_json, 128, "{{ ((value_json.sensor.diagnostic.voltage|float-%.2f)|round(2)*100/%.2f)|int }}", min, max - min); return new BatterySensor{id, "Battery level", value_json}; } + +template +auto batterySensors(const char* id, float min, float max) { + return [id, min, max](Builder& builder) -> Builder& { + builder.addDiagnostic(createVoltageSensor(id)).addDiagnostic(createBatterySensor(id, min, max)).build(); + return builder; + }; +} diff --git a/platformio.ini b/platformio.ini index f7fb245..8dbfee6 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 From 3dc31ff58546cdd3f4735e47fb3dbed310e22e82 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sun, 5 Oct 2025 13:03:12 +0200 Subject: [PATCH 4/5] optimize memory allocation for battery percentage template --- include/rc_devices.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/rc_devices.h b/include/rc_devices.h index 1b33534..32d5c90 100644 --- a/include/rc_devices.h +++ b/include/rc_devices.h @@ -76,9 +76,11 @@ VoltageSensor* createVoltageSensor(const char* id) { return new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"}; } +#define BATTERY_PERCENTAGE_TEMPLATE "{{ ((value_json.sensor.diagnostic.voltage|float-%.2f)|round(2)*100/%.2f)|int }}" BatterySensor* createBatterySensor(const char* id, float min, float max) { - char* value_json = new char[128]; - snprintf(value_json, 128, "{{ ((value_json.sensor.diagnostic.voltage|float-%.2f)|round(2)*100/%.2f)|int }}", min, max - min); + auto len = snprintf(nullptr, 0, BATTERY_PERCENTAGE_TEMPLATE, min, max - min) + 1; + char* value_json = new char[len]; + snprintf(value_json, len, BATTERY_PERCENTAGE_TEMPLATE, min, max - min); return new BatterySensor{id, "Battery level", value_json}; } From 545a6fd2f6564c13448cfe7303a454b22c07edf4 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 6 Oct 2025 17:49:58 +0200 Subject: [PATCH 5/5] reduce memory usage by using string for battery percentage template defined at compile time --- include/devices.h | 6 +++--- include/rc_devices.h | 22 +++++++--------------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/include/devices.h b/include/devices.h index 3c7ad82..100c7a6 100644 --- a/include/devices.h +++ b/include/devices.h @@ -12,7 +12,7 @@ auto roomSensor = Builder::instance(TEMP_SENSOR) .withArea("Basement") .withParent(gatewayDevice)) .withValueTemplate("{{ value_json.sensor.temperature }}") - .addPreconfigured(batterySensors(TEMP_SENSOR, 2.5, 4.5)) + .addPreconfigured(batterySensors(TEMP_SENSOR, BATTERY_PERCENTAGE_TEMPLATE(2.5, 2))) .build(); auto tankSensor = Builder::instance(new Sensor{ "Level", OIL_SENSOR }) @@ -34,7 +34,7 @@ auto tankSensor = Builder::instance(new Sensor{ "Level", OIL_SENSOR }) .withValueTemplate("{{ value_json.sensor.value }}") .build() ) - .addPreconfigured(batterySensors(OIL_SENSOR, 4.0, 6.4)) + .addPreconfigured(batterySensors(OIL_SENSOR, BATTERY_PERCENTAGE_TEMPLATE(4, 2.4))) .build(); auto presenceTracker = Builder::instance(PRESENCE_SENSOR) @@ -44,7 +44,7 @@ auto presenceTracker = Builder::instance(PRESENCE_SENSOR) .withModel("AtTiny85") .withParent(gatewayDevice)) .withValueTemplate("{{ value_json.sensor.state }}") - .addPreconfigured(batterySensors(PRESENCE_SENSOR, 2.7, 3.3)) + .addPreconfigured(batterySensors(PRESENCE_SENSOR, BATTERY_PERCENTAGE_TEMPLATE(2.7, 0.6))) .withOffDelaySeconds(5*60) .withDeviceClass("presence") .build(); diff --git a/include/rc_devices.h b/include/rc_devices.h index 32d5c90..c79ea06 100644 --- a/include/rc_devices.h +++ b/include/rc_devices.h @@ -1,6 +1,7 @@ #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" @@ -72,22 +73,13 @@ struct EasyHomeSwitch : Switch { } }; -VoltageSensor* createVoltageSensor(const char* id) { - return new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"}; -} - -#define BATTERY_PERCENTAGE_TEMPLATE "{{ ((value_json.sensor.diagnostic.voltage|float-%.2f)|round(2)*100/%.2f)|int }}" -BatterySensor* createBatterySensor(const char* id, float min, float max) { - auto len = snprintf(nullptr, 0, BATTERY_PERCENTAGE_TEMPLATE, min, max - min) + 1; - char* value_json = new char[len]; - snprintf(value_json, len, BATTERY_PERCENTAGE_TEMPLATE, min, max - min); - return new BatterySensor{id, "Battery level", value_json}; -} - template -auto batterySensors(const char* id, float min, float max) { - return [id, min, max](Builder& builder) -> Builder& { - builder.addDiagnostic(createVoltageSensor(id)).addDiagnostic(createBatterySensor(id, min, max)).build(); +auto batterySensors(const char* id, const char* batterySensorTemplate) { + return [id, batterySensorTemplate](Builder& builder) -> Builder& { + builder + .addDiagnostic(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"}) + .addDiagnostic(new BatterySensor{id, "Battery level", batterySensorTemplate}) + .build(); return builder; }; }