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

Use instanceof helpers for Item and Quantity class instead of instanceof #365

Merged
merged 1 commit into from
Jul 24, 2024
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
4 changes: 2 additions & 2 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, _isQuantity } = require('../helpers');
const { _toOpenhabPrimitiveType, _isQuantity, _isItem } = require('../helpers');
const { getQuantity, QuantityError } = require('../quantity');

const { UnDefType, OnOffType, events, itemRegistry } = require('@runtime');
Expand Down Expand Up @@ -500,7 +500,7 @@ function removeItem (itemOrItemName) {

if (typeof itemOrItemName === 'string') {
itemName = itemOrItemName;
} else if (itemOrItemName instanceof Item) {
} else if (_isItem(itemOrItemName)) {
itemName = itemOrItemName.name;
} else {
log.warn('Item name is undefined (no Item supplied or supplied name is not a string) so cannot be removed');
Expand Down
4 changes: 2 additions & 2 deletions src/quantity.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { _isItem } = require('./helpers');
const { _isItem, _isQuantity } = require('./helpers');
const QuantityType = Java.type('org.openhab.core.library.types.QuantityType');
/**
* @type {JavaBigDecimal}
Expand Down Expand Up @@ -65,7 +65,7 @@ function _toQtyType (value, errorMsg = 'Argument of wrong type provided, require
} catch (e) {
throw new QuantityError(`Failed to create QuantityType from ${value}: ${e}`);
}
} else if (value instanceof Quantity) {
} else if (_isQuantity(value)) {
value = QuantityType.valueOf(value.rawQtyType.toString()); // Avoid referencing the same underlying QuantityType, so "clone" it
} else {
throw new TypeError(errorMsg);
Expand Down
24 changes: 12 additions & 12 deletions src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

const log = require('./log')('triggers');
const { randomUUID } = require('./utils');
const { _isItem } = require('./helpers');

const ModuleBuilder = Java.type('org.openhab.core.automation.util.ModuleBuilder');
const Configuration = Java.type('org.openhab.core.config.core.Configuration');

/**
* @type {Item}
* @typedef { import("./items/items").Item } Item
* @private
*/
const Item = require('./items').Item;

const ModuleBuilder = Java.type('org.openhab.core.automation.util.ModuleBuilder');
const Configuration = Java.type('org.openhab.core.config.core.Configuration');

/**
* Creates a trigger. Internal function, instead use predefined trigger types.
Expand Down Expand Up @@ -77,7 +77,7 @@ const ChannelEventTrigger = (channel, event, triggerName) =>
*/
const ItemStateChangeTrigger = (itemOrName, oldState, newState, triggerName) =>
_createTrigger('core.ItemStateChangeTrigger', triggerName, {
itemName: (itemOrName instanceof Item) ? itemOrName.name : itemOrName,
itemName: _isItem(itemOrName) ? itemOrName.name : itemOrName,
state: newState,
previousState: oldState
});
Expand All @@ -96,7 +96,7 @@ const ItemStateChangeTrigger = (itemOrName, oldState, newState, triggerName) =>
*/
const ItemStateUpdateTrigger = (itemOrName, state, triggerName) =>
_createTrigger('core.ItemStateUpdateTrigger', triggerName, {
itemName: (itemOrName instanceof Item) ? itemOrName.name : itemOrName,
itemName: _isItem(itemOrName) ? itemOrName.name : itemOrName,
state
});

Expand All @@ -114,7 +114,7 @@ const ItemStateUpdateTrigger = (itemOrName, state, triggerName) =>
*/
const ItemCommandTrigger = (itemOrName, command, triggerName) =>
_createTrigger('core.ItemCommandTrigger', triggerName, {
itemName: (itemOrName instanceof Item) ? itemOrName.name : itemOrName,
itemName: _isItem(itemOrName) ? itemOrName.name : itemOrName,
command
});

Expand All @@ -132,7 +132,7 @@ const ItemCommandTrigger = (itemOrName, command, triggerName) =>
*/
const GroupStateChangeTrigger = (groupOrName, oldState, newState, triggerName) =>
_createTrigger('core.GroupStateChangeTrigger', triggerName, {
groupName: (groupOrName instanceof Item) ? groupOrName.name : groupOrName,
groupName: _isItem(groupOrName) ? groupOrName.name : groupOrName,
state: newState,
previousState: oldState
});
Expand All @@ -150,7 +150,7 @@ const GroupStateChangeTrigger = (groupOrName, oldState, newState, triggerName) =
*/
const GroupStateUpdateTrigger = (groupOrName, state, triggerName) =>
_createTrigger('core.GroupStateUpdateTrigger', triggerName, {
groupName: (groupOrName instanceof Item) ? groupOrName.name : groupOrName,
groupName: _isItem(groupOrName) ? groupOrName.name : groupOrName,
state
});

Expand All @@ -167,7 +167,7 @@ const GroupStateUpdateTrigger = (groupOrName, state, triggerName) =>
*/
const GroupCommandTrigger = (groupOrName, command, triggerName) =>
_createTrigger('core.GroupCommandTrigger', triggerName, {
groupName: (groupOrName instanceof Item) ? groupOrName.name : groupOrName,
groupName: _isItem(groupOrName) ? groupOrName.name : groupOrName,
command
});

Expand Down Expand Up @@ -269,7 +269,7 @@ const TimeOfDayTrigger = (time, triggerName) =>
*/
const DateTimeTrigger = (itemOrName, timeOnly = false, triggerName) =>
_createTrigger('timer.DateTimeTrigger', triggerName, {
itemName: (itemOrName instanceof Item) ? itemOrName.name : itemOrName,
itemName: _isItem(itemOrName) ? itemOrName.name : itemOrName,
timeOnly
});

Expand Down
59 changes: 52 additions & 7 deletions types/triggers.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
export type Item = {
rawItem: HostItem;
persistence: import("./items/item-persistence");
semantics: import("./items/item-semantics");
readonly type: string;
readonly name: string;
readonly label: string;
readonly state: string;
readonly numericState: number;
readonly quantityState: import("./quantity").Quantity;
readonly rawState: HostState;
readonly members: any[];
readonly descendents: any[];
readonly isUninitialized: boolean;
getMetadata(namespace?: string): {
value: string;
configuration: any;
} | {
namespace: {
value: string;
configuration: any;
};
};
replaceMetadata(namespace: string, value: string, configuration?: any): {
configuration: any;
value: string;
};
removeMetadata(namespace?: string): {
value: string;
configuration: any;
};
sendCommand(value: any): void;
sendCommandIfDifferent(value: any): boolean;
getToggleState(): "PAUSE" | "PLAY" | "OPEN" | "CLOSED" | "ON" | "OFF";
sendToggleCommand(): void;
postToggleUpdate(): void;
postUpdate(value: any): void;
readonly groupNames: string[];
addGroups(...groupNamesOrItems: any[]): void;
removeGroups(...groupNamesOrItems: any[]): void;
readonly tags: string[];
addTags(...tagNames: string[]): void;
removeTags(...tagNames: string[]): void;
toString(): any;
};
/**
* Creates a trigger that fires upon specific events in a channel.
*
Expand Down Expand Up @@ -26,7 +71,7 @@ export function ChannelEventTrigger(channel: string, event: string, triggerName?
* @param {string} [newState] the new state of the Item
* @param {string} [triggerName] the optional name of the trigger to create
*/
export function ItemStateChangeTrigger(itemOrName: any | string, oldState?: string, newState?: string, triggerName?: string): HostTrigger;
export function ItemStateChangeTrigger(itemOrName: Item | string, oldState?: string, newState?: string, triggerName?: string): HostTrigger;
/**
* Creates a trigger that fires upon an Item receiving a state update. Note that the Item does not need to change state.
*
Expand All @@ -39,7 +84,7 @@ export function ItemStateChangeTrigger(itemOrName: any | string, oldState?: stri
* @param {string} [state] the new state of the Item
* @param {string} [triggerName] the optional name of the trigger to create
*/
export function ItemStateUpdateTrigger(itemOrName: any | string, state?: string, triggerName?: string): HostTrigger;
export function ItemStateUpdateTrigger(itemOrName: Item | string, state?: string, triggerName?: string): HostTrigger;
/**
* Creates a trigger that fires upon an Item receiving a command. Note that the Item does not need to change state.
*
Expand All @@ -52,7 +97,7 @@ export function ItemStateUpdateTrigger(itemOrName: any | string, state?: string,
* @param {string} [command] the command received
* @param {string} [triggerName] the optional name of the trigger to create
*/
export function ItemCommandTrigger(itemOrName: any | string, command?: string, triggerName?: string): HostTrigger;
export function ItemCommandTrigger(itemOrName: Item | string, command?: string, triggerName?: string): HostTrigger;
/**
* Creates a trigger that fires upon a member of a group changing state. Note that group Item does not need to change state.
*
Expand All @@ -65,7 +110,7 @@ export function ItemCommandTrigger(itemOrName: any | string, command?: string, t
* @param {string} [newState] the new state of the group
* @param {string} [triggerName] the optional name of the trigger to create
*/
export function GroupStateChangeTrigger(groupOrName: any | string, oldState?: string, newState?: string, triggerName?: string): HostTrigger;
export function GroupStateChangeTrigger(groupOrName: Item | string, oldState?: string, newState?: string, triggerName?: string): HostTrigger;
/**
* Creates a trigger that fires upon a member of a group receiving a state update. Note that group Item does not need to change state.
*
Expand All @@ -77,7 +122,7 @@ export function GroupStateChangeTrigger(groupOrName: any | string, oldState?: st
* @param {string} [state] the new state of the group
* @param {string} [triggerName] the optional name of the trigger to create
*/
export function GroupStateUpdateTrigger(groupOrName: any | string, state?: string, triggerName?: string): HostTrigger;
export function GroupStateUpdateTrigger(groupOrName: Item | string, state?: string, triggerName?: string): HostTrigger;
/**
* Creates a trigger that fires upon a member of a group receiving a command. Note that the group Item does not need to change state.
*
Expand All @@ -89,7 +134,7 @@ export function GroupStateUpdateTrigger(groupOrName: any | string, state?: strin
* @param {string} [command] the command received
* @param {string} [triggerName] the optional name of the trigger to create
*/
export function GroupCommandTrigger(groupOrName: any | string, command?: string, triggerName?: string): HostTrigger;
export function GroupCommandTrigger(groupOrName: Item | string, command?: string, triggerName?: string): HostTrigger;
/**
* Creates a trigger that fires upon a Thing status updating.
*
Expand Down Expand Up @@ -182,7 +227,7 @@ export function TimeOfDayTrigger(time: string, triggerName?: string): HostTrigge
* @param {boolean} [timeOnly=false] Specifies whether only the time of the Item should be compared or the date and time.
* @param {string} [triggerName] the optional name of the trigger to create
*/
export function DateTimeTrigger(itemOrName: any | string, timeOnly?: boolean, triggerName?: string): HostTrigger;
export function DateTimeTrigger(itemOrName: Item | string, timeOnly?: boolean, triggerName?: string): HostTrigger;
/**
* Creates a trigger for the {@link https://openhab.org/addons/automation/pwm/ Pulse Width Modulation (PWM) Automation} add-on.
*
Expand Down
2 changes: 1 addition & 1 deletion types/triggers.d.ts.map

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

Loading