balboa_worldwide_app 1.2.5 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/bwa_mqtt_bridge DELETED
@@ -1,283 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'mqtt'
4
- require 'sd_notify'
5
- require 'set'
6
-
7
- require 'bwa/client'
8
- require 'bwa/discovery'
9
-
10
- class MQTTBridge
11
- def initialize(mqtt_uri, bwa, device_id: "bwa", base_topic: "homie")
12
- @base_topic = "#{base_topic}/#{device_id}"
13
- @mqtt = MQTT::Client.new(mqtt_uri)
14
- @mqtt.set_will("#{@base_topic}/$state", "lost", true)
15
- @mqtt.connect
16
- @bwa = bwa
17
- @attributes = {}
18
- @things = Set.new
19
-
20
- publish_basic_attributes
21
-
22
- # Tell systemd we've started up OK. Ignored if systemd not in use.
23
- SdNotify.ready
24
-
25
- bwa_thread = Thread.new do
26
- loop do
27
- begin
28
- message = @bwa.poll
29
- next if message.is_a?(BWA::Messages::Ready)
30
-
31
- puts message.inspect unless message.is_a?(BWA::Messages::Status)
32
- case message
33
- when BWA::Messages::ControlConfiguration
34
- publish("spa/$type", message.model)
35
- when BWA::Messages::ControlConfiguration2
36
- message.pumps.each_with_index do |speed, i|
37
- publish_pump(i + 1, speed) if speed != 0
38
- end
39
- message.lights.each_with_index do |exists, i|
40
- publish_thing("light", i + 1) if exists
41
- end
42
- message.aux.each_with_index do |exists, i|
43
- publish_thing("aux", i + 1) if exists
44
- end
45
- publish_mister if message.mister
46
- publish_blower(message.blower) if message.blower != 0
47
- publish_circpump if message.circ_pump
48
- publish("$state", "ready")
49
- when BWA::Messages::Status
50
- @bwa.request_control_info unless @bwa.last_control_configuration
51
- @bwa.request_control_info2 unless @bwa.last_control_configuration2
52
-
53
- # make sure time is in sync
54
- now = Time.now
55
- now_minutes = now.hour * 60 + now.min
56
- spa_minutes = message.hour * 60 + message.minute
57
- # check the difference in both directions
58
- diff = [(spa_minutes - now_minutes) % 1440, 1440 - (spa_minutes - now_minutes) % 1440].min
59
-
60
- # allow a skew of 1 minute, since the seconds will always be off
61
- if diff > 1
62
- @bwa.set_time(now.hour, now.min, message.twenty_four_hour_time)
63
- end
64
- publish_attribute("spa/priming", message.priming)
65
- publish_attribute("spa/heatingmode", message.heating_mode)
66
- publish_attribute("spa/temperaturescale", message.temperature_scale)
67
- publish_attribute("spa/24htime", message.twenty_four_hour_time)
68
- publish_attribute("spa/heating", message.heating)
69
- publish_attribute("spa/temperaturerange", message.temperature_range)
70
- publish_attribute("spa/currenttemperature", message.current_temperature)
71
- publish_attribute("spa/currenttemperature/$unit", "º#{message.temperature_scale.to_s[0].upcase}")
72
- publish_attribute("spa/settemperature", message.set_temperature)
73
- publish_attribute("spa/settemperature/$unit", "º#{message.temperature_scale.to_s[0].upcase}")
74
- if message.temperature_scale == :celsius
75
- publish_attribute("spa/currenttemperature/$format", message.temperature_range == :high ? "26:40" : "10:26")
76
- publish_attribute("spa/settemperature/$format", message.temperature_range == :high ? "26:40" : "10:26")
77
- else
78
- publish_attribute("spa/currenttemperature/$format", message.temperature_range == :high ? "80:104" : "26:40")
79
- publish_attribute("spa/settemperature/$format", message.temperature_range == :high ? "80:104" : "26:40")
80
- end
81
- publish_attribute("spa/filter1", message.filter[0])
82
- publish_attribute("spa/filter2", message.filter[1])
83
-
84
- publish_attribute("spa/circpump", message.circ_pump) if @bwa.last_control_configuration2&.circ_pump
85
- publish_attribute("spa/blower", message.blower) if @bwa.last_control_configuration2&.blower.to_i != 0
86
- publish_attribute("spa/mister", message.mister) if @bwa.last_control_configuration2&.mister
87
- (0..5).each do |i|
88
- publish_attribute("spa/pump#{i + 1}", message.pumps[i]) if @bwa.last_control_configuration2&.pumps&.[](i).to_i != 0
89
- end
90
- (0..1).each do |i|
91
- publish_attribute("spa/light#{i + 1}", message.lights[i]) if @bwa.last_control_configuration2&.lights&.[](i)
92
- end
93
- (0..1).each do |i|
94
- publish_attribute("spa/aux#{i + 1}", message.lights[i]) if @bwa.last_control_configuration2&.aux&.[](i)
95
- end
96
-
97
- # Tell systemd we are still alive and kicking. Ignored if systemd not in use.
98
- SdNotify.watchdog
99
- end
100
- end
101
- end
102
- end
103
-
104
- @mqtt.get do |topic, value|
105
- puts "got #{value.inspect} at #{topic}"
106
- case topic[@base_topic.length + 1..-1]
107
- when "spa/heatingmode/set"
108
- next @bwa.toggle_heating_mode if value == 'toggle'
109
- next unless %w{ready rest}.include?(value)
110
- @bwa.set_heating_mode(value.to_sym)
111
- when "spa/temperaturescale/set"
112
- next unless %w{fahrenheit celsius}.include?(value)
113
- @bwa.set_temperature_scale(value.to_sym)
114
- when "spa/24htime/set"
115
- next unless %w{true false}.include?(value)
116
- now = Time.now
117
- @bwa.set_time(now.hour, now.min, value == 'true')
118
- when "spa/temperaturerange/set"
119
- next @bwa.toggle_temperature_range if value == 'toggle'
120
- next unless %w{low high}.include?(value)
121
- @bwa.set_temperature_range(value.to_sym)
122
- when %r{^spa/pump([1-6])/set$}
123
- next @bwa.toggle_pump($1.to_i) if value == 'toggle'
124
- @bwa.set_pump($1.to_i, value.to_i)
125
- when %r{^spa/(light|aux)([12])/set$}
126
- next @bwa.send(:"toggle_#{$1}", $2.to_i) if value == 'toggle'
127
- next unless %w{true false}.include?(value)
128
- @bwa.send(:"set_#{$1}", $2.to_i, value == 'true')
129
- when "spa/mister/set"
130
- next @bwa.toggle_mister if value == 'toggle'
131
- next unless %w{true false}.include?(value)
132
- @bwa.set_mister(value == 'true')
133
- when "spa/blower/set"
134
- next @bwa.toggle_blower if value == 'toggle'
135
- @bwa.set_blower(value.to_i)
136
- when "spa/settemperature/set"
137
- @bwa.set_temperature(value.to_f)
138
- end
139
- end
140
- end
141
-
142
- def publish(topic, value)
143
- @mqtt.publish("#{@base_topic}/#{topic}", value, true)
144
- end
145
-
146
- def publish_attribute(attr, value)
147
- if !@attributes.key?(attr) || @attributes[attr] != value
148
- publish(attr, value.to_s)
149
- @attributes[attr] = value
150
- end
151
- end
152
-
153
- def subscribe(topic)
154
- @mqtt.subscribe("#{@base_topic}/#{topic}")
155
- end
156
-
157
- def publish_basic_attributes
158
- publish("$homie", "4.0.0")
159
- publish("$name", "BWA Spa")
160
- publish("$state", "init")
161
- publish("$nodes", "spa")
162
-
163
- publish("spa/$name", "BWA Spa")
164
- publish("spa/$type", "spa")
165
- publish_nodes
166
-
167
- publish("spa/priming/$name", "Is the pump priming")
168
- publish("spa/priming/$datatype", "boolean")
169
-
170
- publish("spa/heatingmode/$name", "Current heating mode")
171
- publish("spa/heatingmode/$datatype", "enum")
172
- publish("spa/heatingmode/$format", "ready,rest,ready_in_rest")
173
- publish("spa/heatingmode/$settable", "true")
174
- subscribe("spa/heatingmode/set")
175
-
176
- publish("spa/temperaturescale/$name", "Temperature scale")
177
- publish("spa/temperaturescale/$datatype", "enum")
178
- publish("spa/temperaturescale/$format", "fahrenheit,celsius")
179
- publish("spa/temperaturescale/$settable", "true")
180
- subscribe("spa/temperaturescale/set")
181
-
182
- publish("spa/24htime/$name", "Clock is 24 hour time")
183
- publish("spa/24htime/$datatype", "boolean")
184
- publish("spa/24htime/$settable", "true")
185
- subscribe("spa/24htime/set")
186
-
187
- publish("spa/heating/$name", "Heater is currently running")
188
- publish("spa/heating/$datatype", "boolean")
189
-
190
- publish("spa/temperaturerange/$name", "Current temperature range")
191
- publish("spa/temperaturerange/$datatype", "enum")
192
- publish("spa/temperaturerange/$format", "high,low")
193
- publish("spa/temperaturerange/$settable", "true")
194
- subscribe("spa/temperaturerange/set")
195
-
196
- publish("spa/currenttemperature/$name", "Current temperature")
197
- publish("spa/currenttemperature/$datatype", "float")
198
-
199
- publish("spa/settemperature/$name", "Set Temperature")
200
- publish("spa/settemperature/$datatype", "float")
201
- publish("spa/settemperature/$settable", "true")
202
- subscribe("spa/settemperature/set")
203
-
204
- publish("spa/filter1/$name", "Filter cycle 1 is currently running")
205
- publish("spa/filter1/$datatype", "boolean")
206
-
207
- publish("spa/filter2/$name", "Filter cycle 2 is currently running")
208
- publish("spa/filter2/$datatype", "boolean")
209
- end
210
-
211
- def publish_pump(i, speeds)
212
- publish("spa/pump#{i}/$name", "Pump #{i} speed")
213
- publish("spa/pump#{i}/$datatype", "integer")
214
- publish("spa/pump#{i}/$format", "0:#{speeds}")
215
- publish("spa/pump#{i}/$settable", "true")
216
- subscribe("spa/pump#{i}/set")
217
-
218
- @things << "pump#{i}"
219
- publish_nodes
220
- end
221
-
222
- def publish_thing(type, i)
223
- publish("spa/#{type}#{i}/$name", "#{type} #{i}")
224
- publish("spa/#{type}#{i}/$datatype", "boolean")
225
- publish("spa/#{type}#{i}/$settable", "true")
226
- subscribe("spa/#{type}#{i}/set")
227
-
228
- @things << "#{type}#{i}"
229
- publish_nodes
230
- end
231
-
232
- def publish_mister
233
- publish("spa/mister/$name", type)
234
- publish("spa/mister/$datatype", "boolean")
235
- publish("spa/mister/$settable", "true")
236
- subscribe("spa/mister/set")
237
-
238
- @things << "mister"
239
- publish_nodes
240
- end
241
-
242
- def publish_blower(speeds)
243
- publish("spa/blower/$name", "Blower speed")
244
- publish("spa/blower/$datatype", "integer")
245
- publish("spa/blower/$format", "0:#{speeds}")
246
- publish("spa/blower/$settable", "true")
247
- subscribe("spa/blower/set")
248
-
249
- @things << "blower"
250
- publish_nodes
251
- end
252
-
253
- def publish_circpump
254
- publish("spa/circpump/$name", "Circ pump is currently running")
255
- publish("spa/circpump/$datatype", "boolean")
256
- @things << "circpump"
257
-
258
- publish_nodes
259
- end
260
-
261
- def publish_nodes
262
- publish("spa/$properties", (["priming,heatingmode,temperaturescale,24htime,heating,temperaturerange,currenttemperature,settemperature,filter1,filter2"] + @things.to_a).join(','))
263
- end
264
- end
265
-
266
- mqtt_uri = ARGV.shift
267
-
268
- if ARGV.empty?
269
- spas = BWA::Discovery.discover
270
- if spas.empty?
271
- $stderr.puts "Could not find spa!"
272
- exit 1
273
- end
274
- spa_ip = "tcp://#{spas.first.first}/"
275
- else
276
- spa_ip = ARGV[0]
277
- end
278
-
279
- spa = BWA::Client.new(spa_ip)
280
-
281
- spa.request_configuration
282
-
283
- MQTTBridge.new(mqtt_uri, spa)