Is it possible to check the IP string is valid when saving the driver preferences ?
I'm thinking so using constraints but not sure of the syntax or if it's even possible.
Is it possible to check the IP string is valid when saving the driver preferences ?
I'm thinking so using constraints but not sure of the syntax or if it's even possible.
Beyond the basic input restrictions it does not appear there would be any way to test if it is valid while saving.
You could have part of update() test the IP however you want, then throw an error if it is invalid (possibly with a state variable or some sort of relevant event so it would be noticeable).
Not normally done, but it can be done in a series of checks in method updated:
> metadata {
> definition (name: "zTEST",
> namespace: "davegut",
> author: "Dave Gutheinz"
> ) {
> }
> preferences {
> input ("ipAddress", "text", title: "Device IP")
> }
> }
> def installed() {
> updated()
> }
> def updated() {
> // Most common error is spaces. Here we correct entered IP address, removing spaces.
> if (ipAddress != ipAddress.replace(" ", "")) {
> log.info "Updating IP address to remove spaces"
> device.updateSetting("ipAddress", [type:"text", value:ipAddress.replace(" ", "")])
> }
> // Will now run the IP address against a function that converts it to hex.
> // If this function fails, then the basic format is incorrect.
> // If an error, we exit updated and generate an error.
> try {
> String ipTest = ipAddress.tokenize( '.' ).collect { String.format( '%02x', it.toInteger() ) }.join()
> } catch (error) {
> log.error "IP Address Format Error: IP address is not in proper format. Update aborted."
> return
> }
> }