From 84a59baf9bb586aa40a47996c30703b04161bf02 Mon Sep 17 00:00:00 2001 From: stefan-hoehn Date: Sun, 17 Sep 2023 14:54:46 +0200 Subject: [PATCH] [blockly] Allow `item xxx` to be used for `get xxx of item` (#2037) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The block can now take both “get item item xxx” or (new) “item xxx” directly. I have also added some hint in the help tooltip that clarifies how to work with the block when using it through variables because Blockly has to make an assumption due to the fact that it cannot detect what is provided in the variable. See https://community.openhab.org/t/less-then-greater-then-comparison-return-value-seems-to-be-inverted/148910/9. Signed-off-by: Stefan Höhn --- .../src/assets/definitions/blockly/blocks-items.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-items.js b/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-items.js index 2e7d8e1e5a..570347dbcb 100644 --- a/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-items.js +++ b/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-items.js @@ -8,6 +8,7 @@ import { javascriptGenerator } from 'blockly/javascript' import { FieldItemModelPicker } from './fields/item-field' import { FieldThingPicker } from './fields/thing-field' import api from '@/js/openhab/api' +import { blockGetCheckedInputType } from './utils' export default function (f7, isGraalJs) { /* Helper block to allow selecting an item */ @@ -190,7 +191,7 @@ export default function (f7, isGraalJs) { block._updateType(newMode) }) this.appendValueInput('item') - .setCheck('oh_itemtype') + .setCheck(['oh_itemtype', 'oh_item']) .appendField('get ') .appendField(dropdown, 'attributeName') .appendField('of item') @@ -201,7 +202,7 @@ export default function (f7, isGraalJs) { this.setTooltip('Retrieve a specific attribute from the item. Note that groups and tags return a list and should be used with the loops-block \'for each item ... in list\'. ') this.setTooltip(function () { const attributeName = block.getFieldValue('attributeName') - const TIP = { + let TIP = { 'Name': 'name of the Item (string)', 'Label': 'label of the Item (string)', 'State': 'state of the Item (string)', @@ -212,7 +213,7 @@ export default function (f7, isGraalJs) { 'NumericState': 'numeric state of the Item (number)', 'QuantityState': 'Unit of Measurement / quantity state of Item (Quantity)' } - return TIP[attributeName] + return TIP[attributeName] + ' \n Note: make sure to use "get item xxx"-Block for the connected block when working with Variables, not "item xxx"-Block' }) this.setHelpUrl('https://www.openhab.org/docs/configuration/blockly/rules-blockly-items-things.html#get-particular-attributes-of-an-item') }, @@ -256,11 +257,13 @@ export default function (f7, isGraalJs) { */ javascriptGenerator['oh_getitem_attribute'] = function (block) { const theItem = javascriptGenerator.valueToCode(block, 'item', javascriptGenerator.ORDER_ATOMIC) + const inputType = blockGetCheckedInputType(block, 'item') let attributeName = block.getFieldValue('attributeName') if (isGraalJs) { attributeName = attributeName.charAt(0).toLowerCase() + attributeName.slice(1) - return [`${theItem}.${attributeName}`, 0] + let code = (inputType === 'oh_item') ? `items.getItem(${theItem}).${attributeName}` : `${theItem}.${attributeName}` + return [code, 0] } else { if (attributeName === 'Tags' || attributeName === 'GroupNames') { return [`Java.from(${theItem}.get${attributeName}())`, 0]