This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
forked from pimatic/pimatic-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
switchbot.coffee
129 lines (110 loc) · 4.55 KB
/
switchbot.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
# #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 what 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 envirement 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'
# Require the [cassert library](https://github.com/rhoot/cassert).
assert = env.require 'cassert'
got = require 'got'
BaseUrl = 'https://api.switch-bot.com/v1.0'
# Include your own depencies with nodes global require function:
#
# someThing = require 'someThing'
#
# ###Switchbot class
# Create a class that extends the Plugin class and implements the following functions:
class SwitchBotPlugin 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) =>
if(@config.debug)
env.logger.debug('Init of pimatic-switchbot')
deviceConfigDef = require('./device-config-schema')
@framework.deviceManager.registerDeviceClass("SwitchBot", {
configDef: deviceConfigDef.SwitchBot,
createCallback: (config) =>
return new SwitchBotDevice(config, this)
})
# auto discovery
@framework.deviceManager.on('discover', (eventData) =>
@framework.deviceManager.discoverMessage(
'pimatic-switchbot', "Scanning for SwitchBot devices"
)
options = {
'headers': {'Authorization': @config.apiKey}
}
#got(BaseUrl + '/devices', options).json()
got(BaseUrl + '/devices/', options).json()
.then (result) =>
if(@config.debug)
env.logger.debug(JSON.stringify(result));
if(result.statusCode != 100)
throw new Error(result.message + ' ' + result.statusCode)
for device in result.body.deviceList
if(device.deviceType == 'Bot')
config = {
class: 'SwitchBot',
id: "bot-" + device.deviceId,
botId: device.deviceId
}
@framework.deviceManager.discoveredDevice('pimatic-switchbot',
"SwitchBot " + device.deviceName + " (#" + device.deviceId + ")", config
)
.catch (error) ->
env.logger.error("Error on searching switchbots " + error)
)
class SwitchBotDevice extends env.devices.SwitchActuator
constructor: (@config, @plugin) ->
@id = @config.id
@name = @config.name
@botId = @config.botId
super();
#Performs a press with the SwitchBot
changeStateTo: (state) ->
if(@config.debug)
env.logger.debug("SwitchBot " + @name + " (" + @botId + ") pressed.")
command = {
command: "press",
parameter: "default",
commandType: "command"
}
headers = {
'Authorization': @plugin.config.apiKey
}
options = {
'method': 'POST',
'json': {command: "press", parameter: "default", commandType: "command"},
'headers': {'Authorization': @plugin.config.apiKey},
responseType: 'json'
}
got(BaseUrl + '/devices/' + @botId + '/commands', options).json()
.then (result) =>
if(result.statusCode != 100)
throw new Error(result.message + ' ' + result.statusCode)
Promise.resolve(result.body)
# .then (body) =>
# env.logger.info('Press command sent')
# Promise.resolve()
destroy: () ->
super()
# ###Finally
# Create a instance of my plugin
switchBotPlugin = new SwitchBotPlugin
# and return it to the framework.
return switchBotPlugin