/*
* AttributeTest
*
* Description:
* This Hubitat driver is designed for testing some attribute names.
*
* Features List:
* Ability to randomly generate some values to test attributes
*
* Licensing:
* Copyright 2022 David Snell
* 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.
*
* Version Control:
* 0.1.0 - Initial start of driver
*
* Thank you(s):
*/
// Returns the driver name
def DriverName(){
return "AttributeTest"
}
// Returns the driver version
def DriverVersion(){
return "0.1.0"
}
metadata{
definition( name: "AttributeTest", namespace: "Snell", author: "David Snell", importUrl: "https://www.drdsnell.com/projects/hubitat/drivers/AttributeTest.groovy" ){
//capability "Configuration"
capability "Refresh"
// Commands that have been implemented
// Attributes being built into the device
attribute "Driver Name", "string" // Driver identifies the driver being used for update purposes
attribute "Driver Version", "string" // Version number of the driver
attribute "Driver Version Status", "string" // Status of the driver version compared to what is currently published
attribute "Status", "string" // Used to provide general status of the device
attribute "Particles", "number"
attribute "Particles2", "number"
attribute "Particles3.", "number"
attribute "Particles4.5", "number"
attribute "Particles5_5", "number"
}
preferences{
section{
if( ShowAllPreferences || ShowAllPreferences == null ){
input( type: "enum", name: "LogType", title: "Enable Logging?", required: true, multiple: false, options: [ "None", "Info", "Debug", "Trace" ], defaultValue: "Info" )
input( type: "bool", name: "ShowAllPreferences", title: "Show All Preferences?", required: false, defaultValue: true )
} else {
input( type: "bool", name: "ShowAllPreferences", title: "Show All Preferences?", required: false, defaultValue: true )
}
}
}
}
// Called when preferences are saved
// This clears state variables then sets some basic information as well as does a reconfig of the device
def updated(){
Logging( "Saved preferences", 2 )
ProcessState( "Driver Name", "${ DriverName() }" )
ProcessState( "Driver Version", "${ DriverVersion() }" )
}
// refresh command
def refresh(){
Logging( "Refreshing device...", 2 )
ProcessEvent( "Particles", ( Math.random() * 10 ).round( 2 ) )
ProcessEvent( "Particles2", ( Math.random() * 10 ).round( 2 ) )
ProcessEvent( "Particles3.", ( Math.random() * 10 ).round( 2 ) )
ProcessEvent( "Particles4.5", ( Math.random() * 10 ).round( 2 ) )
ProcessEvent( "Particles5_5", ( Math.random() * 10 ).round( 2 ) )
}
// Configures the device, typically at install or when preferences are saved
def configure(){
Logging( "Configuring device...", 2 )
}
// installed is called when the device is installed, all it really does is run updated
def installed(){
Logging( "Installed", 2 )
updated()
}
// initialize is called when the device is initialized, all it really does is run updated
def initialize(){
Logging( "Initialized", 2 )
updated()
}
// Process data to check against current state value and then send an event if it has changed
def ProcessEvent( Variable, Value, Unit = null, ForceEvent = false ){
if( ( state."${ Variable }" != Value ) || ( ForceEvent == true ) ){
state."${ Variable }" = Value
if( Unit != null ){
Logging( "Event: ${ Variable } = ${ Value }${ Unit }", 4 )
sendEvent( name: "${ Variable }", value: Value, unit: Unit, isStateChanged: true )
} else {
Logging( "Event: ${ Variable } = ${ Value }", 4 )
sendEvent( name: "${ Variable }", value: Value, isStateChanged: true )
}
}
}
// Process data to check against current state value and then send an event if it has changed
def ProcessState( Variable, Value ){
if( state."${ Variable }" != Value ){
Logging( "State: ${ Variable } = ${ Value }", 4 )
state."${ Variable }" = Value
}
}
// Handles whether logging is enabled and thus what to put there.
def Logging( LogMessage, LogLevel ){
// Add all messages as info logging
if( ( LogLevel == 2 ) && ( LogType != "None" ) ){
log.info( "${ device.displayName } - ${ LogMessage }" )
} else if( ( LogLevel == 3 ) && ( ( LogType == "Debug" ) || ( LogType == "Trace" ) ) ){
log.debug( "${ device.displayName } - ${ LogMessage }" )
} else if( ( LogLevel == 4 ) && ( LogType == "Trace" ) ){
log.trace( "${ device.displayName } - ${ LogMessage }" )
} else if( LogLevel == 5 ){
log.error( "${ device.displayName } - ${ LogMessage }" )
}
}