[hdpowerview] Refactor dynamic channels (#11853)

* Extract dynamic channel creation to separate classes.
* Avoid double list allocations.
* Add test coverage for scenarios with no channels built.
* Extract common builder stuff to super class.
* Fix grammar.
* Reduce constructor access modifiers.
* Removed unneeded this keyword for protected method.
* Fix null annotation issues.

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen
2022-01-17 13:41:33 +01:00
committed by GitHub
parent 8b3bb313eb
commit e44dfe7dbc
13 changed files with 1123 additions and 172 deletions

View File

@@ -0,0 +1,252 @@
/**
* 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.EnumSet;
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.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 extends BaseChannelBuilder {
private final Logger logger = LoggerFactory.getLogger(AutomationChannelBuilder.class);
@Nullable
private Map<Integer, Scene> scenes;
@Nullable
private Map<Integer, SceneCollection> sceneCollections;
@Nullable
private List<ScheduledEvent> scheduledEvents;
private AutomationChannelBuilder(HDPowerViewTranslationProvider translationProvider,
ChannelGroupUID channelGroupUid) {
super(translationProvider, channelGroupUid, HDPowerViewBindingConstants.CHANNELTYPE_AUTOMATION_ENABLED);
}
/**
* 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<Channel> channels) {
this.channels = channels;
return this;
}
/**
* Sets the scenes.
*
* @param scenes the scenes
* @return channel builder
*/
public AutomationChannelBuilder withScenes(List<Scene> 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<SceneCollection> 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<ScheduledEvent> scheduledEvents) {
this.scheduledEvents = scheduledEvents;
return this;
}
/**
* Builds and returns the channels.
*
* @return the {@link Channel} list
*/
public List<Channel> build() {
List<ScheduledEvent> scheduledEvents = this.scheduledEvents;
if (scheduledEvents == null || (scenes == null && sceneCollections == null)) {
return getChannelList(0);
}
List<Channel> channels = getChannelList(scheduledEvents.size());
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) {
Map<Integer, Scene> 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 = 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<Integer, SceneCollection> 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 = 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<DayOfWeek> 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);
}
}

View File

@@ -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.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<Channel> channels;
protected BaseChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid,
String channelTypeId) {
this.translationProvider = translationProvider;
this.channelGroupUid = channelGroupUid;
this.channelTypeUid = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID, channelTypeId);
}
protected List<Channel> getChannelList(int initialCapacity) {
List<Channel> channels = this.channels;
return channels != null ? channels : new ArrayList<>(initialCapacity);
}
}

View File

@@ -0,0 +1,100 @@
/**
* 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.List;
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;
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;
/**
* The {@link SceneChannelBuilder} class creates scene channels
* from structured scene data.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class SceneChannelBuilder extends BaseChannelBuilder {
@Nullable
private List<Scene> scenes;
private SceneChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid) {
super(translationProvider, channelGroupUid, HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE);
}
/**
* Creates a {@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<Channel> channels) {
this.channels = channels;
return this;
}
/**
* Sets the scenes.
*
* @param scenes the scenes
* @return channel builder
*/
public SceneChannelBuilder withScenes(List<Scene> scenes) {
this.scenes = scenes;
return this;
}
/**
* Builds and returns the channels.
*
* @return the {@link Channel} list
*/
public List<Channel> build() {
List<Scene> scenes = this.scenes;
if (scenes == null) {
return getChannelList(0);
}
List<Channel> channels = getChannelList(scenes.size());
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();
}
}

View File

@@ -0,0 +1,102 @@
/**
* 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.List;
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.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;
/**
* The {@link SceneGroupChannelBuilder} class creates scene group channels
* from structured scene collection data.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class SceneGroupChannelBuilder extends BaseChannelBuilder {
@Nullable
private List<SceneCollection> sceneCollections;
private SceneGroupChannelBuilder(HDPowerViewTranslationProvider translationProvider,
ChannelGroupUID channelGroupUid) {
super(translationProvider, channelGroupUid, HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE);
}
/**
* Creates a {@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<Channel> channels) {
this.channels = channels;
return this;
}
/**
* Sets the scene collections.
*
* @param sceneCollections the scene collections
* @return channel builder
*/
public SceneGroupChannelBuilder withSceneCollections(List<SceneCollection> sceneCollections) {
this.sceneCollections = sceneCollections;
return this;
}
/**
* Builds and returns the channels.
*
* @return the {@link Channel} list
*/
public List<Channel> build() {
List<SceneCollection> sceneCollections = this.sceneCollections;
if (sceneCollections == null) {
return getChannelList(0);
}
List<Channel> channels = getChannelList(sceneCollections.size());
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();
}
}

