有趣的想法 - Mac控制家中米家设备
iphone
上通过siri
来控制了,这次想要达成的目标是:
能够在Mac
上控制灯。1. 最初:设想通过Api控制,调用API来实现灯的开、关。
This library (and its accompanying cli tool, miiocli) can be used to control devices using Xiaomi’s miIO and MIoT protocols.
ps: 最近在搞ChatGPT,python环境本地已经ready,库安装过程省略
- 1.1 登录获取设备列表
miiocli cloud
Username: your_email or your_Phone
Password: your_password
---
== 米家床头灯 (已开灯 ) ==
Model: yeelink.*.*
Token: fac******************
IP: 192.*.*.* (mac: *:*:*:*:*:*)
DID: 8*******
Locale: cn
- 1.2 控制设备
genericmiot
,则需要通过关联模块来操作Note that the command field which gives you the direct command to use for controlling the device. If the device is supported by the genericmiot integration as stated in the output, you can also use miiocli genericmiot for controlling it.
设备如果支持genericmiot则会在设备列表command字段显示可用命令
miiocli --help
---
Usage: miiocli [OPTIONS] COMMAND [ARGS]...
Options:
-d, --debug
-o, --output [default|json|json_pretty]
--version Show the version and exit.
--help Show this message and exit.
Commands:
airconditionermiot
airconditioningcompanion
airconditioningcompanionmcn02
airconditioningcompanionv3
airdehumidifier
airdogx3
airfresh
airfresha1
airfresht2017
airhumidifier
airhumidifierjsq
airhumidifierjsqs
airhumidifiermiot
airhumidifiermjjsq
airpurifier
airpurifiermiot
airqualitymonitor
airqualitymonitorcgdn1
alarmclock
aqaracamera
ceil
chuangmicamera
chuangmiir
chuangmiplug
cloud Cloud commands.
cooker
curtainmiot
device
discover Discover devices using both handshake...
dreamevacuum
fan
fan1c
fanleshow
fanmiot
fanp5
fanza5
g1vacuum
gateway
heater
heatermiot
huizuo
huizuolampfan
huizuolampheater
huizuolampscene
miotdevice
petwaterdispenser
philipsbulb
philipseyecare
philipsmoonlight
philipsrwread
philipswhitebulb
powerstrip
pwznrelay
roborockvacuum
roidmivacuummiot
scisharecoffee
toiletlid
viomivacuum
walkingpad
waterpurifier
waterpurifieryunmi
wifirepeater
wifispeaker
yeelight
yeelightdualcontrolmodule
yeelight
模块来操作我的米家床头灯:miiocli yeelight --help
---
Usage: miiocli yeelight [OPTIONS] COMMAND [ARGS]...
Options:
--ip TEXT [required]
--token TEXT [required]
--model TEXT
--help Show this message and exit.
Commands:
dump_ble_debug Dump the BLE debug table, defaults to...
info Get (and cache) miIO protocol information...
off Power off.
on Power on.
raw_command Send a raw command to the device.
set_brightness Set brightness.
set_color_temp Set color temp in kelvin.
set_default Set current state as default.
set_developer_mode Enable or disable the developer mode.
set_name Set an internal name for the bulb.
set_rgb Set color in RGB.
set_save_state_on_change Enable or disable saving the state on changes.
status Retrieve properties.
test_properties Helper to test device properties.
toggle Toggle bulb state.
- 操作控制指令的ip\token从设备列表可以获取到
miiocli yeelight --ip 192.*.*.* --token fac***************** off
---
Powering off
['ok']
miiocli yeelight --ip 192.*.*.* --token fac***************** on
---
Powering on
['ok']
miiocli yeelight --ip 192.*.*.* --token fac***************** set_rgb 255 0 0
---
Setting color to (255, 0, 0)
['ok']
miiocli yeelight --ip 192.*.*.* --token fac***************** set_brightness 100
---
Setting brightness to 100
['ok']
2. 又想:敲命令太麻烦,最好还是通过自动化来执行吧。
light_on.sh
和 light_off.sh
light_on.sh
---
#!/bin/bash
cd /WorkBench/BackendPJ/Jupyter
source jenv/bin/activate
miiocli yeelight --ip 192.*.*.* --token fac***************** on
miiocli yeelight --ip 192.*.*.* --token fac***************** set_brightness 100
light_off.sh
---
#!/bin/bash
cd /Desktop/WorkBench/BackendPJ/Jupyter
source jenv/bin/activate
miiocli yeelight --ip 192.*.*.* --token fac***************** off
./light_on.sh
和./light_off.sh
即可开灯和关灯- 2.1 设定一条快捷指令来自动化执行,带点场景的味道
锁屏后,自动关灯
3. 不行,得加上解锁时开灯才比较好。
MacID
符合我的要求:可以通过 AppleScript 对 MacID 进行扩展。具体来说,你可以让 MacID 在 Mac 睡眠、唤醒、锁屏、解锁时,运行预先定义好的 AppleScript 脚本文件。举个例子,你可以让 Mac 锁屏时自动暂停 itunes 音乐播放,之后解锁 Mac 时再接着播放。 只需要将你的AppleScript脚本放在对应的目录中即可。
AppleScript
调用shell
脚本:do shell script "/path/to/script.sh"
- 3.1 打开脚本编辑器App 新建文稿作为解锁执行脚本, 另存为文本:light_on
do shell script "/WorkBench/BackendPJ/Jupyter/light_on.sh"
do shell script "/WorkBench/BackendPJ/Jupyter/light_off.sh"
.applescript
- 3.2 将脚本放置在对应目录
light_on.applescript
放置在onUnLock
; light_off.applescript
放置在onLock
4. Want More
🚀
-
你可以与
NearLock
结合,离开Mac时锁定,灯灭;靠近Mac工作时,自动解锁Mac,灯开。 -
你可以与
Apple Watch
结合解锁Mac, 控制灯光。意味着:你戴着Apple Watch
坐在电脑面前时,自动解锁Mac,打开氛围灯。
作者: Henry He 发表日期:2023 年 10 月 10 日