Questions on REST Integration

I’ve implemented a very simple REST API on top of bluetoothctl to allow me to connect and disconnect various bluetooth headphones/speakers on a headless media streaming server.

In HA I have a switch

switch:
  - platform: rest
    name: "WH-1000XM4"
    resource: "http://192.168.0.2:5000/bluetoothctl/88:C9:E8:1C:F7:B2"
    body_on: '{"connect":"true"}'
    body_off: '{"connect":"false"}'
    is_on_template: "{{ value_json.connected }}"
    headers:
      Content-Type: application/json

Which allows me to connect and disconnect the headphones. But I would also like to be able to query the current codec and be able to change it. At this point I’m completely lost. Can this all be done within the switch entity, or do I need to be using a dfferent entity type?

The API response is this

{
    "connected": true,
    "active_codec": "ldac_hq",
    "available_codecs": [
        {
            "name": "sbc",
            "description": "SBC"
        },
        {
            "name": "sbc_xq_453",
            "description": "SBC XQ 453kbps"
        },
        {
            "name": "sbc_xq_512",
            "description": "SBC XQ 512kbps"
        },
        {
            "name": "sbc_xq_552",
            "description": "SBC XQ 552kbps"
        },
        {
            "name": "ldac_hq",
            "description": "LDAC (High Quality)"
        },
        {
            "name": "ldac_sq",
            "description": "LDAC (Standard Quality)"
        },
        {
            "name": "ldac_mq",
            "description": "LDAC (Mobile Quality)"
        }
    ]
}

So I’d need to store active codec and supported codecs in attributes and then have a way of selecting from the available list in HA to trigger a POST call back to the API.

Any pointers on how to implement this? It’s way beyond my current HA knowledge.

Create a template select entity: Template - Home Assistant

The options are the available codecs and the action would be to call your restful post command, passing the selected option to the rest command.

Thanks. I’ve ended up with this, which seems to work.

platform: rest
    name: "WH-1000XM4"
    resource: "http://192.168.0.2:5000/bluetoothctl/88:C9:E8:1C:F7:B2"
    body_on: '{"connect":"true"}'
    body_off: '{"connect":"false"}'
    is_on_template: "{{ value_json.connected }}"
    headers:
      Content-Type: application/json

sensor:
  - platform: rest
    name: "WH-1000XM4"
    resource: "http://192.168.0.2:5000/bluetoothctl/88:C9:E8:1C:F7:B2"
    json_attributes:
      - active_codec
      - available_codecs
    value_template: "{{ 'connected' if value_json.connected else 'disconnected' }}"

rest_command:
  set_codec:
    url: "http://192.168.0.2:5000/bluetoothctl/{{ address }}"
    method: post
    content_type: application/json
    payload: '{"codec":"{{ codec }}"}'

template:
  - select:
    - name: "WH-1000XM4"
      state: "{{ state_attr('sensor.wh_1000xm4', 'active_codec') }}"
      options: "{{ state_attr('sensor.wh_1000xm4', 'available_codecs') | map(attribute='name') | list }}"
      select_option:
        - action: rest_command.set_codec
          data:
            codec: "{{ option }}"
            address: "88:C9:E8:1C:F7:B2"
1 Like