The bulk of the work a driver does is done inside its 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 Z-Wave 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 drivers will vary greatly depending on what the device driver is intended to do.
Device drivers can contain a few built-in callback methods that are called by the Hubitat environment in response to certain actions.
This method is called when the device is first created and can be used to initialize any device specific configuration and setup.
void installed()
This method is called when the device is removed to allow for any necessary cleanup.
void uninstalled()
This method is called when the preferences of a device are updated.
void updated()
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:
interfaces.mqtt.parseMessage
parseLanMessage
zigbee.parse
zwave.parse
Event parse(String description)
User-defined methods are where your driver does "everything else." It is where you implement commands, define handler methods for schedules or event subscriptions, etc. Essentially you can do just about anything in a user defined method that you can do in a normal Groovy method.
Among these methods, you will also need any methods required by capabilities your driver implements. The Groovy method name should match the name of the command name as specified, including the appropriate number and type of parameters if specified. For example, capability "Switch"
requires two methods, on()
and off()
(neither of which takes any parameters).