Skip to content

Commit

Permalink
[items] Item: Fix sendCommandIfDifferent fails to compare Quantity (#343
Browse files Browse the repository at this point in the history
)

Fixes #330.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
  • Loading branch information
florian-h05 authored Jun 16, 2024
1 parent 104bbfc commit 84820e6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
27 changes: 22 additions & 5 deletions src/items/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const osgi = require('../osgi');
const utils = require('../utils');
const log = require('../log')('items');
const { _toOpenhabPrimitiveType } = require('../helpers');
const { _toOpenhabPrimitiveType, _isQuantity } = require('../helpers');
const { getQuantity, QuantityError } = require('../quantity');

const { UnDefType, OnOffType, events, itemRegistry } = require('@runtime');
Expand Down Expand Up @@ -245,12 +245,29 @@ class Item {
* @see sendCommand
*/
sendCommandIfDifferent (value) {
// value and current state both are Quantity and have equal value
if (_isQuantity(value) && this.quantityState !== null) {
if (this.quantityState.equal(value)) {
return false;
}
}

// value and current state are both numeric and have equal value
if (typeof value === 'number' && this.numericState !== null) {
if (value === this.numericState) {
return false;
}
}

// stringified value and string state are equal
value = _toOpenhabPrimitiveType(value);
if (value.toString() !== this.state) {
this.sendCommand(value);
return true;
if (value.toString() === this.state) {
return false;
}
return false;

// else send the command
this.sendCommand(value);
return true;
}

/**
Expand Down
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 84820e6

Please sign in to comment.