-
Notifications
You must be signed in to change notification settings - Fork 4
/
tplink-smartplug.coffee
executable file
·236 lines (196 loc) · 7.44 KB
/
tplink-smartplug.coffee
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# #Plugin template
# This is an plugin template and mini tutorial for creating pimatic plugins. It will explain the
# basics of how the plugin system works and how a plugin should look like.
# ##The plugin code
# Your plugin must export a single function, that takes one argument and returns a instance of
# your plugin class. The parameter is an environment object containing all pimatic related functions
# and classes. See the [startup.coffee](http://sweetpi.de/pimatic/docs/startup.html) for details.
module.exports = (env) ->
# ###require modules included in pimatic
# To require modules that are included in pimatic use `env.require`. For available packages take
# a look at the dependencies section in pimatics package.json
# Require the bluebird promise library
Promise = env.require 'bluebird'
# Include you own dependencies with nodes global require function:
#
# someThing = require 'someThing'
#
TPlinkAPI = require 'tplink-smarthome-api'
class TPlinkSmartplug extends env.plugins.Plugin
# ####init()
# The `init` function is called by the framework to ask your plugin to initialise.
#
# #####params:
# * `app` is the [express] instance the framework is using.
# * `framework` the framework itself
# * `config` the properties the user specified as config for your plugin in the `plugins`
# section of the config.json file
init: (app, @framework, @config) =>
# get the device config schemas
deviceConfigDef = require("./device-config-schema")
env.logger.info("Starting pimatic-tplink-smartplug plugin")
@framework.deviceManager.registerDeviceClass("TPlinkHS100", {
configDef: deviceConfigDef.TPlinkHS100,
createCallback: (config, lastState) =>
return new TPlinkHS100(config, @, lastState)
})
@framework.deviceManager.registerDeviceClass("TPlinkHS110", {
configDef: deviceConfigDef.TPlinkHS110,
createCallback: (config, lastState) =>
return new TPlinkHS110(config, @, lastState)
})
@framework.deviceManager.on 'discover', () =>
@framework.deviceManager.discoverMessage(
'pimatic-tplink-smartplug', "Searching for devices"
)
TPlinkAPIinstance = new TPlinkAPI.Client();
TPlinkAPIinstance.startDiscovery().on 'plug-new', (plug) =>
#fetch the info for the switch
plug.getInfo().then (data)=>
config =
class: if /HS100/.test(data.sysInfo.model) then 'TPlinkHS100' else 'TPlinkHS110',
name: data.sysInfo.alias,
id: "#{data.sysInfo.alias.replace(/[^a-zA-Z0-9\-]/g,"")}-#{data.sysInfo.mac.replace(/[^a-zA-Z0-9\-]/g,"-")}",
ip: plug.host,
interval: 60
@framework.deviceManager.discoveredDevice(
'pimatic-tplink-smartplug', "#{config.name}@#{config.ip}", config
)
class TPlinkBaseDevice extends env.devices.PowerSwitch
#
constructor: (@config, @plugin, lastState) ->
@name = @config.name
@id = @config.id
@ip = @config.ip
@interval = 1000 * @config.interval
@requestPromise = Promise.resolve()
env.logger.warn "#{JSON.stringify(@attributes)}"
@plugConfig =
host: @ip
client = new TPlinkAPI.Client();
@plugInstance = client.getPlug(@plugConfig);
super()
@updateValues()
destroy: () ->
clearTimeout(@timeoutId) if @timeoutId?
@requestPromise.cancel() if @requestPromise?
super()
wrapPromise: (aPromise) ->
@requestPromise.reflect().then( () =>
@requestPromise = new Promise (resolve, reject) =>
aPromise
.then (data) =>
resolve data if not @requestPromise.isCancelled()
.catch (err) =>
reject err if not @requestPromise.isCancelled()
)
updateValues: () =>
if @timeoutId
clearTimeout(@timeoutId)
@timeoutId = null
@fetchValues().finally( =>
if @config.interval > 0
@timeoutId = setTimeout(( =>
@timeoutId = null
@updateValues()
), @interval)
).catch( =>
# ignore error silently, avoid unhandled rejection error
)
fetchValues: () ->
@getState()
getState: () ->
env.logger.debug "getting state"
@wrapPromise(@plugInstance.getPowerState()).then((powerState) =>
env.logger.debug "state is #{powerState}"
@_setState powerState
return Promise.resolve @_state
).catch((error) =>
msg = "Unable to get power state of device: " + error.toString()
env.logger.error(msg)
Promise.reject msg
)
changeStateTo: (state) ->
env.logger.debug "setting state to #{state}"
@wrapPromise(@plugInstance.setPowerState(state)).then(() =>
env.logger.debug "setting state success"
@_setState(state)
).catch((error) =>
msg = "Unable to set power state of device: " + error.toString()
env.logger.error(msg)
Promise.reject msg
)
class TPlinkHSConsumption extends TPlinkBaseDevice
attributes:
state:
description: "Current State"
type: "boolean"
labels: ['on', 'off']
watt:
description: "The measured wattage"
type: "number"
unit: 'W'
voltage:
description: "The measured voltage"
type: "number"
unit: 'V'
displaySparkline: false
current:
description: "The current in Ampere"
type: "number"
unit: 'A'
displaySparkline: false
total:
description: "kWh total"
type: "number"
unit: 'kWh'
acronym: 'Total'
displaySparkline: false
constructor: (@config, @plugin, lastState) ->
@name = @config.name
@id = @config.id
@ip = @config.ip
@_watt = lastState?.watt?.value
@_voltage = lastState?.voltage?.value
@_current = lastState?.current?.value
@_total = lastState?.total?.value
@interval = 1000 * @config.interval
@plugConfig =
host: @ip
super(@config, @plugin, lastState)
destroy: () ->
super()
fetchValues: () ->
Promise.all([
@getState()
@getConsumption()
])
getConsumption: () ->
env.logger.debug "getting consumption"
@wrapPromise(@plugInstance.emeter.getRealtime()).then((realtime) =>
env.logger.debug "consumption data is", realtime
@_watt = Math.round(realtime.power ? realtime.power_mw / 1000)
@emit "watt", @_watt
@_voltage = Math.round(realtime.voltage ? realtime.voltage_mv / 1000)
@emit "voltage", @_voltage
@_current = realtime.current ? realtime.current_ma / 1000
@emit "current", @_current
@_total = realtime.total ? realtime.total_wh / 1000
@emit "total", @_total
Promise.resolve()
).catch((error) =>
msg = "Unable to get consumption of device: " + error.toString()
env.logger.error(msg)
Promise.reject msg
)
getWatt: -> Promise.resolve @_watt
getVoltage: -> Promise.resolve @_voltage
getCurrent: -> Promise.resolve @_current
getTotal: -> Promise.resolve @_total
class TPlinkHS100 extends TPlinkBaseDevice
class TPlinkHS110 extends TPlinkHSConsumption
# ###Finally
# Create a instance of my plugin
myTPlinkSmartplug = new TPlinkSmartplug
# and return it to the framework.
return myTPlinkSmartplug