Search in sources :

Example 1 with ItemStateEvent

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();
}
Also used : ItemStatePredictedEvent(org.openhab.core.items.events.ItemStatePredictedEvent) ItemStateEvent(org.openhab.core.items.events.ItemStateEvent) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) Event(org.openhab.core.events.Event) ItemStateEvent(org.openhab.core.items.events.ItemStateEvent)

Example 2 with ItemStateEvent

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)));
}
Also used : EventSubscriber(org.openhab.core.events.EventSubscriber) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) StringType(org.openhab.core.library.types.StringType) ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink) ArrayList(java.util.ArrayList) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) StringItem(org.openhab.core.library.items.StringItem) Item(org.openhab.core.items.Item) StringItem(org.openhab.core.library.items.StringItem) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ItemStateEvent(org.openhab.core.items.events.ItemStateEvent) ThingStatusInfoEvent(org.openhab.core.thing.events.ThingStatusInfoEvent) Event(org.openhab.core.events.Event) ThingStatusInfoChangedEvent(org.openhab.core.thing.events.ThingStatusInfoChangedEvent) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) TopicEventFilter(org.openhab.core.events.TopicEventFilter) ItemStateEvent(org.openhab.core.items.events.ItemStateEvent) Nullable(org.eclipse.jdt.annotation.Nullable) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 3 with ItemStateEvent

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);
        }
    }
}
Also used : HashMap(java.util.HashMap) ItemStateChangedEvent(org.openhab.core.items.events.ItemStateChangedEvent) GroupItemStateChangedEvent(org.openhab.core.items.events.GroupItemStateChangedEvent) State(org.openhab.core.types.State) ModuleHandlerCallback(org.openhab.core.automation.ModuleHandlerCallback) TriggerHandlerCallback(org.openhab.core.automation.handler.TriggerHandlerCallback) ItemStateEvent(org.openhab.core.items.events.ItemStateEvent)

Example 4 with ItemStateEvent

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;
}
Also used : Item(org.openhab.core.items.Item) Configuration(org.openhab.core.config.core.Configuration) State(org.openhab.core.types.State) ItemStateEvent(org.openhab.core.items.events.ItemStateEvent) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 5 with ItemStateEvent

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);
    }
}
Also used : ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) ItemStateChangedEvent(org.openhab.core.items.events.ItemStateChangedEvent) GroupItemStateChangedEvent(org.openhab.core.items.events.GroupItemStateChangedEvent) ItemStateEvent(org.openhab.core.items.events.ItemStateEvent)

Aggregations

ItemStateEvent (org.openhab.core.items.events.ItemStateEvent)7 Item (org.openhab.core.items.Item)3 GroupItemStateChangedEvent (org.openhab.core.items.events.GroupItemStateChangedEvent)3 ItemStateChangedEvent (org.openhab.core.items.events.ItemStateChangedEvent)3 State (org.openhab.core.types.State)3 HashMap (java.util.HashMap)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 TriggerHandlerCallback (org.openhab.core.automation.handler.TriggerHandlerCallback)2 Event (org.openhab.core.events.Event)2 ItemCommandEvent (org.openhab.core.items.events.ItemCommandEvent)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)1 Test (org.junit.jupiter.api.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 PWMException (org.openhab.automation.pwm.internal.PWMException)1 StateMachine (org.openhab.automation.pwm.internal.handler.state.StateMachine)1 ModuleHandlerCallback (org.openhab.core.automation.ModuleHandlerCallback)1 Configuration (org.openhab.core.config.core.Configuration)1 EventSubscriber (org.openhab.core.events.EventSubscriber)1