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

[lcn] Add configuration option invertOpenClosed to binary sensor channel #8102

Merged
merged 5 commits into from
Aug 1, 2020
Merged

[lcn] Add configuration option invertOpenClosed to binary sensor channel #8102

merged 5 commits into from
Aug 1, 2020

Conversation

toweosp
Copy link

@toweosp toweosp commented Jul 10, 2020

Added configuration option invertOpenClosed to binarysensor channel. Per default lcn binary sensor state 0 corresponds to "closed" while 1 corresponds to "open". Use this parameter to invert that logic.

closes #8069

@toweosp toweosp requested a review from fwolter as a code owner July 10, 2020 20:40
@TravisBuddy
Copy link

Travis tests have failed

Hey @toweosp,
please read the following log in order to understand the failure reason. There might also be some helpful tips along the way.
It will be awesome if you fix what is wrong and commit the changes.

@fwolter fwolter changed the title [lcn] Added configuration option invertOpenClosed to binarysensor channel [lcn] Add configuration option invertOpenClosed to binary sensor channel Jul 10, 2020
@fwolter fwolter added the enhancement An enhancement or new feature for an existing add-on label Jul 10, 2020
Copy link
Member

@fwolter fwolter left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution! Here is my feedback.

Comment on lines 295 to 304
if (channelGroup.equals(LcnChannelGroup.BINARYSENSOR)) {
Channel channel = thing.getChannel(channelUid);
if (channel != null) {
Object invertConfig = channel.getConfiguration().get("invertOpenClosed");
boolean invertOpenClosed = invertConfig instanceof Boolean && (boolean) invertConfig;
if (invertOpenClosed) {
if (state.equals(OpenClosedType.OPEN)) {
convertedState = OpenClosedType.CLOSED;
} else {
convertedState = OpenClosedType.OPEN;
Copy link
Member

Choose a reason for hiding this comment

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

Can you move this into LcnModuleBinarySensorSubHandler similar to the inversion of the roller shutter up/down in LcnModuleRollershutterRelaySubHandler?

Copy link
Author

Choose a reason for hiding this comment

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

BaseThingHandler.updateState is not visible from AbstractLcnModuleSubHandler. So one can not delegate channel update handling to a sub handler similar to the rollershutter command handling (without revealing BaseThingHandler.updateState in LcnModuleHandler). We could delegate state conversion within LcnModuleHandler.updateChannel to a converter (similar to Converter.onStateUpdateFromHandler(State)). This would require Converter to be an (abstract) superclass or better interface for arbitrary converters which are initialized in LcnModuleHandler.initialize. At the moment Converter is specialized to deal with variables only.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

If I see correctly, BaseThingHandler.updateState() is invoked via LcnModuleHandler.updateChannel() -> AbstractLcnModuleSubHandler.fireUpdate() -> LcnModuleBinarySensorSubHandler.handleStatusMessage(). I think the conversion could happen in LcnModuleBinarySensorSubHandler:55.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for your feedback! If I didn't miss something when following your suggestion channel update (and status message handling resp.) would differ between binary sensors and variables. So from a design perspective I think we should choose between using the converter approach or uniquely handle value conversion within the sub-handler's handleStatusMessage.

Would you like to decide how to uniquely handle value conversion and adapt the code correspondingly? Perhaps it would make sense to handle this on a separate enhancement/pull request due to the necessary refactorings.

Copy link
Member

Choose a reason for hiding this comment

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

Although I'm a fan a of abstracting things, I wouldn't combine conversion of raw data into human readable values and the inversion of binary sensor states. First is a conversion, the other is a manipulation. From the functional perspective, I think they have to less in common to merge these functions.

Copy link
Member

Choose a reason for hiding this comment

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

WDYT?

Copy link
Author

Choose a reason for hiding this comment

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

From my point of view there is no conceptual difference between converting raw variable data (i.e. lcn value) into temperature, speed etc. and converting raw lcn binary sensor data (0 or 1) into open/closed states. In my opinion in both cases conversion applies semantic to raw sensor data leading to human readable values.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, you convinced me. I refactored the conversion code and made Converter an interface. Do you want to implement an inversion Converter?

Can you give me access rights to merge the changes into your branch? Alternatively, you could merge my lcn-converter branch into yours. Before doing so you have to rebase this PR's branch to bb6722c.

Copy link
Author

Choose a reason for hiding this comment

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

I added an InversionConverter. For this I moved Converter.onCommandFromItem(double) and Converter.onCommandFromItem(QuantityType<?>) down the hierarchy to ValueConverter because I think these methods make only sense when converting numerical values. The InversionConverter might later additionally be used e.g. for visualizing channels with inversed OnOff or UpDown states.

@TravisBuddy
Copy link

Travis tests have failed

Hey @toweosp,
please read the following log in order to understand the failure reason. There might also be some helpful tips along the way.
It will be awesome if you fix what is wrong and commit the changes.

1 similar comment
@TravisBuddy
Copy link

Travis tests have failed

Hey @toweosp,
please read the following log in order to understand the failure reason. There might also be some helpful tips along the way.
It will be awesome if you fix what is wrong and commit the changes.

@toweosp toweosp requested a review from J-N-K as a code owner July 21, 2020 14:35
Copy link
Member

@fwolter fwolter left a comment

Choose a reason for hiding this comment

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

Thanks! There seem to be commits from the 2.5.x branch in this PR, which don't belong to it.

There are some formatting issues. You can view them with mvn spotless:check -Dspotless.check.skip=false and fix them with mvn spotless:apply.

Comment on lines 184 to 188
if (valueConverter instanceof ValueConverter) {
DecimalType nativeValue = ((ValueConverter) valueConverter)
.onCommandFromItem(decimalType.doubleValue());
subHandler.handleCommandDecimal(nativeValue, channelGroup, number.get());
}else{
Copy link
Member

Choose a reason for hiding this comment

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

This casting bothers me a bit. Is there an other way?

Copy link
Author

@toweosp toweosp Jul 21, 2020

Choose a reason for hiding this comment

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

Sorry, had some issues with my eclipse/EGit installation. I hopefully managed to "clean" the branch from the commits not belonging to this PR, but I still have the following issues:

  • I can't satisfy DCO (your commit apparently was missing signature). Tried to apply the steps mentioned in the DCO log but with no success.
  • J-N-K is still listed as a reviewer for this PR, although the commits not belonging to this PR are now (hopefully) removed.

To your question regarding the cast: One could move ValueConverter.onCommandFromItem(double) and ValueConverter.onCommandFromItem(QuantityType<?>) up to Converter again making them Nullable so InversionConverter could return null. But in my opinion this would violate separation of concerns, see my last comment.

From a design perspective it might be better to separate conversion for representation (lcn bus -> UI) from conversion of commands (UI -> lcn bus) leading to two different converter hierarchies/interfaces, e.g. StateRepresentationConverter.onStateUpdateFromHandler and CommandConverter.onCommandFromItem - or to be okay with the casting ;-)

Copy link
Member

Choose a reason for hiding this comment

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

I'd go for a modified version of your first proposal. Move the methods back to Converter. Make Converter a class and add a default implementation for all four methods, that return the identity. Then, Nullable is not needed. Casting, instanceof and semantic based null values violate OOP paradigms most often.

I don't think splitting the converters by the message direction doesn't solve the problem as the differentiation between values and binary states is still necessary.

Copy link
Author

Choose a reason for hiding this comment

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

Converters refactored as you requested.

@TravisBuddy
Copy link

Travis tests were successful

Hey @toweosp,
we found no major flaws with your code. Still you might want to look at this logfile, as we usually suggest some optional improvements.

@TravisBuddy
Copy link

Travis tests have failed

Hey @toweosp,
please read the following log in order to understand the failure reason. There might also be some helpful tips along the way.
It will be awesome if you fix what is wrong and commit the changes.

1 similar comment
@TravisBuddy
Copy link

Travis tests have failed

Hey @toweosp,
please read the following log in order to understand the failure reason. There might also be some helpful tips along the way.
It will be awesome if you fix what is wrong and commit the changes.

@TravisBuddy
Copy link

Travis tests were successful

Hey @toweosp,
we found no major flaws with your code. Still you might want to look at this logfile, as we usually suggest some optional improvements.

@TravisBuddy
Copy link

Travis tests have failed

Hey @toweosp,
please read the following log in order to understand the failure reason. There might also be some helpful tips along the way.
It will be awesome if you fix what is wrong and commit the changes.

1 similar comment
@TravisBuddy
Copy link

Travis tests have failed

Hey @toweosp,
please read the following log in order to understand the failure reason. There might also be some helpful tips along the way.
It will be awesome if you fix what is wrong and commit the changes.

Copy link
Member

@fwolter fwolter left a comment

Choose a reason for hiding this comment

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

Thanks! Just one small change.

@TravisBuddy
Copy link

Travis tests have failed

Hey @toweosp,
please read the following log in order to understand the failure reason. There might also be some helpful tips along the way.
It will be awesome if you fix what is wrong and commit the changes.

Copy link
Member

@fwolter fwolter left a comment

Choose a reason for hiding this comment

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

LGTM

@fwolter
Copy link
Member

fwolter commented Jul 26, 2020

Can you adopt the sign-off of my commit to the merge commit 507251a to satisfy the DCO check?

fwolter and others added 4 commits July 31, 2020 16:23
Signed-off-by: Fabian Wolter <github@fabian-wolter.de>
References PR #8102

Signed-off-by: Thomas Weiler <toweosp@gmail.com>
Signed-off-by: Thomas Weiler <toweosp@gmail.com>
Signed-off-by: Thomas Weiler <toweosp@gmail.com>
@TravisBuddy
Copy link

Travis tests have failed

Hey @toweosp,
please read the following log in order to understand the failure reason. There might also be some helpful tips along the way.
It will be awesome if you fix what is wrong and commit the changes.

@toweosp
Copy link
Author

toweosp commented Jul 31, 2020

Done (beside failing jdk11 build #8094)

@fwolter
Copy link
Member

fwolter commented Aug 1, 2020

Can you fix the conflicts raised by the other PR?

@TravisBuddy
Copy link

Travis tests have failed

Hey @toweosp,
please read the following log in order to understand the failure reason. There might also be some helpful tips along the way.
It will be awesome if you fix what is wrong and commit the changes.

Copy link
Member

@fwolter fwolter left a comment

Choose a reason for hiding this comment

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

LGTM

@fwolter fwolter merged commit b9d13c7 into openhab:2.5.x Aug 1, 2020
@fwolter fwolter added this to the 2.5.8 milestone Aug 1, 2020
@toweosp toweosp deleted the invertOpenClosed branch August 1, 2020 13:21
MPH80 pushed a commit to MPH80/openhab-addons that referenced this pull request Aug 3, 2020
…nel (openhab#8102)

Closes openhab#8069

Signed-off-by: Thomas Weiler <toweosp@gmail.com>
Signed-off-by: MPH80 <michael@hazelden.me>
bern77 pushed a commit to bern77/openhab-addons that referenced this pull request Aug 6, 2020
…nel (openhab#8102)

Closes openhab#8069

Signed-off-by: Thomas Weiler <toweosp@gmail.com>
Signed-off-by: Bernhard <bern77@gmail.com>
andrewfg pushed a commit to andrewfg/openhab-addons that referenced this pull request Aug 31, 2020
…nel (openhab#8102)

Closes openhab#8069

Signed-off-by: Thomas Weiler <toweosp@gmail.com>
andrewfg pushed a commit to andrewfg/openhab-addons that referenced this pull request Aug 31, 2020
…nel (openhab#8102)

Closes openhab#8069

Signed-off-by: Thomas Weiler <toweosp@gmail.com>
andrewfg pushed a commit to andrewfg/openhab-addons that referenced this pull request Aug 31, 2020
…nel (openhab#8102)

Closes openhab#8069

Signed-off-by: Thomas Weiler <toweosp@gmail.com>
andrewfg pushed a commit to andrewfg/openhab-addons that referenced this pull request Aug 31, 2020
…nel (openhab#8102)

Closes openhab#8069

Signed-off-by: Thomas Weiler <toweosp@gmail.com>
DaanMeijer pushed a commit to DaanMeijer/openhab-addons that referenced this pull request Sep 1, 2020
…nel (openhab#8102)

Closes openhab#8069

Signed-off-by: Thomas Weiler <toweosp@gmail.com>
Signed-off-by: Daan Meijer <daan@studioseptember.nl>
CSchlipp pushed a commit to CSchlipp/openhab-addons that referenced this pull request Sep 12, 2020
…nel (openhab#8102)

Closes openhab#8069

Signed-off-by: Thomas Weiler <toweosp@gmail.com>
markus7017 pushed a commit to markus7017/openhab-addons that referenced this pull request Sep 19, 2020
…nel (openhab#8102)

Closes openhab#8069

Signed-off-by: Thomas Weiler <toweosp@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement or new feature for an existing add-on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[LCN] Contact Item shows wrong state
3 participants