Skip to content

Commit a539e28

Browse files
authored
[items] Fix NULL Item state not recognized as null (#448)
Fixes #446. Signed-off-by: Florian Hotze <dev@florianhotze.com>
1 parent 0d695c8 commit a539e28

7 files changed

Lines changed: 34 additions & 22 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
## Unreleased
44

5-
| Type | Namespace | Description | | Reference | Breaking |
6-
|-------------|---------------|-----------------------------------------------------------------------------------|:--|--------------------------------------------------------|----------|
7-
| Enhancement | `environment` | Add `isFileBasedScript()` function to determine whether script is file-based | | [#441](https://github.com/openhab/openhab-js/pull/441) | no |
8-
| Enhancement | `items` | Add support for providing Items, metadata & channel links from file-based scripts | | [#441](https://github.com/openhab/openhab-js/pull/441) | no |
5+
| Type | Namespace | Description | Reference | Breaking |
6+
|-------------|---------------|-----------------------------------------------------------------------------------|--------------------------------------------------------|----------|
7+
| Enhancement | `environment` | Add `isFileBasedScript()` function to determine whether script is file-based | [#441](https://github.com/openhab/openhab-js/pull/441) | no |
8+
| Enhancement | `items` | Add support for providing Items, metadata & channel links from file-based scripts | [#441](https://github.com/openhab/openhab-js/pull/441) | no |
9+
| Bugfix | `items` | Fix `NULL` Item state (UnDefType) not recognized as `null` | [#448](https://github.com/openhab/openhab-js/pull/448) | no |
910

1011
## 5.11.1 (5.11.0)
1112

src/items/helpers.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
// Helper functions used internally across the items namespace
22

3-
const { PercentType } = require('@runtime');
3+
const { UnDefType, PercentType } = require('@runtime');
44

55
const { getQuantity, QuantityError } = require('../quantity');
66

7+
/**
8+
* Check if a value is `null`, `undefined` or an instance of {@link https://www.openhab.org/javadoc/latest/org/openhab/core/types/undeftype `UnDefType`}.
9+
*
10+
* @private
11+
* @param {*} value
12+
* @returns {boolean}
13+
*/
14+
function _isNullOrUndefined (value) {
15+
return value === null || value === undefined || value instanceof UnDefType;
16+
}
17+
718
/**
819
* Return a string representation of a state.
920
*
@@ -12,7 +23,7 @@ const { getQuantity, QuantityError } = require('../quantity');
1223
* @returns {string|null} string representation or `null` if `rawState` was `null`
1324
*/
1425
function _stateOrNull (rawState) {
15-
if (rawState === null) return null;
26+
if (_isNullOrUndefined(rawState)) return null;
1627
return rawState.toString();
1728
}
1829

@@ -25,7 +36,7 @@ function _stateOrNull (rawState) {
2536
* @returns {number|null} numeric representation or `null` if `rawState` was `null`
2637
*/
2738
function _numericStateOrNull (rawState, type) {
28-
if (rawState === null) return null;
39+
if (_isNullOrUndefined(rawState)) return null;
2940
let state = rawState.toString();
3041
if (type === 'Color') state = rawState.as(PercentType).toString();
3142
const numericState = parseFloat(state);
@@ -41,7 +52,7 @@ function _numericStateOrNull (rawState, type) {
4152
* @throws failed to create quantityState
4253
*/
4354
function _quantityStateOrNull (rawState) {
44-
if (rawState === null) return null;
55+
if (_isNullOrUndefined(rawState)) return null;
4556
try {
4657
const qty = getQuantity(rawState);
4758
return (qty !== null && qty.symbol !== null) ? qty : null;
@@ -55,6 +66,7 @@ function _quantityStateOrNull (rawState) {
5566
}
5667

5768
module.exports = {
69+
_isNullOrUndefined,
5870
_stateOrNull,
5971
_numericStateOrNull,
6072
_quantityStateOrNull

src/items/items.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ const cache = require('../cache');
1212
const time = require('../time');
1313
const environment = require('../environment');
1414

15-
const { OnOffType, UnDefType, events } = require('@runtime');
15+
const { OnOffType, events } = require('@runtime');
1616
const itemRegistry = environment.useProviderRegistries()
1717
? require('@runtime/provider').itemRegistry
1818
: require('@runtime').itemRegistry;
1919

20-
const { _stateOrNull, _numericStateOrNull, _quantityStateOrNull } = require('./helpers');
20+
const { _stateOrNull, _numericStateOrNull, _quantityStateOrNull, _isNullOrUndefined } = require('./helpers');
2121
const metadata = require('./metadata');
2222
const itemChannelLink = require('./itemchannellink');
2323
const ItemPersistence = require('./item-persistence');
@@ -251,10 +251,7 @@ class Item {
251251
* @type {boolean}
252252
*/
253253
get isUninitialized () {
254-
return (this.rawItem.getState() instanceof UnDefType ||
255-
this.rawItem.getState().toString() === 'Undefined' ||
256-
this.rawItem.getState().toString() === 'Uninitialized'
257-
);
254+
return _isNullOrUndefined(this.rawState);
258255
}
259256

260257
/**

src/things.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,6 @@ class Thing {
145145
}
146146
}
147147

148-
function addThing () {
149-
150-
}
151-
152148
/**
153149
* Gets an openHAB Thing.
154150
* Returns `null` if no Thing with the given UID exists.
@@ -175,7 +171,6 @@ function getThings () {
175171

176172
module.exports = {
177173
Thing,
178-
addThing,
179174
getThing,
180175
getThings
181176
};

types/items/items.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

types/quantity.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@ export type Item = {
22
rawItem: HostItem;
33
persistence: import("./items/item-persistence");
44
semantics: import("./items/item-semantics");
5-
readonly type: string;
5+
readonly type: string; /**
6+
* @type {QuantityType}
7+
* @private
8+
*/
69
readonly name: string;
10+
/**
11+
* @type {QuantityType}
12+
* @private
13+
*/
714
readonly label: string;
815
readonly state: string;
916
readonly numericState: number;

types/quantity.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)