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

Conversation

rkoshak
Copy link
Contributor

@rkoshak rkoshak commented Feb 17, 2022

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 current getStateAs(OnOffType) as an update or command. Any Item that supports OpenClosedType updates will receive the opposite of its current getStateOs(OpenClosedType). In all other cases an error is thrown (including attempting to toggle a Contact with a command). There is an argument update which, when provided with true will cause the new toggled state to be posted as an update. The default is false 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:

  • toggle on an Item whose state is UnDefType: generates error
  • toggle on Switch without update argument: Item was commanded
  • toggle on Switch with update=false: Item was commanded
  • toggle on Switch with update=true: Item was updated
  • toggle on Contact with update=false: generates error
  • toggle on Contact with update=true: Item was updated

florian-h05 and others added 8 commits February 8, 2022 23:59
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>
@rkoshak
Copy link
Contributor Author

rkoshak commented Feb 17, 2022

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 sendCommandIfDifferent I figured it'd be appropriate to add.

@rkoshak
Copy link
Contributor Author

rkoshak commented Feb 17, 2022

Would it make sense to update/command the Item if its current state is NULL or UNDEF instead of throwing an error? It occurs to me that with the current error it might confuse users into thinking that the problem is the Item type and not that it's current state is the problem.

Time: Add updateTimeout & ZDTtoMillisFromNow
Copy link
Contributor

@digitaldan digitaldan left a 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 ?

we could probably move this onto an item and reuse/reduce the amount of code needed.

@rkoshak
Copy link
Contributor Author

rkoshak commented Feb 18, 2022

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 doToggle function as appropriate.

I do notice though that that existing implementation is incomplete. Dimmer too can be toggled. I like the approach of using getStateAs() to determine if it's toggle-able though because even if OH adds a new Item type that handles ON/OFF (unlikely I know) the function will still work.

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 toggleCommand and toggleUpdate?

The rest I'll change as suggested. I might experiment with Player Items too. It seems like Play/Pause is another type of toggle that should be supported.

rkoshak added 4 commits March 1, 2022 11:42
Signed-off-by: Richard Koshak <rlkoshak@gmail.com>
Signed-off-by: Richard Koshak <rlkoshak@gmail.com>
Signed-off-by: Richard Koshak <rlkoshak@gmail.com>
@rkoshak
Copy link
Contributor Author

rkoshak commented Mar 1, 2022

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>
Copy link
Contributor

@digitaldan digitaldan left a 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

@@ -404,21 +404,7 @@ class ToggleOperation extends OperationConfig {
*/
doToggle() {
let item = items.getItem(this.itemName);
Copy link
Contributor

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

Suggested change
let item = items.getItem(this.itemName);
items.getItem(this.itemName).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.

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();
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

rkoshak added 5 commits March 15, 2022 12:08
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>
@rkoshak
Copy link
Contributor Author

rkoshak commented Mar 15, 2022

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() {
Copy link
Contributor

@digitaldan digitaldan Mar 19, 2022

Choose a reason for hiding this comment

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

Suggested change
toggleCommand() {
sendToggleCommand() {

wdyt? I'm thinking we should try and be consistant since we use sendCommand elsewhere

Copy link
Contributor Author

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.

Copy link
Contributor Author

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() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
toggleUpdate() {
postToggleUpdate() {

wdyt? I'm thinking we should try and be consistant since we use postUpdate elsewhere

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

@digitaldan digitaldan left a 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

@digitaldan
Copy link
Contributor

Hey @rkoshak any update on this?

@rkoshak
Copy link
Contributor Author

rkoshak commented Apr 23, 2022

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>
@rkoshak
Copy link
Contributor Author

rkoshak commented Apr 27, 2022

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.

Copy link
Contributor

@florian-h05 florian-h05 left a 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!

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

@digitaldan
Copy link
Contributor

@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>
@rkoshak
Copy link
Contributor Author

rkoshak commented May 2, 2022

Should be good to go now.

Copy link
Contributor

@florian-h05 florian-h05 left a 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.

@digitaldan
Copy link
Contributor

lgtm thanks!

@digitaldan digitaldan merged commit ccea6bd into openhab:main May 7, 2022
florian-h05 added a commit to florian-h05/openhab-js that referenced this pull request May 7, 2022
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>
ssalonen added a commit to ssalonen/openhab-js that referenced this pull request May 8, 2022
Emulating merge error with openhab#99

Signed-off-by: Sami Salonen <ssalonen@gmail.com>
ssalonen added a commit to ssalonen/openhab-js that referenced this pull request May 8, 2022
Emulating merge error with openhab#99

Signed-off-by: Sami Salonen <ssalonen@gmail.com>
ssalonen added a commit to ssalonen/openhab-js that referenced this pull request May 8, 2022
Emulating merge error with openhab#99

Signed-off-by: Sami Salonen <ssalonen@gmail.com>

Signed-off-by: Sami Salonen <ssalonen@gmail.com>
digitaldan added a commit that referenced this pull request May 13, 2022
* 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>
digitaldan pushed a commit that referenced this pull request May 20, 2022
* Revert unwanted removals from previous PR #99

This reverts the removal of `time.ZonedDateTime.millisFromNow()` and
Timer docs from the README from PR #99.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>

* README.md: Style fixes

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants