有趣的想法2023 年 10 月 10 日

有趣的想法 - Mac控制家中米家设备

App、语音可以控制之后,又想到能否在Mac上控制。
Desk Light
Desk Light
家里有吸顶灯、床头灯,都已经可以在iphone上通过siri来控制了,这次想要达成的目标是: 能够在Mac上控制灯。

1. 最初:设想通过Api控制,调用API来实现灯的开、关。

通过查找资料,找到开源库:miiocli,详情请查看对应文档。

This library (and its accompanying cli tool, miiocli) can be used to control devices using Xiaomi’s miIO and MIoT protocols.

python-miio
python-miio

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']
至此,可以通过命令的方式在Mac上控制米家灯咯。Wow 🔥

2. 又想:敲命令太麻烦,最好还是通过自动化来执行吧。

我先把开、关的行为封装成两个脚本:light_on.shlight_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 设定一条快捷指令来自动化执行,带点场景的味道

锁屏后,自动关灯

打开Mac的快捷指令,添加锁屏,添加shell脚本执行,保存快捷指令至Dock栏。
快捷指令
快捷指令
执行快捷指令,屏幕锁定,灯光关闭。Nice 🔥🔥

3. 不行,得加上解锁时开灯才比较好。

我想要做到:Mac解锁时可以开灯,锁定时可以关灯。 这样比较符合个人氛围习惯。
查找后,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"
新建文稿作为锁定执行脚本, 另存为文本:light_off
do shell script "/WorkBench/BackendPJ/Jupyter/light_off.sh"
保存后文件后缀为.applescript
  • 3.2 将脚本放置在对应目录
打开 Finder,在菜单栏中选择「前往」,然后点击「前往文件夹」选项(或使用快捷键Shift+Command+G),在弹出的对话框中输入路径~/Library/Application Support/MacID 并按下回车。
MacID AppleScript
MacID AppleScript
light_on.applescript放置在onUnLock; light_off.applescript放置在onLock
接下来就是见证奇迹的时刻 🔥🔥🔥:
敲击锁屏快捷键,锁屏 -> 灯灭; 输入密码,解锁Mac -> 灯开。

4. Want More 🚀

  • 你可以与NearLock结合,离开Mac时锁定,灯灭;靠近Mac工作时,自动解锁Mac,灯开。

  • 你可以与Apple Watch结合解锁Mac, 控制灯光。意味着:你戴着Apple Watch坐在电脑面前时,自动解锁Mac,打开氛围灯。