Finding Code for built in drivers

I use several IP-driven switches with a community HTTP driver which works fine.

However many of the devices need a "button or momentary on action" in the same way as the built-in virtual switch driver.

By way of a learning exercise, I want to try and modify the driver myself.

Question: where can I find the code for the built-in virtual switch driver so I can learn what I need to alter in my HTTP driver!

Thanks

Unfortunately the code for the built-in drivers are not made available. But if you need assistance with developing / enhancing a driver there are plenty of developers on this forum than can help with any questions you may have. If you want to ask a question though, perhaps post under another thread with a title relating to your question.

EDIT: By way of an answer I suggested you post in a different thread :slight_smile: ... Scheduling options may be something that you need to look at. I may be able to assist by documenting some of the approaches I have taken in my drivers, but there are also notes on other threads.

1 Like

Hubitat does publish a few sample drivers. HubitatPublic/examples/drivers at master · hubitat/HubitatPublic · GitHub

I am not a coder of any type, so I am not sure if these will be helpful or not.

2 Likes

I just threw this together quick (including some copy/paste from another driver I had already written :slight_smile: ), and it is likely more or less how Hubitat's internal virtual switch driver was written if you do still want to use that as a starting point. To be clear, this is a "reverse engineered" community driver that is similar (identical?) in functionality; as mentioned above, the built-in drivers are (mostly) all closed-source:

2 Likes

Thank you very much. This is exactly the functionality I am looking for. I think I can now integrate this into the HTTP switch driver.

Again, Much appreciated.

Neil

There are a ton of community drivers and most are posted publicly. So you can always base a driver off what someone has written before and most developers are totally happy as long as you give them credit.

For example, I usually have a Thanks section at the end of my opening comments just before the actual code starts.

1 Like

With the help I got from Robert Morris I was able to make the changes I wanted to the HTTP Driver I currently use. It's now tested and working just fine.

I am just posting it here in case anyone else finds it useful.

/*
* ======================  Virtual Switch (Community Driver) ==========================
*
*  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
*  in compliance with the License. You may obtain a copy of the License at:
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
*  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
*  for the specific language governing permissions and limitations under the License.
*
* =======================================================================================
*
*  PLATFORM: Hubitat
*  PURPOSE: HTTP Switch including "auto-off" functionality
*  Last modified: 2021-06-29
*
*  Changelog:
*  v1.0    - Initial Release - Thanks to Robert Morris for his guidance in getting this to work.
*/

metadata {
    definition(name: "HTTP Switch with Auto Off", namespace: "community", author: "Robert Morris/N Vass") {
        capability "Actuator"
        capability "Switch"

    }
}

preferences {
    section("URIs") {
        input "onURI", "text", title: "On URI", required: false
        input "offURI", "text", title: "Off URI", required: false
        input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: true
        input name: "autoOff", type: "enum", title: "Auto off",
            options: [[0:"Never"],[1:"1 second"],[2:"2 seconds"],[5:"5 seconds"],[10:"10 seconds"],[30:"30 seconds"]]
    }
}

void installed() {
log.debug "installed()"
}

void updated() {
log.debug "updated()"
}


def on() {

    if (logEnable) log.debug "Sending on GET request to [${settings.onURI}]"

    try {
            httpGet(settings.onURI) { resp ->
            
                if (resp.success) {
                sendEvent(name: "switch", value: "on", isStateChange: true)
                }
                
                if (logEnable)
                
                if (resp.data) log.debug "${resp.data}"
        }
    }
                
                catch (Exception e) {
                log.warn "Call to on failed: ${e.message}"
                }
    
            if (autoOff) {
                Integer disableTime = autoOff as Integer
                    if (disableTime > 0) {
                        runIn(disableTime,"off")
                        }
                }
}


def off() {
    
    if (logEnable) log.debug "Sending off GET request to [${settings.offURI}]"

    try {
            httpGet(settings.offURI) { resp ->
                
                if (resp.success) {
                sendEvent(name: "switch", value: "off", isStateChange: true)
                }
                
                if (logEnable)
                
                if (resp.data) log.debug "${resp.data}"
        }
    }
    
                catch (Exception e) {
                log.warn "Call to off failed: ${e.message}"
                }
    
}
2 Likes