How Do I Get a List of All Devices within an App

I think I'm overlooking something obvious but ...

Is there a way, within an app's "preferences" to get a list of all devices in com.hubitat.app.DeviceWrapper form?

I'm pretty sur e* I was able to do it with "allDevices" a.k.a., "getAllDevices()"

  • Maybe I'm having a mix-up on the function name?

  • What I"m trying to do is collect a list of all Switches that are not also Dimmers and present them to the user.

  • In the past, I'm pretty sure I could do it using code like I've shown below, but the "allDevices in line 20 seems to always return a null (and I've tried getAllDevices() which also returns null).

Am I forgetting something? Thanks.

definition(
	name: "Testing: Get List of All Non-Dimmable Devices",
	namespace: "jvm",
	author: "Jim Mahon",
	description: "Testing: Get List of All Non-Dimmable Devices",
	category: "Convenience",
	iconUrl: "",
	iconX2Url: "",
    singleInstance:true
)

preferences {
	page(name: "mainPage")
}

Map mainPage() {
	dynamicPage(name: "mainPage", title: "Non-Dimmable Switches", uninstall: true, install: true) {
        
    
        Map selectionList = allDevices?.findAll {  it.hasCapability("Switch") && !it.hasCapability("SwitchLevel") }
                                    .collectEntries{ [(it.deviceNetworkId):(it.displayName)] }
                                    .sort{e1, e2 -> (String) e1.value <=> (String) e2.value}
        
		section("Non-Dimmable Switches") {
			input( name:"mySelectedSwitches", type:"enum", title: "Refreshable Devices", multiple:true, submitOnChange:true, options: (selectionList))
            log.debug "Selected Switches are: ${mySelectedSwitches}"

        }
	}
}

void updated() {
	unsubscribe()
	initialize()
}

void installed() {
	initialize()
}

void initialize() {
}



input "deviceList", "capability.*", title: "Devices to use:", multiple: true, required: false, submitOnChange: true

@thebearmay : I knew of that technique of getting the list via drop-down, but what I'm trying to do is to get the list without having to do it through the "input" selection process. I.e., get the list without user intereraction.

I'm looking for something like the getChildDevices() method that you use in a driver to returns a list of child device wrappers, but rather than getting "child" devices, I wanted to get All Devices in the system - i.e., all parents and all of their child devices.

Basically, what I want to do is . . .

  1. Without user interaction, get the list of all devices as a List of
  2. Filter it
  3. And then use an "input" of type "enum" to present the filtered list, so the user can select from a pre-filtered list.

Anything like this? Thanks.

I don't believe this functionality is available to custom/user apps; they can only obtain references to devices the user has selected via an input.

1 Like

The method to do this is only available for our internal applications.

Reason for this being to prevent abuse by bad actors...

2 Likes

image
Stare deeply into the image and repeat after me... :smiley:

4 Likes

There's the really interesting approach @tony.fleisher does in his Z-Wave Mesh Details app. Under the "Grant access to z-wave devices" he has a check box to select all Z-Wave devices, which does an interesting dance to preselect the appropriate devices. It looks like it invokes an OAuth endpoint in his app which injects some JavaScript which seems to do a REST call to get all the device IDs and pre-select them.

1 Like

I guess my secret is out. It works because javascript can do lots of fun things on the pages and since the browser has login context to the hub, the requests to the hub all have authorization as the user that is viewing the page. To use hub endpoints without on page javascript (assuming hub security is enabled), you have to have settings with authorization details first; @dman2306 used this approach with HPM.

As for getting the list of devices by capability, the best way I know of for this is the device listJson endpoint: /device/listJson?capability=capability.*

2 Likes

@mike.maxwell It would be really useful to expose the getDevices() method, I don't see the issue as long as the user could authorize the app as having special privilege.

e.g. I'd like to create an 'Associations' app which calls AssociationGroupingsGet when setup (on configure()) to check what associations exists on all devices... Then use the app to manage all associations. I'm thinking a virtual device could handle all lifeline events of a particular group in scenarios where the logging of events is required / event attributes are useful for more complex automation.

It's still a bit of a half-baked idea but without getDevices(), the wonkiness you have to through via JavaScript like @tony.fleisher just makes me put off this project continuously...

Our concern for wildcard methods like this within user apps being its potential use by bad actors as these methods don't require any user interaction to extract possibly sensitive data.

1 Like