My first attempt at doing a driver in Hubitat..
With some help here (thanks tomw!) I quickly got a trivial driver working for controlling my old x10 devices via a raw socket interface to the existing mochad server. for reference, it is pasted below.
But, I have some questions about the Raw Socket interface.
-
The signature for the connect method has no return. I'd expect a "handle" of some sort to be used for subsequent sendMessage invocations. I think this implies there can be one and only one raw socket connection?
-
When should the connect be done? I did it in a capability "Initialize", but I don't know if that makes sense, or when "Inititalize" will be called. For testing, I'm just clicking on Initialize where the device prefs and buttons for on and off are presented.
-
When should the close be done? I do a close in Initialize before doing the connect. Should I invoke close somewhere else?
-
There is no feedback about success when the connect is done. Should socketStatus I provided be called to indicate success or failure of the connection attempt?
-
parse IS called when a response is sent from the connected (mochad) server. When I log it, it looks like this:
dev:842021-05-27 08:58:39.053 am debug30352F32372030383A35383A333820547820524620486F757365556E69743A2041342046756E633A204F66660A
The response looks like this when I send the message using netcat (nc):
05/27 08:29:23 Tx RF HouseUnit: A4 Func: Off
So, apparently, parse is getting called with a string that is the ascii version of the string. Should I check the groovy docs for how to convert it, or should I use a different signature for my implementation of parse"
Thanks!
The driver code follows...
def version() {"v0.1"}
import hubitat.helper.InterfaceUtils
metadata {
definition (name: "X10 via MochaD", namespace: "Flying-Nerd", author: "Dave Thomas") {
capability "Initialize"
capability "Switch"
}
}
preferences {
input("ip", "text", title: "IP Address", description: "[IP Address of your mochad server", required: true)
input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: true
input name: "station", title:"X10 Station", description: "X10 Station ID"
input name :"port", title:"Mochad port", type:"text", description: "Port for Mocha Server", required:true
}
void updated() {
log.info "Updated..."
}
void installed() {
log.info "Installed..."
refresh()
}
def refresh() {
}
def Close() {
if (logEnable) log.debug "closing socket"
interfaces.rawSocket.close()
}
def on() {
if (logEnable) log.debug "Sending on request to [${settings.ip}], station ${settings.station}"
sendEvent(name: "switch", value: "on", isStateChange: true)
interfaces.rawSocket.sendMessage("rf ${settings.station} on\n")
//refresh()
}
def off() {
if (logEnable) log.debug "Sending off request to [${settings.ip}, station ${settings.station}]"
sendEvent(name: "switch", value: "off", isStateChange: true)
interfaces.rawSocket.sendMessage("rf ${settings.station} off\n")
//refresh()
}
def initialize() {
if (logEnable) log.info "initialize() called"
Close()
interfaces.rawSocket.connect(ip, port.toInteger())
//refresh()
}
def socketStatus(String message) {
if (logEnable) log.info "socketStatus() called"
log.info message
}
def parse(String description) {
if (logEnable) log.info "parse called"
if (logEnable) log.debug(description)
}