手机静音找不到怎么办?原创全网最优雅的找手机方法 米家智能网关接入home assistant
技术爬爬虾
编辑于 2023年11月26日 03:19

本期视频:

爬爬虾我是一个粗心大意的人,经常把手机落在家里的某个角落,一天要找八百遍手机。跟我一样的朋友可以点个弹幕扣个1。 这时候最简单的办法是拿我老婆的手机给自己打个电话,当然免不了会被她嘲讽一番,显得我很不优雅。还有个办法是打开手机的语音唤醒,找不到手机的时候就大喊手机语音助手的名字,缺点是一直开着唤醒增加耗电,在家里大喊大叫也很不优雅。 于是我定制了一个找手机的家庭应用,这是一个小米无线蓝牙开关,单机开关就是通过微信机器人提醒发声,我拿着不停按开关,顺着声音就能找到手机。有的观众朋友可能要问了,如果手机开了静音,或者连着耳机发不出提醒你又该如何应对??这种情况我就双击开关,双击是直接调用手机厂商的查找手机功能,可以无视手机静音状态,最大音量发出鸣笛。 这样我一个开关就解决找手机的难题,手机后台不用装任何软件,不增加耗电,可以说是十分优雅。

蓝牙网关接入HomeAssistant

首先第一步,我们将这个蓝牙开关接入HomeAssistant 以下简称HA , 我们先从家庭服务器上安装HA,安装HACS插件市场,对这一步不熟悉的朋友可以看一下我的这两期视频。 这个蓝牙开关需要接入蓝牙网关才可以使用,所以我这里我的网关是小米多模网关2。这里我先装好这俩设备,并且接入米家。这里有个重点 一定要禁止米家的固件自动升级,然后小米多模网关2也不要升级固件,就让他保持在原始版本。

这里看到HA支持的小米网关版本是1.0.3-1.0.6,如果升级到了1.0.7就无法接入HA了。

我们先打开HACS插件仓库,搜索Xiaomi gateway3,将这个插件下载下来。如果已经装过插件就选择redownlaod将插件更新一下。

我们打开配置->设备->添加集成->搜索Xiaomi gateway3,然后选择第一个登陆米家账户。

登录后点击集成条目里面的选项

然后找到米家多模网关,

将以下网关的信息保存好

然后添加条目,选择添加网关,

填好网关tonken,key提交即可。

修改HA配置文件

打开HA的notepad.cmd脚本 我们来修改配置文件,在最下面添加这一段

代码块
YAML
自动换行
复制代码
rest_command:
  wechat_msg:
    url: "http://localhost:9999/text"
    method: post
    content_type: "application/json"
    payload: '{"receiver":"wxid_xxxxxxxxxx","msg":"{{msg}}"}'
  search_realme:
    url: "http://localhost:9998/search_realme"
    method: post
复制成功

这里wechat_msg是调用微信机器人发消息,search_realme是调用我手机的云服务查找手机。注意将wxid_xxxxxxxxxx修改成自己的微信ID,有关微信机器人的内容可以参看这篇:

https://www.bilibili.com/read/cv27878465/​

关联按钮事件

Xiaomi Gateway 3 找到里面的设备,选择Xiaomi wireless switch,在自动化上点击加号

选择按钮点击,进行如下配置:

点击保存即配置成功

云服务查找手机

如果手机静音了,发不出微信提醒怎么办?

这时候就要用到手机的厂商的查找手机功能了。我写了一个爬虫用来自动手机的云服务网站,并且点击查找手机按钮。

电脑安装Python,并且安装以下Python依赖

代码块
Shell
自动换行
复制代码
pip3 install selenium beautifulsoup4 webdriver-manager fastapi uvicorn -i https://mirrors.aliyun.com/pypi/simple/
复制成功

爬虫源代码,启动python 爬虫我们就拥有了一个9998端口的爬虫服务,HA通过调用http://localhost:9998/search_realme唤醒爬虫,爬虫自动登陆手机云服务网站,并且点击查找手机按钮。

代码块
Python
自动换行
复制代码
import time
import uvicorn
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service as EdgeService
from fastapi import FastAPI
from webdriver_manager.microsoft import EdgeChromiumDriverManager


def login(driver, user_name, pass_word):
    # 先登陆保存cookie
    driver.get('https://cloud.heytap.com/login.html')
    # 登陆按钮的位置
    button_xpath = "/html/body/div[1]/div[2]/div[1]/div/div/span[3]"
    # 等待页面加载完成
    WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, button_xpath)))
    button = driver.find_element(By.XPATH, button_xpath)
    # 点击按钮
    button.click()

    # 跳入iframe
    driver.switch_to.frame(0)
    user_name_xpath = "/html/body/div[2]/div[2]/div[2]/div/div/div[2]/div[1]/form/div[1]/div/div/input"
    password_xpath = "/html/body/div[2]/div[2]/div[2]/div/div/div[2]/div[1]/form/div[2]/div[1]/input"
    login_button_xpath = "/html/body/div[2]/div[2]/div[2]/div/div/div[2]/div[1]/form/div[5]/button"

    time.sleep(1)
    # 等待页面加载完成
    WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.XPATH, login_button_xpath)))
    user_name_input = driver.find_element(By.XPATH, user_name_xpath)
    user_name_input.send_keys(user_name)
    password_input = driver.find_element(By.XPATH, password_xpath)
    password_input.send_keys(pass_word)
    WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, login_button_xpath)))
    login_button = driver.find_element(By.XPATH, login_button_xpath)
    time.sleep(3)
    login_button.click()
    time.sleep(3)

def click_search_button(driver):
    # 先登陆保存cookie
    # 进入查找页面
    driver.get("https://cloud.heytap.com/pagemodule.html#/find")
    # 直接给浏览器一个假地址,避免请求位置权限
    driver.execute_script(
        "navigator.geolocation.getCurrentPosition = function(success) { success({coords: {latitude: 50.455755, longitude: 30.511565}}); }")
    # 查找按钮的位置
    button_xpath = "/html/body/div[1]/div/div/div/div[3]/div[1]/div[2]/div/div[3]/div/ul/li[1]"
    # 等待页面加载完成
    WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, button_xpath)))
    button = driver.find_element(By.XPATH, button_xpath)
    # 点击按钮
    # button.click()
    time.sleep(3)
    driver.quit()

app = FastAPI()


@app.post("/search_realme/")
def search_realme():
    # 设置浏览器参数
    options = Options()
    options.add_argument('--disable-plugins-discovery')
    options.add_argument('--mute-audio')
    options.add_argument('--no-sandbox')
    # 开启无头模式,禁用视频、音频、图片加载,开启无痕模式,减少内存占用
    # chrome_options.add_argument('--headless')  # 开启无头模式以节省内存占用,较低版本的浏览器可能不支持这一功能
    # 禁用GPU加速,避免浏览器崩溃
    options.add_argument("--disable-gpu")

    driver = webdriver.Edge(service=EdgeService(EdgeChromiumDriverManager().install()), options=options)
    # 先登录,这里配置好你的账户密码
    login(driver, "", "")
    # 进入查找页面 点击差执照那妞
    click_search_button(driver)

    return "success"

uvicorn.run(app=app, host="0.0.0.0", port=9998)
复制成功

对开关进行如下设置,双击开关即唤醒爬虫,爬虫自动登陆云服务网站,并且点击查找手机