-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathted5000.groovy
134 lines (113 loc) · 4.33 KB
/
ted5000.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
preferences {
input("deviceIP", "string", title:"IP Address", description: "IP Address", required: true, displayDuringSetup: true)
}
metadata {
// Automatically generated. Make future change here.
definition (name: "TED5000", author: "swap-file") {
capability "Energy Meter"
capability "Polling"
capability "Refresh"
capability "Sensor"
capability "Power Meter"
}
// UI tile definitions
tiles (scale: 2){
valueTile( "power", "device.power", width: 4, height:4) {
state( "device.power",label:'${currentValue} W', backgroundColors:[
[value: 200, color: "#153591"],
[value: 400, color: "#1e9cbb"],
[value: 600, color: "#90d2a7"],
[value: 700, color: "#44b621"],
[value: 1000, color: "#f1d801"],
[value: 1200, color: "#d04e00"],
[value: 1400, color: "#bc2323"]
]
)
}
valueTile( "voltage", "device.voltage",width: 2, height: 1 ) {
state("device.voltage", label:'${currentValue} Volts' )
}
valueTile( "power_factor", "device.power_factor",width: 2, height: 1 ) {
state("device.power_factor", label:'PF: ${currentValue}%' )
}
valueTile("refresh", "command.refresh",width: 2, height: 2) {
state "default", label:'refresh', action:"refresh.refresh", icon:"st.secondary.refresh-icon"
}
valueTile( "daily_max_power","device.daily_max_power",width: 4,height: 1) {
state("device.daily_max_power", label:'Daily Max: ${currentValue} W')
}
valueTile( "daily_total_power","device.daily_total_power",width: 4,height: 1) {
state("device.daily_total_power", label:'Total: ${currentValue} kWh')
}
valueTile( "daily_min_power","device.daily_min_power",width: 4,height: 1) {
state("device.daily_min_power",label:'Daily Min: ${currentValue} w')
}
valueTile( "cost", "device.cost",width: 2, height: 1) {
state("device.cost", label:'\044${currentValue} CPH')
}
main(["power"])
details(["power","daily_max_power","daily_min_power","daily_total_power","refresh", "cost","voltage","power_factor"])
}
}
def poll() {
log.trace 'Poll Called'
runCmd()
}
def refresh() {
log.trace 'Refresh Called'
runCmd()
}
def runCmd() {
def host = deviceIP
def hosthex = convertIPtoHex(host).toUpperCase()
def LocalDevicePort = "80"
def porthex = convertPortToHex(LocalDevicePort).toUpperCase()
device.deviceNetworkId = "$hosthex:$porthex"
//log.debug "The device id configured is: $device.deviceNetworkId"
def headers = [:]
headers.put("HOST", "$host:$LocalDevicePort")
headers.put("Content-Type", "application/x-www-form-urlencoded")
//log.debug "The Header is $headers"
def path = '/api/LiveData.xml'
def body = ''
//log.debug "Uses which method: $DevicePostGet"
def method = "GET"
try {
log.debug "Making TED5000 request to $device.deviceNetworkId"
def hubAction = new physicalgraph.device.HubAction(
method: method,
path: path,
body: body,
headers: headers
)
hubAction.options = [outputMsgToS3:false]
//log.debug hubAction
hubAction
}
catch (Exception e) {
log.debug "Hit Exception $e on $hubAction"
}
}
private String convertIPtoHex(ipAddress) {
String hex = ipAddress.tokenize( '.' ).collect { String.format( '%02x', it.toInteger() ) }.join()
//log.debug "IP address entered is $ipAddress and the converted hex code is $hex"
return hex
}
private String convertPortToHex(port) {
String hexport = port.toString().format( '%04x', port.toInteger() )
//log.debug hexport
return hexport
}
def parse(String description) {
//this is automatically called when the hub action returns
log.debug "Got Reply"
def xml = parseLanMessage(description).xml
def evt1 = createEvent (name: "power", value: (xml.Power.Total.PowerNow).toInteger(), unit:"W")
def evt2 = createEvent (name: "voltage", value: ((xml.Voltage.Total.VoltageNow).toDouble() / 10.0), unit:"V")
def evt3 = createEvent (name: "cost", value: ((xml.Cost.Total.CostNow).toDouble() / 100.0), unit:"\044")
def evt4 = createEvent (name: "daily_max_power", value: (xml.Power.Total.PeakTdy).toInteger(), unit:"W")
def evt5 = createEvent (name: "daily_min_power", value: (xml.Power.Total.MinTdy).toInteger(), unit:"W")
def evt6 = createEvent (name: "daily_total_power", value: ((xml.Power.Total.PowerTDY).toDouble() / 1000.0).round(2), unit:"kWh")
def evt7 = createEvent (name: "power_factor", value: ((xml.Power.MTU1.PF).toDouble() / 10.0), unit:"%")
return [evt1, evt2,evt3,evt4,evt5,evt6,evt7]
}