From ac2d89abdd61b1eda6dc958dfe800a289aca8134 Mon Sep 17 00:00:00 2001 From: stefan-hoehn Date: Sun, 15 Jun 2025 20:05:12 +0200 Subject: [PATCH] Add blockly "rule enable" block (#3209) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes #3096 Adds a new block to allow enabling/disabling rules. Variables of type Boolean and String are supported. A String with "true" or "enabled" will be converted to true, all other values will result into false. image I also added Boolean to typed variables as this was missing: image I used "as" because I think this is the most intuitive way of describing what the block does, in particular when you use "enabled". If anyone has a better idea to make the block design better, then please provide feedback. Using a dropdown with Note that "Enable/Disable" ruleUID would be nicer but would not allow to use variables. --------- Signed-off-by: Stefan Höhn --- .../definitions/blockly/blocks-scripts.js | 27 +++++++++++++++++++ .../config/controls/blockly-editor.vue | 13 +++++++++ 2 files changed, 40 insertions(+) diff --git a/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-scripts.js b/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-scripts.js index ceaa0f83f..e19b2d929 100644 --- a/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-scripts.js +++ b/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-scripts.js @@ -7,6 +7,7 @@ */ import Blockly from 'blockly' import { javascriptGenerator } from 'blockly/javascript.js' +import { blockGetCheckedInputType } from '@/assets/definitions/blockly/utils.js' export default function defineOHBlocks_Scripts (f7, transformationServices) { /* @@ -324,4 +325,30 @@ export default function defineOHBlocks_Scripts (f7, transformationServices) { const code = block.getFieldValue('inlineScript') + '\n' return code } + + Blockly.Blocks['oh_rule_enable'] = { + init: function () { + this.appendValueInput('ruleUID') + .setCheck('String') + .appendField('Set Rule ') + this.appendValueInput('enable') + .appendField('as ') + .setCheck(['Boolean', 'String']) + this.setInputsInline(true) + this.setPreviousStatement(true, null) + this.setNextStatement(true, null) + this.setColour(0) + this.setTooltip('Allows to enable or disable a rule') + + this.setHelpUrl('https://www.openhab.org/docs/configuration/blockly/rules-blockly-run-and-process.html#enableRule') + } + } + + javascriptGenerator.forBlock['oh_rule_enable'] = function (block) { + const ruleUID = javascriptGenerator.valueToCode(block, 'ruleUID', javascriptGenerator.ORDER_ATOMIC) + const enableValue = javascriptGenerator.valueToCode(block, 'enable', javascriptGenerator.ORDER_ATOMIC) + const enableType = blockGetCheckedInputType(block, 'enable') + let enable = (enableType === 'Boolean') ? enableValue : (enableValue === '\'true\'' || enableValue === '\'enabled\'') + return `rules.setEnabled(${ruleUID}, ${enable});\n` + } } diff --git a/bundles/org.openhab.ui/web/src/components/config/controls/blockly-editor.vue b/bundles/org.openhab.ui/web/src/components/config/controls/blockly-editor.vue index 33d8d4492..691384f26 100644 --- a/bundles/org.openhab.ui/web/src/components/config/controls/blockly-editor.vue +++ b/bundles/org.openhab.ui/web/src/components/config/controls/blockly-editor.vue @@ -1112,6 +1112,18 @@ + + + + ruleUID + + + + + TRUE + + + @@ -1358,6 +1370,7 @@ export default { ['Thing object', 'oh_thingtype'], ['Quantity', 'oh_quantity'], ['String', 'String'], + ['Boolean', 'Boolean'], ['Number', 'Number'], ['Dictionary', 'Dictionary'], ['Colour', 'Colour']