I expanded the script to also install Json 3rd party add-ons.
CFG_FILE = '{{ openhab_conf_dir }}/services/addons.cfg'
iniLines = nil
def readIni(key, iniFile=CFG_FILE)
logger.debug "Reading addons.cfg and return \"#{key}\" items"
# Extract the requested entry line from `conf/services/addons.cfg`
iniLines = IO.readlines(iniFile) if iniLines.nil?
if lineIndex = iniLines.index{ |line| line =~ /^#{key}/ } then
return iniLines[lineIndex].split('=')[1].strip
end
return nil
end
def httpAction(httpRequest, httpUrl, httpBody=nil)
logger.debug "Sending HTTP #{httpRequest} to #{httpUrl} with body \"#{httpBody}\""
# Create the HTTP object
uri = URI.parse(httpUrl)
header = {'Content-Type': 'application/json', 'Accept': '*/*', 'X-OPENHAB-TOKEN': '{{ openhab_api_token }}'}
http = Net::HTTP.new(uri.host, uri.port)
request = httpRequest.downcase == "put" ? Net::HTTP::Put.new(uri.request_uri, header) : Net::HTTP::Post.new(uri.request_uri, header)
request.body = httpBody unless httpBody.nil?
response = http.request(request)
logger.warn "Result #{response.code} (#{response.body}) - installing service/add-on @ #{httpUrl} failed" unless response.code == "200"
return true ? response.code == "200" : false
end
rule "InstallAddons" do
description "Install marketplace add-ons at system start"
on_start at_level: 40
# Step 1: Install any Json 3rd party add-on services specified
run do
logger.info "Install Json 3rd party add-on services (defined in addons.cfg) at system start"
services = readIni("addonservice")
unless services.nil?
services.split("|").each do |service|
serviceName, jsonUrl = service.split(",")
serviceName = serviceName.strip
jsonUrl = jsonUrl.strip
#TODO: Check for ill-formatted entries
logger.info "Install JSON 3rd party add-on service \"#{serviceName}\"@\"#{jsonUrl}\""
result = httpAction("Put", "{{ openhab_api_url }}/services/org.openhab.jsonaddonservice/config", "{ \"urls\": \"#{jsonUrl}\" }")
end
end
end
#TODO: Check if services are installed (async) before we continue
delay 10.seconds # For now: let OH do its magic
# Step 2: Install the defined add-ons
run do
# Extract the marketplace add-ons entry from the ini file
addons = readIni("marketplace")
logger.info "Install Marketplace add-ons (defined in addons.cfg) at system start"
unless addons.nil?
addons.split("|").each do |addon|
addonName, addonType, addonId = addon.split(",")
addonName = addonName.strip
addonType = addonType.strip.downcase
addonId = addonId.strip
#TODO: Check for ill-formatted entries
logger.info "Install addon \"#{addonName}\" from #{addonType == "marketplace" ? "Community Marketplace" : "Other Add-ons"}"
result = httpAction("POST", "{{ openhab_api_url }}/addons/#{addonType}:#{addonId}/install?serviceId=#{addonType}")
# Note: The API always returns 200 with an empty body (see PR )
#TODO: Add check if addon is actually installed (async)
end
end
end
end
There are basically 3 types of add-ons: OpenHAB provided add-ons, marketplace add-ons, and Json 3rd party add-ons (shown under āOther Add-onsā in the Add-on Store).
- The OpenHAB Distribution add-ons can of course be installed in the usual way (either specified in
addons.cfg
or in the Add-on Store).
- The Community Marketplace add-ons can be installed through the Add-on Store or REST API. For this I added functionality in the script to install it from
addons.cfg
as well.
- The Other Add-ons are in the Add-on Store referencing Json 3rd Party Add-on services as shown in the System Settings. For this I also added functionality in the script to install them via
addons.cfg
.
There are two new entries for addons.cfg
: one to define any Json 3rd party add-on services (as sources for other add-ons) and one to define the non-distribution add-ons to install at system start.
The first entry looks like this:
addonservice = SmartHome/J Add-ons, https://download.smarthomej.org/addons.json
You can add more services, separated by ā|ā.
The actual add-ons to install are specified with:
marketplace = SemanticHomeMenu1, marketplace, 152205 | Unifi Protect Binding, marketplace, 107692 | Tuya SmartHome Binding, json, org-smarthome-binding-tuya
The marketplace
and json
elements specify if it is a Community Marketplace add-on or Json 3rd party add-on. The number specifies the ID as the add-on is known in the Marketplace. org-smarthome-binding-tuya
is how the 3rd party add-on is referenced in the Json add-on store.
You can find these specifications by running the browser with DevTools (F12) and see what is send when installing from the Add-on Store.