Simple http server that triggers system commands on predefined URLs
- written in Node.js with minimum dependencies
- support Apple intel platform only, for Apple ARM64 please check the switch-api-rs project
- used in conjunction with https://home-assistant.io/components/switch.rest/
- http://pm2.keymetrics.io for automatic startup
GET /display
: get current screen state (sleeping or not)POST /display body=ON|OFF
: switch screeen state
switch:
- platform: rest
name: Macbook Display
resource: 'http://mymac:8182/display'
GET /power
sensor:
- platform: rest
name: Macbook battery
resource: 'http://mymac:8182/power'
json_attributes:
- isOnAC
- isOnBattery
- isCharged
- chargingStatus
- chargePercent
- remainingChargeTime
- message
value_template: '{{ value_json.chargePercent }}'
unit_of_measurement: '%'
GET /cpu
/cpu?avg=1
last mn/cpu?avg=5
last 5mn/cpu?avg=15
last 15mn
sensor:
- platform: rest
name: Macbook CPU
resource: 'http://mymac:8182/cpu'
value_template: '{{ value | round(1) }}'
unit_of_measurement: '%'
node app.js --verbose
Sat Dec 21 2019 09:01:49 GMT-0600 (Central Standard Time) - switch-api server started on http://localhost:8182
Sat Dec 21 2019 09:02:03 GMT-0600 (Central Standard Time) - GET /cpu
Sat Dec 21 2019 09:02:17 GMT-0600 (Central Standard Time) - GET /display
- sleep display:
pmset displaysleepnow
- wake display:
caffeinate -u -t 1
- test state :
pmset -g powerstate IODisplayWrangler | tail -1 | cut -c29
result <4 are sleeping - battery power status :
pmset -g batt
(and a painful parsing)