Standalone version

This commit is contained in:
Devmel Apps
2025-10-06 10:49:59 +02:00
parent 827a1fcdb8
commit c5f99dae9b
8 changed files with 117 additions and 40 deletions
+5 -1
View File
@@ -1,9 +1,13 @@
ARG BUILD_FROM
FROM $BUILD_FROM
ENV HASS_HOST=
ENV HASS_TOKEN=
ENV HASS_AUTOINCLUDE=0
# Copy data for add-on
COPY hassapi.class.php /home
COPY callback.php /home
COPY hass_cb.php /home
COPY state_post.sh /home
COPY states_get.sh /home
COPY run.sh /
+5 -5
View File
@@ -1,9 +1,9 @@
build_from:
aarch64: ghcr.io/home-assistant/aarch64-base:3.18
amd64: ghcr.io/home-assistant/amd64-base:3.18
armhf: ghcr.io/home-assistant/armhf-base:3.18
armv7: ghcr.io/home-assistant/armv7-base:3.18
i386: ghcr.io/home-assistant/i386-base:3.18
aarch64: ghcr.io/home-assistant/aarch64-base:3.22
amd64: ghcr.io/home-assistant/amd64-base:3.22
armhf: ghcr.io/home-assistant/armhf-base:3.22
armv7: ghcr.io/home-assistant/armv7-base:3.22
i386: ghcr.io/home-assistant/i386-base:3.22
codenotary:
signer: notary@home-assistant.io
base_image: notary@home-assistant.io
+1 -1
View File
@@ -1,6 +1,6 @@
name: "AirSend"
description: "AirSend Web Service (local connection, no internet, no cloud)"
version: "1.2"
version: "1.3"
slug: "airsend"
url: https://github.com/devmel/hass_airsend-addon
codenotary: notary@home-assistant.io
@@ -1,14 +1,25 @@
<?php
require_once dirname(__FILE__) . "/hassapi.class.php";
$BASE_HASS_API = "http://supervisor/core/api";
$HASS_API_TOKEN = @file_get_contents('hass_api.token');
$SUPERVISOR_TOKEN = @getenv("SUPERVISOR_TOKEN");
$HASS_HOST= @getenv("HASS_HOST");
$HASS_TOKEN= @getenv("HASS_TOKEN");
//On an external machine, replace with your ha values
//$BASE_HASS_API = "http://homeassistant.local:8123/api";
//$HASS_API_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI3YTJjYjE2M2VjZjQ0MGM0OGUwYzdkOTc2MjM4YWY5MCIsImlhdCI6MTY2ODg5NzA4OCwiZXhwIjoxOTg0MjU3MDg4fQ.gyDg_jYbD561OdQ0IngAMga-4LE3DTsd6bEIGkITGTc';
$auto_include = false;
if(!empty($HASS_HOST) && !empty($HASS_TOKEN)){
$machine = HassAPI::extractHostAndPort($HASS_HOST);
$HASS_API_URL = "http://".$machine['hostname'].":".$machine['port']."/api";
$HASS_API_TOKEN = $HASS_TOKEN;
$HASS_AUTOINCLUDE = @getenv("HASS_AUTOINCLUDE");
$auto_include = filter_var($HASS_AUTOINCLUDE, FILTER_VALIDATE_BOOLEAN);
}else if(!empty($SUPERVISOR_TOKEN)){
$HASS_API_URL = "http://supervisor/core/api";
$HASS_API_TOKEN = $SUPERVISOR_TOKEN;
$fbvalue = @file_get_contents('auto_include.cfg');
$auto_include = filter_var($fbvalue, FILTER_VALIDATE_BOOLEAN);
}
$api = new HassAPI($BASE_HASS_API, $HASS_API_TOKEN);
$api = new HassAPI($HASS_API_URL, $HASS_API_TOKEN, $auto_include);
if(!$api->isAuthorized()){
header("HTTP/1.1 401 Unauthorized");
@@ -28,10 +39,10 @@ if (is_array($data) && isset($data['events'])) {
if($val['type'] == 3 || $val['type'] == 2 || $val['type'] == 1){
$states = $api->convertNotesToStates($val['thingnotes']['notes']);
foreach ($states as $j => $state) {
$api->setState($entity_id, $state[0], $state[1], $val['timestamp']);
$api->setState($entity_id, $state[0], $state[1], $val['timestamp'], $val['channel']);
}
}else{
$api->setState($entity_id, 'error', 'error_'.$val['type'], $val['timestamp']);
$api->setState($entity_id, 'error', 'error_'.$val['type'], $val['timestamp'], $val['channel']);
}
}
//Interrupt event
@@ -47,7 +58,7 @@ if (is_array($data) && isset($data['events'])) {
//Search entity and update
$entities = $api->searchEntitiesFromChannelAndType($val['channel'], $state[0]);
foreach ($entities as $k => $entity_id) {
$api->setState($entity_id, $state[0], $state[1], $val['timestamp']);
$api->setState($entity_id, $state[0], $state[1], $val['timestamp'], $val['channel']);
}
//Creates if not exists
if(count($entities) == 0){
+27 -7
View File
@@ -3,11 +3,10 @@
class HassAPI{
var $states = null;
function __construct($BASE_HASS_API, $HASS_API_TOKEN) {
function __construct($BASE_HASS_API, $HASS_API_TOKEN, $auto_include = false) {
$this->BASE_HASS_API = $BASE_HASS_API;
$this->HASS_API_TOKEN = $HASS_API_TOKEN;
$fbvalue = @file_get_contents('auto_include.cfg');
$this->auto_include = filter_var($fbvalue, FILTER_VALIDATE_BOOLEAN);
$this->auto_include = $auto_include;
}
public function isAuthorized() {
@@ -64,6 +63,11 @@ class HassAPI{
function setState($entity_id, $type, $state, $timestamp_ms, $channel = null) {
$reloadcache = false;
$attributes = null;
if(isset($channel) && ((int)$channel["id"]) == 1){
if($type === 'data'){
return; //accept AirSend state only
}
}
if($entity_id == null && isset($channel)){
$reloadcache = true;
//New entity
@@ -148,6 +152,24 @@ class HassAPI{
return null;
}
public static function extractHostAndPort($input, $default_port = 8123){
$hostWithPort = preg_replace('#^https?://#', '', $input);
$hostWithPort = explode('/', $hostWithPort)[0];
if (strpos($hostWithPort, ':') !== false) {
list($hostname, $port) = explode(':', $hostWithPort, 2);
$port = (int)$port;
} else {
$hostname = $hostWithPort;
}
if(empty($port) || ($port <= 0 && $port >= 0x1000)){
$port = $default_port;
}
return [
'hostname' => $hostname,
'port' => $port
];
}
public static function convertNotesToStates($notes){
$ret = array();
if(is_array($notes) && count($notes) > 0){
@@ -332,10 +354,8 @@ class HassAPI{
}
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, strtoupper($method));
if (isset($token)){
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
curl_setopt($curl,CURLOPT_XOAUTH2_BEARER, $token);
$options = array(CURLOPT_HTTPHEADER => array('Content-Type: application/json'));
curl_setopt_array($curl, $options);
$options = array('Content-Type: application/json', 'Authorization: Bearer ' . $token);
curl_setopt($curl, CURLOPT_HTTPHEADER, $options);
}
$result = array();
$result['data'] = curl_exec($curl);
+44 -12
View File
@@ -10,18 +10,50 @@ case "$arch" in \
i386) arch='x86' ;; \
esac;
echo "AirSendWebService arch: ${arch}"
hname="$(hostname -i)"
echo "internal_url: http://${hname}:33863/"
if [ -n "${SUPERVISOR_TOKEN:-}" ]
then
echo ${SUPERVISOR_TOKEN} > hass_api.token
echo $(bashio::config 'auto_include') > auto_include.cfg
else
echo "Not running on Home Assistant machine..."
fi
# Start AirSendWebService
ulimit -n 4096
./bin/unix/${arch}/AirSendWebService 99399
php -S 127.0.0.1:80 callback.php
sleep infinity
CALLBACK_PID=
if [ -n "${SUPERVISOR_TOKEN:-}" ]
then
hname="$(hostname -i)"
echo "internal_url: http://${hname}:33863/"
echo $(bashio::config 'auto_include') > auto_include.cfg
# Start hass callback service
php -S 127.0.0.1:80 hass_cb.php & CALLBACK_PID=$!
elif [ -n "${HASS_HOST:-}" ] && [ -n "${HASS_TOKEN:-}" ]
then
echo "internal_url: http://{YOUR_DOCKER_MACHINE_IP}:33863/"
# Start hass callback service
php -S 127.0.0.1:80 hass_cb.php & CALLBACK_PID=$!
else
echo "Running the AirSendWebService only..."
fi
# Give services time to start
sleep 5
# Monitor services
ASW_PID=$(cat AirSendWebService.lock)
while true; do
sleep 30
# Check if AirSendWebService is still running
if ! kill -0 "$ASW_PID" 2>/dev/null; then
echo "ERROR: AirSendWebService (PID: $ASW_PID) died..." >&2
break
fi
# Check if Callback service exists and if is still running
if [[ -n "$CALLBACK_PID" && "$CALLBACK_PID" =~ ^[0-9]+$ ]] && ! kill -0 "$CALLBACK_PID" 2>/dev/null; then
echo "ERROR: Callback Service (PID: $CALLBACK_PID) died..." >&2
break
fi
done
kill $ASW_PID 2>/dev/null || true
kill $CALLBACK_PID 2>/dev/null || true
echo "exiting..."
exit 1