From 28c2697031c732fcbcd6b35facc7cc829ef672c9 Mon Sep 17 00:00:00 2001 From: Jorge Schrauwen Date: Tue, 5 Jul 2022 15:57:16 +0200 Subject: [PATCH] Ensure filtered_attributes and filtered_optimistic are also regex Bring filtered_attributes and filtered_optimistic in line with the newer filtered_cache. All three of them are now regex matches instead of absolute matches. --- lib/controller.ts | 6 +++++- lib/extension/publish.ts | 9 ++++++++- lib/util/settings.schema.json | 12 ++++++------ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/controller.ts b/lib/controller.ts index 31c946216..ad42d5f14 100644 --- a/lib/controller.ts +++ b/lib/controller.ts @@ -267,7 +267,11 @@ class Controller { // Filter mqtt message attributes if (entity.options.filtered_attributes) { - entity.options.filtered_attributes.forEach((a) => delete message[a]); + for (const property of Object.keys(message)) { + if (entity.options.filtered_attributes.find((p) => property.match(p))) { + delete message[property]; + } + } } if (Object.entries(message).length) { diff --git a/lib/extension/publish.ts b/lib/extension/publish.ts index d98966651..11acc3256 100644 --- a/lib/extension/publish.ts +++ b/lib/extension/publish.ts @@ -256,7 +256,14 @@ export default class Publish extends Extension { } // filter out attribute listed in filtered_optimistic - entitySettings.filtered_optimistic?.forEach((a) => delete msg[a]); + if (entitySettings.filtered_optimistic) { + for (const property of Object.keys(msg)) { + if (entitySettings.filtered_optimistic.find((p) => property.match(p))) { + delete msg[property]; + } + } + } + addToToPublish(re, msg); } diff --git a/lib/util/settings.schema.json b/lib/util/settings.schema.json index 4ffa20d5a..bdd57d347 100644 --- a/lib/util/settings.schema.json +++ b/lib/util/settings.schema.json @@ -817,27 +817,27 @@ "items": { "type": "string" }, - "examples": ["temperature", "battery", "action"], + "examples": ["^temperature$", "^battery$", "^action$"], "title": "Filtered publish attributes", - "description": "Filter attributes from publish payload." + "description": "Filter attributes with regex from published payload." }, "filtered_cache": { "type": "array", "items": { "type": "string" }, - "examples": ["action", "input_actions"], + "examples": ["^input_actions$"], "title": "Filtered attributes from cache", - "description": "Filter attributes from being added to the cache, this prevents the attribute from being in the published payload when the value didn't change." + "description": "Filter attributes with regex from being added to the cache, this prevents the attribute from being in the published payload when the value didn't change." }, "filtered_optimistic": { "type": "array", "items": { "type": "string" }, - "examples": ["color_mode", "color_temp", "color"], + "examples": ["^color_(mode|temp)$", "color"], "title": "Filtered optimistic attributes", - "description": "Filter attributes from optimistic publish payload when calling /set. (This has no effect if optimistic is set to false)." + "description": "Filter attributes with regex from optimistic publish payload when calling /set. (This has no effect if optimistic is set to false)." }, "icon": { "type": "string",