use of org.openhab.core.items.events.ItemStateEvent in project openhab-core by openhab.
the class AutoUpdateManagerTest method assertStateEvent.
private void assertStateEvent(String expectedContent, String extectedSource) {
ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
verify(eventPublisherMock, atLeastOnce()).post(eventCaptor.capture());
Event event = eventCaptor.getAllValues().stream().filter(e -> e instanceof ItemStateEvent).findFirst().get();
assertEquals(expectedContent, ((ItemStateEvent) event).getItemState().toFullString());
assertEquals(extectedSource, event.getSource());
assertNothingHappened();
}
use of org.openhab.core.items.events.ItemStateEvent in project openhab-core by openhab.
the class ThingManagerOSGiTest method thingManagerHandlesStateUpdatesCorrectly.
@Test
public void thingManagerHandlesStateUpdatesCorrectly() {
registerThingTypeProvider();
class ThingHandlerState {
boolean thingUpdatedWasCalled;
@Nullable
ThingHandlerCallback callback;
}
final ThingHandlerState state = new ThingHandlerState();
ThingHandler thingHandler = mock(ThingHandler.class);
doAnswer(new Answer<Void>() {
@Override
@Nullable
public Void answer(InvocationOnMock invocation) throws Throwable {
state.callback = (ThingHandlerCallback) invocation.getArgument(0);
return null;
}
}).when(thingHandler).setCallback(any(ThingHandlerCallback.class));
doAnswer(new Answer<Void>() {
@Override
@Nullable
public Void answer(InvocationOnMock invocation) throws Throwable {
state.thingUpdatedWasCalled = true;
return null;
}
}).when(thingHandler).thingUpdated(any(Thing.class));
when(thingHandler.getThing()).thenReturn(thing);
ThingHandlerFactory thingHandlerFactory = mock(ThingHandlerFactory.class);
when(thingHandlerFactory.supportsThingType(any(ThingTypeUID.class))).thenReturn(true);
when(thingHandlerFactory.registerHandler(any(Thing.class))).thenReturn(thingHandler);
registerService(thingHandlerFactory);
// Create item
String itemName = "name";
Item item = new StringItem(itemName);
itemRegistry.add(item);
managedThingProvider.add(thing);
managedItemChannelLinkProvider.add(new ItemChannelLink(itemName, CHANNEL_UID));
state.callback.statusUpdated(thing, ThingStatusInfoBuilder.create(ThingStatus.ONLINE).build());
final List<Event> receivedEvents = new ArrayList<>();
@NonNullByDefault EventSubscriber itemUpdateEventSubscriber = new EventSubscriber() {
@Override
public void receive(Event event) {
receivedEvents.add(event);
}
@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(ItemStateEvent.TYPE);
}
@Override
@Nullable
public EventFilter getEventFilter() {
return new TopicEventFilter("openhab/items/.*/state");
}
};
registerService(itemUpdateEventSubscriber);
// thing manager posts the update to the event bus via EventPublisher
state.callback.stateUpdated(CHANNEL_UID, new StringType("Value"));
waitForAssert(() -> assertThat(receivedEvents.size(), is(1)));
assertThat(receivedEvents.get(0), is(instanceOf(ItemStateEvent.class)));
ItemStateEvent itemUpdateEvent = (ItemStateEvent) receivedEvents.get(0);
assertThat(itemUpdateEvent.getTopic(), is("openhab/items/name/state"));
assertThat(itemUpdateEvent.getItemName(), is(itemName));
assertThat(itemUpdateEvent.getSource(), is(CHANNEL_UID.toString()));
assertThat(itemUpdateEvent.getItemState(), is(instanceOf(StringType.class)));
assertThat(itemUpdateEvent.getItemState(), is("Value"));
receivedEvents.clear();
Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).withChannel(ChannelBuilder.create(CHANNEL_UID, CoreItemFactory.SWITCH).build()).build();
managedThingProvider.update(thing);
state.callback.stateUpdated(CHANNEL_UID, new StringType("Value"));
waitForAssert(() -> assertThat(receivedEvents.size(), is(1)));
assertThat(receivedEvents.get(0), is(instanceOf(ItemStateEvent.class)));
itemUpdateEvent = (ItemStateEvent) receivedEvents.get(0);
assertThat(itemUpdateEvent.getTopic(), is("openhab/items/name/state"));
assertThat(itemUpdateEvent.getItemName(), is(itemName));
assertThat(itemUpdateEvent.getSource(), is(CHANNEL_UID.toString()));
assertThat(itemUpdateEvent.getItemState(), is(instanceOf(StringType.class)));
assertThat(itemUpdateEvent.getItemState(), is("Value"));
waitForAssert(() -> assertThat(state.thingUpdatedWasCalled, is(true)));
}
use of org.openhab.core.items.events.ItemStateEvent in project openhab-core by openhab.
the class ItemStateTriggerHandler method receive.
@Override
public void receive(Event event) {
ModuleHandlerCallback callback = this.callback;
if (callback != null) {
logger.trace("Received Event: Source: {} Topic: {} Type: {} Payload: {}", event.getSource(), event.getTopic(), event.getType(), event.getPayload());
Map<String, Object> values = new HashMap<>();
if (event instanceof ItemStateEvent && UPDATE_MODULE_TYPE_ID.equals(module.getTypeUID())) {
String state = this.state;
State itemState = ((ItemStateEvent) event).getItemState();
if ((state == null || state.equals(itemState.toFullString()))) {
values.put("state", itemState);
}
} else if (event instanceof ItemStateChangedEvent && CHANGE_MODULE_TYPE_ID.equals(module.getTypeUID())) {
State itemState = ((ItemStateChangedEvent) event).getItemState();
State oldItemState = ((ItemStateChangedEvent) event).getOldItemState();
if (stateMatches(this.state, itemState) && stateMatches(this.previousState, oldItemState)) {
values.put("oldState", oldItemState);
values.put("newState", itemState);
}
}
if (!values.isEmpty()) {
values.put("event", event);
((TriggerHandlerCallback) callback).triggered(this.module, values);
}
}
}
use of org.openhab.core.items.events.ItemStateEvent in project openhab-core by openhab.
the class ItemStateUpdateActionHandler method execute.
@Override
@Nullable
public Map<String, Object> execute(Map<String, Object> inputs) {
Configuration config = module.getConfiguration();
String itemName = (String) config.get(ITEM_NAME);
String state = (String) config.get(STATE);
if (itemName != null) {
try {
Item item = itemRegistry.getItem(itemName);
State stateObj = null;
if (state != null) {
stateObj = TypeParser.parseState(item.getAcceptedDataTypes(), state);
} else {
final Object st = inputs.get(STATE);
if (st instanceof State) {
if (item.getAcceptedDataTypes().contains(st.getClass())) {
stateObj = (State) st;
}
}
}
if (stateObj != null) {
final ItemStateEvent itemStateEvent = (ItemStateEvent) ItemEventFactory.createStateEvent(itemName, stateObj);
logger.debug("Executing ItemStateEvent on Item {} with State {}", itemStateEvent.getItemName(), itemStateEvent.getItemState());
eventPublisher.post(itemStateEvent);
} else {
logger.debug("State '{}' is not valid for item '{}'.", state, itemName);
}
} catch (ItemNotFoundException e) {
logger.error("Item with name {} not found in ItemRegistry.", itemName);
}
} else {
logger.error("Item state was not updated because the configuration was not correct: ItemName: {}, State: {}", itemName, state);
}
return null;
}
use of org.openhab.core.items.events.ItemStateEvent in project openhab-core by openhab.
the class ExpireManager method receive.
@Override
public void receive(Event event) {
if (!enabled) {
return;
}
if (event instanceof ItemStateEvent) {
ItemStateEvent isEvent = (ItemStateEvent) event;
processEvent(isEvent.getItemName(), isEvent.getItemState(), true);
} else if (event instanceof ItemCommandEvent) {
ItemCommandEvent icEvent = (ItemCommandEvent) event;
processEvent(icEvent.getItemName(), icEvent.getItemCommand(), false);
} else if (event instanceof ItemStateChangedEvent) {
ItemStateChangedEvent icEvent = (ItemStateChangedEvent) event;
processEvent(icEvent.getItemName(), icEvent.getItemState(), false);
}
}
Aggregations