小米电视api方式驱动更改相关指令

发布于 2024-07-13  279 次阅读


# 写一个链接mqtt 监听/home-self/miTV/mode 判断收到的字符串 切换小米电视模式。

import paho.mqtt.client as mqtt
import requests
import time


# MQTT连接回调函数
def on_connect(client, userdata, flags, rc):
    with open("mitv_data.log", "a") as f:
        f.write(
            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            + " Connected with result code "
            + str(rc)
            + "\n"
        )
    client.subscribe("/home-self/miTV/mode")
    client.subscribe("/home-self/miTV/key")


# MQTT消息回调函数
def on_message(client, userdata, msg):
    with open("mitv_data.log", "a") as f:
        f.write(
            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            + " "
            + msg.topic
            + " "
            + str(msg.payload)
            + "\n"
        )
    if msg.topic == "/home-self/miTV/mode":
        mode = msg.payload.decode("utf-8")
        # 先获取安装的app
        try:
            res = requests.get(
                f"http://192.168.2.252:6095/controller?action=getinstalledapp&count=999&changeIcon=1",
                timeout=1,
            ).json()
        except:
            return
        installed_apps = []
        for i in res["data"]["AppInfo"]:
            installed_apps.append(i["PackageName"])
        # print(installed_apps)
        # 根据mode切换小米电视模式
        if mode == "hdmi1":
            try:
                requests.get(
                    "http://192.168.2.252:6095/controller?action=changesource&source=hdmi1",
                    timeout=1,
                )
            except:
                return
        elif mode == "hdmi2":
            try:
                requests.get(
                    "http://192.168.2.252:6095/controller?action=changesource&source=hdmi2",
                    timeout=1,
                )
            except:
                return
        elif mode == "hdmi3":
            try:
                requests.get(
                    "http://192.168.2.252:6095/controller?action=changesource&source=hdmi3",
                    timeout=1,
                )
            except:
                return
        elif mode == "qqmusic" and "com.tencent.qqmusictv" in installed_apps:
            try:
                requests.get(
                    f"http://192.168.2.252:6095/controller?action=startapp&type=packagename&packagename=com.tencent.qqmusictv",
                    timeout=1,
                )
            except:
                return
        elif mode == "tvbox" and "com.github.tvbox.osc.tk" in installed_apps:
            try:
                requests.get(
                    f"http://192.168.2.252:6095/controller?action=startapp&type=packagename&packagename=com.github.tvbox.osc.tk",
                    timeout=1,
                )
            except:
                return
        elif mode == "bilibili" and "com.xiaodianshi.tv.yst" in installed_apps:
            try:
                requests.get(
                    f"http://192.168.2.252:6095/controller?action=startapp&type=packagename&packagename=com.xiaodianshi.tv.yst",
                    timeout=1,
                )
            except:
                return
        elif mode == "airdrop" and "com.xiaomi.mitv.smartshare" in installed_apps:
            try:
                requests.get(
                    f"http://192.168.2.252:6095/controller?action=startapp&type=packagename&packagename=com.xiaomi.mitv.smartshare",
                    timeout=1,
                )
            except:
                return
            time.sleep(0.5)
            send_key(right)
            time.sleep(0.1)
            send_key(down)
            time.sleep(0.1)
            send_key(enter)
    if msg.topic == "/home-self/miTV/key":
        if msg.payload.decode("utf-8") == "mute":
            for i in range(30):
                send_key(volume_down)
        if msg.payload.decode("utf-8") == "unmute":
            for i in range(30):
                send_key(volume_up)
        try:
            requests.get(
                f"http://192.168.2.252:6095/controller?action=keyevent&keycode={msg.payload.decode('utf-8')}",
                timeout=1,
            )
        except:
            return


turn_on = ["power"]
turn_off = ["power"]
sleep = ["power", "wait", "right", "wait", "right", "wait", "enter"]
wake = ["power"]
up = ["up"]
down = ["down"]
right = ["right"]
left = ["left"]
home = ["home"]
enter = ["enter"]
back = ["back"]
menu = ["menu"]
volume_down = ["volumedown"]
volume_up = ["volumeup"]


def send_key(keys):
    url = (
        f"http://192.168.2.252:6095/controller?action=keyevent&keycode={','.join(keys)}"
    )
    try:
        requests.get(url, timeout=1)
    except:
        pass


if __name__ == "__main__":
    # MQTT客户端初始化和连接
    client = mqtt.Client()
    client.on_connect = on_connect
    client.on_message = on_message
    client.username_pw_set("****", "****")
    client.connect("***.***.***.***", 1883, 60)
    client.loop_forever()

向日葵没了