From 5ca4cf4acd758b9c8163c3f8d07cb9903d91e118 Mon Sep 17 00:00:00 2001 From: Der Mundschenk & Compagnie Date: Wed, 27 Nov 2024 08:11:07 +0100 Subject: [PATCH] fix: Home Assistant: template errors when `action` key is missing or empty in state payload (#24931) --- lib/extension/homeassistant.ts | 5 +++-- test/homeassistant.test.js | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/extension/homeassistant.ts b/lib/extension/homeassistant.ts index 3d8febd529..8937d871ca 100644 --- a/lib/extension/homeassistant.ts +++ b/lib/extension/homeassistant.ts @@ -2270,9 +2270,10 @@ export default class HomeAssistant extends Extension { const value_template = `{% set patterns = [\n${patterns}\n] %}\n` + - `{% set ns = namespace(r=[('event_type', value_json.action)]) %}\n` + + `{% set action_value = value_json.action|default(None) %}\n` + + `{% set ns = namespace(r=[('action', action_value)]) %}\n` + `{% for p in patterns %}\n` + - ` {% set m = value_json.action|regex_findall(p.pattern) %}\n` + + ` {% set m = action_value|regex_findall(p.pattern) %}\n` + ` {% if m[0] is undefined %}{% continue %}{% endif %}\n` + ` {% for key, value in zip(p.groups, m[0]) %}\n` + ` {% set ns.r = ns.r + [(key, value)] %}\n` + diff --git a/test/homeassistant.test.js b/test/homeassistant.test.js index 8fb2f95a0f..ce72125d89 100644 --- a/test/homeassistant.test.js +++ b/test/homeassistant.test.js @@ -428,7 +428,7 @@ describe('HomeAssistant extension', () => { unique_id: '0x0017880104e45520_action_zigbee2mqtt', // Needs to be updated whenever one of the ACTION_*_PATTERN constants changes. value_template: - '{% set patterns = [\n{"pattern": \'^(?P