mirror of
https://github.com/hassio-addons/bashio.git
synced 2025-05-06 20:11:24 +00:00
Adds support for services (#12)
* ✨ Adds support for services * ✨ Adds type handling * 🚑 Fixes incorrect function name
This commit is contained in:
parent
0416242047
commit
abf27b4fbe
2 changed files with 99 additions and 0 deletions
|
@ -82,6 +82,8 @@ source "${__BASHIO_LIB_DIR}/pwned.sh"
|
|||
source "${__BASHIO_LIB_DIR}/repositories.sh"
|
||||
# shellcheck source=lib/secrets.sh
|
||||
source "${__BASHIO_LIB_DIR}/secrets.sh"
|
||||
# shellcheck source=lib/services.sh
|
||||
source "${__BASHIO_LIB_DIR}/services.sh"
|
||||
# shellcheck source=lib/string.sh
|
||||
source "${__BASHIO_LIB_DIR}/string.sh"
|
||||
# shellcheck source=lib/supervisor.sh
|
||||
|
|
97
lib/services.sh
Normal file
97
lib/services.sh
Normal file
|
@ -0,0 +1,97 @@
|
|||
#!/usr/bin/env bash
|
||||
# ==============================================================================
|
||||
# Community Hass.io Add-ons: Bashio
|
||||
# Bashio is an bash function library for use with Hass.io add-ons.
|
||||
#
|
||||
# It contains a set of commonly used operations and can be used
|
||||
# to be included in add-on scripts to reduce code duplication across add-ons.
|
||||
# ==============================================================================
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Get configuration object or configuration options from a service.
|
||||
#
|
||||
# Arguments:
|
||||
# $1 Service name
|
||||
# $2 Config option to get (optional)
|
||||
# ------------------------------------------------------------------------------
|
||||
function bashio::services() {
|
||||
local service=${1}
|
||||
local key=${2:-}
|
||||
local cache_key="service.info.${service}"
|
||||
local config
|
||||
local query
|
||||
local response
|
||||
|
||||
bashio::log.trace "${FUNCNAME[0]}" "$@"
|
||||
|
||||
if bashio::cache.exists "${cache_key}"; then
|
||||
config=$(bashio::cache.get "${cache_key}")
|
||||
else
|
||||
config=$(bashio::api.hassio GET "/services/${service}" false)
|
||||
bashio::cache.set "${cache_key}" "${config}"
|
||||
fi
|
||||
|
||||
response="${config}"
|
||||
if bashio::var.has_value "${key}"; then
|
||||
|
||||
read -r -d '' query << QUERY
|
||||
if (.${key} == null) then
|
||||
null
|
||||
elif (.${key} | type == "string") then
|
||||
.${key} // empty
|
||||
elif (.${key} | type == "boolean") then
|
||||
.${key} // false
|
||||
elif (.${key} | type == "array") then
|
||||
if (.${key} == []) then
|
||||
empty
|
||||
else
|
||||
.${key}[]
|
||||
end
|
||||
elif (.${key} | type == "object") then
|
||||
if (.${key} == {}) then
|
||||
empty
|
||||
else
|
||||
.${key}
|
||||
end
|
||||
else
|
||||
.${key}
|
||||
end
|
||||
QUERY
|
||||
response=$(bashio::jq "${config}" "${query}")
|
||||
fi
|
||||
|
||||
printf "%s" "${response}"
|
||||
|
||||
return "${__BASHIO_EXIT_OK}"
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Publish a new configuration object for this service.
|
||||
#
|
||||
# Arguments:
|
||||
# $1 Service name
|
||||
# $2 Configuration object (JSON)
|
||||
# ------------------------------------------------------------------------------
|
||||
function bashio::services.publish() {
|
||||
local service=${1}
|
||||
local config=${2}
|
||||
|
||||
bashio::log.trace "${FUNCNAME[0]}:" "$@"
|
||||
|
||||
bashio::api.hassio "POST" "/services/${service}" "${config}"
|
||||
bashio::cache.flush_all
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Deletes configuration object for this service.
|
||||
#
|
||||
# Arguments:
|
||||
# $1 Service name
|
||||
# ------------------------------------------------------------------------------
|
||||
function bashio::services.delete() {
|
||||
local service=${1}
|
||||
|
||||
bashio::log.trace "${FUNCNAME[0]}:" "$@"
|
||||
bashio::api.hassio "DELETE" "/services/${service}"
|
||||
bashio::cache.flush_all
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue