在SELFHOSTED发现了貌似HomeAssistant一键睡眠/唤醒linux的帖子,想着在Windows也试一下
就不用在早上离开被窝开机,睡觉时候离开被窝关机 ;)
虽然说通过RD客户端也可以实现,但是我还是喜欢“一键启动”的方式
docker-compose.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13
| version: '3' services: homeassistant: container_name: homeassistant image: "ghcr.io/home-assistant/home-assistant:stable" volumes: - ./config:/config environment: - TZ=Asia/ShangHai ports: - 8123:8123 restart: unless-stopped privileged: true
|
一键睡眠/唤醒PC
Ref: https://www.ninjazumbi.com/articles/linux/ha-power-mgmt.html#ha-power-mgmt
1.config添加两个文件
sleep.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #!/bin/bash
sshpass -p 00294572 ssh Dell@10.10.22.46 "rundll32.exe powrprof.dll,SetSuspendState 0,1,0"
|
wake.sh
1 2
| #!/bin/bash wakeonlan -i 10.10.22.254 C0-25-A5-BC-A9-F1
|
2.configuration.yaml添加配置
1 2 3
| shell_command: sleep_script: /config/sleep.sh wake_script: /config/wake.sh
|
3.scripts.yaml添加配置
1 2 3 4 5 6 7 8 9 10 11 12
| pc_sleep: alias: pc_sleep sequence: - service: shell_command.sleep_script data: {} mode: single pc_wake: alias: pc_wake sequence: - service: shell_command.wake_script data: {} mode: single
|
4.HA添加仪表盘
配置-添加仪表盘-进入仪表盘-添加卡片-点击按钮
5.系统睡眠之后,运行在windows的HA就无法被调用,此时可以用Wake On Lan 安卓应用配置开启
使用代码也可达到一样的效果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import socket import struct
def wake_on_lan(mac_address): mac_bytes = bytes.fromhex(mac_address.replace(':', '').replace('-', '').replace('.', ''))
magic_packet = b'\xFF' * 6 + mac_bytes * 16
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) s.sendto(magic_packet, ('<broadcast>', 9))
wake_on_lan('C0:25:A5:BC:A9:F2')
|