Search in sources :

Example 1 with ThingHandlerCallback

use of org.openhab.core.thing.binding.ThingHandlerCallback in project org.openhab.binding.zwave by openhab.

the class ZWaveThingHandlerTest method doConfigurationUpdate.

private ZWaveThingHandler doConfigurationUpdate(String param, Object value) {
    ThingType thingType = ThingTypeBuilder.instance("bindingId", "thingTypeId", "label").build();
    Thing thing = ThingBuilder.create(thingType.getUID(), new ThingUID(thingType.getUID(), "thingId")).withConfiguration(new Configuration()).build();
    ZWaveNode node = Mockito.mock(ZWaveNode.class);
    ZWaveController controller = Mockito.mock(ZWaveController.class);
    ZWaveControllerHandler controllerHandler = Mockito.mock(ZWaveControllerHandler.class);
    Mockito.when(controllerHandler.isControllerMaster()).thenReturn(false);
    ThingHandlerCallback thingCallback = Mockito.mock(ThingHandlerCallback.class);
    ZWaveThingHandler thingHandler = new ZWaveThingHandlerForTest(thing);
    thingHandler.setCallback(thingCallback);
    payloadCaptor = ArgumentCaptor.forClass(ZWaveCommandClassTransactionPayload.class);
    Field fieldControllerHandler;
    try {
        ZWaveWakeUpCommandClass wakeupClass = new ZWaveWakeUpCommandClass(node, controller, null);
        ZWaveAssociationCommandClass associationClass = new ZWaveAssociationCommandClass(node, controller, null);
        ZWaveNodeNamingCommandClass namingClass = new ZWaveNodeNamingCommandClass(node, controller, null);
        Mockito.doNothing().when(node).sendMessage(payloadCaptor.capture());
        fieldControllerHandler = ZWaveThingHandler.class.getDeclaredField("controllerHandler");
        fieldControllerHandler.setAccessible(true);
        fieldControllerHandler.set(thingHandler, controllerHandler);
        Mockito.when(controller.getOwnNodeId()).thenReturn(1);
        Mockito.when(controllerHandler.getOwnNodeId()).thenReturn(1);
        Mockito.when(controllerHandler.getNode(ArgumentMatchers.anyInt())).thenReturn(node);
        Mockito.when(node.getNodeId()).thenReturn(1);
        Mockito.when(node.getAssociationGroup(ArgumentMatchers.anyInt())).thenReturn(new ZWaveAssociationGroup(1));
        Mockito.when(node.getCommandClass(ArgumentMatchers.eq(CommandClass.COMMAND_CLASS_WAKE_UP))).thenReturn(wakeupClass);
        Mockito.when(node.getCommandClass(ArgumentMatchers.eq(CommandClass.COMMAND_CLASS_ASSOCIATION))).thenReturn(associationClass);
        Mockito.when(node.getCommandClass(ArgumentMatchers.eq(CommandClass.COMMAND_CLASS_NODE_NAMING))).thenReturn(namingClass);
    } catch (NoSuchFieldException | SecurityException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    Map<String, Object> config = new HashMap<String, Object>();
    config.put(param, value);
    thingHandler.handleConfigurationUpdate(config);
    configResult = thingHandler.getThing().getConfiguration();
    return thingHandler;
}
Also used : Configuration(org.openhab.core.config.core.Configuration) HashMap(java.util.HashMap) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ThingType(org.openhab.core.thing.type.ThingType) ZWaveWakeUpCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveWakeUpCommandClass) ZWaveAssociationCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveAssociationCommandClass) Field(java.lang.reflect.Field) Thing(org.openhab.core.thing.Thing) ZWaveAssociationGroup(org.openhab.binding.zwave.internal.protocol.ZWaveAssociationGroup) ZWaveNodeNamingCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveNodeNamingCommandClass) ZWaveNode(org.openhab.binding.zwave.internal.protocol.ZWaveNode) ZWaveCommandClassTransactionPayload(org.openhab.binding.zwave.internal.protocol.transaction.ZWaveCommandClassTransactionPayload) ThingUID(org.openhab.core.thing.ThingUID) ZWaveController(org.openhab.binding.zwave.internal.protocol.ZWaveController)

Example 2 with ThingHandlerCallback

use of org.openhab.core.thing.binding.ThingHandlerCallback in project openhab-addons by openhab.

the class AhaWasteCollectionHandlerTest method testUpdateChannels.

