Device Code

From Hubitat Documentation
Revision as of 18:54, 16 April 2020 by Dman2306 (talk | contribs) (Initial work on describing the code inside a device driver.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Overview

The bulk of the work a driver does is done inside it's general code section. This is where you write methods and members using the Groovy programming language to define how you driver functions. Drivers can be very different depending on what they do. A driver that communicates with a ZWave device is going to look very different than one that is a virtual device with nothing physical in the real world or one that communicates over Telnet to a LAN device. This article gives you a general overview of device driver code but they will vary greatly depending on what the device driver is intended to do.

Built-In Callback Methods

Device drivers can contain a few built-in callback methods that are triggered but the Hubitat environment in response to certain actions.

installed

This method is called when the device is first created and can be used to initialize any device specific configuration and setup.
Signature
void installed()

uninstalled

This method is called when the device is removed to allow for any necessary cleanup.
Signature
void uninstalled()

updated

This method is called when the preferences of a device are updated.
Signature
void updated()

parse

This method is called in response to a message received by the device driver. Depending on the driver this could be from any number of sources. This could be a Z-Wave message, Zigbee message, telnet message, websocket message, etc. Depending on the type of message received you will likely need to parse the description string into something more useful. The following list of methods are useful for decoding the description:
  • MQTT - interfaces.mqtt.parseMessage
  • RAW_LAN - parseLanMessage
  • Zigbee - zigbee.parse
  • Z-Wave - zwave.parse
Signature
Event parse(String description)

User Defined Methods

User defined methods are where your driver does "everything else." It is where you implement commands, define callbacks for scheduled events, create callbacks for event subscriptions, etc. Essentially you can do just about anything in a user defined method that you can do in a normal Groovy method.