Lua Plugins Development
Plugin Structure
Plugin is developed as a single lua file that returns a table containing plugin definition. 1
This includes next fields:
name
<string> - plugin name, should be unique among all pluginsversion
<table> - plugin version, should be a table of 3 numbers (i.e.{0,0,1}
)provided_interfaces
<table> - table of interfaces provided by plugin (see: Interface Definition)overriden_interfaces
<table> - table of interfaces that plugin overrides (see: Interface Override)devices
<table> - device prototypes available for registration (see: Device Type Definition)
Also plugin
table should contain callback functions for plugin lifecycle events 2:
init(reason)
- plugin initialization, called once on plugin loadreason
<string> - reason for plugin initialization
register(uuid)
- plugin registered, called when plugin is register in hub's main applicationuuid
<string> - plugin UUID
unregister(topic, payload)
- plugin unregistered, called when plugin is unregistered from hub's main applicationtopic
<string> - MQTT topicpayload
<table> - MQTT payload
reconnect()
- plugin need to re-register, called when main application is restarteddiscover_devices(topic, payload)
- discover devices, called when plugin is requested to discover devicestopic
<string> - MQTT topicpayload
<table> - MQTT payload
device_create(topic, payload)
- called when device creation requested, needs to send request to create new devicetopic
<string> - MQTT topicpayload
<table> - MQTT payload
device_created(topic, payload)
- device created callback, this callback should finalize device creation by adding it to DB or whatevertopic
<string> - MQTT topicpayload
<table> - MQTT payload
device_registered(id, name, params)
- device registered, called when device is registered in hub's main applicationid
<string> - device IDname
<string> - device nameparams
<table> - device parameters
device_removed(topic, payload)
- device removed, called when device is removed from hub's main applicationtopic
<string> - MQTT topicpayload
<table> - MQTT payload
Interface Definition
Interface is a set of actions and parameters that can be implemented by device, it allows to define device capabilities and provide applications to have a common way to understand device functionality.
Interface is defined as a table with next fields
actions
<table> - table of actions provided by interface (see: Action Definition)parameters
<table> - table of parameters provided by interface (see: Provided Parameter Definition)
Action Definition
Action is a function that can be called by application to perform some action on device.
Every action definition contain name
, description
, arguments
and handler
fields.
name
<string> - action name, should be unique among all actions provided by interfacedescription
<string> - action descriptionarguments
<table> - table of arguments that action acceptsname
<string> - argument name, should be unique among all arguments provided by actiondescription
<string> - argument descriptiontype
<string> - argument type, can be one of:string
,number
,boolean
,json
optional
<boolean> - is argument optionalposition
<number> - argument number for positional action calling (starting from 0)
handler
<function> - action handler function, should accept 2 arguments:self
andargs
Provided Parameter Definition
Parameter is a value that is reported in params
field of device.
Parameter is represented in parameters
field of interface definition as a table where key is parameter name and value is parameter descriptor table with next fields:
type
<string> - parameter type, can be one of:string
,number
,boolean
,json
value
<string> - default parameter value (?)
Interface Override
Not implemented yet
Device Type Definition
Device type is a preferred way to make device creation easier by providing a prototype of device that can be used to create new device.
Device type definition consists of fields that are needed to be forwarded to hub in a device creation request. Also it provides a way to display additional fields in device setting UI:
type
<string> - device type (for available types see: Device Types)role
<string> - device role (for available roles see: Device Roles)icon
<string> - device iconinterfaces
<table> - list of interfaces implemented by deviceparameters
<table> - table of parameters provided by device (see: Parameter Definition)
Device Types
See C-Home API documentation for available device types and their requirements.
Device Roles
See C-Home API documentation for available device roles and their requirements.
Parameter Definition
Parameter is defined in parameters
field of device type definition as a table where key is parameter name and value is parameter descriptor table with next fields:
type
<string> - parameter type, can be one of:string
,number
,boolean
,json
value
<string> - default parameter valuerole
<string> - parameter role for UI (show
oredit
), if field is not present, parameter is not shown in UIlocale
<table> - display name for parameter in UI, is map of locale names to locale values (only relevant ifrole
is present)
Example Plugin
Here is an example plugin that allows to add computer as a device to Butler Smart Home, it provides a way to issue WOL packets to computer.
Add actual plugin code