官网文档位于:https://developers.home-assistant.io/docs/api/rest/

Home Assistant的API
需要在configuration.ymal中启用:

启用Home Assistant的API

在configuration.ymal中启用Home Assistant的API
然后获取访问API时使用的Token:

获取Home Assistant的API Token
然后用curl测试一下:
1 2 3 4 |
curl -X GET \ -H "Authorization: Bearer eyJ*****************************************" \ -H "Content-Type: application/json" \ http://172.16.1.51:8123/api/ |

获取Home Assistant的API测试
使用/api/services会Returns an array of service objects. Each object contains the domain and which services it contains:
1 2 3 4 |
curl -X GET \ -H "Authorization: Bearer eyJ*****************************************" \ -H "Content-Type: application/json" \ http://172.16.1.51:8123/api/services |

Home Assistant列出所有服务
这是一个很大的Json,我们只关注其中有关开关的:

Home Assistant列出所有服务(JSON格式)
然后使用/api/states/<entity_id>返回对应开关的状态:
1 2 3 4 |
curl -X GET \ -H "Authorization: Bearer eyJ*****************************************" \ -H "Content-Type: application/json" \ http://172.16.1.51:8123/api/states/switch.xiaomi_plug |

Home Assistant获取实体状态
然后使用/api/services/<domain>/<service>操作开关即可:
1 2 3 4 5 |
curl -X POST \ -H "Authorization: Bearer eyJ*****************************************" \ -H "Content-Type: application/json" \ -d '{"entity_id": "switch.xiaomi_plug"}' \ http://172.16.1.51:8123/api/services/switch/turn_on |
转载时请保留出处,违法转载追究到底:进城务工人员小梅 » Home Assistant/Hassos使用API并操作小米米家智能插座