Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for services #12

Merged
merged 3 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/bashio.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
97 changes: 97 additions & 0 deletions lib/services.sh
Original file line number Diff line number Diff line change
@@ -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::discovery.delete() {
frenck marked this conversation as resolved.
Show resolved Hide resolved
local service=${1}

bashio::log.trace "${FUNCNAME[0]}:" "$@"
bashio::api.hassio "DELETE" "/services/${service}"
bashio::cache.flush_all
}