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

Implemented a toggle method on Item #99

Merged
merged 21 commits into from
May 7, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
bb7d578
Time: Add `updateTimeout` & `ZDTtoMillisFromNow`
florian-h05 Feb 8, 2022
0d56361
Time: Remove unnecessary checks from `updateTimeout`
florian-h05 Feb 11, 2022
4e0e2f0
Time: Monkey-patch ZonedDateTime `millisFromNow` & Add README section…
florian-h05 Feb 12, 2022
3a861df
Time: Remove `updateTimeout` & Document the returned Timer
florian-h05 Feb 12, 2022
d8cf009
Time: `millisFromNow` improvements
florian-h05 Feb 13, 2022
840f290
README: Improvements for JS-Joda and Timers
florian-h05 Feb 13, 2022
28acaeb
Implemented a toggle method on Item
rkoshak Feb 17, 2022
94e8b95
Fixed docstring error breaking the build
rkoshak Feb 17, 2022
d1b3cd5
Merge pull request #93 from florian-h05/timer-improvements
digitaldan Feb 18, 2022
28b3a21
Implemented a toggle method on Item
rkoshak Feb 17, 2022
2bc83d7
Fixed docstring error breaking the build
rkoshak Feb 17, 2022
0bcb93f
Addressing comments
rkoshak Mar 1, 2022
25ec0fb
Merge branch 'toggle' of github.com:rkoshak/openhab-js into toggle
rkoshak Mar 1, 2022
35f0108
Forgot to remove the old toggle method--
rkoshak Mar 1, 2022
1f8a800
README.md Undoing erroneous merge
rkoshak Mar 15, 2022
a874def
Addressing comment to make cleaner
rkoshak Mar 15, 2022
94a2f92
time.js: removing changes from erroneous merger
rkoshak Mar 15, 2022
c743259
Removing remenants of bad merge
rkoshak Mar 15, 2022
97c7297
Removing remnants of bad merge
rkoshak Mar 15, 2022
e67d5a5
changed names of toggle methods
rkoshak Apr 27, 2022
5b925b8
Addressing @florian-h05's comments.
rkoshak May 2, 2022
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
51 changes: 50 additions & 1 deletion items/managed.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const log = require('../log')('items');
const metadata = require('../metadata');
const ItemHistory = require('./item-history');

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

const itemBuilderFactory = osgi.getService("org.openhab.core.items.ItemBuilderFactory");

Expand Down Expand Up @@ -181,6 +181,55 @@ class Item {

return false;
}

/**
* Calculates the toggled state of this Item. For Items like Color and
* Dimmer, getStateAs(OnOffType) is used and the toggle calculated off
* of that.
* @returns the toggled state (e.g. 'OFF' if the Item is 'ON')
* @throws error if the Item is uninitialized or is a type that doesn't make sense to toggle
*/
#getToggleState() {
if(this.isUninitialized) {
throw Error('Cannot toggle uninitialized Items');
}
switch (this.type) {
case 'PlayerItem' :
return this.state == 'PAUSE' ? 'PLAY' : 'PAUSE';
case 'ContactItem' :
return this.state == 'OPEN' ? 'CLOSED' : 'OPEN';
default: {
const oldState = this.rawItem.getStateAs(OnOffType);
if(oldState) {
return oldState.toString() == 'ON' ? 'OFF' : 'ON';
}
else {
throw Error('Toggle not supported for items of type ' + this.type);
}
}
}
}

/**
* Sends a command to flip the Item's state (e.g. if it is 'ON' an 'OFF'
* command is sent).
* @throws error if the Item is uninitialized or a type that cannot be toggled or commanded
*/
sendToggleCommand() {
if(this.type == 'ContactItem'){
throw Error('Cannot command Contact Items');
}
this.sendCommand(this.#getToggleState());
}

/**
* Posts an update to flip the Item's state (e.g. if it is 'ON' an 'OFF'
* update is posted).
* @throws error if the Item is uninitialized or a type that cannot be toggled
*/
postToggleUpdate() {
this.postUpdate(this.#getToggleState());
}

/**
* Posts an update to the item
Expand Down
17 changes: 1 addition & 16 deletions rules/operation-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,22 +403,7 @@ class ToggleOperation extends OperationConfig {
* @returns {OperationBuilder.SendCommandOrUpdateOperation} this
*/
doToggle() {
let item = items.getItem(this.itemName);

switch (item.type) {
case "SwitchItem": {
let toSend = ('ON' == item.state) ? 'OFF' : 'ON';
item.sendCommand(toSend);
break;
}
case "ColorItem": {
let toSend = ('0' != item.rawState.getBrightness().toString()) ? 'OFF' : 'ON';
item.sendCommand(toSend);
break;
}
default:
throw Error(`Toggle not supported for items of type ${item.type}`);
}
items.getItem(this.itemName).toggleCommand();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this be sendToggleCommand instead of toggleCommand?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

}
}

Expand Down