use of org.openhab.core.thing.ThingUID in project org.openhab.binding.zwave by openhab.
the class ZWaveDiscoveryService method deviceDiscovered.
/**
* Initial device discovered call.
* This is called when the device is first found. We know very little about the device at
* this stage - just its node number. We add this to the inbox so the users have feedback
* that the device is included into the network.
*
* {@link #deviceAdded} is called once more information is known about the device to add
* manufacturer information and other properties.
*
* @param nodeId the node to be added
*/
public void deviceDiscovered(int nodeId) {
// Don't add the controller as a thing, and only add valid nodes
if (controllerHandler.getOwnNodeId() == nodeId || nodeId == 0 || nodeId > 232) {
return;
}
ThingUID bridgeUID = controllerHandler.getThing().getUID();
ThingUID thingUID = new ThingUID(new ThingTypeUID(ZWaveBindingConstants.ZWAVE_THING), bridgeUID, String.format("node%d", nodeId));
logger.debug("NODE {}: Device discovered", nodeId);
Map<String, Object> properties = new HashMap<>(1);
properties.put(ZWaveBindingConstants.PROPERTY_NODEID, Integer.toString(nodeId));
properties.put(ZWaveBindingConstants.CONFIGURATION_NODEID, new BigDecimal(nodeId));
DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID).withProperties(properties).withLabel(String.format(ZWAVE_NODE_LABEL, nodeId)).build();
thingDiscovered(discoveryResult);
}
use of org.openhab.core.thing.ThingUID 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.ThingUID in project org.openhab.binding.zwave by openhab.
the class ZWaveConfigProvider method getConfigDescription.
@Override
public ConfigDescription getConfigDescription(URI uri, Locale locale) {
if (!"thing".equals(uri.getScheme()) && !"thing-type".equals(uri.getScheme())) {
return null;
}
ThingTypeUID thingTypeUID = new ThingTypeUID(uri.getSchemeSpecificPart());
// Is this a zwave thing?
if (!thingTypeUID.getBindingId().equals(ZWaveBindingConstants.BINDING_ID)) {
return null;
}
List<ConfigDescriptionParameter> parameters = new ArrayList<ConfigDescriptionParameter>();
if ("thing-type".equals(uri.getScheme())) {
if (uri.getSchemeSpecificPart().equals(ZWaveBindingConstants.CONTROLLER_SERIAL.toString())) {
return null;
}
parameters.add(ConfigDescriptionParameterBuilder.create(ZWaveBindingConstants.CONFIGURATION_NODEID, Type.INTEGER).withLabel("Node ID").withMinimum(new BigDecimal("1")).withMaximum(new BigDecimal("232")).withAdvanced(true).withReadOnly(true).withRequired(true).withDescription("Sets the node ID<BR/>" + "The node ID is assigned by the controller and can not be changed.").withGroupName("thingcfg").build());
return ConfigDescriptionBuilder.create(uri).withParameters(parameters).build();
}
ThingUID thingUID = new ThingUID(uri.getSchemeSpecificPart());
Thing thing = getThing(thingUID);
if (thing == null) {
logger.debug("No thing found in getConfigDescription {}", uri);
return null;
}
ThingUID bridgeUID = thing.getBridgeUID();
if (bridgeUID == null) {
logger.debug("No bridgeUID found in getConfigDescription {}", uri);
return null;
}
// Get the controller for this thing
Thing bridge = getThing(bridgeUID);
if (bridge == null) {
logger.debug("No bridge found in getConfigDescription {}", uri);
return null;
}
final BigDecimal cfgNodeId = (BigDecimal) thing.getConfiguration().get(ZWaveBindingConstants.CONFIGURATION_NODEID);
if (cfgNodeId == null) {
logger.debug("No nodeId found in getConfigDescription {}", uri);
return null;
}
int nodeId = cfgNodeId.intValue();
// Get its handler and node
ZWaveControllerHandler handler = (ZWaveControllerHandler) bridge.getHandler();
if (handler == null) {
logger.debug("NODE {}: No bridge handler found in getConfigDescription", nodeId);
return null;
}
ZWaveNode node = handler.getNode(nodeId);
if (node == null) {
logger.debug("NODE {}: Node not found in getConfigDescription", nodeId);
return null;
}
List<ConfigDescriptionParameterGroup> groups = new ArrayList<ConfigDescriptionParameterGroup>();
groups.add(ConfigDescriptionParameterGroupBuilder.create("actions").withLabel("Actions").withDescription("Actions").build());
groups.add(ConfigDescriptionParameterGroupBuilder.create("thingcfg").withContext("home").withLabel("Device Configuration").withDescription("Device Configuration").build());
List<ParameterOption> options = new ArrayList<ParameterOption>();
options.add(new ParameterOption("600", "10 Minutes"));
options.add(new ParameterOption("1800", "30 Minutes"));
options.add(new ParameterOption("3600", "1 Hour"));
options.add(new ParameterOption("7200", "2 Hours"));
options.add(new ParameterOption("10800", "3 Hours"));
options.add(new ParameterOption("21600", "6 Hours"));
options.add(new ParameterOption("43200", "12 Hours"));
options.add(new ParameterOption("86400", "1 Day"));
options.add(new ParameterOption("172800", "2 Days"));
options.add(new ParameterOption("864000", "10 Days"));
parameters.add(ConfigDescriptionParameterBuilder.create(ZWaveBindingConstants.CONFIGURATION_POLLPERIOD, Type.INTEGER).withLabel(ZWaveBindingConstants.CONFIG_BINDING_POLLINGPERIOD_LABEL).withDescription(ZWaveBindingConstants.CONFIG_BINDING_POLLINGPERIOD_DESC).withDefault("86400").withMinimum(new BigDecimal(15)).withMaximum(new BigDecimal(864000)).withOptions(options).withLimitToOptions(false).withGroupName("thingcfg").build());
options = new ArrayList<ParameterOption>();
options.add(new ParameterOption("0", "Disabled"));
parameters.add(ConfigDescriptionParameterBuilder.create(ZWaveBindingConstants.CONFIGURATION_CMDREPOLLPERIOD, Type.INTEGER).withLabel(ZWaveBindingConstants.CONFIG_BINDING_CMDREPOLLPERIOD_LABEL).withDescription(ZWaveBindingConstants.CONFIG_BINDING_CMDREPOLLPERIOD_DESC).withDefault("1500").withMinimum(new BigDecimal(100)).withMaximum(new BigDecimal(15000)).withOptions(options).withLimitToOptions(false).withGroupName("thingcfg").build());
// If we support the wakeup class, then add the configuration
ZWaveWakeUpCommandClass wakeupCmdClass = (ZWaveWakeUpCommandClass) node.getCommandClass(ZWaveCommandClass.CommandClass.COMMAND_CLASS_WAKE_UP);
if (wakeupCmdClass != null) {
groups.add(ConfigDescriptionParameterGroupBuilder.create("wakeup").withContext("sleep").withLabel("Wakeup Configuration").withDescription("Configuration for wakeup parameters on battery devices").build());
parameters.add(ConfigDescriptionParameterBuilder.create(ZWaveBindingConstants.CONFIGURATION_WAKEUPINTERVAL, Type.INTEGER).withLabel("Wakeup Interval").withMinimum(new BigDecimal(wakeupCmdClass.getMinInterval())).withMaximum(new BigDecimal(wakeupCmdClass.getMaxInterval())).withDescription("Sets the number of seconds that the device will wakeup<BR/>" + "Setting a shorter time will allow openHAB to configure the device more regularly, but may use more battery power.<BR>" + "<B>Note:</B> This setting does not impact device notifications such as alarms.").withGroupName("wakeup").build());
parameters.add(ConfigDescriptionParameterBuilder.create(ZWaveBindingConstants.CONFIGURATION_WAKEUPNODE, Type.INTEGER).withLabel("Wakeup Node").withAdvanced(true).withMinimum(new BigDecimal(1)).withMaximum(new BigDecimal(232)).withDescription("Sets the wakeup node to which the device will send notifications.<BR/>" + "This should normally be set to the openHAB controller - " + "if it isn't, openHAB will not receive notifications when the device wakes up, " + "and will not be able to configure the device.").withGroupName("wakeup").build());
}
// If we support the node name class, then add the configuration
if (node.getCommandClass(ZWaveCommandClass.CommandClass.COMMAND_CLASS_NODE_NAMING) != null) {
parameters.add(ConfigDescriptionParameterBuilder.create(ZWaveBindingConstants.CONFIGURATION_NODENAME, Type.TEXT).withLabel("Node Name").withDescription("Sets a string for the device name").withGroupName("thingcfg").build());
parameters.add(ConfigDescriptionParameterBuilder.create(ZWaveBindingConstants.CONFIGURATION_NODELOCATION, Type.TEXT).withDescription("Sets a string for the device location").withLabel("Node Location").withGroupName("thingcfg").build());
}
// If we support the switch_all class, then add the configuration
if (node.getCommandClass(ZWaveCommandClass.CommandClass.COMMAND_CLASS_SWITCH_ALL) != null) {
options = new ArrayList<ParameterOption>();
options.add(new ParameterOption("0", "Exclude from All On and All Off groups"));
options.add(new ParameterOption("1", "Include in All On group"));
options.add(new ParameterOption("2", "Include in All Off group"));
options.add(new ParameterOption("255", "Include in All On and All Off groups"));
parameters.add(ConfigDescriptionParameterBuilder.create(ZWaveBindingConstants.CONFIGURATION_SWITCHALLMODE, Type.INTEGER).withLabel("Switch All Mode").withDescription("Set the mode for the switch when receiving SWITCH ALL commands.").withDefault("0").withGroupName("thingcfg").withOptions(options).withLimitToOptions(true).build());
}
// If we support DOOR_LOCK - add options
if (node.getCommandClass(ZWaveCommandClass.CommandClass.COMMAND_CLASS_DOOR_LOCK) != null) {
parameters.add(ConfigDescriptionParameterBuilder.create(ZWaveBindingConstants.CONFIGURATION_DOORLOCKTIMEOUT, Type.INTEGER).withLabel("Lock Timeout").withDescription("Set the timeout on the lock.").withDefault("30").withGroupName("thingcfg").build());
}
ZWaveUserCodeCommandClass userCodeClass = (ZWaveUserCodeCommandClass) node.getCommandClass(ZWaveCommandClass.CommandClass.COMMAND_CLASS_USER_CODE);
if (userCodeClass != null && userCodeClass.getNumberOfSupportedCodes() > 0) {
groups.add(ConfigDescriptionParameterGroupBuilder.create("usercode").withContext("lock").withLabel("User Code").withDescription("Define the user codes for locks").build());
for (int code = 1; code <= userCodeClass.getNumberOfSupportedCodes(); code++) {
UserCode userCode = userCodeClass.getCachedUserCode(code);
boolean readOnly = false;
if (userCode != null) {
readOnly = userCode.getState() == UserIdStatusType.RESERVED_BY_ADMINISTRATOR;
}
parameters.add(ConfigDescriptionParameterBuilder.create(ZWaveBindingConstants.CONFIGURATION_USERCODE_LABEL + code, Type.TEXT).withLabel("Code " + code + " Label").withDescription("Name for user code " + code).withGroupName("usercode").build());
parameters.add(ConfigDescriptionParameterBuilder.create(ZWaveBindingConstants.CONFIGURATION_USERCODE_CODE + code, Type.TEXT).withLabel("Code " + code).withDescription("Set the user code (4 to 10 numbers)").withReadOnly(readOnly).withGroupName("usercode").build());
}
}
// If we're FAILED, allow removing from the controller
// if (node.getNodeState() == ZWaveNodeState.FAILED) {
parameters.add(ConfigDescriptionParameterBuilder.create("action_remove", Type.BOOLEAN).withLabel("Remove device from controller").withAdvanced(true).withOptions(options).withDefault("false").withGroupName("actions").build());
// } else {
// Otherwise, allow us to put this on the failed list
parameters.add(ConfigDescriptionParameterBuilder.create("action_failed", Type.BOOLEAN).withLabel("Set device as FAILed").withAdvanced(true).withOptions(options).withDefault("false").withGroupName("actions").build());
if (node.isInitializationComplete() == true) {
parameters.add(ConfigDescriptionParameterBuilder.create("action_reinit", Type.BOOLEAN).withLabel("Reinitialise the device").withAdvanced(true).withOptions(options).withDefault("false").withGroupName("actions").build());
}
parameters.add(ConfigDescriptionParameterBuilder.create("action_heal", Type.BOOLEAN).withLabel("Heal the device").withAdvanced(true).withOptions(options).withDefault("false").withGroupName("actions").build());
return ConfigDescriptionBuilder.create(uri).withParameters(parameters).withParameterGroups(groups).build();
}
use of org.openhab.core.thing.ThingUID 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.ThingUID in project openhab-addons by openhab.
the class AmazonDashButtonDiscoveryService method startCapturing.
/**
* Starts capturing for packets for the given {@link PcapNetworkInterface}. If the network interface is already
* captured this method returns without doing anything.
*
* @param pcapNetworkInterface The {@link PcapNetworkInterface} to be captured
*/
private void startCapturing(final PcapNetworkInterfaceWrapper pcapNetworkInterface) {
if (packetCapturingServices.containsKey(pcapNetworkInterface)) {
// We already have a tracker
return;
}
PacketCapturingService packetCapturingService = new PacketCapturingService(pcapNetworkInterface);
packetCapturingServices.put(pcapNetworkInterface, packetCapturingService);
final String interfaceName = pcapNetworkInterface.getName();
final boolean capturingStarted = packetCapturingService.startCapturing(new PacketCapturingHandler() {
@Override
public void packetCaptured(MacAddress macAddress) {
String macAdressString = macAddress.toString();
if (isAmazonVendor(macAdressString)) {
logger.debug("Captured a packet from {} which seems to be sent from an Amazon Dash Button device.", macAdressString);
ThingUID dashButtonThing = new ThingUID(DASH_BUTTON_THING_TYPE, macAdressString.replace(":", "-"));
// @formatter:off
DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(dashButtonThing).withLabel("Dash Button").withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS).withProperty(Thing.PROPERTY_MAC_ADDRESS, macAdressString).withProperty(PROPERTY_NETWORK_INTERFACE_NAME, interfaceName).withProperty(PROPERTY_PACKET_INTERVAL, BigDecimal.valueOf(5000)).build();
// @formatter:on
thingDiscovered(discoveryResult);
} else {
logger.trace("Captured a packet from {} which is ignored as it's not on the list of supported vendor prefixes.", macAdressString);
}
}
});
if (capturingStarted) {
logger.debug("Started capturing for {}.", interfaceName);
}
}
Aggregations