View File

@@ -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;
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 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
pollShades();
List<Scene> scenes = updateSceneChannels();
List<SceneCollection> sceneCollections = updateSceneCollectionChannels();
List<ScheduledEvent> scheduledEvents = updateScheduledEventChannels(scenes, sceneCollections);
List<SceneCollection> sceneCollections = updateSceneGroupChannels();
List<ScheduledEvent> 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,25 +381,19 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
List<Channel> 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());
SceneChannelBuilder channelBuilder = SceneChannelBuilder
.create(this.translationProvider,
new ChannelGroupUID(thing.getUID(), HDPowerViewBindingConstants.CHANNEL_GROUP_SCENES))
.withScenes(scenes).withChannels(allChannels);
updateThing(editThing().withChannels(channelBuilder.build()).build());
createDeprecatedSceneChannels(scenes);
return scenes;
}
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();
return channel;
}
/**
* Create backwards compatible scene channels if any items configured before release 3.2
* are still linked. Users should have a reasonable amount of time to migrate to the new
@@ -460,42 +452,34 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
return sceneCollectionData;
}
private List<SceneCollection> updateSceneCollectionChannels()
private List<SceneCollection> updateSceneGroupChannels()
throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException {
List<SceneCollection> 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<SceneCollection>(sceneCollections);
List<Channel> 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());
SceneGroupChannelBuilder channelBuilder = SceneGroupChannelBuilder
.create(this.translationProvider,
new ChannelGroupUID(thing.getUID(), HDPowerViewBindingConstants.CHANNEL_GROUP_SCENE_GROUPS))
.withSceneCollections(sceneCollections).withChannels(allChannels);
updateThing(editThing().withChannels(channelBuilder.build()).build());
return sceneCollections;
}
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;
}
private List<ScheduledEvent> fetchScheduledEvents()
throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException {
HDPowerViewWebTargets webTargets = this.webTargets;
@@ -513,140 +497,33 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
return scheduledEventData;
}
private List<ScheduledEvent> updateScheduledEventChannels(List<Scene> scenes,
List<SceneCollection> sceneCollections)
private List<ScheduledEvent> updateAutomationChannels(List<Scene> scenes, List<SceneCollection> sceneCollections)
throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException {
List<ScheduledEvent> 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<ScheduledEvent>(scheduledEvents);
List<Channel> 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<Scene> scenes,
List<SceneCollection> 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<Scene> scenes,
List<SceneCollection> 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<DayOfWeek> 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<ScheduledEvent> scheduledEvents) {
private void updateAutomationStates(List<ScheduledEvent> scheduledEvents) {
ChannelGroupUID channelGroupUid = new ChannelGroupUID(thing.getUID(),
HDPowerViewBindingConstants.CHANNEL_GROUP_AUTOMATIONS);
for (ScheduledEvent scheduledEvent : scheduledEvents) {

View File

@@ -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

View File

@@ -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

View File

@@ -18,6 +18,7 @@
<property name="vendor">Hunter Douglas (Luxaflex)</property>
<property name="modelId">PowerView Hub</property>
</properties>
<representation-property>host</representation-property>
<config-description>
@@ -70,10 +71,6 @@
<properties>
<property name="vendor">Hunter Douglas (Luxaflex)</property>
<property name="modelId">PowerView Motorized Shade</property>
<property name="type"></property>
<property name="capabilities"></property>
<property name="secondaryRailDetected"></property>
<property name="tiltAnywhereDetected"></property>
</properties>
<representation-property>id</representation-property>