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());
}
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);
}
}
}
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());
}
}
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();
}
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));
}
Aggregations