Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[items] Allow Item lookup by name directly on the items namespace #233

Merged
merged 10 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
| Enhancement | `time` | Add isBefore(Date/Time/DateTime) and isAfter(Date/Time/DateTime) | [#227](https://github.com/openhab/openhab-js/pull/227) | No |
| Enhancement | `items` | ItemHistory: return an object with state and timestamp instead of just state where applicable | [#228](https://github.com/openhab/openhab-js/pull/228) | **Yes** |
| Enhancement | `items` | Metadata: Accept Item as param in addition to Item name | [#230](https://github.com/openhab/openhab-js/pull/230) | No |
| Enhancement | `items` | Allow Item lookup by name directly on the `items` namespace | [#233](https://github.com/openhab/openhab-js/pull/233) | No |

Also see the [Release Milestone](https://github.com/openhab/openhab-js/milestone/11).

Expand Down
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ You can now write rules using standard ES6 JavaScript along with the included op
For example, turning a light on:

```javascript
items.getItem("KitchenLight").sendCommand("ON");
console.log("Kitchen Light State", items.getItem("KitchenLight").state);
items.KitchenLight.sendCommand("ON");
console.log("Kitchen Light State", items.KitchenLight.state);
```

Sending a notification
Expand Down Expand Up @@ -339,6 +339,7 @@ The Items namespace allows interactions with openHAB items.
See [openhab-js : items](https://openhab.github.io/openhab-js/items.html) for full API documentation.

- items : `object`
- .NAME ⇒ `Item`
- .getItem(name, nullIfMissing) ⇒ `Item`
- .getItems() ⇒ `Array[Item]`
- .getItemsByTag(...tagNames) ⇒ `Array[Item]`
Expand All @@ -348,13 +349,13 @@ See [openhab-js : items](https://openhab.github.io/openhab-js/items.html) for fu
- .safeItemName(s) ⇒ `string`

```javascript
var item = items.getItem("KitchenLight");
var item = items.KitchenLight;
console.log("Kitchen Light State", item.state);
```

#### `getItem(name, nullIfMissing)`

Calling `getItem(...)` returns an `Item` object with the following properties:
Calling `getItem(...)` or `...` returns an `Item` object with the following properties:

- Item : `object`
- .rawItem ⇒ `HostItem`
Expand Down Expand Up @@ -382,6 +383,7 @@ Calling `getItem(...)` returns an `Item` object with the following properties:
- .removeTags(...tagNames)

```javascript
// Equivalent to items.KitchenLight
var item = items.getItem("KitchenLight");
// Send an ON command
item.sendCommand("ON");
Expand Down Expand Up @@ -490,7 +492,7 @@ Note: `serviceId` is optional, if omitted, the default persistence service will

```javascript
var yesterday = new Date(new Date().getTime() - (24 * 60 * 60 * 1000));
var item = items.getItem('KitchenDimmer');
var item = items.KitchenDimmer;
console.log('KitchenDimmer averageSince', item.history.averageSince(yesterday));
```

Expand All @@ -503,7 +505,7 @@ The `HistoricItem` object contains the following properties, representing Item s

```javascript
var midnight = time.toZDT('00:00');
var historic = items.getItem('KitchenDimmer').history.maximumSince(midnight);
var historic = items.KitchenDimmer.history.maximumSince(midnight);
console.log('KitchenDimmer maximum was ', historic.state, ' at ', historic.timestamp);
```

Expand Down Expand Up @@ -763,7 +765,7 @@ Examples:
```javascript
var now = time.ZonedDateTime.now();
var yesterday = time.ZonedDateTime.now().minusHours(24);
var item = items.getItem("Kitchen");
var item = items.Kitchen;
console.log("averageSince", item.history.averageSince(yesterday));
```

Expand Down Expand Up @@ -803,7 +805,7 @@ When you have a `time.ZonedDateTime`, a new `toToday()` method was added which w
For example, if the time was 13:45 and today was a DST changeover, the time will still be 13:45 instead of one hour off.

```javascript
var alarm = items.getItem('Alarm');
var alarm = items.Alarm;
alarm.postUpdate(time.toZDT(alarm).toToday());
```

Expand Down Expand Up @@ -835,6 +837,7 @@ Tests whether this `time.ZonedDateTime` is after the time passed in `timestamp`,
`timestamp` can be anything supported by `time.toZDT()`.

```javascript
// Equivalent to items.Sunset
time.toZDT().isAfterTime(items.getItem('Sunset')) // is now after sunset?
time.toZDT().isAfterDateTime('2022-12-01T12:00Z') // is now after 2022-12-01 noon?
```
Expand All @@ -850,7 +853,9 @@ Examples:

```javascript
time.toZDT().isBetweenTimes('22:00', '05:00') // currently between 11:00 pm and 5:00 am
// Equivalent to items.Sunset
time.toZDT().isBetweenTimes(items.getItem('Sunset'), '11:30 PM') // is now between sunset and 11:30 PM?
// Equivalent to items.StartTime
time.toZDT(items.getItem('StartTime')).isBetweenTimes(time.toZDT(), 'PT1H'); // is the state of StartTime between now and one hour from now
```

Expand Down Expand Up @@ -1020,6 +1025,7 @@ rules.JSRule({
description: "Light will turn on when it's 5:00pm",
triggers: [triggers.GenericCronTrigger("0 0 17 * * ?")],
execute: (event) => {
// Equivalent to items.BalconyLights.sendCommand("ON")
items.getItem("BalconyLights").sendCommand("ON");
actions.NotificationAction.sendNotification(email, "Balcony lights are ON");
},
Expand Down
39 changes: 22 additions & 17 deletions items/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ const safeItemName = s => s
.replace(/["']/g, '') // delete
.replace(/[^a-zA-Z0-9]/g, '_'); // replace with underscore

module.exports = {
const itemProperties = {
safeItemName,
getItem,
getItems,
Expand All @@ -568,21 +568,26 @@ module.exports = {
createItem,
removeItem,
Item,
/**
* Custom indexer, to allow static Item lookup.
* @example
* let { my_object_name } = require('openhab').items.objects;
* ...
* let my_state = my_object_name.state; //my_object_name is an Item
*
* @returns {object} a collection of items allowing indexing by Item name
*/
objects: () => new Proxy({}, {
get: function (target, name) {
if (typeof name === 'string' && /^-?\d+$/.test(name)) { return getItem(name); }

throw Error('unsupported function call: ' + name);
}
}),
metadata
};

/**
* Gets an openHAB Item by name directly on the {@link items} namespace.
* Equivalent to {@link items.getItem}
*
* @example
* // retrieve item by name directly on the items namespace
* console.log(items.KitchenLight.state) // returns 'ON'
* // equivalent to
* console.log(items.getItem('KitchenLight').state) // returns 'ON'
*
* @name NAME
* @memberof items
* @function
* @returns {Item|null} {@link items.Item} Item or `null` if Item is missing
*/
module.exports = new Proxy(itemProperties, {
get: function (target, prop) {
return target[prop] || target.getItem(prop, true);
}
});
1 change: 0 additions & 1 deletion types/items/items.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,5 @@ declare namespace time {
type ZonedDateTime = import('@js-joda/core').ZonedDateTime;
}
import time = require("../time");
export declare function objects(): any;
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.