From 011f9d608efaf33c746ad9672af15579c2002c18 Mon Sep 17 00:00:00 2001 From: roleo Date: Wed, 25 Oct 2023 16:19:12 +0200 Subject: [PATCH] Add sound detection for cams that support it --- .../static/home/yi-hack/etc/camera.conf | 2 ++ .../static/home/yi-hack/script/check_conf.sh | 7 ++++- .../mqtt_advertise/mqtt_adv_homeassistant.sh | 22 ++++++++++++- .../script/mqtt_advertise/mqtt_set_config.sh | 3 ++ src/www/httpd/cgi-bin/camera_settings.sh | 12 ++++++- .../htdocs/js/modules/camera_settings.js | 5 ++- .../httpd/htdocs/pages/camera_settings.html | 31 +++++++++++++++++++ src/www/httpd/htdocs/pages/mqtt.html | 18 +++++++++++ 8 files changed, 96 insertions(+), 4 deletions(-) diff --git a/src/static/static/home/yi-hack/etc/camera.conf b/src/static/static/home/yi-hack/etc/camera.conf index b4d64ec..a8cd351 100644 --- a/src/static/static/home/yi-hack/etc/camera.conf +++ b/src/static/static/home/yi-hack/etc/camera.conf @@ -2,6 +2,8 @@ SWITCH_ON=yes SAVE_VIDEO_ON_MOTION=yes SENSITIVITY=low BABY_CRYING_DETECT=no +SOUND_DETECTION=no +SOUND_SENSITIVITY=80 LED=no ROTATE=no IR=yes diff --git a/src/static/static/home/yi-hack/script/check_conf.sh b/src/static/static/home/yi-hack/script/check_conf.sh index 243afe6..9107a29 100755 --- a/src/static/static/home/yi-hack/script/check_conf.sh +++ b/src/static/static/home/yi-hack/script/check_conf.sh @@ -55,6 +55,8 @@ SWITCH_ON=yes SAVE_VIDEO_ON_MOTION=yes SENSITIVITY=low BABY_CRYING_DETECT=no +SOUND_DETECTION=no +SOUND_SENSITIVITY=80 LED=no ROTATE=no IR=yes @@ -72,17 +74,20 @@ TOPIC_MOTION=motion_detection TOPIC_MOTION_IMAGE=motion_detection_image MOTION_IMAGE_DELAY=0.5 TOPIC_MOTION_FILES=motion_files +TOPIC_SOUND_DETECTION=sound_detection BIRTH_MSG=online WILL_MSG=offline MOTION_START_MSG=motion_start MOTION_STOP_MSG=motion_stop BABY_CRYING_MSG=crying +SOUND_DETECTION_MSG=sound_detected MQTT_KEEPALIVE=120 MQTT_QOS=1 MQTT_RETAIN_BIRTH_WILL=1 MQTT_RETAIN_MOTION=0 MQTT_RETAIN_MOTION_IMAGE=0 -MQTT_RETAIN_MOTION_FILES=0" +MQTT_RETAIN_MOTION_FILES=0 +MQTT_RETAIN_SOUND_DETECTION=0" if [ ! -f $SYSTEM_CONF_FILE ]; then touch $SYSTEM_CONF_FILE diff --git a/src/static/static/home/yi-hack/script/mqtt_advertise/mqtt_adv_homeassistant.sh b/src/static/static/home/yi-hack/script/mqtt_advertise/mqtt_adv_homeassistant.sh index 2e2397d..6d314a3 100755 --- a/src/static/static/home/yi-hack/script/mqtt_advertise/mqtt_adv_homeassistant.sh +++ b/src/static/static/home/yi-hack/script/mqtt_advertise/mqtt_adv_homeassistant.sh @@ -50,6 +50,9 @@ MOTION_STOP_MSG=$(get_config MOTION_STOP_MSG) BABY_CRYING_MSG=$(get_config BABY_CRYING_MSG) +TOPIC_SOUND_DETECTION=$(get_config TOPIC_SOUND_DETECTION) +SOUND_DETECTION_MSG=$(get_config SOUND_DETECTION_MSG) + TOPIC_MOTION_IMAGE=$(get_config TOPIC_MOTION_IMAGE) HOST=$MQTT_IP @@ -258,6 +261,17 @@ MQTT_RETAIN_MOTION=$(get_config MQTT_RETAIN_MOTION) # fi CONTENT='{"availability_topic":"'$MQTT_PREFIX'/'$TOPIC_BIRTH_WILL'","payload_available":"'$BIRTH_MSG'","payload_not_available":"'$WILL_MSG'","device":'$DEVICE_DETAILS', "qos": "'$MQTT_QOS'", '$RETAIN' "device_class":"sound","state_topic":"'$MQTT_PREFIX'/'$TOPIC_MOTION'","name":"'$UNIQUE_NAME'","unique_id":"'$UNIQUE_ID'","payload_on":"'$BABY_CRYING_MSG'","off_delay":60, "platform": "mqtt"}' mqtt_publish +# Sound Detection +hass_topic "binary_sensor" "sound_detection" "Sound Detection" +MQTT_RETAIN_SOUND_DETECTION=$(get_config MQTT_RETAIN_SOUND_DETECTION) +#Don't know why... ..Home Assistant don't allow retain for Sensor and Binary Sensor +# if [ "$MQTT_RETAIN_SOUND_DETECTION" == "1" ]; then +# RETAIN='"retain":true, ' +# else + RETAIN="" +# fi +CONTENT='{"availability_topic":"'$MQTT_PREFIX'/'$TOPIC_BIRTH_WILL'","payload_available":"'$BIRTH_MSG'","payload_not_available":"'$WILL_MSG'","device":'$DEVICE_DETAILS', "qos": "'$MQTT_QOS'", '$RETAIN' "device_class":"sound","state_topic":"'$MQTT_PREFIX'/'$TOPIC_SOUND_DETECTION'","name":"'$UNIQUE_NAME'","unique_id":"'$UNIQUE_ID'","payload_on":"'$SOUND_DETECTION_MSG'","off_delay":60, "platform": "mqtt"}' +mqtt_publish # Motion Detection Image hass_topic "camera" "motion_detection_image" "Motion Detection Image" MQTT_RETAIN_MOTION_IMAGE=$(get_config MQTT_RETAIN_MOTION_IMAGE) @@ -292,6 +306,12 @@ if [ "$MQTT_ADV_CAMERA_SETTING_ENABLE" == "yes" ]; then # Baby Crying Detection hass_setup_switch "BABY_CRYING_DETECT" "Baby crying detect" "music-note" $MQTT_ADV_CAMERA_SETTING_TOPIC "config" mqtt_publish + # Sound Detection + hass_setup_switch "SOUND_DETECTION" "Sound Detection" "music-note" $MQTT_ADV_CAMERA_SETTING_TOPIC "config" + mqtt_publish + # Sound detection sensitivity + hass_setup_number "SOUND_SENSITIVITY" "Sound Detection sensitivity" "account-voice" $MQTT_ADV_CAMERA_SETTING_TOPIC 30 90 5 "slider" "config" + mqtt_publish # Led hass_setup_switch "LED" "Status Led" "led-on" $MQTT_ADV_CAMERA_SETTING_TOPIC "config" mqtt_publish @@ -302,7 +322,7 @@ if [ "$MQTT_ADV_CAMERA_SETTING_ENABLE" == "yes" ]; then hass_setup_switch "ROTATE" "Rotate" "rotate-right" $MQTT_ADV_CAMERA_SETTING_TOPIC "config" mqtt_publish else - for ITEM in SWITCH_ON BABY_CRYING_DETECT LED IR ROTATE; do + for ITEM in SWITCH_ON BABY_CRYING_DETECT SOUND_DETECTION LED IR ROTATE; do hass_topic "switch" "$ITEM" $YI_HACK_PREFIX/bin/mosquitto_pub $HA_QOS $HA_RETAIN -h $HOST -t $TOPIC -n done diff --git a/src/static/static/home/yi-hack/script/mqtt_advertise/mqtt_set_config.sh b/src/static/static/home/yi-hack/script/mqtt_advertise/mqtt_set_config.sh index a31810b..46af17c 100755 --- a/src/static/static/home/yi-hack/script/mqtt_advertise/mqtt_set_config.sh +++ b/src/static/static/home/yi-hack/script/mqtt_advertise/mqtt_set_config.sh @@ -60,6 +60,9 @@ $YI_HACK_PREFIX/bin/mosquitto_sub -v -h $HOST -t $TOPIC | while read -r SUBSCRIB baby_crying_detect) IPC_OPT="-b" ;; + sound_detection) + IPC_OPT="-b" + ;; led) IPC_OPT="-l" ;; diff --git a/src/www/httpd/cgi-bin/camera_settings.sh b/src/www/httpd/cgi-bin/camera_settings.sh index 6abe67e..6b8f70d 100755 --- a/src/www/httpd/cgi-bin/camera_settings.sh +++ b/src/www/httpd/cgi-bin/camera_settings.sh @@ -15,7 +15,7 @@ fi CONF_LAST="CONF_LAST" -for I in 1 2 3 4 5 6 7 8 +for I in 1 2 3 4 5 6 7 8 9 10 do CONF="$(echo $QUERY_STRING | cut -d'&' -f$I | cut -d'=' -f1)" VAL="$(echo $QUERY_STRING | cut -d'&' -f$I | cut -d'=' -f2)" @@ -64,6 +64,16 @@ do else ipc_cmd -B on fi + elif [ "$CONF" == "sound_detection" ] ; then + if [ "$VAL" == "no" ] ; then + ipc_cmd -b off + else + ipc_cmd -b on + fi + elif [ "$CONF" == "sound_sensitivity" ] ; then + if [ "$VAL" == "30" ] || [ "$VAL" == "35" ] || [ "$VAL" == "40" ] || [ "$VAL" == "45" ] || [ "$VAL" == "50" ] || [ "$VAL" == "60" ] || [ "$VAL" == "70" ] || [ "$VAL" == "80" ] || [ "$VAL" == "90" ] ; then + ipc_cmd -n $VAL + fi elif [ "$CONF" == "led" ] ; then if [ "$VAL" == "no" ] ; then ipc_cmd -l off diff --git a/src/www/httpd/htdocs/js/modules/camera_settings.js b/src/www/httpd/htdocs/js/modules/camera_settings.js index c7df92a..ac4e764 100644 --- a/src/www/httpd/htdocs/js/modules/camera_settings.js +++ b/src/www/httpd/htdocs/js/modules/camera_settings.js @@ -26,7 +26,7 @@ APP.camera_settings = (function($) { loadingStatusElem.fadeOut(500); $.each(response, function(key, state) { - if (key == "SENSITIVITY" || key=="CRUISE") + if(key=="SENSITIVITY" || key=="SOUND_SENSITIVITY" || key=="CRUISE") $('select[data-key="' + key + '"]').prop('value', state); else $('input[type="checkbox"][data-key="' + key + '"]').prop('checked', state === 'yes'); @@ -51,6 +51,7 @@ APP.camera_settings = (function($) { }); configs["SENSITIVITY"] = $('select[data-key="SENSITIVITY"]').prop('value'); + configs["SOUND_SENSITIVITY"] = $('select[data-key="SOUND_SENSITIVITY"]').prop('value'); configs["CRUISE"] = $('select[data-key="CRUISE"]').prop('value'); $.ajax({ @@ -59,6 +60,8 @@ APP.camera_settings = (function($) { 'save_video_on_motion=' + configs["SAVE_VIDEO_ON_MOTION"] + '&sensitivity=' + configs["SENSITIVITY"] + '&baby_crying_detect=' + configs["BABY_CRYING_DETECT"] + + '&sound_detection=' + configs["SOUND_DETECTION"] + + '&sound_sensitivity=' + configs["SOUND_SENSITIVITY"] + '&led=' + configs["LED"] + '&ir=' + configs["IR"] + '&rotate=' + configs["ROTATE"] + diff --git a/src/www/httpd/htdocs/pages/camera_settings.html b/src/www/httpd/htdocs/pages/camera_settings.html index 5b4ef7c..a081868 100644 --- a/src/www/httpd/htdocs/pages/camera_settings.html +++ b/src/www/httpd/htdocs/pages/camera_settings.html @@ -85,6 +85,37 @@

General

+ + Sound Detection + + + + Enable/disable sound detection. + + + + + Sound detection sensitivity + +
+ +
+ + Status led diff --git a/src/www/httpd/htdocs/pages/mqtt.html b/src/www/httpd/htdocs/pages/mqtt.html index 9996e2a..5360f7a 100644 --- a/src/www/httpd/htdocs/pages/mqtt.html +++ b/src/www/httpd/htdocs/pages/mqtt.html @@ -143,6 +143,15 @@

Topics

+ + Sound Detection + + + + + + +