diff --git a/items/managed.js b/items/managed.js index 8443fd039..f2f865219 100644 --- a/items/managed.js +++ b/items/managed.js @@ -6,7 +6,7 @@ const log = require('../log')('items'); const metadata = require('../metadata'); const ItemHistory = require('./item-history'); -const { UnDefType, events, itemRegistry } = require('@runtime'); +const { UnDefType, OnOffType, OpenClosedType, events, itemRegistry } = require('@runtime'); const itemBuilderFactory = osgi.getService("org.openhab.core.items.ItemBuilderFactory"); @@ -181,6 +181,55 @@ class Item { return false; } + + /** + * Calculates the toggled state of this Item. For Items like Color and + * Dimmer, getStateAs(OnOffType) is used and the toggle calculated off + * of that. + * @returns the toggled state (e.g. 'OFF' if the Item is 'ON') + * @throws error if the Item is uninitialized or is a type that doesn't make sense to toggle + */ + #getToggleState() { + if(this.isUninitialized) { + throw Error('Cannot toggle uninitialized Items'); + } + switch (this.type) { + case 'PlayerItem' : + return this.state == 'PAUSE' ? 'PLAY' : 'PAUSE'; + case 'ContactItem' : + return this.state == 'OPEN' ? 'CLOSED' : 'OPEN'; + default: { + const oldState = this.rawItem.getStateAs(OnOffType); + if(oldState) { + return oldState.toString() == 'ON' ? 'OFF' : 'ON'; + } + else { + throw Error('Toggle not supported for items of type ' + this.type); + } + } + } + } + + /** + * Sends a command to flip the Item's state (e.g. if it is 'ON' an 'OFF' + * command is sent). + * @throws error if the Item is uninitialized or a type that cannot be toggled or commanded + */ + sendToggleCommand() { + if(this.type == 'ContactItem'){ + throw Error('Cannot command Contact Items'); + } + this.sendCommand(this.#getToggleState()); + } + + /** + * Posts an update to flip the Item's state (e.g. if it is 'ON' an 'OFF' + * update is posted). + * @throws error if the Item is uninitialized or a type that cannot be toggled + */ + postToggleUpdate() { + this.postUpdate(this.#getToggleState()); + } /** * Posts an update to the item diff --git a/rules/operation-builder.js b/rules/operation-builder.js index 694f662e1..90cc10457 100644 --- a/rules/operation-builder.js +++ b/rules/operation-builder.js @@ -403,22 +403,7 @@ class ToggleOperation extends OperationConfig { * @returns {OperationBuilder.SendCommandOrUpdateOperation} this */ doToggle() { - let item = items.getItem(this.itemName); - - switch (item.type) { - case "SwitchItem": { - let toSend = ('ON' == item.state) ? 'OFF' : 'ON'; - item.sendCommand(toSend); - break; - } - case "ColorItem": { - let toSend = ('0' != item.rawState.getBrightness().toString()) ? 'OFF' : 'ON'; - item.sendCommand(toSend); - break; - } - default: - throw Error(`Toggle not supported for items of type ${item.type}`); - } + items.getItem(this.itemName).sendToggleCommand(); } }