Search in sources :

Example 16 with ThingHandlerCallback

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

the class EventFilterHandler method updateChannelSet.

/**
 * Checks existing channels, adds missing and removes extraneous channels from the Thing.
 *
 * @param config The validated Configuration of the Thing.
 */
private void updateChannelSet(EventFilterConfiguration config) {
    final ThingHandlerCallback handlerCallback = getCallback();
    if (handlerCallback == null) {
        return;
    }
    final List<Channel> currentChannels = getThing().getChannels();
    final ThingBuilder thingBuilder = editThing();
    BigDecimal maxEvents = config.maxEvents;
    if (maxEvents == null || maxEvents.compareTo(BigDecimal.ZERO) < 1) {
        thingBuilder.withoutChannels(currentChannels);
        updateThing(thingBuilder.build());
        return;
    }
    generateExpectedChannelList(maxEvents.intValue());
    synchronized (resultChannels) {
        currentChannels.stream().filter((Channel current) -> {
            String currentGroupId = current.getUID().getGroupId();
            if (currentGroupId == null) {
                return true;
            }
            for (ResultChannelSet channelSet : resultChannels) {
                if (channelSet.resultGroup.getId().contentEquals(currentGroupId)) {
                    return false;
                }
            }
            return true;
        }).forEach((Channel toDelete) -> {
            thingBuilder.withoutChannel(toDelete.getUID());
        });
        resultChannels.stream().filter((ResultChannelSet current) -> {
            return (getThing().getChannelsOfGroup(current.resultGroup.toString()).size() == 0);
        }).forEach((ResultChannelSet current) -> {
            for (ChannelBuilder builder : handlerCallback.createChannelBuilders(current.resultGroup, GROUP_TYPE_UID)) {
                Channel currentChannel = builder.build();
                Channel existingChannel = getThing().getChannel(currentChannel.getUID());
                if (existingChannel == null) {
                    thingBuilder.withChannel(currentChannel);
                }
            }
        });
    }
    updateThing(thingBuilder.build());
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) Channel(org.openhab.core.thing.Channel) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ChannelBuilder(org.openhab.core.thing.binding.builder.ChannelBuilder) BigDecimal(java.math.BigDecimal)

Example 17 with ThingHandlerCallback

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

the class TrackerHandler method createBasicDistanceChannel.

/**
 * Create distance channel for measuring the distance between the tracker and the szstem.
 */
private void createBasicDistanceChannel() {
    @Nullable ThingHandlerCallback callback = getCallback();
    if (callback != null) {
        // find the system distance channel
        ChannelUID systemDistanceChannelUID = new ChannelUID(thing.getUID(), CHANNEL_DISTANCE_SYSTEM_ID);
        Channel systemDistance = thing.getChannel(CHANNEL_DISTANCE_SYSTEM_ID);
        ChannelBuilder channelBuilder = null;
        if (systemDistance != null) {
            if (!systemDistance.getConfiguration().get(CONFIG_REGION_CENTER_LOCATION).equals(sysLocation.toFullString())) {
                logger.trace("Existing distance channel for system. Changing system location config parameter: {}", sysLocation.toFullString());
                channelBuilder = callback.editChannel(thing, systemDistanceChannelUID);
                Configuration configToUpdate = systemDistance.getConfiguration();
                configToUpdate.put(CONFIG_REGION_CENTER_LOCATION, sysLocation.toFullString());
                channelBuilder.withConfiguration(configToUpdate);
            } else {
                logger.trace("Existing distance channel for system. No change.");
            }
        } else {
            logger.trace("Creating missing distance channel for system.");
            Configuration config = new Configuration();
            config.put(ConfigHelper.CONFIG_REGION_NAME, CHANNEL_DISTANCE_SYSTEM_NAME);
            config.put(CONFIG_REGION_CENTER_LOCATION, sysLocation.toFullString());
            config.put(ConfigHelper.CONFIG_REGION_RADIUS, CHANNEL_DISTANCE_SYSTEM_RADIUS);
            config.put(ConfigHelper.CONFIG_ACCURACY_THRESHOLD, 0);
            channelBuilder = callback.createChannelBuilder(systemDistanceChannelUID, CHANNEL_TYPE_DISTANCE).withLabel("System Distance").withConfiguration(config);
        }
        // update the thing with system distance channel
        if (channelBuilder != null) {
            List<Channel> channels = new ArrayList<>(thing.getChannels());
            if (systemDistance != null) {
                channels.remove(systemDistance);
            }
            channels.add(channelBuilder.build());
            ThingBuilder thingBuilder = editThing();
            thingBuilder.withChannels(channels);
            updateThing(thingBuilder.build());
            logger.debug("Distance channel created for system: {}", systemDistanceChannelUID);
        }
    }
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) Configuration(org.openhab.core.config.core.Configuration) ChannelUID(org.openhab.core.thing.ChannelUID) Channel(org.openhab.core.thing.Channel) ArrayList(java.util.ArrayList) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ChannelBuilder(org.openhab.core.thing.binding.builder.ChannelBuilder) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 18 with ThingHandlerCallback

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

the class BaseDeviceHandler method verifyTspFhbChannels.

