diff --git a/ESH-INF/thing/thing-types.xml b/ESH-INF/thing/thing-types.xml index a2f70995eb5a2c00a9a5f906ec1f85ef1b389d7e..3b9bf05abad0af5969c68a5aa0a5c7d2842f2939 100644 --- a/ESH-INF/thing/thing-types.xml +++ b/ESH-INF/thing/thing-types.xml @@ -31,9 +31,14 @@ <description>MQTT topic prefix for messages leaving openHAB to eraser.</description> <default>oh/out/</default> </parameter> - <parameter name="publish-group" type="text" required="true"> + <parameter name="publish-all" type="boolean" required="true"> + <label>Publish All</label> + <description>If enabled, changes of every item will be published. Otherwise only those of the members of group set in the parameter "Publish Group"</description> + <default>true</default> + </parameter> + <parameter name="publish-group" type="text" required="false"> <label>Publish group</label> - <description>Group whose member will trigger an update</description> + <description>Group whose members will trigger an update</description> <context>item</context> </parameter> </config-description> diff --git a/src/main/java/org/openhab/binding/openlicht/BindingConstants.java b/src/main/java/org/openhab/binding/openlicht/BindingConstants.java index aa9fb76e3f0fc018696d79dba5ac0072107134c2..6e66ee775793dea248cb5ca89a6911b5f93e8da1 100644 --- a/src/main/java/org/openhab/binding/openlicht/BindingConstants.java +++ b/src/main/java/org/openhab/binding/openlicht/BindingConstants.java @@ -33,6 +33,7 @@ public class BindingConstants { public static final String CONFIG_TIMEOUT_MQTT_UNSUPPORTED_CATEGORIES = "unsupported-category-reset"; public static final String CONFIG_ERASER_OUT_TOPIC = "outTopic"; public static final String CONFIG_ERASER_PUBLISH_GROUP = "publish-group"; + public static final String CONFIG_ERASER_PUBLISH_ALL = "publish-all"; // List of all Thing Type UIDs public static final ThingTypeUID THING_TYPE_SKYWRITER_HAT = new ThingTypeUID(BINDING_ID, "skywriter-hat"); diff --git a/src/main/java/org/openhab/binding/openlicht/handler/AbstractMqttHandler.java b/src/main/java/org/openhab/binding/openlicht/handler/AbstractMqttHandler.java index 4f2f1862a6ae7ba332db047cd6ded4d19a3b5b60..7fcd087f6d970831ab5550de4ab3f8b2693c2dea 100644 --- a/src/main/java/org/openhab/binding/openlicht/handler/AbstractMqttHandler.java +++ b/src/main/java/org/openhab/binding/openlicht/handler/AbstractMqttHandler.java @@ -103,6 +103,9 @@ public abstract class AbstractMqttHandler extends BaseThingHandler logger.warn("Broker name of {} not set", me()); return false; } + if (thing == null) { + return false; + } if (!"mqtt:systemBroker".equals(thing.getThingTypeUID().getAsString()) && !"mqtt:broker".equals(thing.getThingTypeUID().getAsString())) { return false; diff --git a/src/main/java/org/openhab/binding/openlicht/handler/EraserHandler.java b/src/main/java/org/openhab/binding/openlicht/handler/EraserHandler.java index 704471fe5bdc42af824f42abbde362993b7bd604..04899e4b1f3c01dea3692e997b4391c8261c68e9 100644 --- a/src/main/java/org/openhab/binding/openlicht/handler/EraserHandler.java +++ b/src/main/java/org/openhab/binding/openlicht/handler/EraserHandler.java @@ -33,6 +33,7 @@ public class EraserHandler extends AbstractMqttHandler implements StateChangeLis private final @NonNull Logger logger = LoggerFactory.getLogger(EraserHandler.class); private @Nullable ItemRegistry itemRegistry; private String outTopic; + private boolean publishAll = false; private String publishGroupName; private boolean publishGroupFound = false; private Set<GenericItem> observedItems = new HashSet<>(); @@ -49,6 +50,8 @@ public class EraserHandler extends AbstractMqttHandler implements StateChangeLis delegateListener = new WeakReference<DelegateEraserRegistryChangeListener>( new DelegateEraserRegistryChangeListener(this)); registry.addRegistryChangeListener(delegateListener.get()); + } else { + logger.warn("No item registry set, so no updates for item changes"); } } @@ -56,19 +59,28 @@ public class EraserHandler extends AbstractMqttHandler implements StateChangeLis protected void moreInitializeBefore() { this.outTopic = getConfigValueAsString(BindingConstants.CONFIG_ERASER_OUT_TOPIC); this.publishGroupName = getConfigValueAsString(BindingConstants.CONFIG_ERASER_PUBLISH_GROUP); + this.publishAll = getConfigValueAsBoolean(BindingConstants.CONFIG_ERASER_PUBLISH_ALL); if (this.outTopic.charAt(this.outTopic.length() - 1) != '/') { this.outTopic += "/"; } - if (this.itemRegistry != null) { - try { - Item publishGroupItem = this.itemRegistry.getItem(this.publishGroupName); - myGroupFound(publishGroupItem); - } catch (ItemNotFoundException e) { - this.publishGroupFound = false; - logger.warn("Could not find group with name '" + this.publishGroupName + "'"); + if (this.publishAll) { + if (this.itemRegistry != null) { + for (Item item : this.itemRegistry.getAll()) { + updated(item, item); + } } } else { - logger.warn("No item registry set, so no updates for item changes"); + if (this.itemRegistry != null) { + try { + Item publishGroupItem = this.itemRegistry.getItem(this.publishGroupName); + myGroupFound(publishGroupItem); + } catch (ItemNotFoundException e) { + this.publishGroupFound = false; + logger.warn("Could not find group with name '" + this.publishGroupName + "'"); + } + } else { + logger.warn("No item registry set, so could not search for publish group"); + } } } @@ -89,6 +101,10 @@ public class EraserHandler extends AbstractMqttHandler implements StateChangeLis this.lastStatusUpdatePhase = phase; this.lastStatusUpdateSuccess = success; this.lastStatusUpdateDescription = description; + if (this.publishAll) { + super.statusUpdate(phase, success, description); + return; + } String newDescription = description; if (!this.publishGroupFound) { // append information to description @@ -149,7 +165,6 @@ public class EraserHandler extends AbstractMqttHandler implements StateChangeLis } else { logger.warn("Could not set state for {} using '{}'", genericItem, new String(payload)); } - genericItem.setState(new StringType(new String(payload))); } else { logger.debug("No item registry to process message"); } @@ -199,23 +214,31 @@ public class EraserHandler extends AbstractMqttHandler implements StateChangeLis public void added(Item element) { GenericItem genericItem = (GenericItem) element; - if (!this.publishGroupFound && genericItem.getName().equals(this.publishGroupName)) { - myGroupFound(genericItem); - // update status with last known value (to remove the clause about missing group item) - statusUpdate(lastStatusUpdatePhase, lastStatusUpdateSuccess, lastStatusUpdateDescription); - } - List<@NonNull String> groupNames = genericItem.getGroupNames(); - if (groupNames.contains(this.publishGroupName)) { + if (this.publishAll) { if (observedItems.add(genericItem)) { genericItem.addStateChangeListener(this); } + } else { + if (!this.publishGroupFound && genericItem.getName().equals(this.publishGroupName)) { + myGroupFound(genericItem); + // update status with last known value (to remove the clause about missing group item) + statusUpdate(lastStatusUpdatePhase, lastStatusUpdateSuccess, lastStatusUpdateDescription); + } + List<@NonNull String> groupNames = genericItem.getGroupNames(); + if (groupNames.contains(this.publishGroupName)) { + if (observedItems.add(genericItem)) { + genericItem.addStateChangeListener(this); + } + } } } public void removed(Item element) { GenericItem genericItem = (GenericItem) element; - genericItem.removeStateChangeListener(this); - if (this.publishGroupFound && genericItem.getName().equals(this.publishGroupName)) { + if (observedItems.remove(genericItem)) { + genericItem.removeStateChangeListener(this); + } + if (!this.publishAll && this.publishGroupFound && genericItem.getName().equals(this.publishGroupName)) { // someone evil deleted the publish group this.publishGroupFound = false; GroupItem groupItem = (GroupItem) genericItem;