@Test
public void testUpdateChannels() {
    final Thing thing = mockThing(CONFIG);
    final ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
    final AhaWasteCollectionHandler handler = createAndInitHandler(callback, thing);
    try {
        verify(callback).statusUpdated(eq(thing), argThat(arg -> arg.getStatus().equals(ThingStatus.UNKNOWN)));
        verify(callback, timeout(1000)).statusUpdated(eq(thing), argThat(arg -> arg.getStatus().equals(ThingStatus.ONLINE)));
        verify(callback, timeout(1000)).stateUpdated(new ChannelUID(thing.getUID(), AhaWasteCollectionBindingConstants.BIOWASTE), getDateTime(AhaCollectionScheduleStub.BIO_WASTE_DATE));
        verify(callback, timeout(1000)).stateUpdated(new ChannelUID(thing.getUID(), AhaWasteCollectionBindingConstants.GENERAL_WASTE), getDateTime(AhaCollectionScheduleStub.GENERAL_WASTE_DATE));
        verify(callback, timeout(1000)).stateUpdated(new ChannelUID(thing.getUID(), AhaWasteCollectionBindingConstants.LEIGHTWEIGHT_PACKAGING), getDateTime(AhaCollectionScheduleStub.LEIGHTWEIGHT_PACKAGING_DATE));
        verify(callback, timeout(1000)).stateUpdated(new ChannelUID(thing.getUID(), AhaWasteCollectionBindingConstants.PAPER), getDateTime(AhaCollectionScheduleStub.PAPER_DATE));
    } finally {
        handler.dispose();
    }
}
Also used : Arrays(java.util.Arrays) ScheduledCompletableFuture(org.openhab.core.scheduler.ScheduledCompletableFuture) SchedulerRunnable(org.openhab.core.scheduler.SchedulerRunnable) ArgumentMatchers(org.mockito.ArgumentMatchers) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ThingStatus(org.openhab.core.thing.ThingStatus) ThingUID(org.openhab.core.thing.ThingUID) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) State(org.openhab.core.types.State) DateTimeType(org.openhab.core.library.types.DateTimeType) ZoneId(java.time.ZoneId) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Channel(org.openhab.core.thing.Channel) Thing(org.openhab.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable) Configuration(org.openhab.core.config.core.Configuration) CronJob(org.openhab.core.scheduler.CronJob) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Map(java.util.Map) ChannelUID(org.openhab.core.thing.ChannelUID) CronScheduler(org.openhab.core.scheduler.CronScheduler) ChannelUID(org.openhab.core.thing.ChannelUID) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test)

Example 3 with ThingHandlerCallback

use of org.openhab.core.thing.binding.ThingHandlerCallback in project openhab-addons by openhab.

the class AVMFritzBaseThingHandler method createChannel.

/**
 * Creates new channels for the thing.
 *
 * @param channelId ID of the channel to be created.
 */
private void createChannel(String channelId) {
    ThingHandlerCallback callback = getCallback();
    if (callback != null) {
        ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
        ChannelTypeUID channelTypeUID = createChannelTypeUID(channelId);
        Channel channel = callback.createChannelBuilder(channelUID, channelTypeUID).build();
        updateThing(editThing().withoutChannel(channelUID).withChannel(channel).build());
    }
}
Also used : ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ChannelUID(org.openhab.core.thing.ChannelUID) Channel(org.openhab.core.thing.Channel) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback)

Example 4 with ThingHandlerCallback

use of org.openhab.core.thing.binding.ThingHandlerCallback in project openhab-addons by openhab.

the class Powerline546EHandler method createChannel.

/**
 * Creates new channels for the thing.
 *
 * @param channelId ID of the channel to be created.
 */
private void createChannel(String channelId) {
    ThingHandlerCallback callback = getCallback();
    if (callback != null) {
        ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
        ChannelTypeUID channelTypeUID = CHANNEL_BATTERY.equals(channelId) ? DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_BATTERY_LEVEL.getUID() : new ChannelTypeUID(BINDING_ID, channelId);
        Channel channel = callback.createChannelBuilder(channelUID, channelTypeUID).build();
        updateThing(editThing().withoutChannel(channelUID).withChannel(channel).build());
    }
}
Also used : ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ChannelUID(org.openhab.core.thing.ChannelUID) Channel(org.openhab.core.thing.Channel) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback)

Example 5 with ThingHandlerCallback

use of org.openhab.core.thing.binding.ThingHandlerCallback in project openhab-addons by openhab.

the class HomeConnectBridgeHandler method handleConfigurationUpdate.

