@@ -64,12 +64,11 @@ void AnalogSensor::reload() {
64
64
// load the list of analog sensors from the customization service
65
65
// and store them locally and then activate them
66
66
EMSESP::webCustomizationService.read ([&](WebCustomization & settings) {
67
- auto sensors = settings.analogCustomizations ;
68
- auto it = sensors_.begin ();
67
+ auto it = sensors_.begin ();
69
68
for (auto & sensor_ : sensors_) {
70
69
// update existing sensors
71
70
bool found = false ;
72
- for (auto & sensor : sensors ) { // search customlist
71
+ for (const auto & sensor : settings. analogCustomizations ) { // search customlist
73
72
if (sensor_.id () == sensor.id ) {
74
73
// for output sensors set value to new start-value
75
74
if ((sensor.type == AnalogType::COUNTER || sensor.type >= AnalogType::DIGITAL_OUT)
@@ -90,10 +89,11 @@ void AnalogSensor::reload() {
90
89
}
91
90
it++;
92
91
}
92
+
93
93
// add new sensors from list
94
- for (auto & sensor : sensors ) {
94
+ for (const auto & sensor : settings. analogCustomizations ) {
95
95
bool found = false ;
96
- for (auto & sensor_ : sensors_) {
96
+ for (const auto & sensor_ : sensors_) {
97
97
if (sensor_.id () == sensor.id ) {
98
98
found = true ;
99
99
}
@@ -295,13 +295,13 @@ bool AnalogSensor::update(uint8_t id, const std::string & name, float offset, fl
295
295
if (!found_sensor) {
296
296
EMSESP::webCustomizationService.update (
297
297
[&](WebCustomization & settings) {
298
- AnalogCustomization newSensor = AnalogCustomization ();
299
- newSensor.id = id;
300
- newSensor.name = name;
301
- newSensor.offset = offset;
302
- newSensor.factor = factor;
303
- newSensor.uom = uom;
304
- newSensor.type = type;
298
+ auto newSensor = AnalogCustomization ();
299
+ newSensor.id = id;
300
+ newSensor.name = name;
301
+ newSensor.offset = offset;
302
+ newSensor.factor = factor;
303
+ newSensor.uom = uom;
304
+ newSensor.type = type;
305
305
settings.analogCustomizations .push_back (newSensor);
306
306
LOG_DEBUG (F (" Adding new customization for analog sensor ID %d" ), id);
307
307
return StateUpdateResult::CHANGED; // persist the change
@@ -325,7 +325,7 @@ bool AnalogSensor::updated_values() {
325
325
}
326
326
327
327
// publish a single sensor to MQTT
328
- void AnalogSensor::publish_sensor (const Sensor & sensor) {
328
+ void AnalogSensor::publish_sensor (const Sensor & sensor) const {
329
329
if (Mqtt::publish_single ()) {
330
330
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
331
331
if (Mqtt::publish_single2cmd ()) {
@@ -339,7 +339,7 @@ void AnalogSensor::publish_sensor(const Sensor & sensor) {
339
339
}
340
340
341
341
// send empty config topic to remove the entry from HA
342
- void AnalogSensor::remove_ha_topic (const uint8_t id) {
342
+ void AnalogSensor::remove_ha_topic (const uint8_t id) const {
343
343
if (!Mqtt::ha_enabled ()) {
344
344
return ;
345
345
}
@@ -363,7 +363,6 @@ void AnalogSensor::publish_values(const bool force) {
363
363
for (const auto & sensor : sensors_) {
364
364
publish_sensor (sensor);
365
365
}
366
- // return;
367
366
}
368
367
369
368
DynamicJsonDocument doc (120 * num_sensors);
@@ -383,61 +382,59 @@ void AnalogSensor::publish_values(const bool force) {
383
382
case AnalogType::PWM_0:
384
383
case AnalogType::PWM_1:
385
384
case AnalogType::PWM_2:
386
- dataSensor[" value" ] = ( float ) sensor.value (); // float
385
+ dataSensor[" value" ] = sensor.value (); // float
387
386
break ;
388
- case AnalogType::DIGITAL_IN:
389
- case AnalogType::DIGITAL_OUT:
390
387
default :
391
388
dataSensor[" value" ] = (uint8_t )sensor.value (); // convert to char for 1 or 0
392
389
break ;
393
390
}
394
391
395
392
// create HA config
396
- if (Mqtt::ha_enabled ()) {
397
- if (!sensor.ha_registered || force) {
398
- LOG_DEBUG (F (" Recreating HA config for analog sensor ID %d" ), sensor.id ());
393
+ if (Mqtt::ha_enabled () && (!sensor.ha_registered || force)) {
394
+ LOG_DEBUG (F (" Recreating HA config for analog sensor ID %d" ), sensor.id ());
399
395
400
- StaticJsonDocument<EMSESP_JSON_SIZE_MEDIUM> config;
396
+ StaticJsonDocument<EMSESP_JSON_SIZE_MEDIUM> config;
401
397
402
- char stat_t [50 ];
403
- snprintf (stat_t , sizeof (stat_t ), " %s/analogsensor_data" , Mqtt::base ().c_str ());
404
- config[" stat_t" ] = stat_t ;
398
+ char stat_t [50 ];
399
+ snprintf (stat_t , sizeof (stat_t ), " %s/analogsensor_data" , Mqtt::base ().c_str ());
400
+ config[" stat_t" ] = stat_t ;
405
401
406
- char str[50 ];
407
- snprintf (str, sizeof (str), " {{value_json['%d'].value}}" , sensor.id ());
408
- config[" val_tpl" ] = str;
402
+ char str[50 ];
403
+ snprintf (str, sizeof (str), " {{value_json['%d'].value}}" , sensor.id ());
404
+ config[" val_tpl" ] = str;
409
405
410
- snprintf (str, sizeof (str), " Analog Sensor %s" , sensor.name ().c_str ());
411
- config[" name" ] = str;
406
+ snprintf (str, sizeof (str), " Analog Sensor %s" , sensor.name ().c_str ());
407
+ config[" name" ] = str;
412
408
413
- snprintf (str, sizeof (str), " analogsensor_%d" , sensor.id ());
414
- config[" uniq_id" ] = str;
409
+ snprintf (str, sizeof (str), " analogsensor_%d" , sensor.id ());
410
+ config[" uniq_id" ] = str;
415
411
416
- JsonObject dev = config.createNestedObject (" dev" );
417
- JsonArray ids = dev.createNestedArray (" ids" );
418
- ids.add (" ems-esp" );
412
+ JsonObject dev = config.createNestedObject (" dev" );
413
+ JsonArray ids = dev.createNestedArray (" ids" );
414
+ ids.add (" ems-esp" );
419
415
420
- char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
421
- snprintf (topic, sizeof (topic), " sensor/%s/analogsensor_%d/config" , Mqtt::base ().c_str (), sensor.id ());
416
+ char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
417
+ snprintf (topic, sizeof (topic), " sensor/%s/analogsensor_%d/config" , Mqtt::base ().c_str (), sensor.id ());
422
418
423
- Mqtt::publish_ha (topic, config.as <JsonObject>());
419
+ Mqtt::publish_ha (topic, config.as <JsonObject>());
424
420
425
- sensor.ha_registered = true ;
426
- }
421
+ sensor.ha_registered = true ;
427
422
}
428
423
424
+
429
425
} else {
430
426
// not nested
431
427
doc[sensor.name ()] = sensor.value ();
432
428
}
433
429
}
434
430
}
431
+
435
432
Mqtt::publish (F (" analogsensor_data" ), doc.as <JsonObject>());
436
433
}
437
434
438
435
// called from emsesp.cpp, similar to the emsdevice->get_value_info
439
436
// searches by name
440
- bool AnalogSensor::get_value_info (JsonObject & output, const char * cmd, const int8_t id) {
437
+ bool AnalogSensor::get_value_info (JsonObject & output, const char * cmd, const int8_t id) const {
441
438
for (const auto & sensor : sensors_) {
442
439
if (strcmp (cmd, sensor.name ().c_str ()) == 0 ) {
443
440
output[" id" ] = sensor.id ();
@@ -455,8 +452,8 @@ bool AnalogSensor::get_value_info(JsonObject & output, const char * cmd, const i
455
452
456
453
// creates JSON doc from values
457
454
// returns false if there are no sensors
458
- bool AnalogSensor::command_info (const char * value, const int8_t id, JsonObject & output) {
459
- if (sensors_.size () == 0 ) {
455
+ bool AnalogSensor::command_info (const char * value, const int8_t id, JsonObject & output) const {
456
+ if (sensors_.empty () ) {
460
457
return false ;
461
458
}
462
459
@@ -532,7 +529,7 @@ bool AnalogSensor::command_setvalue(const char * value, const int8_t id) {
532
529
return true ;
533
530
} else if (sensor.type () == AnalogType::DIGITAL_OUT) {
534
531
uint8_t v = val;
535
- if (( sensor.id () == 25 || sensor.id () == 26 ) ) {
532
+ if (sensor.id () == 25 || sensor.id () == 26 ) {
536
533
sensor.set_offset (v);
537
534
sensor.set_value (v);
538
535
pinMode (sensor.id (), OUTPUT);
@@ -573,7 +570,6 @@ bool AnalogSensor::command_commands(const char * value, const int8_t id, JsonObj
573
570
// hard coded tests
574
571
#ifdef EMSESP_DEBUG
575
572
void AnalogSensor::test () {
576
- // Sensor(const uint8_t id, const std::string & name, const float offset, const float factor, const uint8_t uom, const int8_t type);
577
573
sensors_.emplace_back (36 , " test12" , 0 , 0.1 , 17 , AnalogType::ADC);
578
574
sensors_.back ().set_value (12.4 );
579
575
0 commit comments