-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
Conversation
updateTimeout can reschedule a timer created by setTimeout. ZDTtoMillisFromNow parses the duration from now to the given ZDT in milliseconds. Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
… about monkey patches Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Richard Koshak <rlkoshak@gmail.com>
Signed-off-by: Richard Koshak <rlkoshak@gmail.com>
This is ready for review. I found myself using this over and over from my personal library and figured others would find it useful too. Since it's inline with |
Would it make sense to update/command the Item if its current state is |
Time: Add updateTimeout & ZDTtoMillisFromNow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made a couple comments, one thing that seems odd to me about this is that we have one method which handles both a command and an update, which is kinda awkward to explain given some types support update and commands and some don't. I get toggling switch like items and sending a command, rolling in updates and contacts seems to confuse this. Maybe we should stick to what we support in rules as well ?
openhab-js/rules/operation-builder.js
Line 405 in d1b3cd5
doToggle() { |
we could probably move this onto an item and reuse/reduce the amount of code needed.
I did look for something like a toggle already in the library and never thought to look in the fluent rule builder stuff. Yes, I'd like to see that moved to Item so we don't duplicate code and everyone can benefit. I'll update that I do notice though that that existing implementation is incomplete. Dimmer too can be toggled. I like the approach of using I also don't mind splitting it into two separate functions but don't know what to call the one that does the updates. Maybe use The rest I'll change as suggested. I might experiment with |
Signed-off-by: Richard Koshak <rlkoshak@gmail.com>
Signed-off-by: Richard Koshak <rlkoshak@gmail.com>
Signed-off-by: Richard Koshak <rlkoshak@gmail.com>
I think I may have accidentally rebaselined this PR. I thought I was on main on my fork. That wasn't my intention. I'm not sure how to proceed from here. Clearly git and I do not get along. :-( Anyway, I've addressed all the comments from above. I added a toggleCommand and toggleUpdate to Item and changed doToggle to call toggleCommand so it's all centralized. |
Signed-off-by: Richard Koshak <rlkoshak@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Richard, it looks like there are a lot of changes related to your PR around time, I also left a very small comment. Can you fix this so it just includes toggle functionality? Might be easier to start fresh and cherry pick changes
rules/operation-builder.js
Outdated
@@ -404,21 +404,7 @@ class ToggleOperation extends OperationConfig { | |||
*/ | |||
doToggle() { | |||
let item = items.getItem(this.itemName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we only use the reference once, might be cleaner to do
let item = items.getItem(this.itemName); | |
items.getItem(this.itemName).toggleCommand(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
time.js
Outdated
* @returns {number} duration from now to the ZonedDateTime in milliseconds | ||
*/ | ||
time.ZonedDateTime.prototype.millisFromNow = function () { | ||
return time.Duration.between(time.ZonedDateTime.now(), this).toMillis(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this should be in this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I mucked things up and somehow merged upstream on this branch when I meant to on main instead. That's not actually my code. I'll try to get back to just my PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
README.md
Outdated
@@ -144,19 +143,23 @@ The string representations of each of these objects are appended together in the | |||
|
|||
see https://developer.mozilla.org/en-US/docs/Web/API/console for more information about console logging. | |||
|
|||
### SetTimeout | |||
### Timers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is from a another PR, as well as all the other time changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Signed-off-by: Richard Koshak <rlkoshak@gmail.com>
Signed-off-by: Richard Koshak <rlkoshak@gmail.com>
Signed-off-by: Richard Koshak <rlkoshak@gmail.com>
Signed-off-by: Richard Koshak <rlkoshak@gmail.com>
Signed-off-by: Richard Koshak <rlkoshak@gmail.com>
Ready for review. I've backed out the changes caused by my merging on the wrong branch and I've made the requested change in operation-builder.js. |
items/managed.js
Outdated
* command is sent). | ||
* @throws error if the Item is uninitialized or a type that cannot be toggled or commanded | ||
*/ | ||
toggleCommand() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toggleCommand() { | |
sendToggleCommand() { |
wdyt? I'm thinking we should try and be consistant since we use sendCommand
elsewhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That works for me. I like consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
items/managed.js
Outdated
* update is posted). | ||
* @throws error if the Item is uninitialized or a type that cannot be toggled | ||
*/ | ||
toggleUpdate() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toggleUpdate() { | |
postToggleUpdate() { |
wdyt? I'm thinking we should try and be consistant since we use postUpdate
elsewhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above. I'll get a PR submitted later today with these two changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @rkoshak ! Looks pretty good to me, i only had 2 small comments about naming
Hey @rkoshak any update on this? |
I've been trying to figure out how to service the unit tests using Mocha but haven't had a lot of time these last few weeks to do much of anything. I've but forgotten and am still working it. I can commit the charges without the tests to get this moving. |
Signed-off-by: Richard Koshak <rlkoshak@gmail.com>
I gave up on the uint testing. I might come back at a date with a new PR once I figure it out. Your requested changes have been made with this latest PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM overall, but please have a look at my comment!
rules/operation-builder.js
Outdated
default: | ||
throw Error(`Toggle not supported for items of type ${item.type}`); | ||
} | ||
items.getItem(this.itemName).toggleCommand(); |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
@rkoshak i'm ok without the testing , if you want to fix the one issue mentioned i'm happy to merge, thanks! |
Signed-off-by: Richard Koshak <rlkoshak@gmail.com>
Should be good to go now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you @rkoshak.
I did no testing but it should work.
lgtm thanks! |
This reverts the removal of `time.ZonedDateTime.millisFromNow()` and Timer docs from the README from PR openhab#99. Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Emulating merge error with openhab#99 Signed-off-by: Sami Salonen <ssalonen@gmail.com>
Emulating merge error with openhab#99 Signed-off-by: Sami Salonen <ssalonen@gmail.com>
Emulating merge error with openhab#99 Signed-off-by: Sami Salonen <ssalonen@gmail.com> Signed-off-by: Sami Salonen <ssalonen@gmail.com>
* Docs: document UI rules event and @runtime Signed-off-by: Sami Salonen <ssalonen@gmail.com> * docs: additional disclaimer with String items Signed-off-by: Sami Salonen <ssalonen@gmail.com> * docs: TOC showing h2..h3 only Signed-off-by: Sami Salonen <ssalonen@gmail.com> * Docs: Capitalization of Java/JavaScript Signed-off-by: Sami Salonen <ssalonen@gmail.com> * docs: Wording correections. Mention "jsscripting" also Signed-off-by: Sami Salonen <ssalonen@gmail.com> * docs: cross-reference event object file/UI documentation sections Signed-off-by: Sami Salonen <ssalonen@gmail.com> * docs: Remove empty column Signed-off-by: Sami Salonen <ssalonen@gmail.com> * Docs: clarifying installation section Signed-off-by: Sami Salonen <ssalonen@gmail.com> Co-Authored-By: Dan Cunningham <dan@digitaldan.com> Co-authored-by: Dan Cunningham <dan@digitaldan.com> * docs: Advanced Scripting with @runtime Signed-off-by: Sami Salonen <ssalonen@gmail.com> * docs: Move scriptLoaded/scriptUnloaded hooks under File Based Rules Also: - title capitalization now reflecting code Signed-off-by: Sami Salonen <ssalonen@gmail.com> * Remove openhab timer Emulating merge error with #99 Signed-off-by: Sami Salonen <ssalonen@gmail.com> * Merge fixing Emulating merge error with #99 Signed-off-by: Sami Salonen <ssalonen@gmail.com> Signed-off-by: Sami Salonen <ssalonen@gmail.com> Co-authored-by: Dan Cunningham <dan@digitaldan.com>
Signed-off-by: Richard Koshak rlkoshak@gmail.com
[Item] Added a Toggle Method to Item
Description
Added a
toggle
method to Item which will toggle the state of the Item based on its current state. Any Item that supports OnOffType command or update will receive the opposite of its currentgetStateAs(OnOffType)
as an update or command. Any Item that supports OpenClosedType updates will receive the opposite of its currentgetStateOs(OpenClosedType)
. In all other cases an error is thrown (including attempting to toggle a Contact with a command). There is an argumentupdate
which, when provided withtrue
will cause the new toggled state to be posted as an update. The default isfalse
causing the new toggled state to be sent as a command.There are many situations where one needs to toggle a binary type Item and this saves the user from needing to repeat that sort of logic over and over, similar to the already present
sendCommandIfDifferent()
.Testing
I tested most of the different permutations including:
update
argument: Item was commandedupdate=false
: Item was commandedupdate=true
: Item was updatedupdate=false
: generates errorupdate=true
: Item was updated