private void verifyTspFhbChannels(int id, int size) {
    // Dynamically create TSP or FHB value channels based on TSP or FHB size message
    ThingHandlerCallback callback = getCallback();
    if (callback == null) {
        logger.debug("Unable to get thing handler callback");
        return;
    }
    DataItem[] dataItems = DataItemGroup.DATAITEMGROUPS.get(id);
    if (dataItems == null) {
        logger.debug("Unable to find dataItem for id {}", id);
        return;
    }
    if (dataItems.length != 1) {
        logger.debug("Found zero or multiple dataItems for id {}", id);
        return;
    }
    TspFhbValueDataItem dataItem = (TspFhbValueDataItem) dataItems[0];
    logger.debug("Checking number of TSP or FHB channels for DATA-ID {}: {}", id, size);
    // A generic Number:Dimensionless channel type for TSP and FHB values
    ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, CHANNEL_TSPFHB);
    List<Channel> channels = new ArrayList<>(getThing().getChannels());
    boolean changed = false;
    for (int i = 0; i < size; i++) {
        String channelId = dataItem.getChannelId(i);
        ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
        if (!channels.stream().map(Channel::getUID).anyMatch(channelUID::equals)) {
            String label = dataItem.getLabel(i);
            logger.debug("Adding channel {}", channelId);
            channels.add(callback.createChannelBuilder(channelUID, channelTypeUID).withKind(ChannelKind.STATE).withLabel(label).build());
            changed = true;
        } else {
            logger.debug("Channel {} already exists", channelId);
        }
    }
    if (changed) {
        logger.debug("Updating Thing with new channels");
        updateThing(editThing().withChannels(channels).build());
    }
}
Also used : ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) TspFhbSizeDataItem(org.openhab.binding.openthermgateway.internal.TspFhbSizeDataItem) DataItem(org.openhab.binding.openthermgateway.internal.DataItem) TspFhbValueDataItem(org.openhab.binding.openthermgateway.internal.TspFhbValueDataItem) ChannelUID(org.openhab.core.thing.ChannelUID) Channel(org.openhab.core.thing.Channel) ArrayList(java.util.ArrayList) TspFhbValueDataItem(org.openhab.binding.openthermgateway.internal.TspFhbValueDataItem) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback)

Example 19 with ThingHandlerCallback

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

the class AVMFritzThingHandlerOSGiTest method setUp.

@BeforeEach
public void setUp() {
    registerService(volatileStorageService);
    managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
    assertNotNull(managedThingProvider, "Could not get ManagedThingProvider");
    bridge = buildBridge();
    assertNotNull(bridge.getConfiguration());
    managedThingProvider.add(bridge);
    ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
    bridgeHandler = new BoxHandler(bridge, httpClient, mock(AVMFritzDynamicCommandDescriptionProvider.class));
    assertNotNull(bridgeHandler);
    bridgeHandler.setCallback(callback);
    assertNull(bridge.getHandler());
    bridge.setHandler(bridgeHandler);
    assertNotNull(bridge.getHandler());
    bridgeHandler.initialize();
}
Also used : ThingProvider(org.openhab.core.thing.ThingProvider) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 20 with ThingHandlerCallback

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

the class AstroCommandTest method testRefreshCommandUpdatesTheStateOfTheChannels.

@Test
public void testRefreshCommandUpdatesTheStateOfTheChannels() {
    ThingUID thingUID = new ThingUID(THING_TYPE_SUN, TEST_SUN_THING_ID);
    ChannelUID channelUID = new ChannelUID(thingUID, DEFAULT_TEST_CHANNEL_ID);
    Channel channel = ChannelBuilder.create(channelUID, DEFAULT_IMEM_TYPE).build();
    Configuration thingConfiguration = new Configuration();
    thingConfiguration.put(GEOLOCATION_PROPERTY, GEOLOCATION_VALUE);
    thingConfiguration.put(INTERVAL_PROPERTY, INTERVAL_DEFAULT_VALUE);
    Thing thing = mock(Thing.class);
    when(thing.getConfiguration()).thenReturn(thingConfiguration);
    when(thing.getUID()).thenReturn(thingUID);
    when(thing.getChannel(DEFAULT_TEST_CHANNEL_ID)).thenReturn(channel);
    ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
    CronScheduler cronScheduler = mock(CronScheduler.class);
    TimeZoneProvider timeZoneProvider = mock(TimeZoneProvider.class);
    when(timeZoneProvider.getTimeZone()).thenReturn(ZoneId.systemDefault());
    AstroThingHandler sunHandler = spy(new SunHandler(thing, cronScheduler, timeZoneProvider));
    // Required from the AstroThingHandler to send the status update
    doReturn(true).when(callback).isChannelLinked(eq(channelUID));
    doReturn(new Sun()).when(sunHandler).getPlanet();
    sunHandler.setCallback(callback);
    sunHandler.handleCommand(channelUID, RefreshType.REFRESH);
    verify(callback, times(1)).stateUpdated(eq(channelUID), any(State.class));
}
Also used : CronScheduler(org.openhab.core.scheduler.CronScheduler) Configuration(org.openhab.core.config.core.Configuration) AstroThingHandler(org.openhab.binding.astro.internal.handler.AstroThingHandler) SunHandler(org.openhab.binding.astro.internal.handler.SunHandler) ChannelUID(org.openhab.core.thing.ChannelUID) State(org.openhab.core.types.State) ThingUID(org.openhab.core.thing.ThingUID) Channel(org.openhab.core.thing.Channel) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) TimeZoneProvider(org.openhab.core.i18n.TimeZoneProvider) Sun(org.openhab.binding.astro.internal.model.Sun) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test)

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