Add blacklist/remove entities #891

This commit is contained in:
MichaelDvP
2023-01-24 17:26:18 +01:00
parent ede8e7dfce
commit 0e0e9eccf1
15 changed files with 207 additions and 133 deletions

View File

@@ -40,7 +40,16 @@ bool EMSdevice::has_entities() const {
return true;
}
}
return false;
bool found = false;
EMSESP::webCustomizationService.read([&](WebCustomization & settings) {
for (EntityCustomization entityCustomization : settings.entityCustomizations) {
if (entityCustomization.device_id == device_id() && entityCustomization.entity_ids.size()) {
found = true;
break;
}
}
});
return found;
}
std::string EMSdevice::tag_to_string(uint8_t tag, const bool translate) {
@@ -511,14 +520,9 @@ void EMSdevice::add_device_value(uint8_t tag,
for (std::string entity_id : entityCustomization.entity_ids) {
// if there is an appended custom name, strip it to get the true entity name
// and extract the new custom name
std::string shortname;
auto custom_name_pos = entity_id.find('|');
bool has_custom_name = (custom_name_pos != std::string::npos);
if (has_custom_name) {
shortname = entity_id.substr(2, custom_name_pos - 2);
} else {
shortname = entity_id.substr(2);
}
std::string shortname = has_custom_name ? entity_id.substr(2, custom_name_pos - 2) : entity_id.substr(2);
// we found the device entity
if (shortname == entity) {
@@ -1014,6 +1018,22 @@ void EMSdevice::generate_values_web_customization(JsonArray & output) {
}
}
}
EMSESP::webCustomizationService.read([&](WebCustomization & settings) {
for (EntityCustomization entityCustomization : settings.entityCustomizations) {
if ((entityCustomization.device_id == device_id())) {
for (std::string entity_id : entityCustomization.entity_ids) {
uint8_t mask = Helpers::hextoint(entity_id.substr(0, 2).c_str());
if (mask & 0x80) {
JsonObject obj = output.createNestedObject();
obj["id"] = DeviceValue::get_name(entity_id);
obj["m"] = mask;
obj["w"] = false;
}
}
break;
}
}
});
}
void EMSdevice::set_climate_minmax(uint8_t tag, int16_t min, uint16_t max) {
@@ -1037,14 +1057,9 @@ void EMSdevice::setCustomEntity(const std::string & entity_id) {
std::string entity_name = dv.tag < DeviceValueTAG::TAG_HC1 ? dv.short_name : tag_to_mqtt(dv.tag) + "/" + dv.short_name;
// extra shortname
std::string shortname;
auto custom_name_pos = entity_id.find('|');
bool has_custom_name = (custom_name_pos != std::string::npos);
if (has_custom_name) {
shortname = entity_id.substr(2, custom_name_pos - 2);
} else {
shortname = entity_id.substr(2);
}
std::string shortname = has_custom_name ? entity_id.substr(2, custom_name_pos - 2) : entity_id.substr(2);
if (entity_name == shortname) {
// check the masks
@@ -1088,8 +1103,14 @@ void EMSdevice::getCustomEntities(std::vector<std::string> & entity_ids) {
for (const auto & dv : devicevalues_) {
std::string entity_name = dv.tag < DeviceValueTAG::TAG_HC1 ? dv.short_name : tag_to_mqtt(dv.tag) + "/" + dv.short_name;
uint8_t mask = dv.state >> 4;
if (mask || !dv.custom_fullname.empty()) {
bool is_set = false;
for (auto & eid : entity_ids) {
if (DeviceValue::get_name(eid) == entity_name) {
is_set = true;
break;
}
}
if (!is_set && (mask || !dv.custom_fullname.empty())) {
if (dv.custom_fullname.empty()) {
entity_ids.push_back(Helpers::hextoa(mask, false) + entity_name);
} else {

View File

@@ -378,4 +378,12 @@ std::string DeviceValue::get_fullname() const {
return customname;
}
std::string DeviceValue::get_name(std::string & entity) {
auto pos = entity.find('|');
if (pos != std::string::npos) {
return entity.substr(2, pos - 2);
}
return entity.substr(2);
}
} // namespace emsesp

View File

@@ -186,6 +186,7 @@ class DeviceValue {
bool get_custom_max(uint16_t & val);
std::string get_custom_fullname() const;
std::string get_fullname() const;
static std::string get_name(std::string & entity);
// dv state flags
void add_state(uint8_t s) {

View File

@@ -235,6 +235,7 @@ void WebCustomizationService::device_entities(AsyncWebServerRequest * request, J
// saves it in the customization service
// and updates the entity list real-time
void WebCustomizationService::custom_entities(AsyncWebServerRequest * request, JsonVariant & json) {
bool need_reboot = false;
if (json.is<JsonObject>()) {
// find the device using the unique_id
for (const auto & emsdevice : EMSESP::emsdevices) {
@@ -245,10 +246,47 @@ void WebCustomizationService::custom_entities(AsyncWebServerRequest * request, J
uint8_t device_id = emsdevice->device_id();
// and set the mask and custom names immediately for any listed entities
JsonArray entity_ids_json = json["entity_ids"];
JsonArray entity_ids_json = json["entity_ids"];
std::vector<std::string> entity_ids;
for (const JsonVariant id : entity_ids_json) {
emsdevice->setCustomEntity(id.as<std::string>());
std::string id_s = id.as<std::string>();
if (id_s[0] == '8') {
entity_ids.push_back(id_s);
need_reboot = true;
} else {
emsdevice->setCustomEntity(id_s);
}
// emsesp::EMSESP::logger().info(id.as<const char *>());
}
// add deleted entities from file
EMSESP::webCustomizationService.read([&](WebCustomization & settings) {
for (EntityCustomization entityCustomization : settings.entityCustomizations) {
if (entityCustomization.device_id == device_id) {
for (std::string entity_id : entityCustomization.entity_ids) {
uint8_t mask = Helpers::hextoint(entity_id.substr(0, 2).c_str());
std::string name = DeviceValue::get_name(entity_id);
if (mask & 0x80) {
bool is_set = false;
for (const JsonVariant id : entity_ids_json) {
std::string id_s = id.as<std::string>();
id_s = DeviceValue::get_name(id_s);
if (id_s == name) {
is_set = true;
need_reboot = true;
break;
}
}
if (!is_set) {
entity_ids.push_back(entity_id);
}
}
}
break;
}
}
});
// get list of entities that have masks set or a custom fullname
emsdevice->getCustomEntities(entity_ids);
// Save the list to the customization file
EMSESP::webCustomizationService.update(
@@ -263,18 +301,11 @@ void WebCustomizationService::custom_entities(AsyncWebServerRequest * request, J
}
}
if (!entity_ids_json.size()) {
return StateUpdateResult::UNCHANGED; // nothing to add
}
// create a new entry for this device if there are values
EntityCustomization new_entry;
new_entry.product_id = product_id;
new_entry.device_id = device_id;
// get list of entities that have masks set or a custom fullname
std::vector<std::string> entity_ids;
emsdevice->getCustomEntities(entity_ids);
new_entry.entity_ids = entity_ids;
// add the record and save
@@ -289,7 +320,7 @@ void WebCustomizationService::custom_entities(AsyncWebServerRequest * request, J
}
}
AsyncWebServerResponse * response = request->beginResponse(200); // OK
AsyncWebServerResponse * response = request->beginResponse(need_reboot ? 201 : 200); // OK
request->send(response);
}