Skip to content

General-Purpose Methods that are Reusable across Hubitat Projects

License

Notifications You must be signed in to change notification settings

WesleyMConner/WmcUtils

Repository files navigation

WmcUtil

The Apps | Devices in the Wmc namespace utilize this foundational library. This library can be used in isolation. Key features include:

HTML-like wrapper functions for use in Hubitat Pages AND Logs:
paragraph h1('My Top-Level Heading')
paragraph h2('My Second-Level Heading')
paragraph h2('My Second-Level Heading')

log.info h1('My Top-Level Heading')
log.info bullet1('First level bullet')
log.info bullet2('Second level bullet')
log.info "Received the following event:<br/>${eventDetails(e)}"
log.trace "Results Map (highlighted values): ${bMap(results)}""
log.trace "Results Map (as table): ${bMapTable(results)}""
log.trace "Options List (highlighted values): ${bList(options)}"
Colorized and Conditional Logging (sample image):
logInfo('fnName', 'some content')` +
logWarn('fnName', "some ${b('important')} content')` +
if (ifLogDebug()) {
  Map x = expensiveMethod1()
  ArrayList y = expensiveMethod2()
  logDebug('fnName', ['This text appears to right of fnName.',
    bMap(x),          // New row with Map's values in bold.
    'My Table X',     // A text row
    bMapHtml(x),      // Map as HTML table, one row per <k, v>
    "y: ${bList(y)}"  // The list's contents are in bold.
  ])

Installation Options

There are several different ways to install this library and/or the demonstration application.

Important
The Hubitat Package Manager (HPM) is the preferred installation mechanism. If new versions of a package are made available, HPM offers an automated update facility. All other installation mechanisms are brutally manual.
lUtils.groovy (Hubitat Library)

To install only the library using the Hubitat GUI …​
(1) Developer ToolsLibraries code+ New Library → then …​
Paste the contents of lUtils.groovy into the edit window → click the Save button.
(2) The library should be available for import.

DemoUtils.groovy (Hubitat Application)

To install only the demonstration App (which requires the library) using the Hubitat GUI …​
(1) Developer ToolsApps codeAdd User App → then …​
Paste the contents of Demo-Utils.groovy into the edit window and click the Save button.
(2) Use Apps+ Add User App and select Demo-Utils to install the application.

lUtils.zip (Hubitat Bundle)

To install the library AND the demonstration App using the Hubitat GUI …​
(1) Developer ToolsBundlesImport .ZIP
Provide the UtilsLibrary.zip file (a) from a copy on the local file system, (b) using a URL, or (c) by dragging and dropping the zip file.
(2) Use Apps+ Add User App and select Demo-Utils to install the application.

packageManifest.json

Make sure that the Hubitat Package Manager (HPM) is installed.
HPM Hubitat Community · Installing HPM · HpmBundle.ZIP
In a Nutshell: (1) Install the HpmBundle.ZIP then (2) Install the Hubitat Package Manager application (available via the bundle). (3) In HPM installBrowse by Tags (allow any refreshing to occur) → Choose the tag "utility" and click "Next" → I N C O M P L E T E

A Hubitat Package Manager (HPM) package manifest used to make the lUtils.zip bundle available via the moniker Wmc-UtilsLibrary in HPM.

Advanced Logging

IMPORTANT

When PBSG leverages sendEvent(), the descriptionText field may include "fancy labels" from Wmc.WmcUtilsLib_1.0.0. The relevant HTML can be removed using String stripHued(String html), which is also found in Wmc.WmcUtilsLib_1.0.0.

Name

Arguments{ref}

Description

General Purpose returned Map.

safeParseInt

String s

s.toInteger() with null and empty-string handling.

cleanStrings

ArrayList

Prunes nulls, empty strings and duplicates

modeNames

n/a

Shorthand for getLocation().getModes()*.name

toJson

def thing

Returns a String with 'thing' serialized using JSON.

** parseJson

String json

THIS IS BUILT INTO HUBITAT.
See Common Methods Object !!!

eventDetails

Event e

Produces an summary HTML table of the event fields.

Hubitat HTML Utility Methods

blackBar

n/a

Displays a medium-thick HTML black bar.

greenBar

n/a

Displays a medium-thick HTML green bar.

redBar

n/a

Displays a medium-thick HTML red bar.

h1, h2, h3

String s

Insert text resembling historic HTML headers

bullet1, bullet2

String s

Two levels of HTML bullets

alert

String s

Red bordered alert text

b, i, bi

def val

Produce bold, italic and bold-italic HTML content.

bList

ArrayList list

HTML with easier-to-read data for a list.

bMap

Map map

HTML with easier-to-read data for a map.

tdBordered

String content

Insert a single table cell <td>..</td> with a border.

bMapTable

Map map

Produce HTML Table with one row per Map key and value.

Hubitat Logging Utilities

hued

InstAppW a

Returns an HTML label with a differentiating color.

hued

DevW | ChildDevW d

Returns an HTML label with a differentiating color.

hued

Event e

Returns an HTML label with a differentiating color.

hued

n/a

Returns an HTML label with a differentiating color.

stripHued

String html

Strips the HTML produced by
hued, hued, hued and hued.

setLogLevel

String logThreshold

Issue logs at or above the threshold
TRACE (lowest), DEBUG, INFO, WARN, ERROR (highest)

logTrace, logDebug, logInfo, logWarn, logError

String fnName,
String|ArrayList x

Conditionally send content (x) from method (fnName) to Hubitat’s Log. [Hubitat’s GUI has filters for reviewing log data received by Hubitat. This facility reduces the data sent to Hubitat. Typically, more data is good during development and heavy debugging, but bad as a steady-state solution.]

ifLogTrace, ifLogDebug, ifLogInfo, ifLogWarn

n/a

These conditional tests can be used to limit executing expensive statements or code blocks that exist to support logging.
Examples:
ifLogDebug() && <statement>
or
if (ifLogDebug()) { <code-block> }

About

General-Purpose Methods that are Reusable across Hubitat Projects

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages