The library definition provides the Hubitat Elevation hub with information about the library, which can be added to Libraries Code in the hub Developer Tools. Libraries provide a way to share code among multiple apps or drivers by defining a library with variables, methods, etc. just as they would appear in app/driver:
library (
author: "myName",
category: "samples",
description: "Some description",
name: "SampleLibrary1",
namespace: "myNamespace",
documentationLink: "http://www.example.com/"
)
// put methods, etc. here
The name
and namespace
can include only letters (upper or lower case), numbers, dots, dashes, and underscores. It is recommended the namespace be unique to you as a developer. All name plus namespace combinations must be unique for each library on a given hub. The name
, namespace
, and description
parameters are the only required parameters at this time, though the additional parameters may be used in the future.
To use a library in an app or driver, begin a line with #include
followed by a space, the library namespace, a period, and the library name. For example, to include the above library, write the following line in app or driver code:
#include myNamespace.SampleLibrary1
No whitespace is allowed before
#include
(it must appear at the beginning of the line).
When the app or driver is saved, the “included” code will be inserted behind the scenes automatically. The included code will be appended to the end of the app or driver file, regardless of where #include
appears in the original code. All apps or drivers using a library are recompiled when the library is changed. Each line in the library produces two lines in the resulting full code file: the first line is a comment with the line number in the original library, and the second is the actual line of code. (Note that this may affect use of features inside library code such as multi-line strings.)
You can select the download icon (downward arrow) next to Import button in the Apps Code or Drivers Code editor to see exactly how the included code appears in the final the source code. The full code may be helpful for troubleshooting errors in logs that refer to specific line numbers, etc. You can also use this feature to download a full version for distribution to others if you do not want to share the app/driver and library code separately.