-
Notifications
You must be signed in to change notification settings - Fork 0
/
disconnect_from_wifi_when_on_ethernet.lua
65 lines (52 loc) · 2.5 KB
/
disconnect_from_wifi_when_on_ethernet.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require("utils/log")
-----------------------------------------------------------------------------------------------
-- Toggle Wifi based on Ethernet status and Screen Lock status
-----------------------------------------------------------------------------------------------
local ethernetInterface = "en6" -- Change to your Ethernet interface identifier
local wifiInterface = "en0" -- Change to your WiFi interface identifier
local function isEthernetActive(interface)
local interfaceDetails = hs.network.interfaceDetails(interface)
return interfaceDetails and interfaceDetails.IPv4 ~= nil
end
local function isWifiPowered(interface)
local wifiDetails = hs.wifi.interfaceDetails(interface)
return wifiDetails and wifiDetails.power
end
local function logNetworkStatus(ethernetActive, wifiPower)
log("🌐 Network Status: Ethernet Interface - " .. ethernetInterface ..
", WiFi Interface - " .. wifiInterface)
log("🌐 Network Status: Ethernet Active - " .. tostring(ethernetActive) ..
", WiFi Power - " .. tostring(wifiPower))
end
local function logScreenLockStatus(screenLocked)
log("🔒 Screen Locked Status: " .. tostring(screenLocked))
log("🕒 Timestamp: " .. os.date("%c"))
end
local function toggleWifiBasedOnEthernetAndScreenLock(screenLocked)
local ethernetActive = isEthernetActive(ethernetInterface)
local wifiPower = isWifiPowered(wifiInterface)
logNetworkStatus(ethernetActive, wifiPower)
if screenLocked == nil then
screenLocked = hs.caffeinate.get("displayIdle")
end
logScreenLockStatus(screenLocked)
log("🕒 Timestamp: " .. os.date("%c"))
if ethernetActive and not screenLocked and wifiPower then
hs.wifi.setPower(false, wifiInterface)
log("🔓 Screen Unlocked & Ethernet Connected: 📶 WiFi turned off.")
elseif not ethernetActive or screenLocked and not wifiPower then
hs.wifi.setPower(true, wifiInterface)
log("📶 WiFi turned on for connectivity.")
end
end
-- Execute the callback once at Hammerspoon startup to ensure correct network status
toggleWifiBasedOnEthernetAndScreenLock()
-- Watch for network changes
wifiWatcher = hs.network.reachability.internet():setCallback(function(event)
toggleWifiBasedOnEthernetAndScreenLock()
end):start()
-- Watch for screen lock/unlock events
screenWatcher = hs.caffeinate.watcher.new(function(event)
local screenLocked = event == hs.caffeinate.watcher.screensDidLock
toggleWifiBasedOnEthernetAndScreenLock(screenLocked)
end):start()