Skip to content

Commit

Permalink
Merge pull request #2050 from MichaelDvP/dev
Browse files Browse the repository at this point in the history
show emsesp-devices in system info, add common fields to value_info #2033
  • Loading branch information
proddy authored Sep 25, 2024
2 parents 9ca16bd + 83d5b91 commit d60cbc6
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 35 deletions.
4 changes: 4 additions & 0 deletions src/analogsensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -665,11 +665,15 @@ bool AnalogSensor::get_value_info(JsonObject output, const char * cmd, const int
}

void AnalogSensor::get_value_json(JsonObject output, const Sensor & sensor) {
output["name"] = sensor.name();
output["fullname"] = sensor.name();
output["gpio"] = sensor.gpio();
output["type"] = F_(number);
output["analog"] = FL_(list_sensortype)[sensor.type()];
output["value"] = sensor.value();
output["readable"] = true;
output["writeable"] = sensor.type() == AnalogType::COUNTER || (sensor.type() >= AnalogType::DIGITAL_OUT && sensor.type() <= AnalogType::PWM_2);
output["visible"] = true;
if (sensor.type() == AnalogType::COUNTER) {
output["min"] = 0;
output["max"] = 4000000;
Expand Down
2 changes: 1 addition & 1 deletion src/analogsensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class AnalogSensor {
return (!sensors_.empty());
}

size_t no_sensors() const {
size_t count_entities() const {
return sensors_.size();
}

Expand Down
32 changes: 29 additions & 3 deletions src/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1593,12 +1593,12 @@ bool System::command_info(const char * value, const int8_t id, JsonObject output
// Sensor Status
node = output["sensor"].to<JsonObject>();
if (EMSESP::sensor_enabled()) {
node["temperatureSensors"] = EMSESP::temperaturesensor_.no_sensors();
node["temperatureSensors"] = EMSESP::temperaturesensor_.count_entities();
node["temperatureSensorReads"] = EMSESP::temperaturesensor_.reads();
node["temperatureSensorFails"] = EMSESP::temperaturesensor_.fails();
}
if (EMSESP::analog_enabled()) {
node["analogSensors"] = EMSESP::analogsensor_.no_sensors();
node["analogSensors"] = EMSESP::analogsensor_.count_entities();
node["analogSensorReads"] = EMSESP::analogsensor_.reads();
node["analogSensorFails"] = EMSESP::analogsensor_.fails();
}
Expand Down Expand Up @@ -1691,8 +1691,8 @@ bool System::command_info(const char * value, const int8_t id, JsonObject output
});

// Devices - show EMS devices if we have any
JsonArray devices = output["devices"].to<JsonArray>();
if (!EMSESP::emsdevices.empty()) {
JsonArray devices = output["devices"].to<JsonArray>();
for (const auto & device_class : EMSFactory::device_handlers()) {
for (const auto & emsdevice : EMSESP::emsdevices) {
if (emsdevice && (emsdevice->device_type() == device_class.first)) {
Expand Down Expand Up @@ -1725,6 +1725,32 @@ bool System::command_info(const char * value, const int8_t id, JsonObject output
}
}
}
// Also show EMSESP devices if we have any
if (EMSESP::temperaturesensor_.count_entities()) {
JsonObject obj = devices.add<JsonObject>();
obj["type"] = F_(temperaturesensor);
obj["name"] = F_(temperaturesensor);
obj["entities"] = EMSESP::temperaturesensor_.count_entities();
}
// if (EMSESP::analog_enabled()) {
if (EMSESP::analogsensor_.count_entities()) {
JsonObject obj = devices.add<JsonObject>();
obj["type"] = F_(analogsensor);
obj["name"] = F_(analogsensor);
obj["entities"] = EMSESP::analogsensor_.count_entities();
}
if (EMSESP::webSchedulerService.count_entities()) {
JsonObject obj = devices.add<JsonObject>();
obj["type"] = F_(scheduler);
obj["name"] = F_(scheduler);
obj["entities"] = EMSESP::webSchedulerService.count_entities();
}
if (EMSESP::webCustomEntityService.count_entities()) {
JsonObject obj = devices.add<JsonObject>();
obj["type"] = F_(custom);
obj["name"] = F_(custom);
obj["entities"] = EMSESP::webCustomEntityService.count_entities();
}

return true; // this function always returns true!
}
Expand Down
7 changes: 5 additions & 2 deletions src/temperaturesensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,16 +380,19 @@ bool TemperatureSensor::get_value_info(JsonObject output, const char * cmd, cons
}

void TemperatureSensor::get_value_json(JsonObject output, const Sensor & sensor) {
output["id"] = sensor.id();
output["name"] = sensor.name();
output["id"] = sensor.id();
output["name"] = sensor.name();
output["fullname"] = sensor.name();
if (Helpers::hasValue(sensor.temperature_c)) {
char val[10];
output["value"] = serialized(Helpers::render_value(val, sensor.temperature_c, 10, EMSESP::system_.fahrenheit() ? 2 : 0));
}

output["type"] = F_(number);
output["uom"] = EMSdevice::uom_to_string(DeviceValueUOM::DEGREES);
output["readable"] = true;
output["writeable"] = false;
output["visible"] = true;
}


Expand Down
2 changes: 1 addition & 1 deletion src/temperaturesensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class TemperatureSensor {
return (!sensors_.empty());
}

size_t no_sensors() {
size_t count_entities() {
return sensors_.size();
}

Expand Down
16 changes: 4 additions & 12 deletions src/web/WebCustomEntityService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,10 @@ bool WebCustomEntityService::get_value_info(JsonObject output, const char * cmd)

// build the json for specific entity
void WebCustomEntityService::get_value_json(JsonObject output, const CustomEntityItem & entity) {
output["name"] = entity.name;
output["storage"] = entity.ram ? "ram" : "ems";
output["type"] = entity.value_type == DeviceValueType::BOOL ? "boolean" : entity.value_type == DeviceValueType::STRING ? "string" : F_(number);
output["name"] = entity.name;
output["fullname"] = entity.name;
output["storage"] = entity.ram ? "ram" : "ems";
output["type"] = entity.value_type == DeviceValueType::BOOL ? "boolean" : entity.value_type == DeviceValueType::STRING ? "string" : F_(number);
if (entity.uom > 0) {
output["uom"] = EMSdevice::uom_to_string(entity.uom);
}
Expand Down Expand Up @@ -471,15 +472,6 @@ uint8_t WebCustomEntityService::count_entities() {
return count;
}

uint8_t WebCustomEntityService::has_commands() {
uint8_t count = 0;
for (const CustomEntityItem & entity : *customEntityItems_) {
count += entity.writeable ? 1 : 0;
}

return count;
}

// send to dashboard, msgpack don't like serialized, use number
void WebCustomEntityService::generate_value_web(JsonObject output) {
JsonArray data = output["data"].to<JsonArray>();
Expand Down
1 change: 0 additions & 1 deletion src/web/WebCustomEntityService.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ class WebCustomEntityService : public StatefulService<WebCustomEntity> {
void generate_value_web(JsonObject output);

uint8_t count_entities();
uint8_t has_commands();
void ha_reset() {
ha_registered_ = false;
}
Expand Down
20 changes: 9 additions & 11 deletions src/web/WebSchedulerService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ bool WebSchedulerService::get_value_info(JsonObject output, const char * cmd) {

// build the json for specific entity
void WebSchedulerService::get_value_json(JsonObject output, const ScheduleItem & scheduleItem) {
output["name"] = scheduleItem.name;
output["type"] = "boolean";
output["name"] = scheduleItem.name;
output["fullname"] = scheduleItem.name;
output["type"] = "boolean";
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
output["value"] = scheduleItem.active;
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
Expand Down Expand Up @@ -299,18 +300,15 @@ void WebSchedulerService::publish(const bool force) {
}
}

bool WebSchedulerService::has_commands() {
if (scheduleItems_->size() == 0) {
return false;
}

// count number of entries, default: only named items
uint8_t WebSchedulerService::count_entities(bool cmd_only) {
uint8_t count = 0;
for (const ScheduleItem & scheduleItem : *scheduleItems_) {
if (!scheduleItem.name.empty()) {
return true;
if (!scheduleItem.name.empty() || !cmd_only) {
count++;
}
}

return false;
return count;
}

#include "shuntingYard.hpp"
Expand Down
4 changes: 2 additions & 2 deletions src/web/WebSchedulerService.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ class WebSchedulerService : public StatefulService<WebScheduler> {
void loop();
void publish_single(const char * name, const bool state);
void publish(const bool force = false);
bool has_commands();
bool command_setvalue(const char * value, const int8_t id, const char * name);
bool get_value_info(JsonObject output, const char * cmd);
void get_value_json(JsonObject output, const ScheduleItem & scheduleItem);
void ha_reset() {
ha_registered_ = false;
}
bool onChange(const char * cmd);
uint8_t count_entities(bool cmd_only = false);
bool onChange(const char * cmd);

#if defined(EMSESP_TEST)
void test();
Expand Down
4 changes: 2 additions & 2 deletions src/web/WebStatusService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ void WebStatusService::systemStatus(AsyncWebServerRequest * request) {
root["bus_status"] = EMSESP::bus_status(); // 0, 1 or 2
root["bus_uptime"] = EMSbus::bus_uptime();
root["num_devices"] = EMSESP::count_devices();
root["num_sensors"] = EMSESP::temperaturesensor_.no_sensors();
root["num_analogs"] = EMSESP::analogsensor_.no_sensors();
root["num_sensors"] = EMSESP::temperaturesensor_.count_entities();
root["num_analogs"] = EMSESP::analogsensor_.count_entities();
root["free_heap"] = EMSESP::system_.getHeapMem();
root["uptime"] = uuid::get_uptime_sec();
root["mqtt_status"] = EMSESP::mqtt_.connected();
Expand Down

0 comments on commit d60cbc6

Please sign in to comment.