input_boolean:
  magic_cube_enabled:
    name: Enable Cube
    icon: mdi:cube-outline

input_select:
  magic_cube_mode:
    name: Cube Mode
    icon: mdi:cube-scan
    options:
      - music
      - light

automation:
  - alias: Magic Cube · Mode · Change
    trigger:
      platform: event
      event_type: zha_event
      event_data:
        device_ieee: !secret magic_cube_ieee
        command: slide
    action:
      - service: input_select.select_option
        entity_id: input_select.magic_cube_mode
        data_template:
          option: >-

            {% if trigger.event.data["args"]["activated_face"] == 5 %}
              music
            {% elif trigger.event.data["args"]["activated_face"] == 2 %}
              light
            {% else %}
              {{ states('input_select.magic_cube_mode') }}
            {% endif %}

  # Lighting
  - alias: Magic Cube · Lighting · Toggle on Knock
    trigger:
      platform: event
      event_type: zha_event
      event_data:
        device_ieee: !secret magic_cube_ieee
        command: knock
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: input_boolean.magic_cube_enabled
          state: "on"
        - condition: state
          entity_id: input_select.magic_cube_mode
          state: "light"
    action:
      - service: light.toggle
        entity_id: light.living_room

  - alias: Magic Cube · Lighting · Dim on rotate left
    trigger:
      platform: event
      event_type: zha_event
      event_data:
        device_ieee: !secret magic_cube_ieee
        command: rotate_left
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: input_boolean.magic_cube_enabled
          state: "on"
        - condition: state
          entity_id: input_select.magic_cube_mode
          state: "light"
    action:
      - service: light.turn_on
        entity_id: light.living_room
        data:
          brightness_step_pct: -20

  - alias: Magic Cube · Lighting · Brighten on rotate right
    trigger:
      platform: event
      event_type: zha_event
      event_data:
        device_ieee: !secret magic_cube_ieee
        command: rotate_right
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: input_boolean.magic_cube_enabled
          state: "on"
        - condition: state
          entity_id: input_select.magic_cube_mode
          state: "light"
    action:
      - service: light.turn_on
        entity_id: light.living_room
        data:
          brightness_step_pct: 20

  # Music
  - alias: Magic Cube · Music · Play/Pause on Knock
    trigger:
      platform: event
      event_type: zha_event
      event_data:
        device_ieee: !secret magic_cube_ieee
        command: knock
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: input_boolean.magic_cube_enabled
          state: "on"
        - condition: state
          entity_id: input_select.magic_cube_mode
          state: "music"
    action:
      - service: media_player.media_play_pause
        entity_id: media_player.living_room

  - alias: Magic Cube · Music · Volume down on rotate left
    trigger:
      platform: event
      event_type: zha_event
      event_data:
        device_ieee: !secret magic_cube_ieee
        command: rotate_left
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: input_boolean.magic_cube_enabled
          state: "on"
        - condition: state
          entity_id: input_select.magic_cube_mode
          state: "music"
    action:
      - service: media_player.volume_down
        entity_id: media_player.living_room

  - alias: Magic Cube · Music · Volume up on rotate right
    trigger:
      platform: event
      event_type: zha_event
      event_data:
        device_ieee: !secret magic_cube_ieee
        command: rotate_right
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: input_boolean.magic_cube_enabled
          state: "on"
        - condition: state
          entity_id: input_select.magic_cube_mode
          state: "music"
    action:
      - service: media_player.volume_up
        entity_id: media_player.living_room

#
# ─── CUSTOMIZATION ──────────────────────────────────────────────────────────────
#

homeassistant:
  customize:
    sensor.cube_multistate_input:
      friendly_name: Cube input

    sensor.cube_analog_input:
      friendly_name: Cube rotation

    sensor.cube_power:
      friendly_name: Cube battery
