rspec-openhab-scripting 0.0.18-java → 0.0.19-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rspec/openhab/core/osgi.rb +2 -0
- data/lib/rspec/openhab/dsl/imports.rb +2 -0
- data/lib/rspec/openhab/helpers.rb +304 -0
- data/lib/rspec/openhab/hooks.rb +26 -13
- data/lib/rspec/openhab/version.rb +1 -1
- data/lib/rspec-openhab-scripting.rb +0 -18
- metadata +2 -3
- data/lib/rspec/openhab/items.rb +0 -220
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea386c264164b6c12be46d5483fdd349c3e5fdadcba22bff4987a2db5c6a9803
|
4
|
+
data.tar.gz: 20c364d137e43d5de1a1ab5eda9341318132cbde7532cf9c84901badce83ed49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13ae42f75b1bcaa898969e4077cefd1bbbc0979a7a081b7e904db9ff2c0b9550205bef2f4d20e3c502c2d12c449b64886819622101cb4425ee8ce4083cc02ea8
|
7
|
+
data.tar.gz: dd85b3c9249e88031ed3bf297dd09c1e7698cdb6d0958b04b0efbe35117dc6c71043b22f258b49fa0b52bb58a98ff3c3d5e6e492c6f551c393ce5a16187e9f75
|
@@ -452,6 +452,8 @@ module OpenHAB
|
|
452
452
|
|
453
453
|
rs = org.openhab.core.internal.service.ReadyServiceImpl.new
|
454
454
|
re = org.openhab.core.automation.internal.RuleEngineImpl.new(mtr, rr, ss, rs)
|
455
|
+
OpenHAB::Core::OSGI.register_service(re)
|
456
|
+
|
455
457
|
# overwrite thCallbacks to one that will spy to remove threading
|
456
458
|
field = re.class.java_class.declared_field("thCallbacks")
|
457
459
|
field.accessible = true
|
@@ -1,8 +1,51 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module OpenHAB
|
4
|
+
module Transform
|
5
|
+
class << self
|
6
|
+
def add_script(modules, script)
|
7
|
+
full_name = modules.join("/")
|
8
|
+
name = modules.pop
|
9
|
+
(@scripts ||= {})[full_name] = engine_factory.script_engine.compile(script)
|
10
|
+
|
11
|
+
mod = modules.inject(self) { |m, n| m.const_get(n, false) }
|
12
|
+
mod.singleton_class.define_method(name) do |input, **kwargs|
|
13
|
+
Transform.send(:transform, full_name, input, kwargs)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def engine_factory
|
20
|
+
@engine_factory ||= org.jruby.embed.jsr223.JRubyEngineFactory.new
|
21
|
+
end
|
22
|
+
|
23
|
+
def transform(name, input, kwargs)
|
24
|
+
script = @scripts[name]
|
25
|
+
ctx = script.engine.context
|
26
|
+
ctx.set_attribute("input", input.to_s, javax.script.ScriptContext::ENGINE_SCOPE)
|
27
|
+
kwargs.each do |(k, v)|
|
28
|
+
ctx.set_attribute(k.to_s, v.to_s, javax.script.ScriptContext::ENGINE_SCOPE)
|
29
|
+
end
|
30
|
+
script.eval
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
3
36
|
module RSpec
|
4
37
|
module OpenHAB
|
5
38
|
module Helpers
|
39
|
+
module BindingHelper
|
40
|
+
def add_kwargs_to_current_binding(binding, kwargs)
|
41
|
+
kwargs.each { |(k, v)| binding.local_variable_set(k, v) }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private_constant :BindingHelper
|
46
|
+
|
47
|
+
singleton_class.include(Helpers)
|
48
|
+
|
6
49
|
def autoupdate_all_items
|
7
50
|
@autoupdated_items ||= {}
|
8
51
|
$ir.for_each do |_provider, item|
|
@@ -31,8 +74,151 @@ module RSpec
|
|
31
74
|
thing.handler.callback.channel_triggered(nil, channel, event)
|
32
75
|
end
|
33
76
|
|
77
|
+
def populate_items_from_api
|
78
|
+
api = ::OpenHAB::DSL::Imports.api
|
79
|
+
all_items = api.items
|
80
|
+
|
81
|
+
item_factory = org.openhab.core.library.CoreItemFactory.new
|
82
|
+
|
83
|
+
all_items.each do |item_json|
|
84
|
+
full_type = item_json["type"]
|
85
|
+
name = item_json["name"]
|
86
|
+
|
87
|
+
type, _dimension = full_type.split(":")
|
88
|
+
if type == "Group"
|
89
|
+
base_item = item_factory.create_item(item_json["groupType"], name) if item_json["groupType"]
|
90
|
+
if item_json["function"]
|
91
|
+
dto = org.openhab.core.items.dto.GroupFunctionDTO.new
|
92
|
+
dto.name = item_json.dig("function", "name")
|
93
|
+
dto.params = item_json.dig("function", "params")
|
94
|
+
function = org.openhab.core.items.dto.ItemDTOMapper.map_function(base_item, dto)
|
95
|
+
end
|
96
|
+
item = GroupItem.new(name, base_item, function)
|
97
|
+
else
|
98
|
+
item = item_factory.create_item(full_type, name)
|
99
|
+
end
|
100
|
+
|
101
|
+
item.label = item_json["label"]
|
102
|
+
item_json["tags"].each do |tag|
|
103
|
+
item.add_tag(tag)
|
104
|
+
end
|
105
|
+
item_json["metadata"]&.each do |key, config|
|
106
|
+
item.meta[key] = config["value"], config["config"]
|
107
|
+
end
|
108
|
+
item.meta["stateDescription"] = item_json["stateDescription"] if item_json["stateDescription"]
|
109
|
+
item.category = item_json["category"] if item_json["category"]
|
110
|
+
|
111
|
+
$ir.add(item)
|
112
|
+
|
113
|
+
next unless item.meta["channel"]&.value
|
114
|
+
|
115
|
+
channel_uid = org.openhab.core.thing.ChannelUID.new(item.meta["channel"].value)
|
116
|
+
channel = $things.get_channel(channel_uid)
|
117
|
+
next unless channel
|
118
|
+
|
119
|
+
link = org.openhab.core.thing.link.ItemChannelLink.new(item.name, channel_uid)
|
120
|
+
Core::Mocks::ItemChannelLinkProvider.instance.add(link)
|
121
|
+
end
|
122
|
+
all_items.each do |item_json| # rubocop:disable Style/CombinableLoops
|
123
|
+
item_json["groupNames"].each do |group_name|
|
124
|
+
next unless (group = $ir.get(group_name))
|
125
|
+
|
126
|
+
group.add_member($ir.get(item_json["name"]))
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def populate_things_from_api
|
132
|
+
api = ::OpenHAB::DSL::Imports.api
|
133
|
+
populate_channel_types_from_api(api)
|
134
|
+
populate_thing_types_from_api(api)
|
135
|
+
|
136
|
+
thing_type_registry = ::OpenHAB::Core::OSGI.service("org.openhab.core.thing.type.ThingTypeRegistry")
|
137
|
+
|
138
|
+
api.things.each do |thing_json|
|
139
|
+
uid = org.openhab.core.thing.ThingUID.new(thing_json["UID"])
|
140
|
+
type_uid = org.openhab.core.thing.ThingTypeUID.new(thing_json["thingTypeUID"])
|
141
|
+
bridge_uid = org.openhab.core.thing.ThingUID.new(thing_json["bridgeUID"]) if thing_json["bridgeUID"]
|
142
|
+
|
143
|
+
type = thing_type_registry.get_thing_type(type_uid)
|
144
|
+
klass = if type.is_a?(org.openhab.core.thing.type.BridgeType)
|
145
|
+
org.openhab.core.thing.binding.builder.BridgeBuilder
|
146
|
+
else
|
147
|
+
org.openhab.core.thing.binding.builder.ThingBuilder
|
148
|
+
end
|
149
|
+
builder = klass.create(type_uid, uid)
|
150
|
+
builder.with_bridge(bridge_uid) if bridge_uid
|
151
|
+
|
152
|
+
thing_json.each do |(k, v)|
|
153
|
+
case k
|
154
|
+
when "UID", "thingTypeUID", "bridgeUID", "statusInfo", "editable"
|
155
|
+
nil
|
156
|
+
when "channels"
|
157
|
+
builder.with_channels(v.map { |c| build_channel(c) })
|
158
|
+
when "configuration"
|
159
|
+
builder.with_configuration(org.openhab.core.config.core.Configuration.new(v))
|
160
|
+
else
|
161
|
+
builder.send(:"with_#{k}", v)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
$things.add(builder.build)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def load_rules
|
170
|
+
automation_path = "#{org.openhab.core.OpenHAB.config_folder}/automation/jsr223/ruby/personal"
|
171
|
+
|
172
|
+
RSpec::OpenHAB::SuspendRules.suspend_rules do
|
173
|
+
Dir["#{automation_path}/*.rb"].each do |f|
|
174
|
+
load f
|
175
|
+
rescue Exception => e
|
176
|
+
warn "Failed loading #{f}: #{e.inspect}"
|
177
|
+
warn e.backtrace
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
def load_transforms
|
183
|
+
transform_path = "#{org.openhab.core.OpenHAB.config_folder}/transform"
|
184
|
+
Dir["#{transform_path}/**/*.script"].each do |filename|
|
185
|
+
script = File.read(filename)
|
186
|
+
next unless ruby_file?(script)
|
187
|
+
|
188
|
+
filename.slice!(0..transform_path.length)
|
189
|
+
dir = File.dirname(filename)
|
190
|
+
modules = dir == "." ? [] : moduleize(dir)
|
191
|
+
basename = File.basename(filename)
|
192
|
+
method = basename[0...-7]
|
193
|
+
modules << method
|
194
|
+
::OpenHAB::Transform.add_script(modules, script)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
34
198
|
private
|
35
199
|
|
200
|
+
EMACS_MODELINE_REGEXP = /# -\*-(.+)-\*-/.freeze
|
201
|
+
|
202
|
+
def parse_emacs_modeline(line)
|
203
|
+
line[EMACS_MODELINE_REGEXP, 1]
|
204
|
+
&.split(";")
|
205
|
+
&.map(&:strip)
|
206
|
+
&.map { |l| l.split(":", 2).map(&:strip).tap { |a| a[1] ||= nil } }
|
207
|
+
&.to_h
|
208
|
+
end
|
209
|
+
|
210
|
+
def ruby_file?(script)
|
211
|
+
# check the first 1KB for an emacs magic comment
|
212
|
+
script[0..1024].split("\n").any? { |line| parse_emacs_modeline(line)&.dig("mode") == "ruby" }
|
213
|
+
end
|
214
|
+
|
215
|
+
def moduleize(term)
|
216
|
+
term
|
217
|
+
.sub(/^[a-z\d]*/, &:capitalize)
|
218
|
+
.gsub(%r{(?:_|(/))([a-z\d]*)}) { "#{$1}#{$2.capitalize}" }
|
219
|
+
.split("/")
|
220
|
+
end
|
221
|
+
|
36
222
|
def restore_autoupdate_items
|
37
223
|
return unless instance_variable_defined?(:@autoupdated_items)
|
38
224
|
|
@@ -40,6 +226,124 @@ module RSpec
|
|
40
226
|
item.meta["autoupdate"] = meta
|
41
227
|
end
|
42
228
|
end
|
229
|
+
|
230
|
+
def populate_channel_types_from_api(api)
|
231
|
+
api.channel_types.each do |ct_json|
|
232
|
+
uid = org.openhab.core.thing.type.ChannelTypeUID.new(ct_json["UID"])
|
233
|
+
builder = case ct_json["kind"]
|
234
|
+
when "STATE"
|
235
|
+
org.openhab.core.thing.type.ChannelTypeBuilder.state(uid, ct_json["label"], ct_json["itemType"])
|
236
|
+
when "TRIGGER"
|
237
|
+
org.openhab.core.thing.type.ChannelTypeBuilder.trigger(uid, ct_json["label"])
|
238
|
+
else
|
239
|
+
raise ArgumentError, "Unrecognized channel type kind #{ct_json["kind"]} for #{uid}"
|
240
|
+
end
|
241
|
+
|
242
|
+
ct_json.each do |(k, v)|
|
243
|
+
case k
|
244
|
+
when "parameters", "parameterGroups", "label", "kind", "UID", "itemType"
|
245
|
+
nil
|
246
|
+
when "commandDescription"
|
247
|
+
builder.with_command_description(build_command_description(v))
|
248
|
+
when "stateDescription"
|
249
|
+
builder.with_state_description_fragment(build_state_description_fragment(v))
|
250
|
+
when "advanced"
|
251
|
+
builder.is_advanced(v)
|
252
|
+
else
|
253
|
+
builder.send(:"with_#{k}", v)
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
ct = builder.build
|
258
|
+
Core::Mocks::ChannelTypeProvider.instance.add(ct)
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
def build_command_description(json)
|
263
|
+
org.openhab.core.types.CommandDescriptionBuilder.create
|
264
|
+
.with_command_options(json["commandOptions"].map do |o|
|
265
|
+
org.openhab.core.types.CommandOption.new(o["command"], o["label"])
|
266
|
+
end)
|
267
|
+
.build
|
268
|
+
end
|
269
|
+
|
270
|
+
def build_state_description_fragment(json)
|
271
|
+
org.openhab.core.types.StateDescriptionFragmentBuilder.create
|
272
|
+
.with_minimum(json["minimum"]&.to_d)
|
273
|
+
.with_maximum(json["maximum"]&.to_d)
|
274
|
+
.with_step(json["step"&.to_d])
|
275
|
+
.with_pattern(json["pattern"])
|
276
|
+
.with_read_only(json["readOnly"])
|
277
|
+
.with_options(json["options"].map { |o| org.openhab.core.types.StateOption.new(o["value"], o["label"]) })
|
278
|
+
.build
|
279
|
+
end
|
280
|
+
|
281
|
+
def populate_thing_types_from_api(api)
|
282
|
+
api.thing_types.each do |tt_json|
|
283
|
+
uid = org.openhab.core.thing.ThingTypeUID.new(tt_json["UID"])
|
284
|
+
builder = org.openhab.core.thing.type.ThingTypeBuilder.instance(uid, tt_json["label"])
|
285
|
+
tt_json.each do |(k, v)|
|
286
|
+
case k
|
287
|
+
when "UID", "label", "bridge"
|
288
|
+
nil
|
289
|
+
when "listed"
|
290
|
+
builder.is_listed(v)
|
291
|
+
when "channels"
|
292
|
+
builder.with_channels(v.map { |c| build_channel_definition(c) })
|
293
|
+
when "channelGroups"
|
294
|
+
builder.with_channel_groups(v.map { |cg| build_channel_group_definition(cg) })
|
295
|
+
else
|
296
|
+
builder.send(:"with#{k[0].upcase}#{k[1..]}", v)
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
tt = tt_json["bridge"] ? builder.build_bridge : builder.build
|
301
|
+
Core::Mocks::ThingTypeProvider.instance.add(tt)
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
def build_channel_definition(json)
|
306
|
+
org.openhab.core.thing.type.ChannelDefinition.new(
|
307
|
+
json["uid"],
|
308
|
+
org.openhab.core.thing.type.ChannelTypeUID.new(json["typeUID"]),
|
309
|
+
json["description"],
|
310
|
+
json["properties"],
|
311
|
+
nil
|
312
|
+
)
|
313
|
+
end
|
314
|
+
|
315
|
+
def build_channel_group_definition(json)
|
316
|
+
org.openhab.core.thing.type.ChannelGroupDefinition.new(
|
317
|
+
json["uid"],
|
318
|
+
org.openhab.core.thing.type.ChannelGroupTypeUID.new(json["typeUID"]),
|
319
|
+
json["label"],
|
320
|
+
json["description"]
|
321
|
+
)
|
322
|
+
end
|
323
|
+
|
324
|
+
def build_channel(json)
|
325
|
+
uid = org.openhab.core.thing.ChannelUID.new(json["uid"])
|
326
|
+
builder = org.openhab.core.thing.binding.builder.ChannelBuilder.create(uid)
|
327
|
+
|
328
|
+
json.each do |(k, v)|
|
329
|
+
case k
|
330
|
+
when "uid", "id", "linkedItems", "itemType"
|
331
|
+
nil
|
332
|
+
when "channelTypeUID"
|
333
|
+
builder.with_type(org.openhab.core.thing.type.ChannelTypeUID.new((v)))
|
334
|
+
when "configuration"
|
335
|
+
builder.with_configuration(org.openhab.core.config.core.Configuration.new(v))
|
336
|
+
when "kind"
|
337
|
+
builder.with_kind(org.openhab.core.thing.type.ChannelKind.const_get(v, false))
|
338
|
+
when "defaultTags"
|
339
|
+
builder.with_default_tags(v.to_set)
|
340
|
+
else
|
341
|
+
builder.send("with_#{k}", v)
|
342
|
+
end
|
343
|
+
end
|
344
|
+
|
345
|
+
builder.build
|
346
|
+
end
|
43
347
|
end
|
44
348
|
|
45
349
|
RSpec.configure do |config|
|
data/lib/rspec/openhab/hooks.rb
CHANGED
@@ -1,20 +1,33 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
RSpec
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
module RSpec
|
4
|
+
module OpenHAB
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.before(:suite) do
|
7
|
+
Helpers.populate_things_from_api if ::OpenHAB::DSL::Imports.api.authenticated?
|
8
|
+
Helpers.populate_items_from_api
|
9
|
+
Helpers.load_transforms
|
10
|
+
Helpers.suspend_rules do
|
11
|
+
Helpers.load_rules
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
config.before do
|
16
|
+
suspend_rules do
|
17
|
+
$ir.for_each do |_provider, item|
|
18
|
+
next if item.is_a?(GroupItem) # groups only have calculated states
|
8
19
|
|
9
|
-
|
20
|
+
item.state = NULL unless item.raw_state == NULL
|
21
|
+
end
|
22
|
+
end
|
10
23
|
end
|
11
|
-
end
|
12
|
-
end
|
13
24
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
25
|
+
config.after do
|
26
|
+
::OpenHAB::DSL::Timers.timer_manager.cancel_all
|
27
|
+
Timecop.return
|
28
|
+
restore_autoupdate_items
|
29
|
+
Core::Mocks::PersistenceService.instance.reset
|
30
|
+
end
|
31
|
+
end
|
19
32
|
end
|
20
33
|
end
|
@@ -88,7 +88,6 @@ require_relative "rspec/openhab/dsl/rules/triggers/watch"
|
|
88
88
|
|
89
89
|
# RSpec additions
|
90
90
|
require "rspec/core"
|
91
|
-
require "rspec/openhab/items"
|
92
91
|
require "rspec/openhab/helpers"
|
93
92
|
require "rspec/openhab/hooks"
|
94
93
|
require "rspec/openhab/suspend_rules"
|
@@ -97,26 +96,9 @@ RSpec.configure do |config|
|
|
97
96
|
config.include OpenHAB::Core::EntityLookup
|
98
97
|
end
|
99
98
|
|
100
|
-
RSpec::OpenHAB::SuspendRules.suspend_rules do
|
101
|
-
RSpec::OpenHAB::Items.populate_things_from_api(api) if api.authenticated?
|
102
|
-
RSpec::OpenHAB::Items.populate_items_from_api(api)
|
103
|
-
end
|
104
|
-
|
105
99
|
# make bundler/inline _not_ destroy the already existing load path
|
106
100
|
module Bundler
|
107
101
|
module SharedHelpers
|
108
102
|
def clean_load_path; end
|
109
103
|
end
|
110
104
|
end
|
111
|
-
|
112
|
-
# load rules files
|
113
|
-
OPENHAB_AUTOMATION_PATH = "#{org.openhab.core.OpenHAB.config_folder}/automation/jsr223/ruby/personal"
|
114
|
-
|
115
|
-
RSpec::OpenHAB::SuspendRules.suspend_rules do
|
116
|
-
Dir["#{OPENHAB_AUTOMATION_PATH}/*.rb"].each do |f|
|
117
|
-
load f
|
118
|
-
rescue Exception => e
|
119
|
-
warn "Failed loading #{f}: #{e.inspect}"
|
120
|
-
warn e.backtrace
|
121
|
-
end
|
122
|
-
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-openhab-scripting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.19
|
5
5
|
platform: java
|
6
6
|
authors:
|
7
7
|
- Cody Cutrer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-08-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|
@@ -205,7 +205,6 @@ files:
|
|
205
205
|
- lib/rspec/openhab/dsl/timers/timer.rb
|
206
206
|
- lib/rspec/openhab/helpers.rb
|
207
207
|
- lib/rspec/openhab/hooks.rb
|
208
|
-
- lib/rspec/openhab/items.rb
|
209
208
|
- lib/rspec/openhab/suspend_rules.rb
|
210
209
|
- lib/rspec/openhab/version.rb
|
211
210
|
- vendor/gems/jar-dependencies-1.0.0/lib/jar-dependencies.rb
|
data/lib/rspec/openhab/items.rb
DELETED
@@ -1,220 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module OpenHAB
|
5
|
-
module Items
|
6
|
-
class << self
|
7
|
-
def populate_items_from_api(api)
|
8
|
-
all_items = api.items
|
9
|
-
|
10
|
-
gfh = org.openhab.core.internal.items.GroupFunctionHelper.new
|
11
|
-
item_factory = org.openhab.core.library.CoreItemFactory.new
|
12
|
-
|
13
|
-
all_items.each do |item_json|
|
14
|
-
full_type = item_json["type"]
|
15
|
-
name = item_json["name"]
|
16
|
-
|
17
|
-
type, _dimension = full_type.split(":")
|
18
|
-
if type == "Group"
|
19
|
-
base_item = item_factory.create_item(item_json["groupType"], name) if item_json["groupType"]
|
20
|
-
if item_json["function"]
|
21
|
-
dto = org.openhab.core.items.dto.GroupFunctionDTO.new
|
22
|
-
dto.name = item_json.dig("function", "name")
|
23
|
-
dto.params = item_json.dig("function", "params")
|
24
|
-
function = gfh.create_group_function(dto, base_item)
|
25
|
-
end
|
26
|
-
item = GroupItem.new(name, base_item, function)
|
27
|
-
else
|
28
|
-
item = item_factory.create_item(full_type, name)
|
29
|
-
end
|
30
|
-
|
31
|
-
item.label = item_json["label"]
|
32
|
-
item_json["tags"].each do |tag|
|
33
|
-
item.add_tag(tag)
|
34
|
-
end
|
35
|
-
item_json["metadata"]&.each do |key, config|
|
36
|
-
item.meta[key] = config["value"], config["config"]
|
37
|
-
end
|
38
|
-
item.meta["stateDescription"] = item_json["stateDescription"] if item_json["stateDescription"]
|
39
|
-
item.category = item_json["category"] if item_json["category"]
|
40
|
-
|
41
|
-
$ir.add(item)
|
42
|
-
|
43
|
-
next unless item.meta["channel"]&.value
|
44
|
-
|
45
|
-
channel_uid = org.openhab.core.thing.ChannelUID.new(item.meta["channel"].value)
|
46
|
-
channel = $things.get_channel(channel_uid)
|
47
|
-
next unless channel
|
48
|
-
|
49
|
-
link = org.openhab.core.thing.link.ItemChannelLink.new(item.name, channel_uid)
|
50
|
-
Core::Mocks::ItemChannelLinkProvider.instance.add(link)
|
51
|
-
end
|
52
|
-
all_items.each do |item_json| # rubocop:disable Style/CombinableLoops
|
53
|
-
item_json["groupNames"].each do |group_name|
|
54
|
-
next unless (group = $ir.get(group_name))
|
55
|
-
|
56
|
-
group.add_member($ir.get(item_json["name"]))
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def populate_things_from_api(api)
|
62
|
-
populate_channel_types_from_api(api)
|
63
|
-
populate_thing_types_from_api(api)
|
64
|
-
|
65
|
-
thing_type_registry = ::OpenHAB::Core::OSGI.service("org.openhab.core.thing.type.ThingTypeRegistry")
|
66
|
-
|
67
|
-
api.things.each do |thing_json|
|
68
|
-
uid = org.openhab.core.thing.ThingUID.new(thing_json["UID"])
|
69
|
-
type_uid = org.openhab.core.thing.ThingTypeUID.new(thing_json["thingTypeUID"])
|
70
|
-
bridge_uid = org.openhab.core.thing.ThingUID.new(thing_json["bridgeUID"]) if thing_json["bridgeUID"]
|
71
|
-
|
72
|
-
type = thing_type_registry.get_thing_type(type_uid)
|
73
|
-
klass = if type.is_a?(org.openhab.core.thing.type.BridgeType)
|
74
|
-
org.openhab.core.thing.binding.builder.BridgeBuilder
|
75
|
-
else
|
76
|
-
org.openhab.core.thing.binding.builder.ThingBuilder
|
77
|
-
end
|
78
|
-
builder = klass.create(type_uid, uid)
|
79
|
-
builder.with_bridge(bridge_uid) if bridge_uid
|
80
|
-
|
81
|
-
thing_json.each do |(k, v)|
|
82
|
-
case k
|
83
|
-
when "UID", "thingTypeUID", "bridgeUID", "statusInfo", "editable"
|
84
|
-
nil
|
85
|
-
when "channels"
|
86
|
-
builder.with_channels(v.map { |c| build_channel(c) })
|
87
|
-
when "configuration"
|
88
|
-
builder.with_configuration(org.openhab.core.config.core.Configuration.new(v))
|
89
|
-
else
|
90
|
-
builder.send(:"with_#{k}", v)
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
$things.add(builder.build)
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
private
|
99
|
-
|
100
|
-
def populate_channel_types_from_api(api)
|
101
|
-
api.channel_types.each do |ct_json|
|
102
|
-
uid = org.openhab.core.thing.type.ChannelTypeUID.new(ct_json["UID"])
|
103
|
-
builder = case ct_json["kind"]
|
104
|
-
when "STATE"
|
105
|
-
org.openhab.core.thing.type.ChannelTypeBuilder.state(uid, ct_json["label"], ct_json["itemType"])
|
106
|
-
when "TRIGGER"
|
107
|
-
org.openhab.core.thing.type.ChannelTypeBuilder.trigger(uid, ct_json["label"])
|
108
|
-
else
|
109
|
-
raise ArgumentError, "Unrecognized channel type kind #{ct_json["kind"]} for #{uid}"
|
110
|
-
end
|
111
|
-
|
112
|
-
ct_json.each do |(k, v)|
|
113
|
-
case k
|
114
|
-
when "parameters", "parameterGroups", "label", "kind", "UID", "itemType"
|
115
|
-
nil
|
116
|
-
when "commandDescription"
|
117
|
-
builder.with_command_description(build_command_description(v))
|
118
|
-
when "stateDescription"
|
119
|
-
builder.with_state_description_fragment(build_state_description_fragment(v))
|
120
|
-
when "advanced"
|
121
|
-
builder.is_advanced(v)
|
122
|
-
else
|
123
|
-
builder.send(:"with_#{k}", v)
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
ct = builder.build
|
128
|
-
Core::Mocks::ChannelTypeProvider.instance.add(ct)
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
def build_command_description(json)
|
133
|
-
org.openhab.core.types.CommandDescriptionBuilder.create
|
134
|
-
.with_command_options(json["commandOptions"].map do |o|
|
135
|
-
org.openhab.core.types.CommandOption.new(o["command"], o["label"])
|
136
|
-
end)
|
137
|
-
.build
|
138
|
-
end
|
139
|
-
|
140
|
-
def build_state_description_fragment(json)
|
141
|
-
org.openhab.core.types.StateDescriptionFragmentBuilder.create
|
142
|
-
.with_minimum(json["minimum"]&.to_d)
|
143
|
-
.with_maximum(json["maximum"]&.to_d)
|
144
|
-
.with_step(json["step"&.to_d])
|
145
|
-
.with_pattern(json["pattern"])
|
146
|
-
.with_read_only(json["readOnly"])
|
147
|
-
.with_options(json["options"].map { |o| org.openhab.core.types.StateOption.new(o["value"], o["label"]) })
|
148
|
-
.build
|
149
|
-
end
|
150
|
-
|
151
|
-
def populate_thing_types_from_api(api)
|
152
|
-
api.thing_types.each do |tt_json|
|
153
|
-
uid = org.openhab.core.thing.ThingTypeUID.new(tt_json["UID"])
|
154
|
-
builder = org.openhab.core.thing.type.ThingTypeBuilder.instance(uid, tt_json["label"])
|
155
|
-
tt_json.each do |(k, v)|
|
156
|
-
case k
|
157
|
-
when "UID", "label", "bridge"
|
158
|
-
nil
|
159
|
-
when "listed"
|
160
|
-
builder.is_listed(v)
|
161
|
-
when "channels"
|
162
|
-
builder.with_channels(v.map { |c| build_channel_definition(c) })
|
163
|
-
when "channelGroups"
|
164
|
-
builder.with_channel_groups(v.map { |cg| build_channel_group_definition(cg) })
|
165
|
-
else
|
166
|
-
builder.send(:"with#{k[0].upcase}#{k[1..]}", v)
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
tt = tt_json["bridge"] ? builder.build_bridge : builder.build
|
171
|
-
Core::Mocks::ThingTypeProvider.instance.add(tt)
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
|
-
def build_channel_definition(json)
|
176
|
-
org.openhab.core.thing.type.ChannelDefinition.new(
|
177
|
-
json["uid"],
|
178
|
-
org.openhab.core.thing.type.ChannelTypeUID.new(json["typeUID"]),
|
179
|
-
json["description"],
|
180
|
-
json["properties"],
|
181
|
-
nil
|
182
|
-
)
|
183
|
-
end
|
184
|
-
|
185
|
-
def build_channel_group_definition(json)
|
186
|
-
org.openhab.core.thing.type.ChannelGroupDefinition.new(
|
187
|
-
json["uid"],
|
188
|
-
org.openhab.core.thing.type.ChannelGroupTypeUID.new(json["typeUID"]),
|
189
|
-
json["label"],
|
190
|
-
json["description"]
|
191
|
-
)
|
192
|
-
end
|
193
|
-
|
194
|
-
def build_channel(json)
|
195
|
-
uid = org.openhab.core.thing.ChannelUID.new(json["uid"])
|
196
|
-
builder = org.openhab.core.thing.binding.builder.ChannelBuilder.create(uid)
|
197
|
-
|
198
|
-
json.each do |(k, v)|
|
199
|
-
case k
|
200
|
-
when "uid", "id", "linkedItems", "itemType"
|
201
|
-
nil
|
202
|
-
when "channelTypeUID"
|
203
|
-
builder.with_type(org.openhab.core.thing.type.ChannelTypeUID.new((v)))
|
204
|
-
when "configuration"
|
205
|
-
builder.with_configuration(org.openhab.core.config.core.Configuration.new(v))
|
206
|
-
when "kind"
|
207
|
-
builder.with_kind(org.openhab.core.thing.type.ChannelKind.const_get(v, false))
|
208
|
-
when "defaultTags"
|
209
|
-
builder.with_default_tags(v.to_set)
|
210
|
-
else
|
211
|
-
builder.send("with_#{k}", v)
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
builder.build
|
216
|
-
end
|
217
|
-
end
|
218
|
-
end
|
219
|
-
end
|
220
|
-
end
|