Set event source for updates and commands from bindings according to new standards (#5073)

* Set event source for updates and commands from bindings according to new standards

In particular, prefix the channel UID with the package (org.openhab.core.thing), and
the channel UID becomes the actor.

Extends ProfileCallback to allow profiles to explicitly specify a source, and then
uses the new delegation syntax to append the channel UID source component in that
case.

Signed-off-by: Cody Cutrer <cody@cutrer.us>
This commit is contained in:
Cody Cutrer
2025-10-24 06:09:32 +02:00
committed by GitHub
parent d10530817f
commit 934431cdb4
3 changed files with 52 additions and 13 deletions
@@ -18,6 +18,7 @@ import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.events.AbstractEvent;
import org.openhab.core.events.AbstractEventFactory;
import org.openhab.core.events.Event;
import org.openhab.core.events.EventFactory;
@@ -47,6 +48,8 @@ import org.osgi.service.component.annotations.Component;
@Component(immediate = true, service = EventFactory.class)
@NonNullByDefault
public class ThingEventFactory extends AbstractEventFactory {
static final String THING_SOURCE = "org.openhab.core.thing";
static final String THING_STATUS_INFO_EVENT_TOPIC = "openhab/things/{thingUID}/status";
static final String THING_STATUS_INFO_CHANGED_EVENT_TOPIC = "openhab/things/{thingUID}/statuschanged";
@@ -330,7 +333,8 @@ public class ThingEventFactory extends AbstractEventFactory {
String topic = buildTopic(CHANNEL_TRIGGERED_EVENT_TOPIC, channelUID);
TriggerEventPayloadBean bean = new TriggerEventPayloadBean(event, channelUID.getAsString());
String payload = serializePayload(bean);
return new ChannelTriggeredEvent(topic, payload, null, event, channelUID);
return new ChannelTriggeredEvent(topic, payload,
AbstractEvent.buildSource(THING_SOURCE, channelUID.getAsString()), event, channelUID);
}
private Event createTriggerEvent(String topic, String payload, @Nullable String source) {
@@ -17,6 +17,7 @@ import java.util.function.Function;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.common.SafeCaller;
import org.openhab.core.events.AbstractEvent;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemStateConverter;
@@ -45,6 +46,7 @@ import org.slf4j.LoggerFactory;
*/
@NonNullByDefault
public class ProfileCallbackImpl implements ProfileCallback {
private static final String THING_SOURCE = "org.openhab.core.thing";
private final Logger logger = LoggerFactory.getLogger(ProfileCallbackImpl.class);
@@ -118,13 +120,12 @@ public class ProfileCallbackImpl implements ProfileCallback {
}
@Override
public void sendCommand(Command command) {
eventPublisher
.post(ItemEventFactory.createCommandEvent(link.getItemName(), command, link.getLinkedUID().toString()));
public void sendCommand(Command command, @Nullable String source) {
eventPublisher.post(ItemEventFactory.createCommandEvent(link.getItemName(), command, buildSource(source)));
}
@Override
public void sendUpdate(State state) {
public void sendUpdate(State state, @Nullable String source) {
Item item = itemProvider.apply(link.getItemName());
if (item == null) {
logger.warn("Cannot post update event '{}' for item '{}', because no item could be found.", state,
@@ -142,12 +143,11 @@ public class ProfileCallbackImpl implements ProfileCallback {
acceptedState = itemStateConverter.convertToAcceptedState(state, item);
}
eventPublisher.post(
ItemEventFactory.createStateEvent(link.getItemName(), acceptedState, link.getLinkedUID().toString()));
eventPublisher.post(ItemEventFactory.createStateEvent(link.getItemName(), acceptedState, buildSource(source)));
}
@Override
public void sendTimeSeries(TimeSeries timeSeries) {
public void sendTimeSeries(TimeSeries timeSeries, @Nullable String source) {
Item item = itemProvider.apply(link.getItemName());
if (item == null) {
logger.warn("Cannot send time series event '{}' for item '{}', because no item could be found.", timeSeries,
@@ -155,8 +155,8 @@ public class ProfileCallbackImpl implements ProfileCallback {
return;
}
eventPublisher.post(
ItemEventFactory.createTimeSeriesEvent(link.getItemName(), timeSeries, link.getLinkedUID().toString()));
eventPublisher
.post(ItemEventFactory.createTimeSeriesEvent(link.getItemName(), timeSeries, buildSource(source)));
}
@FunctionalInterface
@@ -164,4 +164,8 @@ public class ProfileCallbackImpl implements ProfileCallback {
@Nullable
Command toAcceptedCommand(Command originalType, @Nullable Channel channel, @Nullable Item item);
}
private String buildSource(@Nullable String source) {
return AbstractEvent.buildDelegatedSource(source, THING_SOURCE, link.getLinkedUID().toString());
}
}
@@ -13,6 +13,7 @@
package org.openhab.core.thing.profiles;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.thing.link.ItemChannelLink;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
@@ -45,19 +46,49 @@ public interface ProfileCallback {
*
* @param command
*/
void sendCommand(Command command);
default void sendCommand(Command command) {
sendCommand(command, null);
}
/**
* Send a command to the framework.
*
* @param command
* @param source the source of the command event
*/
void sendCommand(Command command, @Nullable String source);
/**
* Send a state update to the framework.
*
* @param state
*/
void sendUpdate(State state);
default void sendUpdate(State state) {
sendUpdate(state, null);
}
/**
* Send a state update to the framework.
*
* @param state
* @param source the source of the update
*/
void sendUpdate(State state, @Nullable String source);
/**
* Send a {@link TimeSeries} update to the framework.
*
* @param timeSeries
*/
void sendTimeSeries(TimeSeries timeSeries);
default void sendTimeSeries(TimeSeries timeSeries) {
sendTimeSeries(timeSeries, null);
}
/**
* Send a {@link TimeSeries} update to the framework.
*
* @param timeSeries
* @param source the source of the update
*/
void sendTimeSeries(TimeSeries timeSeries, @Nullable String source);
}