use of org.openhab.core.thing.type.ThingType in project org.openhab.binding.zwave by openhab.
the class ZWaveDiscoveryService method deviceAdded.
/**
* This is called once the node is fully discovered. At this point we know most of the information about
* the device including manufacturer information.
*
* @param node the node to be added
*/
public void deviceAdded(ZWaveNode node) {
// Don't add the controller as a thing, and only add valid nodes
if (controllerHandler.getOwnNodeId() == node.getNodeId() || node.getNodeId() == 0 || node.getNodeId() > 232) {
return;
}
logger.debug("NODE {}: Device discovery completed", node.getNodeId());
ThingUID bridgeUID = controllerHandler.getThing().getUID();
// Search the database for this product information
ZWaveProduct foundProduct = null;
for (ZWaveProduct product : ZWaveConfigProvider.getProductIndex()) {
if (product == null) {
continue;
}
logger.trace("NODE {}: Checking {}", node.getNodeId(), product.getThingTypeUID());
if (product.match(node) == true) {
foundProduct = product;
break;
}
}
ThingTypeUID thingTypeUID;
// If we didn't find the product, then add the unknown thing
String label = String.format(ZWAVE_NODE_LABEL, node.getNodeId());
if (foundProduct == null) {
if (node.getManufacturer() != Integer.MAX_VALUE) {
logger.warn("NODE {}: Device discovery could not resolve to a thingType! {}:{}:{}::{}", node.getNodeId(), String.format("%04X", node.getManufacturer()), String.format("%04X", node.getDeviceType()), String.format("%04X", node.getDeviceId()), node.getApplicationVersion());
label += String.format(" (%04X:%04X:%04X:%s)", node.getManufacturer(), node.getDeviceType(), node.getDeviceId(), node.getApplicationVersion());
} else {
logger.warn("NODE {}: Device discovery could not resolve to a thingType! Manufacturer data not known.", node.getNodeId());
}
thingTypeUID = new ThingTypeUID(ZWaveBindingConstants.ZWAVE_THING);
} else {
logger.debug("NODE {}: Device discovery resolved to thingType {}", node.getNodeId(), foundProduct.getThingTypeUID());
// And create the new thing
ThingType thingType = ZWaveConfigProvider.getThingType(foundProduct.getThingTypeUID());
label += String.format(": %s", thingType.getLabel());
thingTypeUID = foundProduct.getThingTypeUID();
}
// thingTypeUID = new ThingTypeUID(ZWaveBindingConstants.ZWAVE_THING);
// Create the thing UID
ThingUID thingUID = new ThingUID(new ThingTypeUID(ZWaveBindingConstants.ZWAVE_THING), bridgeUID, String.format("node%d", node.getNodeId()));
// Add some device properties that might be useful for the system to know
Map<String, Object> properties = new HashMap<>(11);
properties.put(ZWaveBindingConstants.CONFIGURATION_NODEID, new BigDecimal(node.getNodeId()));
// This should also prevent it from being overwritten if it was added previously
if (node.getManufacturer() != Integer.MAX_VALUE) {
properties.put(ZWaveBindingConstants.PROPERTY_MANUFACTURER, Integer.toString(node.getManufacturer()));
properties.put(ZWaveBindingConstants.PROPERTY_DEVICETYPE, Integer.toString(node.getDeviceType()));
properties.put(ZWaveBindingConstants.PROPERTY_DEVICEID, Integer.toString(node.getDeviceId()));
}
properties.put(ZWaveBindingConstants.PROPERTY_VERSION, node.getApplicationVersion());
// The following properties should always be known as they come from the controller
properties.put(ZWaveBindingConstants.PROPERTY_CLASS_BASIC, node.getDeviceClass().getBasicDeviceClass().toString());
properties.put(ZWaveBindingConstants.PROPERTY_CLASS_GENERIC, node.getDeviceClass().getGenericDeviceClass().toString());
properties.put(ZWaveBindingConstants.PROPERTY_CLASS_SPECIFIC, node.getDeviceClass().getSpecificDeviceClass().toString());
properties.put(ZWaveBindingConstants.PROPERTY_LISTENING, Boolean.toString(node.isListening()));
properties.put(ZWaveBindingConstants.PROPERTY_FREQUENT, Boolean.toString(node.isFrequentlyListening()));
properties.put(ZWaveBindingConstants.PROPERTY_BEAMING, Boolean.toString(node.isBeaming()));
properties.put(ZWaveBindingConstants.PROPERTY_ROUTING, Boolean.toString(node.isRouting()));
// Create the discovery result and add to the inbox
DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID).withProperties(properties).withBridge(bridgeUID).withLabel(label).build();
thingDiscovered(discoveryResult);
return;
}
use of org.openhab.core.thing.type.ThingType 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;
}
use of org.openhab.core.thing.type.ThingType in project openhab-addons by openhab.
the class TokenSearch method search.
/**
* Searches the registry for all {@link NeeoDevice} matching the query
*
* @param query the non-empty query
* @return a non-null result
*/
public Result search(String query) {
NeeoUtil.requireNotEmpty(query, "query cannot be empty");
final List<TokenScore<NeeoDevice>> results = new ArrayList<>();
final String[] needles = StringUtils.split(query, DELIMITER);
int maxScore = -1;
for (NeeoDevice device : context.getDefinitions().getExposed()) {
int score = search(device.getName(), needles);
score += search("openhab", needles);
// score += searchAlgorithm(thing.getLocation(), needles);
score += search(device.getUid().getBindingId(), needles);
final Thing thing = context.getThingRegistry().get(device.getUid().asThingUID());
if (thing != null) {
final String location = thing.getLocation();
if (location != null && !location.isEmpty()) {
score += search(location, needles);
}
final Map<@NonNull String, String> properties = thing.getProperties();
final String vendor = properties.get(Thing.PROPERTY_VENDOR);
if (vendor != null && !vendor.isEmpty()) {
score += search(vendor, needles);
}
final ThingType tt = context.getThingTypeRegistry().getThingType(thing.getThingTypeUID());
if (tt != null) {
score += search(tt.getLabel(), needles);
final BindingInfo bi = context.getBindingInfoRegistry().getBindingInfo(tt.getBindingId());
if (bi != null) {
score += search(bi.getName(), needles);
}
}
}
maxScore = Math.max(maxScore, score);
results.add(new TokenScore<>(score, device));
}
return new Result(applyThreshold(results, maxScore, threshold), maxScore);
}
use of org.openhab.core.thing.type.ThingType in project openhab-addons by openhab.
the class HomeAssistantThingHandler method updateThingType.
private void updateThingType() {
// if this is a dynamic type, then we update the type
ThingTypeUID typeID = thing.getThingTypeUID();
if (!MqttBindingConstants.HOMEASSISTANT_MQTT_THING.equals(typeID)) {
List<ChannelGroupDefinition> groupDefs;
List<ChannelDefinition> channelDefs;
synchronized (haComponents) {
// sync whenever discoverComponents is started
groupDefs = haComponents.values().stream().map(AbstractComponent::getGroupDefinition).collect(Collectors.toList());
channelDefs = haComponents.values().stream().map(AbstractComponent::getType).map(ChannelGroupType::getChannelDefinitions).flatMap(List::stream).collect(Collectors.toList());
}
ThingType thingType = channelTypeProvider.derive(typeID, MqttBindingConstants.HOMEASSISTANT_MQTT_THING).withChannelDefinitions(channelDefs).withChannelGroupDefinitions(groupDefs).build();
channelTypeProvider.setThingType(typeID, thingType);
}
}
use of org.openhab.core.thing.type.ThingType in project openhab-addons by openhab.
the class SatelDeviceDiscoveryService method startScan.
@Override
protected void startScan() {
scanStopped = false;
if (bridgeHandler.isInitialized()) {
// add virtual things by default
for (ThingTypeUID thingTypeUID : VIRTUAL_THING_TYPES_UIDS) {
ThingType thingType = thingTypeProvider.apply(thingTypeUID);
addThing(thingTypeUID, null, thingType.getLabel(), Collections.emptyMap());
}
}
if (!scanStopped) {
scanForDevices(DeviceType.KEYPAD, 8);
}
if (!scanStopped) {
scanForDevices(DeviceType.EXPANDER, 64);
}
if (!scanStopped) {
scanForDevices(DeviceType.PARTITION_WITH_OBJECT, bridgeHandler.getIntegraType().getPartitions());
}
if (!scanStopped) {
scanForDevices(DeviceType.ZONE_WITH_PARTITION, bridgeHandler.getIntegraType().getZones());
}
if (!scanStopped) {
scanForDevices(DeviceType.OUTPUT, bridgeHandler.getIntegraType().getZones());
}
}
Aggregations