Last active
January 11, 2024 09:10
-
-
Save dacer/7d5110659de11943c1b4cccabf23cb93 to your computer and use it in GitHub Desktop.
Send a post request when any of your Mac's cameras are on or off. (https://www.hammerspoon.org/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
API_URL = 'http://192.168.1.2:8123/api/services/switch/' | |
function sendCameraStatusToHomeAssistant(inUse) | |
print("sending meeting signal: " .. tostring(inUse)) | |
local endpoint = inUse and 'turn_on' or 'turn_off' | |
local fullUrl = API_URL .. endpoint | |
local jsonData = '{"entity_id": "switch.your_id"}' | |
hs.http.post(fullUrl, jsonData, { | |
['Content-Type'] = 'application/json; charset=utf-8', | |
['Authorization'] = 'Bearer YOUR_TOKEN' | |
}) | |
end | |
function atLeastOneCameraInUse() | |
for key, camera in pairs(hs.camera.allCameras()) do | |
if camera:isInUse() then | |
return true | |
end | |
end | |
return false | |
end | |
function cameraPropertyWatcher(camera, _changedProperty, _scope, _element) | |
sendCameraStatusToHomeAssistant(atLeastOneCameraInUse()) | |
end | |
function listenCamera(camera) | |
print("listenCamera:" .. camera:name()) | |
camera:setPropertyWatcherCallback(cameraPropertyWatcher) | |
camera:startPropertyWatcher() | |
end | |
function cameraWatcher(camera, event) | |
if event == 'Added' then | |
listenCamera(camera) | |
end | |
end | |
hs.camera.setWatcherCallback(cameraWatcher) | |
hs.camera.startWatcher() | |
for key, val in pairs(hs.camera.allCameras()) do | |
listenCamera(val) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment