V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xinmans
V2EX  ›  智能家电

有没有大佬玩过 homeassistant 的 API 接口控制空调呀?

  •  1
     
  •   xinmans · 12 天前 via iPhone · 356 次点击
    我现在能和小米温湿度计联动 控制开关空调和温度设置,没办法设置扫风,风速,用的 set_hvac_mode 接口,其他接口没弄明白,也没找到相关的资料。有大佬搞定了更复杂的设置么

    # for hvac
    def hvac_control(home_assistant_api, ACCESS_TOKEN, entity_id, action):
    # Home Assistant 的 API URL
    url = f'http://{home_assistant_api}:8123/api/services/climate/set_hvac_mode'

    headers = {
    'Authorization': f'Bearer {ACCESS_TOKEN}',
    'Content-Type': 'application/json'
    }

    # 请求数据,指定要关闭的实体
    data = {
    'entity_id': entity_id, # shouldn't be a set
    "hvac_mode": action # shouldn't be a string literal
    }

    # 发送 POST 请求关闭开关
    response = requests.post(url, headers=headers, json=data)
    #返回响应结果
    return(response.json())
    2 条回复    2024-06-08 22:14:52 +08:00
    Tink
        1
    Tink  
       12 天前 via iPhone
    import requests
    url = "http://your-home-assistant-url:8123/api/services/climate/set_hvac_mode"
    headers = {
    "Authorization": "Bearer YOUR_LONG_LIVED_ACCESS_TOKEN",
    "Content-Type": "application/json",
    }
    data = {
    "entity_id": "climate.living_room_ac",
    "hvac_mode": "heat" # 要切换的模式,如 cool 、heat 、off 等
    }
    response = requests.post(url, headers=headers, json=data)
    print(response.json())
    xinmans
        2
    xinmans  
    OP
       12 天前 via iPhone
    @Tink 这个我现在就在用,cool 模式,但是无法设置风速和是否关闭扫风。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5125 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 05:55 · PVG 13:55 · LAX 22:55 · JFK 01:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.