mirror of
https://github.com/aljazceru/addons.git
synced 2025-12-18 13:44:20 +01:00
deCONZ ingress & discovery (#561)
* Migrate deconz to ingress/discovery * update chmod * Add discovery * Fix lint * Fix typo * Bump versio 0.91.2 * Fix discovery * Cleanup config * Fix discovery * Fix url * Fix port * Fix script * Support new host config * Use new network feature * add notes * Fix shell * simplify v1 * Cleanup * Fix api port access * Remove waiting * Use more modern version * Fix lint * Add a note to ingress phoscon app * fix format * Fix ingress * Fix ingress * Fix output * Change flow * Fix wait port * Fix config * Add tiemout * Cleanup
This commit is contained in:
86
deconz/data/discovery.sh
Normal file
86
deconz/data/discovery.sh
Normal file
@@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env bashio
|
||||
|
||||
DATA_STORE="/data/hassio.json"
|
||||
|
||||
|
||||
function _discovery_config() {
|
||||
local api_key=${1}
|
||||
local serial=${2}
|
||||
local config
|
||||
|
||||
config=$(bashio::var.json \
|
||||
host "$(bashio::addon.ip_address)" \
|
||||
port "^$(bashio::addon.port 80)" \
|
||||
api_key "${api_key}" \
|
||||
serial "${serial}" \
|
||||
)
|
||||
|
||||
bashio::var.json \
|
||||
service deconz \
|
||||
config "^${config}"
|
||||
}
|
||||
|
||||
|
||||
function _save_data() {
|
||||
local api_key=${1}
|
||||
local serial=${2}
|
||||
local config
|
||||
|
||||
bashio::var.json api_key "${api_key}" serial "${serial}" > ${DATA_STORE}
|
||||
bashio::log.debug "Store API information to ${DATA_STORE}"
|
||||
}
|
||||
|
||||
|
||||
function _deconz_api() {
|
||||
local api_key
|
||||
local result
|
||||
local api_port
|
||||
|
||||
api_port=$(bashio::addon.port 80)
|
||||
while ! nc -z localhost ${api_port} </dev/null; do sleep 10; done
|
||||
|
||||
if ! result="$(curl --silent --show-error --request POST -d '{"devicetype": "Home Assistant"}' "http://127.0.0.1:${api_port}/api")"; then
|
||||
bashio::log.debug "${result}"
|
||||
bashio::exit.nok "Can't get API key from deCONZ gateway"
|
||||
fi
|
||||
api_key="$(echo "${result}" | jq --raw-output '.[0].success.username')"
|
||||
|
||||
sleep 15
|
||||
if ! result="$(curl --silent --show-error --request GET "http://127.0.0.1:${api_port}/api/${api_key}/config")"; then
|
||||
bashio::log.debug "${result}"
|
||||
bashio::exit.nok "Can't get data from deCONZ gateway"
|
||||
fi
|
||||
serial="$(echo "${result}" | jq --raw-output '.bridgeid')"
|
||||
|
||||
_save_data "${api_key}" "${serial}"
|
||||
}
|
||||
|
||||
|
||||
function _send_discovery() {
|
||||
local api_key
|
||||
local result
|
||||
local payload
|
||||
|
||||
api_key="$(jq --raw-output '.api_key' "${DATA_STORE}")"
|
||||
serial="$(jq --raw-output '.serial' "${DATA_STORE}")"
|
||||
|
||||
# Send discovery info
|
||||
payload="$(_discovery_config "${api_key}" "${serial}")"
|
||||
if bashio::api.hassio "POST" "/discovery" "${payload}"; then
|
||||
bashio::log.info "Success send discovery information to Home Assistant"
|
||||
else
|
||||
bashio::log.error "Discovery message to Home Assistant fails!"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
function hassio_discovery() {
|
||||
|
||||
# No API data exists - generate
|
||||
if [ ! -f "$DATA_STORE" ]; then
|
||||
bashio::log.info "Create API data for Home Assistant"
|
||||
_deconz_api
|
||||
fi
|
||||
|
||||
_send_discovery
|
||||
}
|
||||
32
deconz/data/ika-otau-dl.sh
Executable file
32
deconz/data/ika-otau-dl.sh
Executable file
@@ -0,0 +1,32 @@
|
||||
#!/bin/bash
|
||||
|
||||
URL_IKEA="http://fw.ota.homesmart.ikea.net/feed/version_info.json"
|
||||
|
||||
while true
|
||||
do
|
||||
|
||||
# fetch data
|
||||
if ! IKEA_DATA="$(curl -sL ${URL_IKEA})"; then
|
||||
echo "[Info] Can't fetch data from ikea!"
|
||||
sleep 18000
|
||||
continue
|
||||
fi
|
||||
|
||||
IKEA_DATA_SIZE="$(echo "${IKEA_DATA}" | jq --raw-output '. | length')"
|
||||
for (( i=0; i < "${IKEA_DATA_SIZE}"; i++ )); do
|
||||
OTAU_URL=$(echo "${IKEA_DATA}" | jq --raw-output ".[$i].fw_binary_url // empty")
|
||||
|
||||
if [ -z "${OTAU_URL}" ]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
OTAU_FILE="/data/otau/${OTAU_URL##*/}"
|
||||
if [ -f "${OTAU_FILE}" ]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
curl -s -L -o "${OTAU_FILE}" "${OTAU_URL}"
|
||||
done
|
||||
|
||||
sleep 259200
|
||||
done
|
||||
60
deconz/data/nginx.conf
Normal file
60
deconz/data/nginx.conf
Normal file
@@ -0,0 +1,60 @@
|
||||
worker_processes 1;
|
||||
pid /var/run/nginx.pid;
|
||||
error_log /dev/stdout info;
|
||||
daemon off;
|
||||
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
http {
|
||||
include mime.types;
|
||||
default_type application/octet-stream;
|
||||
sendfile on;
|
||||
keepalive_timeout 65;
|
||||
proxy_read_timeout 1200;
|
||||
gzip on;
|
||||
gzip_disable "msie6";
|
||||
resolver 127.0.0.11;
|
||||
|
||||
upstream backend {
|
||||
ip_hash;
|
||||
server 127.0.0.1:%%PORT%%;
|
||||
}
|
||||
|
||||
map $http_upgrade $connection_upgrade {
|
||||
default upgrade;
|
||||
'' close;
|
||||
}
|
||||
|
||||
# Ingress
|
||||
server {
|
||||
listen %%INGRESS_INTERFACE%%:%%INGRESS_PORT%% default_server;
|
||||
|
||||
allow 172.30.32.2;
|
||||
deny all;
|
||||
|
||||
server_name _;
|
||||
access_log /dev/stdout combined;
|
||||
|
||||
client_max_body_size 4G;
|
||||
keepalive_timeout 5;
|
||||
|
||||
root /dev/null;
|
||||
|
||||
location / {
|
||||
proxy_redirect off;
|
||||
proxy_pass http://backend;
|
||||
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection $connection_upgrade;
|
||||
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_set_header Host $http_host;
|
||||
proxy_set_header X-NginX-Proxy true;
|
||||
}
|
||||
}
|
||||
}
|
||||
71
deconz/data/run.sh
Executable file
71
deconz/data/run.sh
Executable file
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bashio
|
||||
set -e
|
||||
|
||||
. /discovery.sh
|
||||
|
||||
WAIT_PIDS=()
|
||||
|
||||
# Load config
|
||||
DECONZ_DEVICE=$(bashio::config 'device')
|
||||
API_PORT=$(bashio::addon.port 80)
|
||||
WEBSOCKET_PORT=$(bashio::addon.port 8080)
|
||||
INGRESS_PORT=$(bashio::addon.ingress_port)
|
||||
INGRESS_INTERFACE=$(bashio::addon.ip_address)
|
||||
|
||||
# Check if port is available
|
||||
if [ -z "${API_PORT}" ] || [ -z "${WEBSOCKET_PORT}" ]; then
|
||||
bashio::exit.nok "You need set API/Websocket port!"
|
||||
fi
|
||||
|
||||
# Start Gateway
|
||||
bashio::log.info "Start deCONZ gateway"
|
||||
deCONZ \
|
||||
-platform minimal \
|
||||
--auto-connect=1 \
|
||||
--dbg-info=1 \
|
||||
--dbg-aps=0 \
|
||||
--dbg-zcl=0 \
|
||||
--dbg-zdp=0 \
|
||||
--dbg-otau=0 \
|
||||
--http-port=${API_PORT} \
|
||||
--ws-port=${WEBSOCKET_PORT} \
|
||||
--upnp=0 \
|
||||
--dev="${DECONZ_DEVICE}" &
|
||||
WAIT_PIDS+=($!)
|
||||
|
||||
# Start OTA updates for deCONZ
|
||||
bashio::log.info "Run deCONZ OTA updater"
|
||||
deCONZ-otau-dl.sh &> /dev/null &
|
||||
WAIT_PIDS+=($!)
|
||||
|
||||
# Start OTA updates for IKEA
|
||||
bashio::log.info "Run IKEA OTA updater"
|
||||
ika-otau-dl.sh &> /dev/null &
|
||||
WAIT_PIDS+=($!)
|
||||
|
||||
# Start Ingress handler
|
||||
bashio::log.info "Start Ingress handler"
|
||||
|
||||
sed -i "s/%%PORT%%/${API_PORT}/g" /etc/nginx/ingress.conf
|
||||
sed -i "s/%%INGRESS_PORT%%/${INGRESS_PORT}/g" /etc/nginx/ingress.conf
|
||||
sed -i "s/%%INGRESS_INTERFACE%%/${INGRESS_INTERFACE}/g" /etc/nginx/ingress.conf
|
||||
nginx -c /etc/nginx/ingress.conf &
|
||||
WAIT_PIDS+=($!)
|
||||
|
||||
# Register stop
|
||||
function stop_addon() {
|
||||
bashio::log.debug "Kill Processes..."
|
||||
kill -15 "${WAIT_PIDS[@]}"
|
||||
|
||||
wait "${WAIT_PIDS[@]}"
|
||||
bashio::log.debug "Done."
|
||||
}
|
||||
trap "stop_addon" SIGTERM SIGHUP
|
||||
|
||||
# Start Hass.io discovery
|
||||
bashio::log.info "Run Hass.io discovery task"
|
||||
hassio_discovery
|
||||
|
||||
# Wait until all is done
|
||||
bashio::log.info "deCONZ is setup and running"
|
||||
wait "${WAIT_PIDS[@]}"
|
||||
Reference in New Issue
Block a user