Skip to content

Instantly share code, notes, and snippets.

@mikeybeck
Created May 22, 2022 04:39
Show Gist options
  • Select an option

  • Save mikeybeck/5a9522bb017e85bb054012d11fb348fb to your computer and use it in GitHub Desktop.

Select an option

Save mikeybeck/5a9522bb017e85bb054012d11fb348fb to your computer and use it in GitHub Desktop.
HomeAssistant light toggle based on camera/microphone status
#!/bin/bash
# Taken with thanks from https://hansenji.medium.com/linux-triggers-for-in-meeting-indicator-ea06cdbe41bd,
# with minor modifications for sending notification to HA.
# Camera status can be monitored with incron &
# `/dev/video* IN_OPEN,IN_CLOSE,IN_CLOSE_WRITE,IN_CLOSE_NOWRITE *PATH*/checkInMeeting.sh`
# Before running this, remember to insert HA address and auth key here,
# and update the entity ID and RGB colour in the cURL requests further down.
ha_address=""
ha_auth_key=""
fuser -s /dev/video*
videoOn=$?
audioSources=$(pactl list source-outputs)
delim="
"
string="$audioSources$delim"
# Break audioSources into chunks
array=()
while [[ "$string" ]]; do
array+=( "${string%%$delim*}" )
string="${string#*$delim}"
done
micOn=1
for i in "${array[@]}"; do
grep -q "media.role = \"phone\"" <<< "$i"
if [ $? -eq 0 ]; then
grep -q "Corked: no" <<< "$i"
if [ $? -eq 0 ]; then
micOn=0
fi
fi
done
if [[ $videoOn -eq 0 ]]
then
echo "A camera is in use"
curl -X POST -H "Authorization: Bearer $ha_auth_key" \
-H "Content-Type: application/json" \
-d '{"entity_id": "light.ewelight_zb_cl01_2a6509c8_level_light_color_on_off", "rgb_color": [215,0,0]}' \
$ha_address/api/services/light/turn_on
else
echo "No camera in use"
curl -X POST -H "Authorization: Bearer $ha_auth_key" \
-H "Content-Type: application/json" \
-d '{"entity_id": "light.ewelight_zb_cl01_2a6509c8_level_light_color_on_off"}' \
$ha_address/api/services/light/turn_off
fi
[ $micOn -eq 0 ] && echo "Microphone is in use" || echo "Microphone not in use"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment