创建homeassistant websocket连接

ws://ip:8123/api/websocket建立连接

返回信息

{
    "type": "auth_required",
    "ha_version": "2024.10.1"
}

然后发送认证信息

发送信息

{
    "type": "auth",
    "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIxZDhmODk4YWE2Mjc0N2YyYjAxZWFlZjRhYjQ0YWIzOSIsImlhdCI6MTcyOTA0MjM1NSwiZXhwIjoyMDQ0NDAyMzU1fQ.qKzGUgqaY_hJRJNEqPkIco3y3IvsppruT_iObxXI98E"
}

返回信息

{
    "type": "auth_ok",
    "ha_version": "2024.10.1"
}

从此连接成功

建立订阅设备变化信息

发送此消息后,当homeassistant中设备发生变化后就会将消息发送给连接客户端

发送信息

{
  "id": 18,
  "type": "subscribe_events",
  "event_type": "state_changed"
}

返回信息

会一直发送变化信息

{
  "type": "event",
  "event": {
    "event_type": "state_changed",
    "data": {
      "entity_id": "climate.kong_diao",
      "old_state": {
        "entity_id": "climate.kong_diao",
        "state": "heat",
        "attributes": {
          "hvac_modes": [
            "off",
            "fan_only",
            "cool",
            "heat"
          ],
          "min_temp": 5.0,
          "max_temp": 35.0,
          "target_temp_step": 1.0,
          "current_temperature": 24.1,
          "temperature": 21.0,
          "friendly_name": "空调",
          "supported_features": 385
        },
        "last_changed": "2024-10-14T08:30:20.722652+00:00",
        "last_reported": "2024-10-16T02:12:11.129586+00:00",
        "last_updated": "2024-10-16T02:12:11.129586+00:00",
        "context": {
          "id": "01JA9HHN7STQ6PXMWBMD1NHY2T",
          "parent_id": null,
          "user_id": null
        }
      },
      "new_state": {
        "entity_id": "climate.kong_diao",
        "state": "heat",
        "attributes": {
          "hvac_modes": [
            "off",
            "fan_only",
            "cool",
            "heat"
          ],
          "min_temp": 5.0,
          "max_temp": 35.0,
          "target_temp_step": 1.0,
          "current_temperature": 24.4,
          "temperature": 21.0,
          "friendly_name": "空调",
          "supported_features": 385
        },
        "last_changed": "2024-10-14T08:30:20.722652+00:00",
        "last_reported": "2024-10-16T02:13:18.874876+00:00",
        "last_updated": "2024-10-16T02:13:18.874876+00:00",
        "context": {
          "id": "01JA9HKQCT9VR64P0APKW62RCJ",
          "parent_id": null,
          "user_id": null
        }
      }
    },
    "origin": "LOCAL",
    "time_fired": "2024-10-16T02:13:18.874876+00:00",
    "context": {
      "id": "01JA9HKQCT9VR64P0APKW62RCJ",
      "parent_id": null,
      "user_id": null
    }
  },
  "id": 18
}

取消订阅事件

发送信息

需要将发送订阅信息的id记录,并且在发送取消订阅信息事件时在subscription 中传入

{
  "id": 19,
  "type": "unsubscribe_events",
  "subscription": 18
}

返回信息

{
  "id": 19,
  "type": "result",
  "success": true,
  "result": null
}

取消订阅成功

homeassistant隐藏接口

获取所有设备注册表信息

发送格式

{
    "id": 1,
	"type": "config/device_registry/list"
}

返回信息切片

{
    "area_id": null,
    "configuration_url": null,
    "config_entries": [
        "01J9XC0FZ9W7CZNKS8QM88VYZ4"
    ],
    "connections": [

    ],
    "created_at": 1728636275.121599,
    "disabled_by": null,
    "entry_type": null,
    "hw_version": null,
    "id": "886d865f6a3ffaffee5e503ab81c4851",
    "identifiers": [
        [
            "tuya",
            "6c66dd3c72a55825114qrp"
        ]
    ],
    "labels": [

    ],
    "manufacturer": "Tuya",
    "model": "Temperature Humidity Sensor",
    "model_id": "qaaysllp",
    "modified_at": 1728735869.476355,
    "name_by_user": null,
    "name": "温湿度传感器",
    "primary_config_entry": "01J9XC0FZ9W7CZNKS8QM88VYZ4",
    "serial_number": null,
    "sw_version": null,
    "via_device_id": null
}

获取设备的实体id

发送信息

{
    "id": 2,
    "item_id": "886d865f6a3ffaffee5e503ab81c4851",    //为获取所有设备注册表信息返回信息中的id属性
    "item_type": "device",
    "type": "search/related"
}

返回信息

{
    "id": 3,
    "type": "result",
    "success": true,
    "result": {
        "config_entry": [
            "01J9XC0FZ9W7CZNKS8QM88VYZ4"
        ],
        "integration": [
            "tuya"
        ],
        "entity": [		//返回信息的实体
            "sensor.wen_shi_du_chuan_gan_qi_humidity",
            "sensor.wen_shi_du_chuan_gan_qi_temperature",
            "sensor.wen_shi_du_chuan_gan_qi_battery",
            "sensor.wen_shi_du_chuan_gan_qi_illuminance",
            "switch.wen_shi_du_chuan_gan_qi_switch"
        ]
    }
}