Search in sources :

Example 1 with ThingType

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;
}
Also used : DiscoveryResult(org.openhab.core.config.discovery.DiscoveryResult) ZWaveProduct(org.openhab.binding.zwave.internal.ZWaveProduct) HashMap(java.util.HashMap) ThingUID(org.openhab.core.thing.ThingUID) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) ThingType(org.openhab.core.thing.type.ThingType) BigDecimal(java.math.BigDecimal)

Example 2 with ThingType

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;
}
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 3 with ThingType

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);
}
Also used : ArrayList(java.util.ArrayList) NeeoDevice(org.openhab.io.neeo.internal.models.NeeoDevice) ThingType(org.openhab.core.thing.type.ThingType) TokenScore(org.openhab.io.neeo.internal.models.TokenScore) BindingInfo(org.openhab.core.binding.BindingInfo) Thing(org.openhab.core.thing.Thing)

Example 4 with ThingType

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);
    }
}
Also used : AbstractComponent(org.openhab.binding.mqtt.homeassistant.internal.component.AbstractComponent) ChannelDefinition(org.openhab.core.thing.type.ChannelDefinition) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) List(java.util.List) ThingType(org.openhab.core.thing.type.ThingType) ChannelGroupDefinition(org.openhab.core.thing.type.ChannelGroupDefinition)

Example 5 with 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());
    }
}
Also used : ThingTypeUID(org.openhab.core.thing.ThingTypeUID) ThingType(org.openhab.core.thing.type.ThingType)

Aggregations

ThingType (org.openhab.core.thing.type.ThingType)52 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)22 Test (org.junit.jupiter.api.Test)21 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)20 Thing (org.openhab.core.thing.Thing)18 ThingUID (org.openhab.core.thing.ThingUID)17 Configuration (org.openhab.core.config.core.Configuration)15 ChannelDefinition (org.openhab.core.thing.type.ChannelDefinition)14 URI (java.net.URI)10 ArrayList (java.util.ArrayList)9 ChannelGroupType (org.openhab.core.thing.type.ChannelGroupType)9 ChannelType (org.openhab.core.thing.type.ChannelType)9 List (java.util.List)8 Nullable (org.eclipse.jdt.annotation.Nullable)8 ConfigDescription (org.openhab.core.config.core.ConfigDescription)8 ChannelGroupDefinition (org.openhab.core.thing.type.ChannelGroupDefinition)7 ThingTypeRegistry (org.openhab.core.thing.type.ThingTypeRegistry)7 Locale (java.util.Locale)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 ThingTypeProvider (org.openhab.core.thing.binding.ThingTypeProvider)6