代码
1、docker-cli:
# 默认root用户运行
docker run -d -v /your/path:/app/data -e WEB_PORT=7678 -e WEB_KEY=yourkey --network host --restart unless-stopped --name powercontrol viklion/powercontrol:latest
# 设置变量PUID和PGID,指定user运行
docker run -d -v /your/path:/app/data -e WEB_PORT=7678 -e WEB_KEY=yourkey -e PUID=1000 -e PGID=100 --network host --restart unless-stopped --name powercontrol viklion/powercontrol:latest
2、docker-compose(注意检查缩进)
services:
powercontrol:
image: viklion/powercontrol:latest
container_name: powercontrol
volumes:
- /your/path:/app/data
environment:
- WEB_PORT=7678
- WEB_KEY=yourkey
# 默认root用户运行,去掉下两行的#,设置指定user运行
#- PUID=1000
#- PGID=100
restart: unless-stopped
network_mode: host
3、自定义指令sshpass
sshpass -p "登录密码" ssh -p 端口 -o "StrictHostKeyChecking=no" 登录名@192.168.xx.xx "echo 'sudo密码' | sudo -S poweroff"
# 示例:立刻关机
sshpass -p "abc123" ssh -p 22 -o "StrictHostKeyChecking=no" viklion@192.168.10.11 "echo 'abc123' | sudo -S poweroff"
# 示例:延迟关机,修改sleep后的秒数
sshpass -p "abc123" ssh -p 22 -o "StrictHostKeyChecking=no" viklion@192.168.10.11 "echo 'abc123' | sudo -S sleep 30 && echo 'abc123' | sudo -S poweroff"
4、homeassistant configuration.yaml(注意检查缩进)
shell_command:
ha_wol_pc: "curl http://192.168.2.75:1856/wol/设备id或设备别名?key=1111"
ha_shutdown_pc: "curl http://192.168.2.75:1856/shutdown/设备id或设备别名?key=1111"
input_text:
powercontrol_state:
name: powercontrol_state
initial: "off"
sensor:
- platform: rest
name: "powercontrol_pc_state"
resource: "http://192.168.2.75:1856/ping/设备id或设备别名?key=1111"
method: GET
scan_interval: 60
value_template: "{{ value_json.device_status }}"
template:
- switch:
- default_entity_id: switch.my_pc:
name: 电脑
unique_id: my_pc_001
icon: mdi:desktop-tower-monitor
state: "{{ states('input_text.powercontrol_state') == 'on' }}"
turn_on:
- action: shell_command.ha_wol_pc
- action: input_text.set_value
target:
entity_id:
- input_text.powercontrol_state
data:
value: "on"
turn_off:
- action: shell_command.ha_shutdown_pc
- action: input_text.set_value
target:
entity_id:
- input_text.powercontrol_state
data:
value: "off"
5、homeassistant automations.yaml(注意检查缩进)
- id: '20250108'
alias: 更新电脑在线状态
triggers:
- trigger: time_pattern
minutes: "/1"
conditions:
- condition: template
value_template: "{{ states('sensor.powercontrol_pc_state') != states('input_text.powercontrol_state') }}"
actions:
- action: input_text.set_value
target:
entity_id: input_text.powercontrol_state
data:
value: "{{ states('sensor.powercontrol_pc_state') }}"