Driver and app code examples

I just do it right in the web interface. Works well for me and is easy to quickly save and test code.

Its missing a few small features (I think there is no search and replace) but if I have to I just cut and paste to notepad and do the replace and paste it back.

1 Like

Example code can be useful when someone else has already done what Iā€™m trying to do, but it canā€™t substitute for full documentation when I need to do something new.

Specifically, Iā€™m trying to adapt a Cooper Aspire Scene Controller driver so that instead of associating with scene-capable devices, it sends button events to the hub. So far Iā€™ve managed to get it to log button events (in the form of BasicSet and SceneActivationSet commands,) but I have no idea how to tell it to send these events to the hub.

For either of these types of commands the driver executes the following statements:

cmds << response(zwave.indicatorV1.indicatorGet())
sendHubCommand(cmds)

The ā€˜indicatorGetā€™ method is called in many places throughout the driver, but it isnā€™t defined there so I have no idea what it does. The ā€˜responseā€™ method isnā€™t used anywhere else or defined anywhere; I put in some log.debug statements and found that this apparently returns the number 8702 regardless of which indicators are lit on the switches. The ā€˜sendHubCommandā€™ also isnā€™t defined anywhere, but I assume thatā€™s something built into Hubitat ā€” itā€™s mentioned in a community post from last October, but it isnā€™t documented.

I have a Simple Lighting rule thatā€™s supposed to trigger when the first button on this scene controller is pressed. It isnā€™t doing anything, even though I can see the event being generated in the driver. So Iā€™m pretty certain itā€™s a matter of sending the right command to the hub, but I have no idea where to start.

This is just a specific example. My point is to illustrate how the lack of adequate documentation hinders development, and that just relying on public driver code as examples is insufficient.

1 Like

Most, if not all of these can be found in the SmartThings developer docs API Documentation ā€” SmartThings Classic Developer Documentation

The ZWave stuff requires you to log in with a Samsung or Smartthings account

Is there any hope of getting generated docs for Hubitat instead of looking at the ST docs and hoping they are right? Since this is all groovy having auto-generated groovydoc for each release would be WONDERFUL. I don't even need comments, just knowing all the method signatures would be huge.

1 Like

Have you looked at the developer documentation at the following? It's not 100% complete, but compared to a year ago, Hubitat has come a very long way, IMHO.

https://docs.hubitat.com/index.php?title=Hubitat_Elevation_Documentation

Also, the Hubitat team welcomes help from the community to improve and complete the documentation.

Sure, there is some stuff there but the vast majority of the API has no docs.

For example the Hub Update 2.0.3 release notes say that HubMultiAction has been added. Searching the wiki for that class gets me to Driver Object but it just mentions HubMultiAction as a return type with no information on how to use it.

Even HubAction isn't really documented. I have to go look at the ST docs to get an idea of how to use the API: HubAction ā€” SmartThings Classic Developer Documentation

1 Like

I understand. Did you come from SmartThings originally? Or another platform? Or is Hubitat your first home automation hub?

For many of us that are SmartThings refugees, we sometimes take for granted our previous experience on that platform with the very similar API. I still pop back over to ST to check their documentation from time to time.

I can say that if you have a specific issue, which the community cannot help with, the Hubitat staff is usually very quick to lend their expertise to help developers sort things out. Just tag @mike.maxwell (drivers), @bravenel (apps), @patrick (dashboard), @bobbyD (support), or @chuck.schwer (platform). They are a great team to work with! :sunglasses:

Welcome to Hubitat!

Coming from ST and more so coming from ~20 years of open source software development. I just feel bad poking on here constantly when I run into things that I feel like I could sort out if I could dig into the API docs.

Currently I'm trying to figure out why my ported ST driver threw an error. I posted a dedicated thread about it: Modify sendHubCommand from ST?

2 Likes

Sorry to ask a stupid question but...

Are you saying that the Apps code is the Groovy language and that if I study the documentation for Groovy then I'd be good to go?

I'd like to write some Apps and I'm looking for useful stuff to read before diving in.

I've found good documentation on Groovy (a simple Google search brings that up), and I'm not looking for some worked examples to go with it.

Any recommendations would be most welcome.

Ya they are written in groovy.

If anything get familiar with the groovy syntax and the layout of the app structure. Then start looking at code for existing apps to understand how they work. Look for something simple.

The example I gave above is a very basic example. You can then start to add your own inputs and act on them from there.

1 Like

Thanks Gavin. Good advice, which I shall follow.

Looking for sample driver that can open local end-point URL to listen for requests (GET/POST) from LAN, any references?

You'll want to use asyncHTTP, I assume....

def poll() {
	def requestParams = [ uri: "https://api.apixu.com/v1/forecast.json?key=$apixuKey&q=$zipCode&days=3" ]
	// log.debug "Poll ApiXU: $requestParams"
	asynchttpGet("pollHandler", requestParams)
}

/*
	pollHandler
	Purpose: the APIXU website response
	Notes: a good response will be processed by doPoll()
*/
def pollHandler(resp, data) {
	if(resp.getStatus() == 200 || resp.getStatus() == 207) {
		obs = parseJson(resp.data)
    //    	if (debugOutput) log.debug "wx-ApiXU returned: $obs"
		doPoll(obs)		// parse the data returned by ApiXU
	} else {
		log.error "wx-ApiXU weather api did not return data"
	}
}

A driver can not open a listener of UDP/TCP/HTTP...

You will need to look at the Maker API application and documentation HERE

Or a custom app. As mentioned above, the underlying issue is that a driver alone cannot do this (or at least I don't think so...I've never done it...EDIT: forgot that a driver can, but only on a specific port so probably not as useful unless you have control over the other end).

This is basically what HubConnect does if you use HTTP, but it's not exactly a minimal example app (and you can't reuse its code as licensed, but it may be a good reference).

A driver can listen for http requests. If device DNI needs to be set to the MAC address of the remote device and the calls are made to port http://hubip:39501, Hubitat will call parse() with the contents of the request.

The easiest approach is to create an oAuth-enabled app that will allow you to define endpoints that are mapped to functions within your app.

Have a look at the Remote Client code, it should provide some guidance on how Hubitat allows for http endpoint calls. There are probably simpler examples around here, I just can't think of any at the moment.

1 Like

Thanks, maker API worked, can send command to virtual switch - by calling URL, no coding needed!

Looking at some examples, I don't see any uninstalled() code, what happens to event subscriptions when the user decides to delete the app? Is that all handled for us? Can it be overridden?

1 Like

This may be a repeat, but take a look at bptworld/Hubitat: Apps for use with Hubitat Elevation (github.com)

yes, child apps, child devices, events, subscriptions, scheduled tasks and anything else associated with the app instance are removed.

4 Likes