From a8519f383972898fdf8ac17b5bc4ecdd99053bd3 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Mon, 19 Aug 2019 21:02:21 +0200 Subject: [PATCH 1/3] :sparkles: Adds support for services --- lib/bashio.sh | 2 ++ lib/services.sh | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 lib/services.sh diff --git a/lib/bashio.sh b/lib/bashio.sh index 779bf27..7b16b0e 100644 --- a/lib/bashio.sh +++ b/lib/bashio.sh @@ -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 diff --git a/lib/services.sh b/lib/services.sh new file mode 100644 index 0000000..70868dd --- /dev/null +++ b/lib/services.sh @@ -0,0 +1,72 @@ +#!/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 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 + response=$(bashio::jq "${config}" ".${key}") + 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() { + local service=${1} + + bashio::log.trace "${FUNCNAME[0]}:" "$@" + bashio::api.hassio "DELETE" "/services/${service}" + bashio::cache.flush_all +} From e95ce8ba05a8aa9c018d2a82e41e22b17ed74f09 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Mon, 19 Aug 2019 21:11:00 +0200 Subject: [PATCH 2/3] :sparkles: Adds type handling --- lib/services.sh | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/services.sh b/lib/services.sh index 70868dd..9bdac9c 100644 --- a/lib/services.sh +++ b/lib/services.sh @@ -19,6 +19,7 @@ function bashio::services() { local key=${2:-} local cache_key="service.info.${service}" local config + local query local response bashio::log.trace "${FUNCNAME[0]}" "$@" @@ -32,7 +33,31 @@ function bashio::services() { response="${config}" if bashio::var.has_value "${key}"; then - response=$(bashio::jq "${config}" ".${key}") + + 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}" From c979f5c3c21c0bd982c0391a7d6b32579637a4e6 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Mon, 19 Aug 2019 21:19:08 +0200 Subject: [PATCH 3/3] :ambulance: Fixes incorrect function name --- lib/services.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services.sh b/lib/services.sh index 9bdac9c..1c3ae34 100644 --- a/lib/services.sh +++ b/lib/services.sh @@ -88,7 +88,7 @@ function bashio::services.publish() { # Arguments: # $1 Service name # ------------------------------------------------------------------------------ -function bashio::discovery.delete() { +function bashio::services.delete() { local service=${1} bashio::log.trace "${FUNCNAME[0]}:" "$@"