From f245443a4d5fc250e39fb9ad7ccc7f96d1f60fb7 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Wed, 1 Oct 2025 18:11:01 +0200 Subject: [PATCH] add presence sensor into rc-gateway --- include/devices.h | 13 +++++++ include/huzzah.h | 2 +- lib/Tiny/Tiny.h | 3 +- platformio.ini | 2 +- .../test_sensor_builder/sensor_builder.cpp | 39 +++++++++++++++---- 5 files changed, 49 insertions(+), 10 deletions(-) diff --git a/include/devices.h b/include/devices.h index 27e7696..bb2068c 100644 --- a/include/devices.h +++ b/include/devices.h @@ -50,6 +50,19 @@ auto tankSensor = Builder::instance(new Sensor{ "Level", OIL_SENSOR }) .addDiagnostic(new BatterySensor{OIL_SENSOR, "Battery level", "{{ ((value_json.sensor.diagnostic.voltage|float-4.0)|round(2)*100/2.4)|int }}"}) .build(); +auto presenceTracker = Builder::instance(PRESENCE_SENSOR) + .asDevice(&DeviceConfig::create(PRESENCE_SENSOR) + .withName("Kid presence") + .withManufacturer("Atmel") + .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.5)|round(2)*100/2)|int }}"}) + .withOffDelaySeconds(5*60) + .withDeviceClass("presence") + .build(); + struct PollinSwitch : Switch { constexpr static const char* man = "Pollin"; const char* group; diff --git a/include/huzzah.h b/include/huzzah.h index 60b31ad..7e76092 100644 --- a/include/huzzah.h +++ b/include/huzzah.h @@ -98,7 +98,7 @@ namespace Board { void parseSensors(JsonDocument& jsonDoc, char* message) { JsonObjectConst json = jsonDoc["sensor"]; string id = to_string((unsigned int)json["id"]); - auto sensor = Sensor::mapSensors[id]; + auto sensor = GenericSensor::mapSensors[id]; if (sensor) sensor->updateState(message); } diff --git a/lib/Tiny/Tiny.h b/lib/Tiny/Tiny.h index a7cae64..1c5e706 100644 --- a/lib/Tiny/Tiny.h +++ b/lib/Tiny/Tiny.h @@ -52,4 +52,5 @@ WATER_SENSOR(3), TEMP_SENSOR(4), LIGHT_SENSOR(5), MOVEMENT_SENSOR(6), -OIL_SENSOR(7); +OIL_SENSOR(7), +PRESENCE_SENSOR(8); diff --git a/platformio.ini b/platformio.ini index 18f0ae1..f7be12c 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.10.0 + https://git.hodos.ro/libraries/ha-mqtt.git@^1.11.0 https://git.hodos.ro/libraries/wifi.git@^2.0.0 esphome/ESPAsyncWebServer-esphome@^3.4.0 upload_port = 192.168.6.161 diff --git a/test/native/test_sensor_builder/sensor_builder.cpp b/test/native/test_sensor_builder/sensor_builder.cpp index 7b094f7..97d22b6 100644 --- a/test/native/test_sensor_builder/sensor_builder.cpp +++ b/test/native/test_sensor_builder/sensor_builder.cpp @@ -12,7 +12,7 @@ void tearDown(void) { void test_unknown_sensor_type(void) { StaticJsonDocument<200> jsonDoc; unsigned long value = TYPE(0); - TEST_ASSERT_EQUAL(false, buildSensorJson(value, jsonDoc)); + TEST_ASSERT_FALSE(buildSensorJson(value, jsonDoc)); } void test_max_temp(void) { @@ -45,7 +45,7 @@ void test_overflow_value(void) { void test_max_voltage(void) { StaticJsonDocument<200> jsonDoc; unsigned long value = VCC(8191) | TYPE(SensorType::GENERIC); - TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc)); + TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc)); JsonObject diagnostic = jsonDoc["sensor"]["diagnostic"]; TEST_ASSERT_EQUAL(8.191, diagnostic["voltage"]); @@ -54,7 +54,7 @@ void test_max_voltage(void) { void test_overflow_voltage(void) { StaticJsonDocument<200> jsonDoc; unsigned long value = VCC(8192) | TYPE(SensorType::GENERIC); - TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc)); + TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc)); JsonObject diagnostic = jsonDoc["sensor"]["diagnostic"]; TEST_ASSERT_EQUAL(0, diagnostic["voltage"]); @@ -63,7 +63,7 @@ void test_overflow_voltage(void) { void test_temp_sensor(void) { StaticJsonDocument<200> jsonDoc; unsigned long value = ID(TEMP_SENSOR) | TEMP(210) | TYPE(SensorType::TEMPERATURE); - TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc)); + TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc)); JsonObject sensor = jsonDoc["sensor"]; TEST_ASSERT_EQUAL(TEMP_SENSOR, sensor["id"]); @@ -73,7 +73,7 @@ void test_temp_sensor(void) { void test_temp_sensor_with_voltage(void) { StaticJsonDocument<200> jsonDoc; unsigned long value = ID(TEMP_SENSOR) | TEMP(320) | TYPE(SensorType::TEMPERATURE) | VCC(2847L); - TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc)); + TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc)); JsonObject sensor = jsonDoc["sensor"]; TEST_ASSERT_EQUAL(TEMP_SENSOR, sensor["id"]); @@ -86,7 +86,7 @@ void test_temp_sensor_with_voltage(void) { void test_oil_sensor(void) { StaticJsonDocument<200> jsonDoc; unsigned long value = ID(OIL_SENSOR) | VALUE(150) | TYPE(SensorType::GENERIC); - TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc)); + TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc)); JsonObject sensor = jsonDoc["sensor"]; TEST_ASSERT_EQUAL(OIL_SENSOR, sensor["id"]); @@ -96,7 +96,7 @@ void test_oil_sensor(void) { void test_oil_sensor_with_voltage(void) { StaticJsonDocument<200> jsonDoc; unsigned long value = ID(OIL_SENSOR) | TEMP(200) | TYPE(SensorType::GENERIC) | VCC(2847L); - TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc)); + TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc)); JsonObject sensor = jsonDoc["sensor"]; TEST_ASSERT_EQUAL(OIL_SENSOR, sensor["id"]); @@ -106,6 +106,29 @@ void test_oil_sensor_with_voltage(void) { TEST_ASSERT_EQUAL(2.847, diagnostic["voltage"]); } +void test_presence_sensor(void) { + StaticJsonDocument<200> jsonDoc; + unsigned long value = ID(PRESENCE_SENSOR) | STATE(1) | TYPE(SensorType::CONTACT); + TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc)); + + JsonObject sensor = jsonDoc["sensor"]; + TEST_ASSERT_EQUAL(PRESENCE_SENSOR, sensor["id"]); + TEST_ASSERT_EQUAL_STRING("ON", sensor["state"]); +} + +void test_presence_sensor_with_voltage(void) { + StaticJsonDocument<200> jsonDoc; + unsigned long value = ID(PRESENCE_SENSOR) | STATE(0) | TYPE(SensorType::CONTACT) | VCC(3847L); + TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc)); + + JsonObject sensor = jsonDoc["sensor"]; + TEST_ASSERT_EQUAL(PRESENCE_SENSOR, sensor["id"]); + TEST_ASSERT_EQUAL_STRING("OFF", sensor["state"]); + + JsonObject diagnostic = sensor["diagnostic"]; + TEST_ASSERT_EQUAL(3.847, diagnostic["voltage"]); +} + int main(int argc, char **argv) { UNITY_BEGIN(); RUN_TEST(test_unknown_sensor_type); @@ -118,6 +141,8 @@ int main(int argc, char **argv) { RUN_TEST(test_temp_sensor_with_voltage); RUN_TEST(test_oil_sensor); RUN_TEST(test_oil_sensor_with_voltage); + RUN_TEST(test_presence_sensor); + RUN_TEST(test_presence_sensor_with_voltage); UNITY_END(); return 0;