From dcfc2b886fb22d0c112b60cf4df96df0541e5306 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Mon, 19 Aug 2019 20:24:59 +0200 Subject: [PATCH] :sparkles: Adds support for Hass.io Discovery (#8) --- lib/bashio.sh | 2 ++ lib/discovery.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 lib/discovery.sh diff --git a/lib/bashio.sh b/lib/bashio.sh index 6da4f72..779bf27 100644 --- a/lib/bashio.sh +++ b/lib/bashio.sh @@ -58,6 +58,8 @@ source "${__BASHIO_LIB_DIR}/config.sh" source "${__BASHIO_LIB_DIR}/debug.sh" # shellcheck source=lib/exit.sh source "${__BASHIO_LIB_DIR}/exit.sh" +# shellcheck source=lib/discovery.sh +source "${__BASHIO_LIB_DIR}/discovery.sh" # shellcheck source=lib/dns.sh source "${__BASHIO_LIB_DIR}/dns.sh" # shellcheck source=lib/hardware.sh diff --git a/lib/discovery.sh b/lib/discovery.sh new file mode 100644 index 0000000..0147f7c --- /dev/null +++ b/lib/discovery.sh @@ -0,0 +1,46 @@ +#!/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. +# ============================================================================== + +# ------------------------------------------------------------------------------ +# Publish a new configuration object for discovery. +# +# Arguments: +# $1 Service name +# $2 Configuration object (JSON) +# ------------------------------------------------------------------------------ +function bashio::discovery() { + local service=${1} + local config=${2} + local payload + + bashio::log.trace "${FUNCNAME[0]}:" "$@" + + payload=$(\ + bashio::var.json \ + service "${service}" \ + config "^${config}" \ + ) + + bashio::api.hassio "POST" "/discovery" "${payload}" + bashio::cache.flush_all +} + +# ------------------------------------------------------------------------------ +# Deletes configuration object for this discovery. +# +# Arguments: +# $1 Discovery UUID +# ------------------------------------------------------------------------------ +function bashio::discovery.delete() { + local uuid=${1} + + bashio::log.trace "${FUNCNAME[0]}:" "$@" + bashio::api.hassio "DELETE" "/discovery/${uuid}" + bashio::cache.flush_all +}