@Override
public void handleConfigurationUpdate(Map<String, Object> configurationParameters) {
    if (isModifyingCurrentConfig(configurationParameters)) {
        List<String> parameters = configurationParameters.entrySet().stream().map((entry) -> {
            if (CLIENT_ID.equals(entry.getKey()) || CLIENT_SECRET.equals(entry.getKey())) {
                return entry.getKey() + ": ***";
            }
            return entry.getKey() + ": " + entry.getValue();
        }).collect(Collectors.toList());
        logger.debug("Update bridge configuration. bridge={}, parameters={}", getThing().getLabel(), parameters);
        validateConfigurationParameters(configurationParameters);
        Configuration configuration = editConfiguration();
        for (Entry<String, Object> configurationParameter : configurationParameters.entrySet()) {
            configuration.put(configurationParameter.getKey(), configurationParameter.getValue());
        }
        // invalidate oAuth credentials
        try {
            logger.debug("Clear oAuth credential store. bridge={}", getThing().getLabel());
            var oAuthClientService = this.oAuthClientService;
            if (oAuthClientService != null) {
                oAuthClientService.remove();
            }
        } catch (OAuthException e) {
            logger.error("Could not clear oAuth credentials. bridge={}", getThing().getLabel(), e);
        }
        if (isInitialized()) {
            // persist new configuration and reinitialize handler
            dispose();
            updateConfiguration(configuration);
            initialize();
        } else {
            // persist new configuration and notify Thing Manager
            updateConfiguration(configuration);
            @Nullable ThingHandlerCallback callback = getCallback();
            if (callback != null) {
                callback.configurationUpdated(this.getThing());
            } else {
                logger.warn("Handler {} tried updating its configuration although the handler was already disposed.", this.getClass().getSimpleName());
            }
        }
    }
}
Also used : ApiRequest(org.openhab.binding.homeconnect.internal.client.model.ApiRequest) ScheduledFuture(java.util.concurrent.ScheduledFuture) OAuthException(org.openhab.core.auth.client.oauth2.OAuthException) ZonedDateTime(java.time.ZonedDateTime) AuthorizationException(org.openhab.binding.homeconnect.internal.client.exception.AuthorizationException) LoggerFactory(org.slf4j.LoggerFactory) ThingHandlerService(org.openhab.core.thing.binding.ThingHandlerService) ArrayList(java.util.ArrayList) HttpClient(org.eclipse.jetty.client.HttpClient) ClientBuilder(javax.ws.rs.client.ClientBuilder) Nullable(org.eclipse.jdt.annotation.Nullable) Configuration(org.openhab.core.config.core.Configuration) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Map(java.util.Map) HomeConnectServlet(org.openhab.binding.homeconnect.internal.servlet.HomeConnectServlet) ChannelUID(org.openhab.core.thing.ChannelUID) CommunicationException(org.openhab.binding.homeconnect.internal.client.exception.CommunicationException) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) HomeConnectApiClient(org.openhab.binding.homeconnect.internal.client.HomeConnectApiClient) ThingStatus(org.openhab.core.thing.ThingStatus) Command(org.openhab.core.types.Command) Logger(org.slf4j.Logger) OAuthResponseException(org.openhab.core.auth.client.oauth2.OAuthResponseException) Collection(java.util.Collection) Event(org.openhab.binding.homeconnect.internal.client.model.Event) IOException(java.io.IOException) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) HomeConnectEventSourceClient(org.openhab.binding.homeconnect.internal.client.HomeConnectEventSourceClient) HomeConnectBindingConstants(org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants) Collectors(java.util.stream.Collectors) SseEventSourceFactory(org.osgi.service.jaxrs.client.SseEventSourceFactory) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ApiBridgeConfiguration(org.openhab.binding.homeconnect.internal.configuration.ApiBridgeConfiguration) AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse) DateTimeFormatter(java.time.format.DateTimeFormatter) OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) Entry(java.util.Map.Entry) HomeConnectDiscoveryService(org.openhab.binding.homeconnect.internal.discovery.HomeConnectDiscoveryService) OAuthFactory(org.openhab.core.auth.client.oauth2.OAuthFactory) BaseBridgeHandler(org.openhab.core.thing.binding.BaseBridgeHandler) Collections(java.util.Collections) Bridge(org.openhab.core.thing.Bridge) Configuration(org.openhab.core.config.core.Configuration) ApiBridgeConfiguration(org.openhab.binding.homeconnect.internal.configuration.ApiBridgeConfiguration) OAuthException(org.openhab.core.auth.client.oauth2.OAuthException) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

ThingHandlerCallback (org.openhab.core.thing.binding.ThingHandlerCallback)79 Thing (org.openhab.core.thing.Thing)48 Test (org.junit.jupiter.api.Test)39 Configuration (org.openhab.core.config.core.Configuration)30 Channel (org.openhab.core.thing.Channel)30 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)28 ThingHandler (org.openhab.core.thing.binding.ThingHandler)26 ChannelUID (org.openhab.core.thing.ChannelUID)25 Nullable (org.eclipse.jdt.annotation.Nullable)23 InvocationOnMock (org.mockito.invocation.InvocationOnMock)23 ArrayList (java.util.ArrayList)19 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)19 ThingHandlerFactory (org.openhab.core.thing.binding.ThingHandlerFactory)19 ThingStatusInfo (org.openhab.core.thing.ThingStatusInfo)18 ThingUID (org.openhab.core.thing.ThingUID)18 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)16 Bridge (org.openhab.core.thing.Bridge)12 ThingStatus (org.openhab.core.thing.ThingStatus)11 ChannelTypeUID (org.openhab.core.thing.type.ChannelTypeUID)11 List (java.util.List)10