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

Fix AutoUpdatePolicy for channel #3888

Merged
merged 2 commits into from Dec 4, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -96,7 +96,7 @@ public static ChannelBuilder create(Channel channel) {
ChannelBuilder channelBuilder = create(channel.getUID(), channel.getAcceptedItemType())
.withConfiguration(channel.getConfiguration()).withDefaultTags(channel.getDefaultTags())
.withKind(channel.getKind()).withProperties(channel.getProperties())
.withType(channel.getChannelTypeUID());
.withType(channel.getChannelTypeUID()).withAutoUpdatePolicy(channel.getAutoUpdatePolicy());
String label = channel.getLabel();
if (label != null) {
channelBuilder.withLabel(label);
Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.ThingFactory;
import org.openhab.core.thing.binding.builder.ChannelBuilder;
import org.openhab.core.thing.type.AutoUpdatePolicy;
import org.openhab.core.thing.type.ChannelDefinition;
import org.openhab.core.thing.type.ChannelGroupDefinition;
import org.openhab.core.thing.type.ChannelGroupType;
Expand Down Expand Up @@ -184,12 +185,17 @@ public static ChannelBuilder createChannelBuilder(ChannelUID channelUID, Channel
label = channelType.getLabel();
}

AutoUpdatePolicy autoUpdatePolicy = channelDefinition.getAutoUpdatePolicy();
if (autoUpdatePolicy == null) {
autoUpdatePolicy = channelType.getAutoUpdatePolicy();
}

final ChannelBuilder channelBuilder = ChannelBuilder.create(channelUID, channelType.getItemType()) //
.withType(channelType.getUID()) //
.withDefaultTags(channelType.getTags()) //
.withKind(channelType.getKind()) //
.withLabel(label) //
.withAutoUpdatePolicy(channelType.getAutoUpdatePolicy());
.withAutoUpdatePolicy(autoUpdatePolicy);
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 add a test-case for this, too? The test should show that

  • no policy is set if neither the channel-definition nor the channel-type-definition set a policy
  • if either channel- or channel-type-definition contain a policy, it is used
  • if both contain a policy, the channel-definition takes precedence

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to be sure, you mean a new test class, right? I didn't find any existing coverage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm afraid I need a bit of guidance here. I found some integration tests that seem related:

@Test
public void testCreateChannelGroupBuilder() throws Exception {
AtomicReference<ThingHandlerCallback> thc = initializeThingHandlerCallback();
List<ChannelBuilder> channelBuilders = thc.get().createChannelBuilders(CHANNEL_GROUP_UID,
CHANNEL_GROUP_TYPE_UID);
assertNotNull(channelBuilders);
assertEquals(2, channelBuilders.size());
assertNotNull(channelBuilders.get(0));
validateChannel(channelBuilders.get(0).build());
assertNotNull(channelBuilders.get(1));
validateChannelOverridden(channelBuilders.get(1).build());
}
private void validateChannel(Channel channel) {
assertNotNull(channel);
assertEquals("Test Label", channel.getLabel());
assertEquals("Test Description", channel.getDescription());
assertEquals(CoreItemFactory.SWITCH, channel.getAcceptedItemType());
assertEquals(CHANNEL_TYPE_UID, channel.getChannelTypeUID());
assertNotNull(channel.getDefaultTags());
assertEquals(1, channel.getDefaultTags().size());
assertEquals("Test Tag", channel.getDefaultTags().iterator().next());
}
private void validateChannelOverridden(Channel channel) {
assertNotNull(channel);
assertEquals("Test Label Overridden", channel.getLabel());
assertEquals("Test Description Overridden", channel.getDescription());
assertEquals(CoreItemFactory.SWITCH, channel.getAcceptedItemType());
assertEquals(CHANNEL_TYPE_UID, channel.getChannelTypeUID());
assertNotNull(channel.getDefaultTags());
assertEquals(1, channel.getDefaultTags().size());
assertEquals("Test Tag", channel.getDefaultTags().iterator().next());
}

Did you mean to add new tests here? In that case, how can I build and run those tests?


String description = channelDefinition.getDescription();
if (description == null) {
Expand Down
Expand Up @@ -84,6 +84,7 @@ public void testChannelBuilderFromChannel() {
assertThat(otherChannel.getDescription(), is(channel.getDescription()));
assertThat(otherChannel.getKind(), is(channel.getKind()));
assertThat(otherChannel.getLabel(), is(channel.getLabel()));
assertThat(otherChannel.getAutoUpdatePolicy(), is(channel.getAutoUpdatePolicy()));
assertThat(otherChannel.getProperties().size(), is(channel.getProperties().size()));
assertThat(otherChannel.getProperties().get(KEY1), is(channel.getProperties().get(KEY1)));
assertThat(otherChannel.getProperties().get(KEY2), is(channel.getProperties().get(KEY2)));
Expand Down