From 05ab1265c4b7ef4f97c5cc010c49758cf05daa2a Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Fri, 24 Dec 2021 14:03:45 +0100 Subject: [PATCH 1/8] Extract dynamic channel creation to separate classes. Signed-off-by: Jacob Laursen --- .../builders/AutomationChannelBuilder.java | 244 +++++++++++++++++ .../builders/SceneChannelBuilder.java | 104 ++++++++ .../builders/SceneGroupChannelBuilder.java | 106 ++++++++ .../handler/HDPowerViewHubHandler.java | 189 +++----------- .../OH-INF/i18n/hdpowerview.properties | 12 +- .../OH-INF/i18n/hdpowerview_da.properties | 12 +- .../resources/OH-INF/thing/thing-types.xml | 5 +- .../AutomationChannelBuilderTest.java | 247 ++++++++++++++++++ .../hdpowerview/LocaleProviderForTests.java | 30 +++ .../hdpowerview/SceneChannelBuilderTest.java | 106 ++++++++ .../SceneGroupChannelBuilderTest.java | 107 ++++++++ .../TranslationProviderForTests.java | 66 +++++ 12 files changed, 1056 insertions(+), 172 deletions(-) create mode 100644 bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java create mode 100644 bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java create mode 100644 bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java create mode 100644 bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/AutomationChannelBuilderTest.java create mode 100644 bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/LocaleProviderForTests.java create mode 100644 bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneChannelBuilderTest.java create mode 100644 bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneGroupChannelBuilderTest.java create mode 100644 bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/TranslationProviderForTests.java diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java new file mode 100644 index 0000000000000..ca5593adb3c31 --- /dev/null +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java @@ -0,0 +1,244 @@ +/** + * Copyright (c) 2010-2022 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.hdpowerview.internal.builders; + +import java.time.DayOfWeek; +import java.time.LocalTime; +import java.time.format.TextStyle; +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.StringJoiner; +import java.util.stream.Collectors; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; +import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider; +import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections.SceneCollection; +import org.openhab.binding.hdpowerview.internal.api.responses.Scenes.Scene; +import org.openhab.binding.hdpowerview.internal.api.responses.ScheduledEvents; +import org.openhab.binding.hdpowerview.internal.api.responses.ScheduledEvents.ScheduledEvent; +import org.openhab.core.library.CoreItemFactory; +import org.openhab.core.thing.Channel; +import org.openhab.core.thing.ChannelGroupUID; +import org.openhab.core.thing.ChannelUID; +import org.openhab.core.thing.binding.builder.ChannelBuilder; +import org.openhab.core.thing.type.ChannelTypeUID; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The {@link AutomationChannelBuilder} class creates automation channels + * from structured scheduled event data. + * + * @author Jacob Laursen - Initial contribution + */ +@NonNullByDefault +public class AutomationChannelBuilder { + + private final Logger logger = LoggerFactory.getLogger(AutomationChannelBuilder.class); + private final HDPowerViewTranslationProvider translationProvider; + private final ChannelGroupUID channelGroupUid; + private final ChannelTypeUID channelTypeUid = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID, + HDPowerViewBindingConstants.CHANNELTYPE_AUTOMATION_ENABLED); + + private List channels; + private Map scenes; + private Map sceneCollections; + private List scheduledEvents; + + public AutomationChannelBuilder(HDPowerViewTranslationProvider translationProvider, + ChannelGroupUID channelGroupUid) { + this.translationProvider = translationProvider; + this.channelGroupUid = channelGroupUid; + this.channels = new ArrayList<>(0); + this.scheduledEvents = new ArrayList<>(0); + this.scenes = new HashMap<>(0); + this.sceneCollections = new HashMap<>(0); + } + + /** + * Creates an {@link AutomationChannelBuilder} for the given {@link HDPowerViewTranslationProvider} and + * {@link ChannelGroupUID}. + * + * @param translationProvider the {@link HDPowerViewTranslationProvider} + * @param channelGroupUid parent {@link ChannelGroupUID} for created channels + * @return channel builder + */ + public static AutomationChannelBuilder create(HDPowerViewTranslationProvider translationProvider, + ChannelGroupUID channelGroupUid) { + return new AutomationChannelBuilder(translationProvider, channelGroupUid); + } + + /** + * Adds created channels to existing list. + * + * @param channels list that channels will be added to + * @return channel builder + */ + public AutomationChannelBuilder withChannels(List channels) { + this.channels = channels; + return this; + } + + /** + * Sets the scenes. + * + * @param scenes the scenes + * @return channel builder + */ + public AutomationChannelBuilder withScenes(List scenes) { + this.scenes = scenes.stream().collect(Collectors.toMap(scene -> scene.id, scene -> scene)); + return this; + } + + /** + * Sets the scene collections. + * + * @param sceneCollections the scene collections + * @return channel builder + */ + public AutomationChannelBuilder withSceneCollections(List sceneCollections) { + this.sceneCollections = sceneCollections.stream() + .collect(Collectors.toMap(sceneCollection -> sceneCollection.id, sceneCollection -> sceneCollection)); + return this; + } + + /** + * Sets the scheduled events. + * + * @param scheduledEvents the sceduled events + * @return channel builder + */ + public AutomationChannelBuilder withScheduledEvents(List scheduledEvents) { + this.scheduledEvents = scheduledEvents; + return this; + } + + /** + * Builds and returns the channels. + * + * @return the {@link Channel} list + */ + public List build() { + scheduledEvents.stream().forEach(scheduledEvent -> { + Channel channel = createChannel(scheduledEvent); + if (channel != null) { + channels.add(channel); + } + }); + + return channels; + } + + private @Nullable Channel createChannel(ScheduledEvent scheduledEvent) { + String referencedName = getReferencedSceneOrSceneCollectionName(scheduledEvent); + if (referencedName == null) { + return null; + } + ChannelUID channelUid = new ChannelUID(channelGroupUid, Integer.toString(scheduledEvent.id)); + String label = getScheduledEventName(referencedName, scheduledEvent); + String description = translationProvider.getText("dynamic-channel.automation-enabled.description", + referencedName); + Channel channel = ChannelBuilder.create(channelUid, CoreItemFactory.SWITCH).withType(channelTypeUid) + .withLabel(label).withDescription(description).build(); + + return channel; + } + + private @Nullable String getReferencedSceneOrSceneCollectionName(ScheduledEvent scheduledEvent) { + if (scheduledEvent.sceneId > 0) { + Scene scene = scenes.get(scheduledEvent.sceneId); + if (scene != null) { + return scene.getName(); + } + logger.warn("Scene '{}' was not found for scheduled event '{}'", scheduledEvent.sceneId, scheduledEvent.id); + return null; + } else if (scheduledEvent.sceneCollectionId > 0) { + SceneCollection sceneCollection = sceneCollections.get(scheduledEvent.sceneCollectionId); + if (sceneCollection != null) { + return sceneCollection.getName(); + } + logger.warn("Scene collection '{}' was not found for scheduled event '{}'", + scheduledEvent.sceneCollectionId, scheduledEvent.id); + return null; + } else { + logger.warn("Scheduled event '{}'' not related to any scene or scene collection", scheduledEvent.id); + return null; + } + } + + private String getScheduledEventName(String sceneName, ScheduledEvent scheduledEvent) { + String timeString, daysString; + + switch (scheduledEvent.eventType) { + case ScheduledEvents.SCHEDULED_EVENT_TYPE_TIME: + timeString = LocalTime.of(scheduledEvent.hour, scheduledEvent.minute).toString(); + break; + case ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE: + if (scheduledEvent.minute == 0) { + timeString = translationProvider.getText("dynamic-channel.automation.at-sunrise"); + } else if (scheduledEvent.minute < 0) { + timeString = translationProvider.getText("dynamic-channel.automation.before-sunrise", + getFormattedTimeOffset(-scheduledEvent.minute)); + } else { + timeString = translationProvider.getText("dynamic-channel.automation.after-sunrise", + getFormattedTimeOffset(scheduledEvent.minute)); + } + break; + case ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNSET: + if (scheduledEvent.minute == 0) { + timeString = translationProvider.getText("dynamic-channel.automation.at-sunset"); + } else if (scheduledEvent.minute < 0) { + timeString = translationProvider.getText("dynamic-channel.automation.before-sunset", + getFormattedTimeOffset(-scheduledEvent.minute)); + } else { + timeString = translationProvider.getText("dynamic-channel.automation.after-sunset", + getFormattedTimeOffset(scheduledEvent.minute)); + } + break; + default: + return sceneName; + } + + EnumSet days = scheduledEvent.getDays(); + if (EnumSet.allOf(DayOfWeek.class).equals(days)) { + daysString = translationProvider.getText("dynamic-channel.automation.all-days"); + } else if (ScheduledEvents.WEEKDAYS.equals(days)) { + daysString = translationProvider.getText("dynamic-channel.automation.weekdays"); + } else if (ScheduledEvents.WEEKENDS.equals(days)) { + daysString = translationProvider.getText("dynamic-channel.automation.weekends"); + } else { + StringJoiner joiner = new StringJoiner(", "); + days.forEach(day -> joiner.add(day.getDisplayName(TextStyle.SHORT, translationProvider.getLocale()))); + daysString = joiner.toString(); + } + + return translationProvider.getText("dynamic-channel.automation-enabled.label", sceneName, timeString, + daysString); + } + + private String getFormattedTimeOffset(int minutes) { + if (minutes >= 60) { + int remainder = minutes % 60; + if (remainder == 0) { + return translationProvider.getText("dynamic-channel.automation.hour", minutes / 60); + } + return translationProvider.getText("dynamic-channel.automation.hour-minute", minutes / 60, remainder); + } + return translationProvider.getText("dynamic-channel.automation.minute", minutes); + } +} diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java new file mode 100644 index 0000000000000..2ef67dcdb42dc --- /dev/null +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java @@ -0,0 +1,104 @@ +/** + * Copyright (c) 2010-2022 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.hdpowerview.internal.builders; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; +import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider; +import org.openhab.binding.hdpowerview.internal.api.responses.Scenes.Scene; +import org.openhab.core.library.CoreItemFactory; +import org.openhab.core.thing.Channel; +import org.openhab.core.thing.ChannelGroupUID; +import org.openhab.core.thing.ChannelUID; +import org.openhab.core.thing.binding.builder.ChannelBuilder; +import org.openhab.core.thing.type.ChannelTypeUID; + +/** + * The {@link SceneChannelBuilder} class creates scene channels + * from structured scene data. + * + * @author Jacob Laursen - Initial contribution + */ +@NonNullByDefault +public class SceneChannelBuilder { + + private final HDPowerViewTranslationProvider translationProvider; + private final ChannelGroupUID channelGroupUid; + private final ChannelTypeUID channelTypeUid = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID, + HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE); + + private List channels; + private List scenes; + + public SceneChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid) { + this.translationProvider = translationProvider; + this.channelGroupUid = channelGroupUid; + this.channels = new ArrayList<>(0); + this.scenes = new ArrayList<>(0); + } + + /** + * Creates an {@link SceneChannelBuilder} for the given {@link HDPowerViewTranslationProvider} and + * {@link ChannelGroupUID}. + * + * @param translationProvider the {@link HDPowerViewTranslationProvider} + * @param channelGroupUid parent {@link ChannelGroupUID} for created channels + * @return channel builder + */ + public static SceneChannelBuilder create(HDPowerViewTranslationProvider translationProvider, + ChannelGroupUID channelGroupUid) { + return new SceneChannelBuilder(translationProvider, channelGroupUid); + } + + /** + * Adds created channels to existing list. + * + * @param channels list that channels will be added to + * @return channel builder + */ + public SceneChannelBuilder withChannels(List channels) { + this.channels = channels; + return this; + } + + /** + * Sets the scenes. + * + * @param scenes the scenes + * @return channel builder + */ + public SceneChannelBuilder withScenes(List scenes) { + this.scenes = scenes; + return this; + } + + /** + * Builds and returns the channels. + * + * @return the {@link Channel} list + */ + public List build() { + scenes.stream().sorted().forEach(scene -> channels.add(createChannel(scene))); + return channels; + } + + private Channel createChannel(Scene scene) { + ChannelUID channelUid = new ChannelUID(channelGroupUid, Integer.toString(scene.id)); + String description = translationProvider.getText("dynamic-channel.scene-activate.description", scene.getName()); + return ChannelBuilder.create(channelUid, CoreItemFactory.SWITCH).withType(channelTypeUid) + .withLabel(scene.getName()).withDescription(description).build(); + } +} diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java new file mode 100644 index 0000000000000..7a2f6ebec01a1 --- /dev/null +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java @@ -0,0 +1,106 @@ +/** + * Copyright (c) 2010-2022 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.hdpowerview.internal.builders; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; +import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider; +import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections.SceneCollection; +import org.openhab.core.library.CoreItemFactory; +import org.openhab.core.thing.Channel; +import org.openhab.core.thing.ChannelGroupUID; +import org.openhab.core.thing.ChannelUID; +import org.openhab.core.thing.binding.builder.ChannelBuilder; +import org.openhab.core.thing.type.ChannelTypeUID; + +/** + * The {@link SceneGroupChannelBuilder} class creates scene group channels + * from structured scene collection data. + * + * @author Jacob Laursen - Initial contribution + */ +@NonNullByDefault +public class SceneGroupChannelBuilder { + + private final HDPowerViewTranslationProvider translationProvider; + private final ChannelGroupUID channelGroupUid; + private final ChannelTypeUID channelTypeUid = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID, + HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE); + + private List channels; + private List sceneCollections; + + public SceneGroupChannelBuilder(HDPowerViewTranslationProvider translationProvider, + ChannelGroupUID channelGroupUid) { + this.translationProvider = translationProvider; + this.channelGroupUid = channelGroupUid; + this.channels = new ArrayList<>(0); + this.sceneCollections = new ArrayList<>(0); + } + + /** + * Creates an {@link SceneGroupChannelBuilder} for the given {@link HDPowerViewTranslationProvider} and + * {@link ChannelGroupUID}. + * + * @param translationProvider the {@link HDPowerViewTranslationProvider} + * @param channelGroupUid parent {@link ChannelGroupUID} for created channels + * @return channel builder + */ + public static SceneGroupChannelBuilder create(HDPowerViewTranslationProvider translationProvider, + ChannelGroupUID channelGroupUid) { + return new SceneGroupChannelBuilder(translationProvider, channelGroupUid); + } + + /** + * Adds created channels to existing list. + * + * @param channels list that channels will be added to + * @return channel builder + */ + public SceneGroupChannelBuilder withChannels(List channels) { + this.channels = channels; + return this; + } + + /** + * Sets the scene collections. + * + * @param sceneCollections the scene collections + * @return channel builder + */ + public SceneGroupChannelBuilder withSceneCollections(List sceneCollections) { + this.sceneCollections = sceneCollections; + return this; + } + + /** + * Builds and returns the channels. + * + * @return the {@link Channel} list + */ + public List build() { + sceneCollections.stream().sorted().forEach(sceneCollection -> channels.add(createChannel(sceneCollection))); + return channels; + } + + private Channel createChannel(SceneCollection sceneCollection) { + ChannelUID channelUid = new ChannelUID(channelGroupUid, Integer.toString(sceneCollection.id)); + String description = translationProvider.getText("dynamic-channel.scene-group-activate.description", + sceneCollection.getName()); + return ChannelBuilder.create(channelUid, CoreItemFactory.SWITCH).withType(channelTypeUid) + .withLabel(sceneCollection.getName()).withDescription(description).build(); + } +} diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/handler/HDPowerViewHubHandler.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/handler/HDPowerViewHubHandler.java index 8b3d2c60e844f..b1f4de389ade6 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/handler/HDPowerViewHubHandler.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/handler/HDPowerViewHubHandler.java @@ -12,16 +12,11 @@ */ package org.openhab.binding.hdpowerview.internal.handler; -import java.time.DayOfWeek; -import java.time.LocalTime; -import java.time.format.TextStyle; import java.util.ArrayList; -import java.util.EnumSet; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.StringJoiner; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -44,6 +39,9 @@ import org.openhab.binding.hdpowerview.internal.api.responses.ScheduledEvents.ScheduledEvent; import org.openhab.binding.hdpowerview.internal.api.responses.Shades; import org.openhab.binding.hdpowerview.internal.api.responses.Shades.ShadeData; +import org.openhab.binding.hdpowerview.internal.builders.AutomationChannelBuilder; +import org.openhab.binding.hdpowerview.internal.builders.SceneChannelBuilder; +import org.openhab.binding.hdpowerview.internal.builders.SceneGroupChannelBuilder; import org.openhab.binding.hdpowerview.internal.config.HDPowerViewHubConfiguration; import org.openhab.binding.hdpowerview.internal.config.HDPowerViewShadeConfiguration; import org.openhab.binding.hdpowerview.internal.exceptions.HubException; @@ -261,12 +259,12 @@ private synchronized void poll() { pollShades(); List scenes = updateSceneChannels(); - List sceneCollections = updateSceneCollectionChannels(); - List scheduledEvents = updateScheduledEventChannels(scenes, sceneCollections); + List sceneCollections = updateSceneGroupChannels(); + List scheduledEvents = updateAutomationChannels(scenes, sceneCollections); // Scheduled events should also have their current state updated if event has been // enabled or disabled through app or other integration. - updateScheduledEventStates(scheduledEvents); + updateAutomationStates(scheduledEvents); } catch (HubInvalidResponseException e) { Throwable cause = e.getCause(); if (cause == null) { @@ -383,23 +381,17 @@ private List updateSceneChannels() List allChannels = new ArrayList<>(getThing().getChannels()); allChannels.removeIf(c -> HDPowerViewBindingConstants.CHANNEL_GROUP_SCENES.equals(c.getUID().getGroupId())); - scenes.stream().sorted().forEach(scene -> allChannels.add(createSceneChannel(scene))); - updateThing(editThing().withChannels(allChannels).build()); - createDeprecatedSceneChannels(scenes); + SceneChannelBuilder channelBuilder = SceneChannelBuilder + .create(this.translationProvider, + new ChannelGroupUID(thing.getUID(), HDPowerViewBindingConstants.CHANNEL_GROUP_SCENES)) + .withScenes(scenes).withChannels(allChannels); - return scenes; - } + updateThing(editThing().withChannels(channelBuilder.build()).build()); - private Channel createSceneChannel(Scene scene) { - ChannelGroupUID channelGroupUid = new ChannelGroupUID(thing.getUID(), - HDPowerViewBindingConstants.CHANNEL_GROUP_SCENES); - ChannelUID channelUid = new ChannelUID(channelGroupUid, Integer.toString(scene.id)); - String description = translationProvider.getText("dynamic-channel.scene-activate.description", scene.getName()); - Channel channel = ChannelBuilder.create(channelUid, CoreItemFactory.SWITCH).withType(sceneChannelTypeUID) - .withLabel(scene.getName()).withDescription(description).build(); + createDeprecatedSceneChannels(scenes); - return channel; + return scenes; } /** @@ -460,40 +452,32 @@ private List fetchSceneCollections() return sceneCollectionData; } - private List updateSceneCollectionChannels() + private List updateSceneGroupChannels() throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException { List sceneCollections = fetchSceneCollections(); if (sceneCollections.size() == sceneCollectionCache.size() && sceneCollectionCache.containsAll(sceneCollections)) { // Duplicates are not allowed. Reordering is not supported. - logger.debug("Preserving scene collection channels, no changes detected"); + logger.debug("Preserving scene group channels, no changes detected"); return sceneCollections; } - logger.debug("Updating all scene collection channels, changes detected"); + logger.debug("Updating all scene group channels, changes detected"); sceneCollectionCache = new CopyOnWriteArrayList(sceneCollections); List allChannels = new ArrayList<>(getThing().getChannels()); allChannels .removeIf(c -> HDPowerViewBindingConstants.CHANNEL_GROUP_SCENE_GROUPS.equals(c.getUID().getGroupId())); - sceneCollections.stream().sorted() - .forEach(sceneCollection -> allChannels.add(createSceneCollectionChannel(sceneCollection))); - updateThing(editThing().withChannels(allChannels).build()); - return sceneCollections; - } + SceneGroupChannelBuilder channelBuilder = SceneGroupChannelBuilder + .create(this.translationProvider, + new ChannelGroupUID(thing.getUID(), HDPowerViewBindingConstants.CHANNEL_GROUP_SCENE_GROUPS)) + .withSceneCollections(sceneCollections).withChannels(allChannels); - private Channel createSceneCollectionChannel(SceneCollection sceneCollection) { - ChannelGroupUID channelGroupUid = new ChannelGroupUID(thing.getUID(), - HDPowerViewBindingConstants.CHANNEL_GROUP_SCENE_GROUPS); - ChannelUID channelUid = new ChannelUID(channelGroupUid, Integer.toString(sceneCollection.id)); - String description = translationProvider.getText("dynamic-channel.scene-group-activate.description", - sceneCollection.getName()); - Channel channel = ChannelBuilder.create(channelUid, CoreItemFactory.SWITCH).withType(sceneGroupChannelTypeUID) - .withLabel(sceneCollection.getName()).withDescription(description).build(); - - return channel; + updateThing(editThing().withChannels(channelBuilder.build()).build()); + + return sceneCollections; } private List fetchScheduledEvents() @@ -513,140 +497,33 @@ private List fetchScheduledEvents() return scheduledEventData; } - private List updateScheduledEventChannels(List scenes, - List sceneCollections) + private List updateAutomationChannels(List scenes, List sceneCollections) throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException { List scheduledEvents = fetchScheduledEvents(); if (scheduledEvents.size() == scheduledEventCache.size() && scheduledEventCache.containsAll(scheduledEvents)) { // Duplicates are not allowed. Reordering is not supported. - logger.debug("Preserving scheduled event channels, no changes detected"); + logger.debug("Preserving automation channels, no changes detected"); return scheduledEvents; } - logger.debug("Updating all scheduled event channels, changes detected"); + logger.debug("Updating all automation channels, changes detected"); scheduledEventCache = new CopyOnWriteArrayList(scheduledEvents); List allChannels = new ArrayList<>(getThing().getChannels()); allChannels .removeIf(c -> HDPowerViewBindingConstants.CHANNEL_GROUP_AUTOMATIONS.equals(c.getUID().getGroupId())); - scheduledEvents.stream().forEach(scheduledEvent -> { - Channel channel = createScheduledEventChannel(scheduledEvent, scenes, sceneCollections); - if (channel != null) { - allChannels.add(channel); - } - }); - updateThing(editThing().withChannels(allChannels).build()); + AutomationChannelBuilder channelBuilder = AutomationChannelBuilder + .create(this.translationProvider, + new ChannelGroupUID(thing.getUID(), HDPowerViewBindingConstants.CHANNEL_GROUP_AUTOMATIONS)) + .withScenes(scenes).withSceneCollections(sceneCollections).withScheduledEvents(scheduledEvents) + .withChannels(allChannels); + updateThing(editThing().withChannels(channelBuilder.build()).build()); return scheduledEvents; } - private @Nullable Channel createScheduledEventChannel(ScheduledEvent scheduledEvent, List scenes, - List sceneCollections) { - String referencedName = getReferencedSceneOrSceneCollectionName(scheduledEvent, scenes, sceneCollections); - if (referencedName == null) { - return null; - } - ChannelGroupUID channelGroupUid = new ChannelGroupUID(thing.getUID(), - HDPowerViewBindingConstants.CHANNEL_GROUP_AUTOMATIONS); - ChannelUID channelUid = new ChannelUID(channelGroupUid, Integer.toString(scheduledEvent.id)); - String label = getScheduledEventName(referencedName, scheduledEvent); - String description = translationProvider.getText("dynamic-channel.automation-enabled.description", - referencedName); - Channel channel = ChannelBuilder.create(channelUid, CoreItemFactory.SWITCH).withType(automationChannelTypeUID) - .withLabel(label).withDescription(description).build(); - - return channel; - } - - private @Nullable String getReferencedSceneOrSceneCollectionName(ScheduledEvent scheduledEvent, List scenes, - List sceneCollections) { - if (scheduledEvent.sceneId > 0) { - for (Scene scene : scenes) { - if (scene.id == scheduledEvent.sceneId) { - return scene.getName(); - } - } - logger.error("Scene '{}' was not found for scheduled event '{}'", scheduledEvent.sceneId, - scheduledEvent.id); - return null; - } else if (scheduledEvent.sceneCollectionId > 0) { - for (SceneCollection sceneCollection : sceneCollections) { - if (sceneCollection.id == scheduledEvent.sceneCollectionId) { - return sceneCollection.getName(); - } - } - logger.error("Scene collection '{}' was not found for scheduled event '{}'", - scheduledEvent.sceneCollectionId, scheduledEvent.id); - return null; - } else { - logger.error("Scheduled event '{}'' not related to any scene or scene collection", scheduledEvent.id); - return null; - } - } - - private String getScheduledEventName(String sceneName, ScheduledEvent scheduledEvent) { - String timeString, daysString; - - switch (scheduledEvent.eventType) { - case ScheduledEvents.SCHEDULED_EVENT_TYPE_TIME: - timeString = LocalTime.of(scheduledEvent.hour, scheduledEvent.minute).toString(); - break; - case ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE: - if (scheduledEvent.minute == 0) { - timeString = translationProvider.getText("dynamic-channel.automation.at_sunrise"); - } else if (scheduledEvent.minute < 0) { - timeString = translationProvider.getText("dynamic-channel.automation.before_sunrise", - getFormattedTimeOffset(-scheduledEvent.minute)); - } else { - timeString = translationProvider.getText("dynamic-channel.automation.after_sunrise", - getFormattedTimeOffset(scheduledEvent.minute)); - } - break; - case ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNSET: - if (scheduledEvent.minute == 0) { - timeString = translationProvider.getText("dynamic-channel.automation.at_sunset"); - } else if (scheduledEvent.minute < 0) { - timeString = translationProvider.getText("dynamic-channel.automation.before_sunset", - getFormattedTimeOffset(-scheduledEvent.minute)); - } else { - timeString = translationProvider.getText("dynamic-channel.automation.after_sunset", - getFormattedTimeOffset(scheduledEvent.minute)); - } - break; - default: - return sceneName; - } - - EnumSet days = scheduledEvent.getDays(); - if (EnumSet.allOf(DayOfWeek.class).equals(days)) { - daysString = translationProvider.getText("dynamic-channel.automation.all-days"); - } else if (ScheduledEvents.WEEKDAYS.equals(days)) { - daysString = translationProvider.getText("dynamic-channel.automation.weekdays"); - } else if (ScheduledEvents.WEEKENDS.equals(days)) { - daysString = translationProvider.getText("dynamic-channel.automation.weekends"); - } else { - StringJoiner joiner = new StringJoiner(", "); - days.forEach(day -> joiner.add(day.getDisplayName(TextStyle.SHORT, translationProvider.getLocale()))); - daysString = joiner.toString(); - } - - return translationProvider.getText("dynamic-channel.automation-enabled.label", sceneName, timeString, - daysString); - } - - private String getFormattedTimeOffset(int minutes) { - if (minutes >= 60) { - int remainder = minutes % 60; - if (remainder == 0) { - return translationProvider.getText("dynamic-channel.automation.hour", minutes / 60); - } - return translationProvider.getText("dynamic-channel.automation.hour-minute", minutes / 60, remainder); - } - return translationProvider.getText("dynamic-channel.automation.minute", minutes); - } - - private void updateScheduledEventStates(List scheduledEvents) { + private void updateAutomationStates(List scheduledEvents) { ChannelGroupUID channelGroupUid = new ChannelGroupUID(thing.getUID(), HDPowerViewBindingConstants.CHANNEL_GROUP_AUTOMATIONS); for (ScheduledEvent scheduledEvent : scheduledEvents) { diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/i18n/hdpowerview.properties b/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/i18n/hdpowerview.properties index 4a6ec1312e3d6..b9b8efc4c9cbc 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/i18n/hdpowerview.properties +++ b/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/i18n/hdpowerview.properties @@ -52,12 +52,12 @@ dynamic-channel.automation-enabled.label = {0}, {1}, {2} dynamic-channel.automation.hour = {0}hr dynamic-channel.automation.minute = {0}m dynamic-channel.automation.hour-minute = {0}hr {1}m -dynamic-channel.automation.at_sunrise = At sunrise -dynamic-channel.automation.before_sunrise = {0} before sunrise -dynamic-channel.automation.after_sunrise = {0} after sunrise -dynamic-channel.automation.at_sunset = At sunset -dynamic-channel.automation.before_sunset = {0} before sunset -dynamic-channel.automation.after_sunset = {0} after sunset +dynamic-channel.automation.at-sunrise = At sunrise +dynamic-channel.automation.before-sunrise = {0} before sunrise +dynamic-channel.automation.after-sunrise = {0} after sunrise +dynamic-channel.automation.at-sunset = At sunset +dynamic-channel.automation.before-sunset = {0} before sunset +dynamic-channel.automation.after-sunset = {0} after sunset dynamic-channel.automation.weekdays = Weekdays dynamic-channel.automation.weekends = Weekends dynamic-channel.automation.all-days = All days diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/i18n/hdpowerview_da.properties b/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/i18n/hdpowerview_da.properties index 6b4483f261e17..7f3a7fdf0c1f0 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/i18n/hdpowerview_da.properties +++ b/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/i18n/hdpowerview_da.properties @@ -8,12 +8,12 @@ dynamic-channel.automation-enabled.label = {0}, {1}, {2} dynamic-channel.automation.hour = {0}t dynamic-channel.automation.minute = {0}m dynamic-channel.automation.hour-minute = {0}t {1}m -dynamic-channel.automation.at_sunrise = Ved solopgang -dynamic-channel.automation.before_sunrise = {0} før solopgang -dynamic-channel.automation.after_sunrise = {0} efter solopgang -dynamic-channel.automation.at_sunset = Ved solnedgang -dynamic-channel.automation.before_sunset = {0} før solnedgang -dynamic-channel.automation.after_sunset = {0} efter solnedgang +dynamic-channel.automation.at-sunrise = Ved solopgang +dynamic-channel.automation.before-sunrise = {0} før solopgang +dynamic-channel.automation.after-sunrise = {0} efter solopgang +dynamic-channel.automation.at-sunset = Ved solnedgang +dynamic-channel.automation.before-sunset = {0} før solnedgang +dynamic-channel.automation.after-sunset = {0} efter solnedgang dynamic-channel.automation.weekdays = Ugedage dynamic-channel.automation.weekends = Weekend dynamic-channel.automation.all-days = Alle dage diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/thing/thing-types.xml index 80b128b60278b..595392183ac75 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/thing/thing-types.xml @@ -18,6 +18,7 @@ Hunter Douglas (Luxaflex) PowerView Hub + host @@ -70,10 +71,6 @@ Hunter Douglas (Luxaflex) PowerView Motorized Shade - - - - id diff --git a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/AutomationChannelBuilderTest.java b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/AutomationChannelBuilderTest.java new file mode 100644 index 0000000000000..374d069482a31 --- /dev/null +++ b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/AutomationChannelBuilderTest.java @@ -0,0 +1,247 @@ +/** + * Copyright (c) 2010-2022 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.hdpowerview; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; + +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +import org.eclipse.jdt.annotation.NonNull; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; +import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider; +import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections.SceneCollection; +import org.openhab.binding.hdpowerview.internal.api.responses.Scenes.Scene; +import org.openhab.binding.hdpowerview.internal.api.responses.ScheduledEvents; +import org.openhab.binding.hdpowerview.internal.api.responses.ScheduledEvents.ScheduledEvent; +import org.openhab.binding.hdpowerview.internal.builders.AutomationChannelBuilder; +import org.openhab.core.thing.Channel; +import org.openhab.core.thing.ChannelGroupUID; +import org.openhab.core.thing.ThingUID; +import org.osgi.framework.Bundle; + +/** + * Unit tests for {@link AutomationChannelBuilder}. + * + * @author Jacob Laursen - Initial contribution + */ +public class AutomationChannelBuilderTest { + + private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID( + new ThingUID(HDPowerViewBindingConstants.BINDING_ID, AutomationChannelBuilderTest.class.getSimpleName()), + HDPowerViewBindingConstants.CHANNELTYPE_AUTOMATION_ENABLED); + + private HDPowerViewTranslationProvider translationProvider; + private AutomationChannelBuilder builder; + private List scenes; + private List sceneCollections; + + @BeforeEach + private void setUp() { + translationProvider = new HDPowerViewTranslationProvider(mock(Bundle.class), new TranslationProviderForTests(), + new LocaleProviderForTests()); + builder = AutomationChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID); + + Scene scene = new Scene(); + scene.id = 1; + scene.name = Base64.getEncoder().encodeToString(("TestScene").getBytes()); + scenes = new ArrayList<>(List.of(scene)); + + SceneCollection sceneCollection = new SceneCollection(); + sceneCollection.id = 2; + sceneCollection.name = Base64.getEncoder().encodeToString(("TestSceneCollection").getBytes()); + sceneCollections = new ArrayList<>(List.of(sceneCollection)); + } + + @Test + public void sceneSunriseWeekends() { + ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE); + scheduledEvent.daySaturday = true; + scheduledEvent.daySunday = true; + + List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + + assertEquals(1, channels.size()); + assertEquals("TestScene, At sunrise, Weekends", channels.get(0).getLabel()); + } + + @Test + public void sceneSunsetWeekdays() { + ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNSET); + scheduledEvent.dayMonday = true; + scheduledEvent.dayTuesday = true; + scheduledEvent.dayWednesday = true; + scheduledEvent.dayThursday = true; + scheduledEvent.dayFriday = true; + + List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + + assertEquals(1, channels.size()); + assertEquals("TestScene, At sunset, Weekdays", channels.get(0).getLabel()); + } + + @Test + public void sceneTimeAllDays() { + ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_TIME); + scheduledEvent.dayMonday = true; + scheduledEvent.dayTuesday = true; + scheduledEvent.dayWednesday = true; + scheduledEvent.dayThursday = true; + scheduledEvent.dayFriday = true; + scheduledEvent.daySaturday = true; + scheduledEvent.daySunday = true; + scheduledEvent.hour = 6; + scheduledEvent.minute = 30; + + List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + + assertEquals(1, channels.size()); + assertEquals("TestScene, 06:30, All days", channels.get(0).getLabel()); + } + + @Test + public void sceneMinutesBeforeSunriseMondayTuesday() { + ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE); + scheduledEvent.dayMonday = true; + scheduledEvent.dayTuesday = true; + scheduledEvent.minute = -15; + + List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + + assertEquals(1, channels.size()); + assertEquals("TestScene, 15m before sunrise, Mon, Tue", channels.get(0).getLabel()); + } + + @Test + public void sceneHoursMinutesAfterSunriseMonday() { + ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE); + scheduledEvent.dayMonday = true; + scheduledEvent.minute = 61; + + List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + + assertEquals(1, channels.size()); + assertEquals("TestScene, 1hr 1m after sunrise, Mon", channels.get(0).getLabel()); + } + + @Test + public void sceneMinutesBeforeSunsetWednesdayThursdayFriday() { + ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNSET); + scheduledEvent.dayWednesday = true; + scheduledEvent.dayThursday = true; + scheduledEvent.dayFriday = true; + scheduledEvent.minute = -59; + + List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + + assertEquals(1, channels.size()); + assertEquals("TestScene, 59m before sunset, Wed, Thu, Fri", channels.get(0).getLabel()); + } + + @Test + public void sceneHourAfterSunsetFridaySaturdaySunday() { + ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNSET); + scheduledEvent.dayFriday = true; + scheduledEvent.daySaturday = true; + scheduledEvent.daySunday = true; + scheduledEvent.minute = 60; + + List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + + assertEquals(1, channels.size()); + assertEquals("TestScene, 1hr after sunset, Fri, Sat, Sun", channels.get(0).getLabel()); + } + + @Test + public void sceneCollection() { + ScheduledEvent scheduledEvent = createScheduledEventWithSceneCollection( + ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE); + + List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); + + @NonNull + List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections) + .withScheduledEvents(scheduledEvents).build(); + + assertEquals(1, channels.size()); + assertEquals("TestSceneCollection, At sunrise, ", channels.get(0).getLabel()); + } + + @Test + public void suppliedListIsUsed() { + ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE); + List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); + List existingChannels = new ArrayList<>(0); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents) + .withChannels(existingChannels).build(); + + assertEquals(existingChannels, channels); + } + + @Test + public void groupAndIdAreCorrect() { + ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE); + scheduledEvent.id = 42; + List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + + assertEquals(1, channels.size()); + assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId()); + assertEquals(Integer.toString(scheduledEvent.id), channels.get(0).getUID().getIdWithoutGroup()); + } + + private ScheduledEvent createScheduledEventWithScene(int eventType) { + ScheduledEvent scheduledEvent = new ScheduledEvent(); + scheduledEvent.id = 1; + scheduledEvent.sceneId = scenes.get(0).id; + scheduledEvent.eventType = eventType; + return scheduledEvent; + } + + private ScheduledEvent createScheduledEventWithSceneCollection(int eventType) { + ScheduledEvent scheduledEvent = new ScheduledEvent(); + scheduledEvent.id = 1; + scheduledEvent.sceneCollectionId = sceneCollections.get(0).id; + scheduledEvent.eventType = eventType; + return scheduledEvent; + } +} diff --git a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/LocaleProviderForTests.java b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/LocaleProviderForTests.java new file mode 100644 index 0000000000000..53747462c29e4 --- /dev/null +++ b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/LocaleProviderForTests.java @@ -0,0 +1,30 @@ +/** + * Copyright (c) 2010-2022 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.hdpowerview; + +import java.util.Locale; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.openhab.core.i18n.LocaleProvider; + +/** + * Locale provider for unit tests. + * + * @author Jacob Laursen - Initial contribution + */ +@NonNullByDefault +public class LocaleProviderForTests implements LocaleProvider { + public Locale getLocale() { + return Locale.ENGLISH; + } +} diff --git a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneChannelBuilderTest.java b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneChannelBuilderTest.java new file mode 100644 index 0000000000000..b354940329a49 --- /dev/null +++ b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneChannelBuilderTest.java @@ -0,0 +1,106 @@ +/** + * Copyright (c) 2010-2022 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.hdpowerview; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; + +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +import org.eclipse.jdt.annotation.NonNull; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; +import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider; +import org.openhab.binding.hdpowerview.internal.api.responses.Scenes.Scene; +import org.openhab.binding.hdpowerview.internal.builders.SceneChannelBuilder; +import org.openhab.core.thing.Channel; +import org.openhab.core.thing.ChannelGroupUID; +import org.openhab.core.thing.ThingUID; +import org.osgi.framework.Bundle; + +/** + * Unit tests for {@link SceneChannelBuilder}. + * + * @author Jacob Laursen - Initial contribution + */ +public class SceneChannelBuilderTest { + + private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID( + new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneChannelBuilderTest.class.getSimpleName()), + HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE); + + private HDPowerViewTranslationProvider translationProvider; + private SceneChannelBuilder builder; + + @BeforeEach + private void setUp() { + translationProvider = new HDPowerViewTranslationProvider(mock(Bundle.class), new TranslationProviderForTests(), + new LocaleProviderForTests()); + builder = SceneChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID); + } + + @Test + public void labelIsCorrect() { + List scenes = createScenes(); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).build(); + + assertEquals(1, channels.size()); + assertEquals("TestScene", channels.get(0).getLabel()); + } + + @Test + public void descriptionIsCorrect() { + List scenes = createScenes(); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).build(); + + assertEquals(1, channels.size()); + assertEquals("Activates the scene 'TestScene'", channels.get(0).getDescription()); + } + + @Test + public void groupAndIdAreCorrect() { + List scenes = createScenes(); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).build(); + + assertEquals(1, channels.size()); + assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId()); + assertEquals(Integer.toString(scenes.get(0).id), channels.get(0).getUID().getIdWithoutGroup()); + } + + @Test + public void suppliedListIsUsed() { + List scenes = createScenes(); + List existingChannels = new ArrayList<>(0); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).withChannels(existingChannels).build(); + + assertEquals(existingChannels, channels); + } + + private List createScenes() { + Scene scene = new Scene(); + scene.id = 1; + scene.name = Base64.getEncoder().encodeToString(("TestScene").getBytes()); + return new ArrayList<>(List.of(scene)); + } +} diff --git a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneGroupChannelBuilderTest.java b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneGroupChannelBuilderTest.java new file mode 100644 index 0000000000000..c4997413c1b4d --- /dev/null +++ b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneGroupChannelBuilderTest.java @@ -0,0 +1,107 @@ +/** + * Copyright (c) 2010-2022 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.hdpowerview; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; + +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +import org.eclipse.jdt.annotation.NonNull; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; +import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider; +import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections.SceneCollection; +import org.openhab.binding.hdpowerview.internal.builders.SceneGroupChannelBuilder; +import org.openhab.core.thing.Channel; +import org.openhab.core.thing.ChannelGroupUID; +import org.openhab.core.thing.ThingUID; +import org.osgi.framework.Bundle; + +/** + * Unit tests for {@link SceneGroupChannelBuilder}. + * + * @author Jacob Laursen - Initial contribution + */ +public class SceneGroupChannelBuilderTest { + + private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID( + new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneGroupChannelBuilderTest.class.getSimpleName()), + HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE); + + private HDPowerViewTranslationProvider translationProvider; + private SceneGroupChannelBuilder builder; + + @BeforeEach + private void setUp() { + translationProvider = new HDPowerViewTranslationProvider(mock(Bundle.class), new TranslationProviderForTests(), + new LocaleProviderForTests()); + builder = SceneGroupChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID); + } + + @Test + public void labelIsCorrect() { + List sceneCollections = createSceneCollections(); + + @NonNull + List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections).build(); + + assertEquals(1, channels.size()); + assertEquals("TestSceneCollection", channels.get(0).getLabel()); + } + + @Test + public void descriptionIsCorrect() { + List sceneCollections = createSceneCollections(); + + @NonNull + List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections).build(); + + assertEquals(1, channels.size()); + assertEquals("Activates the scene group 'TestSceneCollection'", channels.get(0).getDescription()); + } + + @Test + public void groupAndIdAreCorrect() { + List sceneCollections = createSceneCollections(); + + @NonNull + List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections).build(); + + assertEquals(1, channels.size()); + assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId()); + assertEquals(Integer.toString(sceneCollections.get(0).id), channels.get(0).getUID().getIdWithoutGroup()); + } + + @Test + public void suppliedListIsUsed() { + List sceneCollections = createSceneCollections(); + List existingChannels = new ArrayList<>(0); + + @NonNull + List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections).withChannels(existingChannels) + .build(); + + assertEquals(existingChannels, channels); + } + + private List createSceneCollections() { + SceneCollection sceneCollection = new SceneCollection(); + sceneCollection.id = 1; + sceneCollection.name = Base64.getEncoder().encodeToString(("TestSceneCollection").getBytes()); + return new ArrayList<>(List.of(sceneCollection)); + } +} diff --git a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/TranslationProviderForTests.java b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/TranslationProviderForTests.java new file mode 100644 index 0000000000000..282c7875d7493 --- /dev/null +++ b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/TranslationProviderForTests.java @@ -0,0 +1,66 @@ +/** + * Copyright (c) 2010-2022 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.hdpowerview; + +import static java.util.Map.entry; + +import java.text.MessageFormat; +import java.util.Locale; +import java.util.Map; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.i18n.TranslationProvider; +import org.osgi.framework.Bundle; + +/** + * Translation provider for unit tests. + * + * @author Jacob Laursen - Initial contribution + */ +@NonNullByDefault +public class TranslationProviderForTests implements TranslationProvider { + + private final static Map texts = Map.ofEntries( + entry("dynamic-channel.scene-activate.description", "Activates the scene ''{0}''"), + entry("dynamic-channel.scene-group-activate.description", "Activates the scene group ''{0}''"), + entry("dynamic-channel.automation-enabled.description", "Enables/disables the automation ''{0}''"), + entry("dynamic-channel.automation-enabled.label", "{0}, {1}, {2}"), + entry("dynamic-channel.automation.hour", "{0}hr"), entry("dynamic-channel.automation.minute", "{0}m"), + entry("dynamic-channel.automation.hour-minute", "{0}hr {1}m"), + entry("dynamic-channel.automation.at-sunrise", "At sunrise"), + entry("dynamic-channel.automation.before-sunrise", "{0} before sunrise"), + entry("dynamic-channel.automation.after-sunrise", "{0} after sunrise"), + entry("dynamic-channel.automation.at-sunset", "At sunset"), + entry("dynamic-channel.automation.before-sunset", "{0} before sunset"), + entry("dynamic-channel.automation.after-sunset", "{0} after sunset"), + entry("dynamic-channel.automation.weekdays", "Weekdays"), + entry("dynamic-channel.automation.weekends", "Weekends"), + entry("dynamic-channel.automation.all-days", "All days")); + + public TranslationProviderForTests() { + } + + @Nullable + public String getText(@Nullable Bundle bundle, @Nullable String key, @Nullable String defaultText, + @Nullable Locale locale) { + return ""; + } + + @Nullable + public String getText(@Nullable Bundle bundle, @Nullable String key, @Nullable String defaultText, + @Nullable Locale locale, @Nullable Object @Nullable... arguments) { + String text = texts.get(key); + return MessageFormat.format(text != null ? text : key, arguments); + } +} From 901461327af5f35e62f7162d4218e0ba396ab500 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Fri, 7 Jan 2022 23:54:15 +0100 Subject: [PATCH 2/8] Avoid double list allocations. Signed-off-by: Jacob Laursen --- .../builders/AutomationChannelBuilder.java | 35 +++++++++++++++---- .../builders/SceneChannelBuilder.java | 15 ++++++-- .../builders/SceneGroupChannelBuilder.java | 15 ++++++-- 3 files changed, 54 insertions(+), 11 deletions(-) diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java index ca5593adb3c31..6ba37bd8cd94f 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java @@ -17,12 +17,12 @@ import java.time.format.TextStyle; import java.util.ArrayList; import java.util.EnumSet; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.StringJoiner; import java.util.stream.Collectors; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; @@ -55,19 +55,19 @@ public class AutomationChannelBuilder { private final ChannelTypeUID channelTypeUid = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID, HDPowerViewBindingConstants.CHANNELTYPE_AUTOMATION_ENABLED); + @Nullable private List channels; + @Nullable private Map scenes; + @Nullable private Map sceneCollections; + @Nullable private List scheduledEvents; public AutomationChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid) { this.translationProvider = translationProvider; this.channelGroupUid = channelGroupUid; - this.channels = new ArrayList<>(0); - this.scheduledEvents = new ArrayList<>(0); - this.scenes = new HashMap<>(0); - this.sceneCollections = new HashMap<>(0); } /** @@ -134,6 +134,11 @@ public AutomationChannelBuilder withScheduledEvents(List schedul * @return the {@link Channel} list */ public List build() { + if (scheduledEvents == null || (scenes == null && sceneCollections == null)) { + return this.getChannelList(0); + } + List scheduledEvents = (@NonNull List) this.scheduledEvents; + List channels = this.getChannelList(scheduledEvents.size()); scheduledEvents.stream().forEach(scheduledEvent -> { Channel channel = createChannel(scheduledEvent); if (channel != null) { @@ -144,6 +149,10 @@ public List build() { return channels; } + private List getChannelList(int initialCapacity) { + return this.channels != null ? (@NonNull List) this.channels : new ArrayList<>(initialCapacity); + } + private @Nullable Channel createChannel(ScheduledEvent scheduledEvent) { String referencedName = getReferencedSceneOrSceneCollectionName(scheduledEvent); if (referencedName == null) { @@ -161,14 +170,26 @@ public List build() { private @Nullable String getReferencedSceneOrSceneCollectionName(ScheduledEvent scheduledEvent) { if (scheduledEvent.sceneId > 0) { - Scene scene = scenes.get(scheduledEvent.sceneId); + if (scenes == null) { + logger.warn("Scheduled event '{}' references scene '{}', but no scenes are loaded", scheduledEvent.id, + scheduledEvent.sceneId); + return null; + } + Scene scene = ((@NonNull Map) scenes).get(scheduledEvent.sceneId); if (scene != null) { return scene.getName(); } logger.warn("Scene '{}' was not found for scheduled event '{}'", scheduledEvent.sceneId, scheduledEvent.id); return null; } else if (scheduledEvent.sceneCollectionId > 0) { - SceneCollection sceneCollection = sceneCollections.get(scheduledEvent.sceneCollectionId); + if (sceneCollections == null) { + logger.warn( + "Scheduled event '{}' references scene collection '{}', but no scene collections are loaded", + scheduledEvent.id, scheduledEvent.sceneCollectionId); + return null; + } + SceneCollection sceneCollection = ((@NonNull Map) sceneCollections) + .get(scheduledEvent.sceneCollectionId); if (sceneCollection != null) { return sceneCollection.getName(); } diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java index 2ef67dcdb42dc..a1892c3dc3094 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java @@ -15,7 +15,9 @@ import java.util.ArrayList; import java.util.List; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider; import org.openhab.binding.hdpowerview.internal.api.responses.Scenes.Scene; @@ -40,14 +42,14 @@ public class SceneChannelBuilder { private final ChannelTypeUID channelTypeUid = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID, HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE); + @Nullable private List channels; + @Nullable private List scenes; public SceneChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid) { this.translationProvider = translationProvider; this.channelGroupUid = channelGroupUid; - this.channels = new ArrayList<>(0); - this.scenes = new ArrayList<>(0); } /** @@ -91,10 +93,19 @@ public SceneChannelBuilder withScenes(List scenes) { * @return the {@link Channel} list */ public List build() { + if (scenes == null) { + return this.getChannelList(0); + } + List scenes = (@NonNull List) this.scenes; + List channels = this.getChannelList(scenes.size()); scenes.stream().sorted().forEach(scene -> channels.add(createChannel(scene))); return channels; } + private List getChannelList(int initialCapacity) { + return this.channels != null ? (@NonNull List) this.channels : new ArrayList<>(initialCapacity); + } + private Channel createChannel(Scene scene) { ChannelUID channelUid = new ChannelUID(channelGroupUid, Integer.toString(scene.id)); String description = translationProvider.getText("dynamic-channel.scene-activate.description", scene.getName()); diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java index 7a2f6ebec01a1..aaa9db97e5a15 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java @@ -15,7 +15,9 @@ import java.util.ArrayList; import java.util.List; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider; import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections.SceneCollection; @@ -40,15 +42,15 @@ public class SceneGroupChannelBuilder { private final ChannelTypeUID channelTypeUid = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID, HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE); + @Nullable private List channels; + @Nullable private List sceneCollections; public SceneGroupChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid) { this.translationProvider = translationProvider; this.channelGroupUid = channelGroupUid; - this.channels = new ArrayList<>(0); - this.sceneCollections = new ArrayList<>(0); } /** @@ -92,10 +94,19 @@ public SceneGroupChannelBuilder withSceneCollections(List scene * @return the {@link Channel} list */ public List build() { + if (sceneCollections == null) { + return this.getChannelList(0); + } + List sceneCollections = (@NonNull List) this.sceneCollections; + List channels = this.getChannelList(sceneCollections.size()); sceneCollections.stream().sorted().forEach(sceneCollection -> channels.add(createChannel(sceneCollection))); return channels; } + private List getChannelList(int initialCapacity) { + return this.channels != null ? (@NonNull List) this.channels : new ArrayList<>(initialCapacity); + } + private Channel createChannel(SceneCollection sceneCollection) { ChannelUID channelUid = new ChannelUID(channelGroupUid, Integer.toString(sceneCollection.id)); String description = translationProvider.getText("dynamic-channel.scene-group-activate.description", From c1dd6b935f5a75eb51acd69afcd006ef2865be40 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Sat, 8 Jan 2022 19:39:58 +0100 Subject: [PATCH 3/8] Add test coverage for scenarios with no channels built. Signed-off-by: Jacob Laursen --- .../AutomationChannelBuilderTest.java | 43 +++++++++++++++++++ .../hdpowerview/SceneChannelBuilderTest.java | 8 ++++ .../SceneGroupChannelBuilderTest.java | 8 ++++ 3 files changed, 59 insertions(+) diff --git a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/AutomationChannelBuilderTest.java b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/AutomationChannelBuilderTest.java index 374d069482a31..465672926a639 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/AutomationChannelBuilderTest.java +++ b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/AutomationChannelBuilderTest.java @@ -215,6 +215,49 @@ public void suppliedListIsUsed() { assertEquals(existingChannels, channels); } + @Test + public void emptyListWhenNoScheduledEvents() { + @NonNull + List<@NonNull Channel> channels = builder.build(); + + assertEquals(0, channels.size()); + } + + @Test + public void emptyListWhenNoScenesOrSceneCollections() { + ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE); + List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); + + @NonNull + List<@NonNull Channel> channels = builder.withScheduledEvents(scheduledEvents).build(); + + assertEquals(0, channels.size()); + } + + @Test + public void emptyListWhenNoSceneForScheduledEvent() { + ScheduledEvent scheduledEvent = createScheduledEventWithSceneCollection( + ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE); + List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + + assertEquals(0, channels.size()); + } + + @Test + public void emptyListWhenNoSceneCollectionForScheduledEvent() { + ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE); + List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); + + @NonNull + List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections) + .withScheduledEvents(scheduledEvents).build(); + + assertEquals(0, channels.size()); + } + @Test public void groupAndIdAreCorrect() { ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE); diff --git a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneChannelBuilderTest.java b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneChannelBuilderTest.java index b354940329a49..1769b28f34dbe 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneChannelBuilderTest.java +++ b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneChannelBuilderTest.java @@ -97,6 +97,14 @@ public void suppliedListIsUsed() { assertEquals(existingChannels, channels); } + @Test + public void emptyListWhenNoScenes() { + @NonNull + List<@NonNull Channel> channels = builder.build(); + + assertEquals(0, channels.size()); + } + private List createScenes() { Scene scene = new Scene(); scene.id = 1; diff --git a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneGroupChannelBuilderTest.java b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneGroupChannelBuilderTest.java index c4997413c1b4d..dbef2580ff45a 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneGroupChannelBuilderTest.java +++ b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneGroupChannelBuilderTest.java @@ -98,6 +98,14 @@ public void suppliedListIsUsed() { assertEquals(existingChannels, channels); } + @Test + public void emptyListWhenNoSceneCollections() { + @NonNull + List<@NonNull Channel> channels = builder.build(); + + assertEquals(0, channels.size()); + } + private List createSceneCollections() { SceneCollection sceneCollection = new SceneCollection(); sceneCollection.id = 1; From 8799fc8b1c8df627f481d5c35327f1d64a92cb10 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Sat, 8 Jan 2022 20:12:34 +0100 Subject: [PATCH 4/8] Extract common builder stuff to super class. Signed-off-by: Jacob Laursen --- .../builders/AutomationChannelBuilder.java | 17 +----- .../internal/builders/BaseChannelBuilder.java | 53 +++++++++++++++++++ .../builders/SceneChannelBuilder.java | 18 +------ .../builders/SceneGroupChannelBuilder.java | 18 +------ 4 files changed, 59 insertions(+), 47 deletions(-) create mode 100644 bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/BaseChannelBuilder.java diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java index 6ba37bd8cd94f..34a892de3f864 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java @@ -15,7 +15,6 @@ import java.time.DayOfWeek; import java.time.LocalTime; import java.time.format.TextStyle; -import java.util.ArrayList; import java.util.EnumSet; import java.util.List; import java.util.Map; @@ -36,7 +35,6 @@ import org.openhab.core.thing.ChannelGroupUID; import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.binding.builder.ChannelBuilder; -import org.openhab.core.thing.type.ChannelTypeUID; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,16 +45,10 @@ * @author Jacob Laursen - Initial contribution */ @NonNullByDefault -public class AutomationChannelBuilder { +public class AutomationChannelBuilder extends BaseChannelBuilder { private final Logger logger = LoggerFactory.getLogger(AutomationChannelBuilder.class); - private final HDPowerViewTranslationProvider translationProvider; - private final ChannelGroupUID channelGroupUid; - private final ChannelTypeUID channelTypeUid = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID, - HDPowerViewBindingConstants.CHANNELTYPE_AUTOMATION_ENABLED); - @Nullable - private List channels; @Nullable private Map scenes; @Nullable @@ -66,8 +58,7 @@ public class AutomationChannelBuilder { public AutomationChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid) { - this.translationProvider = translationProvider; - this.channelGroupUid = channelGroupUid; + super(translationProvider, channelGroupUid, HDPowerViewBindingConstants.CHANNELTYPE_AUTOMATION_ENABLED); } /** @@ -149,10 +140,6 @@ public List build() { return channels; } - private List getChannelList(int initialCapacity) { - return this.channels != null ? (@NonNull List) this.channels : new ArrayList<>(initialCapacity); - } - private @Nullable Channel createChannel(ScheduledEvent scheduledEvent) { String referencedName = getReferencedSceneOrSceneCollectionName(scheduledEvent); if (referencedName == null) { diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/BaseChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/BaseChannelBuilder.java new file mode 100644 index 0000000000000..1bdbde335250a --- /dev/null +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/BaseChannelBuilder.java @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2010-2022 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.hdpowerview.internal.builders; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; +import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider; +import org.openhab.core.thing.Channel; +import org.openhab.core.thing.ChannelGroupUID; +import org.openhab.core.thing.type.ChannelTypeUID; + +/** + * The {@link BaseChannelBuilder} class is super class for + * channel builders. + * + * @author Jacob Laursen - Initial contribution + */ +@NonNullByDefault +public class BaseChannelBuilder { + + protected final HDPowerViewTranslationProvider translationProvider; + protected final ChannelGroupUID channelGroupUid; + protected final ChannelTypeUID channelTypeUid; + + @Nullable + protected List channels; + + public BaseChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid, + String channelTypeId) { + this.translationProvider = translationProvider; + this.channelGroupUid = channelGroupUid; + this.channelTypeUid = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID, channelTypeId); + } + + protected List getChannelList(int initialCapacity) { + return this.channels != null ? (@NonNull List) this.channels : new ArrayList<>(initialCapacity); + } +} diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java index a1892c3dc3094..3fc92cb803282 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java @@ -12,7 +12,6 @@ */ package org.openhab.binding.hdpowerview.internal.builders; -import java.util.ArrayList; import java.util.List; import org.eclipse.jdt.annotation.NonNull; @@ -26,7 +25,6 @@ import org.openhab.core.thing.ChannelGroupUID; import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.binding.builder.ChannelBuilder; -import org.openhab.core.thing.type.ChannelTypeUID; /** * The {@link SceneChannelBuilder} class creates scene channels @@ -35,21 +33,13 @@ * @author Jacob Laursen - Initial contribution */ @NonNullByDefault -public class SceneChannelBuilder { +public class SceneChannelBuilder extends BaseChannelBuilder { - private final HDPowerViewTranslationProvider translationProvider; - private final ChannelGroupUID channelGroupUid; - private final ChannelTypeUID channelTypeUid = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID, - HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE); - - @Nullable - private List channels; @Nullable private List scenes; public SceneChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid) { - this.translationProvider = translationProvider; - this.channelGroupUid = channelGroupUid; + super(translationProvider, channelGroupUid, HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE); } /** @@ -102,10 +92,6 @@ public List build() { return channels; } - private List getChannelList(int initialCapacity) { - return this.channels != null ? (@NonNull List) this.channels : new ArrayList<>(initialCapacity); - } - private Channel createChannel(Scene scene) { ChannelUID channelUid = new ChannelUID(channelGroupUid, Integer.toString(scene.id)); String description = translationProvider.getText("dynamic-channel.scene-activate.description", scene.getName()); diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java index aaa9db97e5a15..4110fa2ce8646 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java @@ -12,7 +12,6 @@ */ package org.openhab.binding.hdpowerview.internal.builders; -import java.util.ArrayList; import java.util.List; import org.eclipse.jdt.annotation.NonNull; @@ -26,7 +25,6 @@ import org.openhab.core.thing.ChannelGroupUID; import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.binding.builder.ChannelBuilder; -import org.openhab.core.thing.type.ChannelTypeUID; /** * The {@link SceneGroupChannelBuilder} class creates scene group channels @@ -35,22 +33,14 @@ * @author Jacob Laursen - Initial contribution */ @NonNullByDefault -public class SceneGroupChannelBuilder { +public class SceneGroupChannelBuilder extends BaseChannelBuilder { - private final HDPowerViewTranslationProvider translationProvider; - private final ChannelGroupUID channelGroupUid; - private final ChannelTypeUID channelTypeUid = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID, - HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE); - - @Nullable - private List channels; @Nullable private List sceneCollections; public SceneGroupChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid) { - this.translationProvider = translationProvider; - this.channelGroupUid = channelGroupUid; + super(translationProvider, channelGroupUid, HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE); } /** @@ -103,10 +93,6 @@ public List build() { return channels; } - private List getChannelList(int initialCapacity) { - return this.channels != null ? (@NonNull List) this.channels : new ArrayList<>(initialCapacity); - } - private Channel createChannel(SceneCollection sceneCollection) { ChannelUID channelUid = new ChannelUID(channelGroupUid, Integer.toString(sceneCollection.id)); String description = translationProvider.getText("dynamic-channel.scene-group-activate.description", From 7e6304b6a962dc08141c09bd6cbecb3c6f96de99 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Sat, 8 Jan 2022 20:56:12 +0100 Subject: [PATCH 5/8] Fix grammar. Signed-off-by: Jacob Laursen --- .../hdpowerview/internal/builders/SceneChannelBuilder.java | 2 +- .../hdpowerview/internal/builders/SceneGroupChannelBuilder.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java index 3fc92cb803282..503b0411c5428 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java @@ -43,7 +43,7 @@ public SceneChannelBuilder(HDPowerViewTranslationProvider translationProvider, C } /** - * Creates an {@link SceneChannelBuilder} for the given {@link HDPowerViewTranslationProvider} and + * Creates a {@link SceneChannelBuilder} for the given {@link HDPowerViewTranslationProvider} and * {@link ChannelGroupUID}. * * @param translationProvider the {@link HDPowerViewTranslationProvider} diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java index 4110fa2ce8646..eca1d6ea3efc1 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java @@ -44,7 +44,7 @@ public SceneGroupChannelBuilder(HDPowerViewTranslationProvider translationProvid } /** - * Creates an {@link SceneGroupChannelBuilder} for the given {@link HDPowerViewTranslationProvider} and + * Creates a {@link SceneGroupChannelBuilder} for the given {@link HDPowerViewTranslationProvider} and * {@link ChannelGroupUID}. * * @param translationProvider the {@link HDPowerViewTranslationProvider} From fb5e56668ac1e3c444758112d09c036a190437d3 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Sat, 8 Jan 2022 21:02:27 +0100 Subject: [PATCH 6/8] Reduce constructor access modifiers. Signed-off-by: Jacob Laursen --- .../hdpowerview/internal/builders/AutomationChannelBuilder.java | 2 +- .../hdpowerview/internal/builders/BaseChannelBuilder.java | 2 +- .../hdpowerview/internal/builders/SceneChannelBuilder.java | 2 +- .../hdpowerview/internal/builders/SceneGroupChannelBuilder.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java index 34a892de3f864..72a79a00d603d 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java @@ -56,7 +56,7 @@ public class AutomationChannelBuilder extends BaseChannelBuilder { @Nullable private List scheduledEvents; - public AutomationChannelBuilder(HDPowerViewTranslationProvider translationProvider, + private AutomationChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid) { super(translationProvider, channelGroupUid, HDPowerViewBindingConstants.CHANNELTYPE_AUTOMATION_ENABLED); } diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/BaseChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/BaseChannelBuilder.java index 1bdbde335250a..15839d8f43a3e 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/BaseChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/BaseChannelBuilder.java @@ -40,7 +40,7 @@ public class BaseChannelBuilder { @Nullable protected List channels; - public BaseChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid, + protected BaseChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid, String channelTypeId) { this.translationProvider = translationProvider; this.channelGroupUid = channelGroupUid; diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java index 503b0411c5428..b62d1726625a7 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java @@ -38,7 +38,7 @@ public class SceneChannelBuilder extends BaseChannelBuilder { @Nullable private List scenes; - public SceneChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid) { + private SceneChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid) { super(translationProvider, channelGroupUid, HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE); } diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java index eca1d6ea3efc1..bc313408bad1e 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java @@ -38,7 +38,7 @@ public class SceneGroupChannelBuilder extends BaseChannelBuilder { @Nullable private List sceneCollections; - public SceneGroupChannelBuilder(HDPowerViewTranslationProvider translationProvider, + private SceneGroupChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid) { super(translationProvider, channelGroupUid, HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE); } From a763e972386ab0bdcac47c52769537be358e19ac Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Sun, 9 Jan 2022 15:15:10 +0100 Subject: [PATCH 7/8] Removed unneeded this keyword for protected method. Signed-off-by: Jacob Laursen --- .../internal/builders/AutomationChannelBuilder.java | 4 ++-- .../hdpowerview/internal/builders/BaseChannelBuilder.java | 2 +- .../hdpowerview/internal/builders/SceneChannelBuilder.java | 4 ++-- .../internal/builders/SceneGroupChannelBuilder.java | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java index 72a79a00d603d..ed1216a99c263 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java @@ -126,10 +126,10 @@ public AutomationChannelBuilder withScheduledEvents(List schedul */ public List build() { if (scheduledEvents == null || (scenes == null && sceneCollections == null)) { - return this.getChannelList(0); + return getChannelList(0); } List scheduledEvents = (@NonNull List) this.scheduledEvents; - List channels = this.getChannelList(scheduledEvents.size()); + List channels = getChannelList(scheduledEvents.size()); scheduledEvents.stream().forEach(scheduledEvent -> { Channel channel = createChannel(scheduledEvent); if (channel != null) { diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/BaseChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/BaseChannelBuilder.java index 15839d8f43a3e..35f2ac19b8519 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/BaseChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/BaseChannelBuilder.java @@ -48,6 +48,6 @@ protected BaseChannelBuilder(HDPowerViewTranslationProvider translationProvider, } protected List getChannelList(int initialCapacity) { - return this.channels != null ? (@NonNull List) this.channels : new ArrayList<>(initialCapacity); + return channels != null ? (@NonNull List) channels : new ArrayList<>(initialCapacity); } } diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java index b62d1726625a7..06c0bb743ac1b 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java @@ -84,10 +84,10 @@ public SceneChannelBuilder withScenes(List scenes) { */ public List build() { if (scenes == null) { - return this.getChannelList(0); + return getChannelList(0); } List scenes = (@NonNull List) this.scenes; - List channels = this.getChannelList(scenes.size()); + List channels = getChannelList(scenes.size()); scenes.stream().sorted().forEach(scene -> channels.add(createChannel(scene))); return channels; } diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java index bc313408bad1e..971e7c4b062a5 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java @@ -85,10 +85,10 @@ public SceneGroupChannelBuilder withSceneCollections(List scene */ public List build() { if (sceneCollections == null) { - return this.getChannelList(0); + return getChannelList(0); } List sceneCollections = (@NonNull List) this.sceneCollections; - List channels = this.getChannelList(sceneCollections.size()); + List channels = getChannelList(sceneCollections.size()); sceneCollections.stream().sorted().forEach(sceneCollection -> channels.add(createChannel(sceneCollection))); return channels; } From e7a70f5036bf9aac802cd61a82efb289cbd22fb0 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Tue, 11 Jan 2022 23:21:50 +0100 Subject: [PATCH 8/8] Fix null annotation issues. Signed-off-by: Jacob Laursen --- .../builders/AutomationChannelBuilder.java | 10 +-- .../internal/builders/BaseChannelBuilder.java | 4 +- .../builders/SceneChannelBuilder.java | 3 +- .../builders/SceneGroupChannelBuilder.java | 3 +- .../AutomationChannelBuilderTest.java | 72 ++++++------------- .../hdpowerview/SceneChannelBuilderTest.java | 29 +++----- .../SceneGroupChannelBuilderTest.java | 30 +++----- 7 files changed, 52 insertions(+), 99 deletions(-) diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java index ed1216a99c263..10782b22b991c 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java @@ -21,7 +21,6 @@ import java.util.StringJoiner; import java.util.stream.Collectors; -import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; @@ -125,10 +124,10 @@ public AutomationChannelBuilder withScheduledEvents(List schedul * @return the {@link Channel} list */ public List build() { + List scheduledEvents = this.scheduledEvents; if (scheduledEvents == null || (scenes == null && sceneCollections == null)) { return getChannelList(0); } - List scheduledEvents = (@NonNull List) this.scheduledEvents; List channels = getChannelList(scheduledEvents.size()); scheduledEvents.stream().forEach(scheduledEvent -> { Channel channel = createChannel(scheduledEvent); @@ -157,26 +156,27 @@ public List build() { private @Nullable String getReferencedSceneOrSceneCollectionName(ScheduledEvent scheduledEvent) { if (scheduledEvent.sceneId > 0) { + Map scenes = this.scenes; if (scenes == null) { logger.warn("Scheduled event '{}' references scene '{}', but no scenes are loaded", scheduledEvent.id, scheduledEvent.sceneId); return null; } - Scene scene = ((@NonNull Map) scenes).get(scheduledEvent.sceneId); + Scene scene = scenes.get(scheduledEvent.sceneId); if (scene != null) { return scene.getName(); } logger.warn("Scene '{}' was not found for scheduled event '{}'", scheduledEvent.sceneId, scheduledEvent.id); return null; } else if (scheduledEvent.sceneCollectionId > 0) { + Map sceneCollections = this.sceneCollections; if (sceneCollections == null) { logger.warn( "Scheduled event '{}' references scene collection '{}', but no scene collections are loaded", scheduledEvent.id, scheduledEvent.sceneCollectionId); return null; } - SceneCollection sceneCollection = ((@NonNull Map) sceneCollections) - .get(scheduledEvent.sceneCollectionId); + SceneCollection sceneCollection = sceneCollections.get(scheduledEvent.sceneCollectionId); if (sceneCollection != null) { return sceneCollection.getName(); } diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/BaseChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/BaseChannelBuilder.java index 35f2ac19b8519..6ac875c460c90 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/BaseChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/BaseChannelBuilder.java @@ -15,7 +15,6 @@ import java.util.ArrayList; import java.util.List; -import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; @@ -48,6 +47,7 @@ protected BaseChannelBuilder(HDPowerViewTranslationProvider translationProvider, } protected List getChannelList(int initialCapacity) { - return channels != null ? (@NonNull List) channels : new ArrayList<>(initialCapacity); + List channels = this.channels; + return channels != null ? channels : new ArrayList<>(initialCapacity); } } diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java index 06c0bb743ac1b..ad11985cb4cdc 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneChannelBuilder.java @@ -14,7 +14,6 @@ import java.util.List; -import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; @@ -83,10 +82,10 @@ public SceneChannelBuilder withScenes(List scenes) { * @return the {@link Channel} list */ public List build() { + List scenes = this.scenes; if (scenes == null) { return getChannelList(0); } - List scenes = (@NonNull List) this.scenes; List channels = getChannelList(scenes.size()); scenes.stream().sorted().forEach(scene -> channels.add(createChannel(scene))); return channels; diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java index 971e7c4b062a5..2b70b21d2e6a0 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/SceneGroupChannelBuilder.java @@ -14,7 +14,6 @@ import java.util.List; -import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; @@ -84,10 +83,10 @@ public SceneGroupChannelBuilder withSceneCollections(List scene * @return the {@link Channel} list */ public List build() { + List sceneCollections = this.sceneCollections; if (sceneCollections == null) { return getChannelList(0); } - List sceneCollections = (@NonNull List) this.sceneCollections; List channels = getChannelList(sceneCollections.size()); sceneCollections.stream().sorted().forEach(sceneCollection -> channels.add(createChannel(sceneCollection))); return channels; diff --git a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/AutomationChannelBuilderTest.java b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/AutomationChannelBuilderTest.java index 465672926a639..e03b0dd5f0f02 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/AutomationChannelBuilderTest.java +++ b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/AutomationChannelBuilderTest.java @@ -19,7 +19,7 @@ import java.util.Base64; import java.util.List; -import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; @@ -39,21 +39,21 @@ * * @author Jacob Laursen - Initial contribution */ +@NonNullByDefault public class AutomationChannelBuilderTest { private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID( new ThingUID(HDPowerViewBindingConstants.BINDING_ID, AutomationChannelBuilderTest.class.getSimpleName()), HDPowerViewBindingConstants.CHANNELTYPE_AUTOMATION_ENABLED); - private HDPowerViewTranslationProvider translationProvider; - private AutomationChannelBuilder builder; - private List scenes; - private List sceneCollections; + private static final HDPowerViewTranslationProvider translationProvider = new HDPowerViewTranslationProvider( + mock(Bundle.class), new TranslationProviderForTests(), new LocaleProviderForTests()); + private AutomationChannelBuilder builder = AutomationChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID); + private List scenes = new ArrayList<>(); + private List sceneCollections = new ArrayList<>(); @BeforeEach private void setUp() { - translationProvider = new HDPowerViewTranslationProvider(mock(Bundle.class), new TranslationProviderForTests(), - new LocaleProviderForTests()); builder = AutomationChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID); Scene scene = new Scene(); @@ -74,9 +74,7 @@ public void sceneSunriseWeekends() { scheduledEvent.daySunday = true; List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); - - @NonNull - List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + List channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); assertEquals(1, channels.size()); assertEquals("TestScene, At sunrise, Weekends", channels.get(0).getLabel()); @@ -92,9 +90,7 @@ public void sceneSunsetWeekdays() { scheduledEvent.dayFriday = true; List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); - - @NonNull - List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + List channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); assertEquals(1, channels.size()); assertEquals("TestScene, At sunset, Weekdays", channels.get(0).getLabel()); @@ -114,9 +110,7 @@ public void sceneTimeAllDays() { scheduledEvent.minute = 30; List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); - - @NonNull - List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + List channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); assertEquals(1, channels.size()); assertEquals("TestScene, 06:30, All days", channels.get(0).getLabel()); @@ -130,9 +124,7 @@ public void sceneMinutesBeforeSunriseMondayTuesday() { scheduledEvent.minute = -15; List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); - - @NonNull - List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + List channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); assertEquals(1, channels.size()); assertEquals("TestScene, 15m before sunrise, Mon, Tue", channels.get(0).getLabel()); @@ -145,9 +137,7 @@ public void sceneHoursMinutesAfterSunriseMonday() { scheduledEvent.minute = 61; List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); - - @NonNull - List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + List channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); assertEquals(1, channels.size()); assertEquals("TestScene, 1hr 1m after sunrise, Mon", channels.get(0).getLabel()); @@ -162,9 +152,7 @@ public void sceneMinutesBeforeSunsetWednesdayThursdayFriday() { scheduledEvent.minute = -59; List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); - - @NonNull - List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + List channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); assertEquals(1, channels.size()); assertEquals("TestScene, 59m before sunset, Wed, Thu, Fri", channels.get(0).getLabel()); @@ -179,9 +167,7 @@ public void sceneHourAfterSunsetFridaySaturdaySunday() { scheduledEvent.minute = 60; List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); - - @NonNull - List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + List channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); assertEquals(1, channels.size()); assertEquals("TestScene, 1hr after sunset, Fri, Sat, Sun", channels.get(0).getLabel()); @@ -193,10 +179,8 @@ public void sceneCollection() { ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE); List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); - - @NonNull - List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections) - .withScheduledEvents(scheduledEvents).build(); + List channels = builder.withSceneCollections(sceneCollections).withScheduledEvents(scheduledEvents) + .build(); assertEquals(1, channels.size()); assertEquals("TestSceneCollection, At sunrise, ", channels.get(0).getLabel()); @@ -207,9 +191,7 @@ public void suppliedListIsUsed() { ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE); List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); List existingChannels = new ArrayList<>(0); - - @NonNull - List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents) + List channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents) .withChannels(existingChannels).build(); assertEquals(existingChannels, channels); @@ -217,8 +199,7 @@ public void suppliedListIsUsed() { @Test public void emptyListWhenNoScheduledEvents() { - @NonNull - List<@NonNull Channel> channels = builder.build(); + List channels = builder.build(); assertEquals(0, channels.size()); } @@ -227,9 +208,7 @@ public void emptyListWhenNoScheduledEvents() { public void emptyListWhenNoScenesOrSceneCollections() { ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE); List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); - - @NonNull - List<@NonNull Channel> channels = builder.withScheduledEvents(scheduledEvents).build(); + List channels = builder.withScheduledEvents(scheduledEvents).build(); assertEquals(0, channels.size()); } @@ -239,9 +218,7 @@ public void emptyListWhenNoSceneForScheduledEvent() { ScheduledEvent scheduledEvent = createScheduledEventWithSceneCollection( ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE); List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); - - @NonNull - List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + List channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); assertEquals(0, channels.size()); } @@ -251,9 +228,8 @@ public void emptyListWhenNoSceneCollectionForScheduledEvent() { ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE); List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); - @NonNull - List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections) - .withScheduledEvents(scheduledEvents).build(); + List channels = builder.withSceneCollections(sceneCollections).withScheduledEvents(scheduledEvents) + .build(); assertEquals(0, channels.size()); } @@ -263,9 +239,7 @@ public void groupAndIdAreCorrect() { ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE); scheduledEvent.id = 42; List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); - - @NonNull - List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); + List channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build(); assertEquals(1, channels.size()); assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId()); diff --git a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneChannelBuilderTest.java b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneChannelBuilderTest.java index 1769b28f34dbe..94065aa07f043 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneChannelBuilderTest.java +++ b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneChannelBuilderTest.java @@ -19,7 +19,7 @@ import java.util.Base64; import java.util.List; -import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; @@ -36,28 +36,26 @@ * * @author Jacob Laursen - Initial contribution */ +@NonNullByDefault public class SceneChannelBuilderTest { private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID( new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneChannelBuilderTest.class.getSimpleName()), HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE); - private HDPowerViewTranslationProvider translationProvider; - private SceneChannelBuilder builder; + private static final HDPowerViewTranslationProvider translationProvider = new HDPowerViewTranslationProvider( + mock(Bundle.class), new TranslationProviderForTests(), new LocaleProviderForTests()); + private SceneChannelBuilder builder = SceneChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID); @BeforeEach private void setUp() { - translationProvider = new HDPowerViewTranslationProvider(mock(Bundle.class), new TranslationProviderForTests(), - new LocaleProviderForTests()); builder = SceneChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID); } @Test public void labelIsCorrect() { List scenes = createScenes(); - - @NonNull - List<@NonNull Channel> channels = builder.withScenes(scenes).build(); + List channels = builder.withScenes(scenes).build(); assertEquals(1, channels.size()); assertEquals("TestScene", channels.get(0).getLabel()); @@ -66,9 +64,7 @@ public void labelIsCorrect() { @Test public void descriptionIsCorrect() { List scenes = createScenes(); - - @NonNull - List<@NonNull Channel> channels = builder.withScenes(scenes).build(); + List channels = builder.withScenes(scenes).build(); assertEquals(1, channels.size()); assertEquals("Activates the scene 'TestScene'", channels.get(0).getDescription()); @@ -77,9 +73,7 @@ public void descriptionIsCorrect() { @Test public void groupAndIdAreCorrect() { List scenes = createScenes(); - - @NonNull - List<@NonNull Channel> channels = builder.withScenes(scenes).build(); + List channels = builder.withScenes(scenes).build(); assertEquals(1, channels.size()); assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId()); @@ -90,17 +84,14 @@ public void groupAndIdAreCorrect() { public void suppliedListIsUsed() { List scenes = createScenes(); List existingChannels = new ArrayList<>(0); - - @NonNull - List<@NonNull Channel> channels = builder.withScenes(scenes).withChannels(existingChannels).build(); + List channels = builder.withScenes(scenes).withChannels(existingChannels).build(); assertEquals(existingChannels, channels); } @Test public void emptyListWhenNoScenes() { - @NonNull - List<@NonNull Channel> channels = builder.build(); + List channels = builder.build(); assertEquals(0, channels.size()); } diff --git a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneGroupChannelBuilderTest.java b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneGroupChannelBuilderTest.java index dbef2580ff45a..16286650d59fe 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneGroupChannelBuilderTest.java +++ b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneGroupChannelBuilderTest.java @@ -19,7 +19,7 @@ import java.util.Base64; import java.util.List; -import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; @@ -36,28 +36,26 @@ * * @author Jacob Laursen - Initial contribution */ +@NonNullByDefault public class SceneGroupChannelBuilderTest { private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID( new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneGroupChannelBuilderTest.class.getSimpleName()), HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE); - private HDPowerViewTranslationProvider translationProvider; - private SceneGroupChannelBuilder builder; + private static final HDPowerViewTranslationProvider translationProvider = new HDPowerViewTranslationProvider( + mock(Bundle.class), new TranslationProviderForTests(), new LocaleProviderForTests()); + private SceneGroupChannelBuilder builder = SceneGroupChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID); @BeforeEach private void setUp() { - translationProvider = new HDPowerViewTranslationProvider(mock(Bundle.class), new TranslationProviderForTests(), - new LocaleProviderForTests()); builder = SceneGroupChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID); } @Test public void labelIsCorrect() { List sceneCollections = createSceneCollections(); - - @NonNull - List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections).build(); + List channels = builder.withSceneCollections(sceneCollections).build(); assertEquals(1, channels.size()); assertEquals("TestSceneCollection", channels.get(0).getLabel()); @@ -66,9 +64,7 @@ public void labelIsCorrect() { @Test public void descriptionIsCorrect() { List sceneCollections = createSceneCollections(); - - @NonNull - List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections).build(); + List channels = builder.withSceneCollections(sceneCollections).build(); assertEquals(1, channels.size()); assertEquals("Activates the scene group 'TestSceneCollection'", channels.get(0).getDescription()); @@ -77,9 +73,7 @@ public void descriptionIsCorrect() { @Test public void groupAndIdAreCorrect() { List sceneCollections = createSceneCollections(); - - @NonNull - List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections).build(); + List channels = builder.withSceneCollections(sceneCollections).build(); assertEquals(1, channels.size()); assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId()); @@ -90,18 +84,14 @@ public void groupAndIdAreCorrect() { public void suppliedListIsUsed() { List sceneCollections = createSceneCollections(); List existingChannels = new ArrayList<>(0); - - @NonNull - List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections).withChannels(existingChannels) - .build(); + List channels = builder.withSceneCollections(sceneCollections).withChannels(existingChannels).build(); assertEquals(existingChannels, channels); } @Test public void emptyListWhenNoSceneCollections() { - @NonNull - List<@NonNull Channel> channels = builder.build(); + List channels = builder.build(); assertEquals(0, channels.size()); }