Skip to content

Commit

Permalink
[items] Accept Quantity as argument for postUpdate & sendCommand
Browse files Browse the repository at this point in the history
Reported at the community:
https://community.openhab.org/t/openhab-js-uom-postupdate-and-persisten
ce/145666?u=florian-h05.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
  • Loading branch information
florian-h05 committed Apr 2, 2023
1 parent c03241d commit bdd1b98
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,9 @@ Calling `getItem(...)` or `...` returns an `Item` object with the following prop
- .getMetadata(namespace) ⇒ `object|null`
- .replaceMetadata(namespace, value, configuration) ⇒ `object`
- .removeMetadata(namespace) ⇒ `object|null`
- .sendCommand(value): `value` can be a string or a [`time.ZonedDateTime`](#time)
- .sendCommandIfDifferent(value) ⇒ `boolean`: `value` can be a string or a [`time.ZonedDateTime`](#time)
- .postUpdate(value): `value` can be a string or a [`time.ZonedDateTime`](#time)
- .sendCommand(value): `value` can be a string, a [`time.ZonedDateTime`](#time) or a [`Quantity`](#quantity)
- .sendCommandIfDifferent(value) ⇒ `boolean`: `value` can be a string, a [`time.ZonedDateTime`](#time) or a [`Quantity`](#quantity)
- .postUpdate(value): `value` can be a string, a [`time.ZonedDateTime`](#time) or a [`Quantity`](#quantity)
- .addGroups(...groupNamesOrItems)
- .removeGroups(...groupNamesOrItems)
- .addTags(...tagNames)
Expand Down
38 changes: 28 additions & 10 deletions items/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const ItemSemantics = require('./item-semantics');
const Quantity = require('../quantity');
const { QuantityError } = require('../quantity');
/** @typedef {import('@js-joda/core').ZonedDateTime} time.ZonedDateTime */
const time = require('../time');

const { UnDefType, OnOffType, events, itemRegistry } = require('@runtime');

Expand Down Expand Up @@ -52,6 +51,27 @@ const managedItemProvider = osgi.getService('org.openhab.core.items.ManagedItemP
*/
const DYNAMIC_ITEM_TAG = '_DYNAMIC_';

/**
* Helper function to convert JS types to openHAB command/state types' string representation.
*
* Numbers and strings are passed.
* Objects should implement `toOpenHabString` (prioritized) or `toString` to return a openHAB compatible representation.
*
* @private
* @param {*} value
* @returns {*}
*/
const _toOpenhabString = (value) => {
if (typeof value === 'number' || typeof value === 'string') {
return value;
} else if (typeof value.toOpenHabString === 'function') {
return value.toOpenHabString();
} else if (typeof value.toString === 'function') {
return value.toString();
}
return value;
};

/**
* Class representing an openHAB Item
*
Expand Down Expand Up @@ -227,25 +247,24 @@ class Item {
/**
* Sends a command to the Item.
*
* @param {string|time.ZonedDateTime|HostState} value the value of the command to send, such as 'ON'
* @param {string|time.ZonedDateTime|Quantity|HostState} value the value of the command to send, such as 'ON'
* @see sendCommandIfDifferent
* @see postUpdate
*/
sendCommand (value) {
if (value instanceof time.ZonedDateTime) value = value.toOpenHabString();
events.sendCommand(this.rawItem, value);
events.sendCommand(this.rawItem, _toOpenhabString(value));
}

/**
* Sends a command to the Item, but only if the current state is not what is being sent.
*
* @param {string|time.ZonedDateTime|HostState} value the value of the command to send, such as 'ON'
* @param {string|time.ZonedDateTime|Quantity|HostState} value the value of the command to send, such as 'ON'
* @returns {boolean} true if the command was sent, false otherwise
* @see sendCommand
*/
sendCommandIfDifferent (value) {
if (value instanceof time.ZonedDateTime) value = value.toOpenHabString();
if (value.toString() !== this.state.toString()) {
value = _toOpenhabString(value);
if (value.toString() !== this.state) {
this.sendCommand(value);
return true;
}
Expand Down Expand Up @@ -303,13 +322,12 @@ class Item {
/**
* Posts an update to the Item.
*
* @param {string|time.ZonedDateTime|HostState} value the value of the command to send, such as 'ON'
* @param {string|time.ZonedDateTime|Quantity|HostState} value the value of the command to send, such as 'ON'
* @see postToggleUpdate
* @see sendCommand
*/
postUpdate (value) {
if (value instanceof time.ZonedDateTime) value = value.toOpenHabString();
events.postUpdate(this.rawItem, value);
events.postUpdate(this.rawItem, _toOpenhabString(value));
}

/**
Expand Down
13 changes: 6 additions & 7 deletions types/items/items.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,19 +243,19 @@ export class Item {
/**
* Sends a command to the Item.
*
* @param {string|time.ZonedDateTime|HostState} value the value of the command to send, such as 'ON'
* @param {string|time.ZonedDateTime|Quantity|HostState} value the value of the command to send, such as 'ON'
* @see sendCommandIfDifferent
* @see postUpdate
*/
sendCommand(value: string | time.ZonedDateTime | HostState): void;
sendCommand(value: string | time.ZonedDateTime | import("../quantity").QuantityClass | HostState): void;
/**
* Sends a command to the Item, but only if the current state is not what is being sent.
*
* @param {string|time.ZonedDateTime|HostState} value the value of the command to send, such as 'ON'
* @param {string|time.ZonedDateTime|Quantity|HostState} value the value of the command to send, such as 'ON'
* @returns {boolean} true if the command was sent, false otherwise
* @see sendCommand
*/
sendCommandIfDifferent(value: string | time.ZonedDateTime | HostState): boolean;
sendCommandIfDifferent(value: string | time.ZonedDateTime | import("../quantity").QuantityClass | HostState): boolean;
/**
* Calculates the toggled state of this Item. For Items like Color and
* Dimmer, getStateAs(OnOffType) is used and the toggle calculated off
Expand All @@ -279,11 +279,11 @@ export class Item {
/**
* Posts an update to the Item.
*
* @param {string|time.ZonedDateTime|HostState} value the value of the command to send, such as 'ON'
* @param {string|time.ZonedDateTime|Quantity|HostState} value the value of the command to send, such as 'ON'
* @see postToggleUpdate
* @see sendCommand
*/
postUpdate(value: string | time.ZonedDateTime | HostState): void;
postUpdate(value: string | time.ZonedDateTime | import("../quantity").QuantityClass | HostState): void;
/**
* Gets the names of the groups this Item is member of.
* @returns {string[]}
Expand Down Expand Up @@ -322,6 +322,5 @@ import ItemSemantics = require("./item-semantics");
declare namespace time {
type ZonedDateTime = import('@js-joda/core').ZonedDateTime;
}
import time = require("../time");
export { metadata };
//# sourceMappingURL=items.d.ts.map
2 changes: 1 addition & 1 deletion types/items/items.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bdd1b98

Please sign in to comment.