Module: RSpec::OpenHAB::Items

Defined in:
lib/rspec/openhab/items.rb

Class Method Summary collapse

Class Method Details

.populate_items_from_api(api) ⇒ Object



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
# File 'lib/rspec/openhab/items.rb', line 7

def populate_items_from_api(api)
  all_items = api.items

  gfh = org.openhab.core.internal.items.GroupFunctionHelper.new
  item_factory = org.openhab.core.library.CoreItemFactory.new

  all_items.each do |item_json|
    full_type = item_json["type"]
    name = item_json["name"]

    type, _dimension = full_type.split(":")
    if type == "Group"
      base_item = item_factory.create_item(item_json["groupType"], name) if item_json["groupType"]
      if item_json["function"]
        dto = org.openhab.core.items.dto.GroupFunctionDTO.new
        dto.name = item_json.dig("function", "name")
        dto.params = item_json.dig("function", "params")
        function = gfh.create_group_function(dto, base_item)
      end
      item = GroupItem.new(name, base_item, function)
    else
      item = item_factory.create_item(full_type, name)
    end

    item.label = item_json["label"]
    item_json["tags"].each do |tag|
      item.add_tag(tag)
    end
    item_json["metadata"]&.each do |key, config|
      item.meta[key] = config["value"], config["config"]
    end
    item.meta["stateDescription"] = item_json["stateDescription"] if item_json["stateDescription"]
    item.category = item_json["category"] if item_json["category"]

    $ir.add(item)

    next unless item.meta["channel"]&.value

    channel_uid = org.openhab.core.thing.ChannelUID.new(item.meta["channel"].value)
    channel = $things.get_channel(channel_uid)
    next unless channel

    link = org.openhab.core.thing.link.ItemChannelLink.new(item.name, channel_uid)
    Core::Mocks::ItemChannelLinkProvider.instance.add(link)
  end
  all_items.each do |item_json| # rubocop:disable Style/CombinableLoops
    item_json["groupNames"].each do |group_name|
      next unless (group = $ir.get(group_name))

      group.add_member($ir.get(item_json["name"]))
    end
  end
end

.populate_things_from_api(api) ⇒ Object



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
# File 'lib/rspec/openhab/items.rb', line 61

def populate_things_from_api(api)
  populate_channel_types_from_api(api)
  populate_thing_types_from_api(api)

  thing_type_registry = ::OpenHAB::Core::OSGI.service("org.openhab.core.thing.type.ThingTypeRegistry")

  api.things.each do |thing_json|
    uid = org.openhab.core.thing.ThingUID.new(thing_json["UID"])
    type_uid = org.openhab.core.thing.ThingTypeUID.new(thing_json["thingTypeUID"])
    bridge_uid = org.openhab.core.thing.ThingUID.new(thing_json["bridgeUID"]) if thing_json["bridgeUID"]

    type = thing_type_registry.get_thing_type(type_uid)
    klass = if type.is_a?(org.openhab.core.thing.type.BridgeType)
              org.openhab.core.thing.binding.builder.BridgeBuilder
            else
              org.openhab.core.thing.binding.builder.ThingBuilder
            end
    builder = klass.create(type_uid, uid)
    builder.with_bridge(bridge_uid) if bridge_uid

    thing_json.each do |(k, v)|
      case k
      when "UID", "thingTypeUID", "bridgeUID", "statusInfo", "editable"
        nil
      when "channels"
        builder.with_channels(v.map { |c| build_channel(c) })
      when "configuration"
        builder.with_configuration(org.openhab.core.config.core.Configuration.new(v))
      else
        builder.send(:"with_#{k}", v)
      end
    end

    $things.add(builder.build)
  end
end