In a base driver, you can not turn capabilities / commands on/off with a active variable (i.e., one that can be accessed in preferences). You can do this is a driver-code variable (I do this to reduce my number of code files.) See below example from https://raw.githubusercontent.com/DaveGut/HubitatActive/master/KasaDevices/DeviceDrivers/Plug-Switch.groovy)
The def type() allows me to control several the availability of commands and capabilities within the drivers.
- Dimming Switch adds capabilities
- Switch Level
- Level Preset
- If Type contains "EM" the EM capabilities and attributes are added).
Nothing special here. However, this ONLY works on these static type calls. It does not work on getDataValue or anything else. It is all changes made to the driver file itself.
That being said, you could do a preference that creates / deletes children to add specific functions dependent on Scenario. For example, there is an RGBW light controller that could control a RGBW strip, a RGB strip + a Mono Strip, Two CT Strips, A CT Strip and two Mono Strips, or 4 Mono Strips. A preference would allow the creation of the correct children (if it could not be determined from the data from the device) (I actually ran into this example). There is so much else you can do, just develop a use case and the the logic flow. GOOD LUCK.
def driverVer() { return "6.3.2" }
def type() { return "Plug Switch" }
//def type() { return "Dimming Switch" }
//def type() { return "EM Plug" }
def file() {
def filename = type().replaceAll(" ", "-")
if (type() == "Dimming Switch") {
filename = "DimmingSwitch"
}
return filename
}
import groovy.json.JsonSlurper
metadata {
definition (name: "Kasa ${type()}",
namespace: "davegut",
author: "Dave Gutheinz",
importUrl: "https://raw.githubusercontent.com/DaveGut/HubitatActive/master/KasaDevices/DeviceDrivers/${file()}.groovy"
) {
capability "Switch"
if (type() == "Dimming Switch") {
capability "Switch Level"
capability "Level Preset"
}
capability "Actuator"
capability "Refresh"
command "setPollInterval", [[
name: "Poll Interval in seconds",
constraints: ["default", "5 seconds", "10 seconds", "15 seconds",
"30 seconds", "1 minute", "5 minutes", "10 minutes",
"30 minutes"],
type: "ENUM"]]
if (type().contains("EM")) {
capability "Power Meter"
capability "Energy Meter"
attribute "currMonthTotal", "number"
attribute "currMonthAvg", "number"
attribute "lastMonthTotal", "number"
attribute "lastMonthAvg", "number"
}
command "ledOn"
command "ledOff"
attribute "led", "string"
attribute "connection", "string"
attribute "commsError", "string"
}