#:########################################################################################:#
# Master Bedroom 4 Button Scene Switches                                                  #
#:########################################################################################:#
#
# TITLE:
#   Master Bedroom 4 Button Scene Switches
#
# FILE:
#   master_bedroom_4_button_scene_switches.yaml
#
# VERSION:
#   V1.0 2026-06-24
#
# VERSION HISTORY:
#   V1.0 2026-06-24
#     - Initial package.
#     - Added West and East Moes TS004F 4-button Zigbee2MQTT scene switches.
#     - Uses Zigbee2MQTT MQTT action payloads directly.
#
# PURPOSE:
#   Controls Master Bedroom lighting from two Zigbee2MQTT Moes 4-button scene
#   switches.
#
# NOTES:
#   Zigbee2MQTT action payloads used:
#     - 1_single = Top left
#     - 2_single = Top right
#     - 3_single = Bottom left
#     - 4_single = Bottom right
#
#   West switch:
#     - Top left     = Main Bedroom South Lights ON
#     - Top right    = Bedside Light ON
#     - Bottom left  = Main Bedroom South Lights OFF + Main Bedroom North Lights OFF
#     - Bottom right = Bedside Light OFF
#
#   East switch:
#     - Top left     = Bedside Light ON
#     - Top right    = Main Bedroom South Lights ON
#     - Bottom left  = Bedside Light OFF
#     - Bottom right = Main Bedroom South Lights OFF + Main Bedroom North Lights OFF
#
#:########################################################################################:#

#:########################################################################################:#
# AUTOMATIONS                                                                              #
#:########################################################################################:#

automation:
  - id: master_bedroom_4_button_scene_switches
    alias: "Master Bedroom - 4 Button Scene Switches"
    description: "Controls Master Bedroom lights from East and West Moes 4-button Zigbee2MQTT scene switches."
    mode: queued
    max: 10

    variables:
      west_switch_mqtt_topic: "zigbee2mqtt/Master Bedroom 4 Button Switch (West)"
      east_switch_mqtt_topic: "zigbee2mqtt/Master Bedroom 4 Button Switch (East)"

      bedside_light: "switch.master_bedroom_bedside_lamp_arlec_bulb_master_bedroom_bedside_lamp_arlec_bulb_jump_fade_up_down"
      main_bedroom_south_lights: "switch.master_bedroom_master_bedroom_main_lights_south_relay_2_master_bedroom_south_lights"
      main_bedroom_north_lights: "light.tasmo_ifan02_3793_bedrm1_1"

    triggers:
      # West switch actions
      - trigger: mqtt
        topic: "zigbee2mqtt/Master Bedroom 4 Button Switch (West)"
        value_template: "{{ value_json.action | default('') }}"
        payload: "1_single"
        id: west_top_left

      - trigger: mqtt
        topic: "zigbee2mqtt/Master Bedroom 4 Button Switch (West)"
        value_template: "{{ value_json.action | default('') }}"
        payload: "2_single"
        id: west_top_right

      - trigger: mqtt
        topic: "zigbee2mqtt/Master Bedroom 4 Button Switch (West)"
        value_template: "{{ value_json.action | default('') }}"
        payload: "3_single"
        id: west_bottom_left

      - trigger: mqtt
        topic: "zigbee2mqtt/Master Bedroom 4 Button Switch (West)"
        value_template: "{{ value_json.action | default('') }}"
        payload: "4_single"
        id: west_bottom_right

      # East switch actions
      - trigger: mqtt
        topic: "zigbee2mqtt/Master Bedroom 4 Button Switch (East)"
        value_template: "{{ value_json.action | default('') }}"
        payload: "1_single"
        id: east_top_left

      - trigger: mqtt
        topic: "zigbee2mqtt/Master Bedroom 4 Button Switch (East)"
        value_template: "{{ value_json.action | default('') }}"
        payload: "2_single"
        id: east_top_right

      - trigger: mqtt
        topic: "zigbee2mqtt/Master Bedroom 4 Button Switch (East)"
        value_template: "{{ value_json.action | default('') }}"
        payload: "3_single"
        id: east_bottom_left

      - trigger: mqtt
        topic: "zigbee2mqtt/Master Bedroom 4 Button Switch (East)"
        value_template: "{{ value_json.action | default('') }}"
        payload: "4_single"
        id: east_bottom_right

    actions:
      - choose:
          #:##############################################################################:#
          # West Switch                                                                    #
          #:##############################################################################:#

          # West top left:
          # Main Bedroom South Lights ON
          - conditions:
              - condition: trigger
                id: west_top_left
            sequence:
              - action: homeassistant.turn_on
                target:
                  entity_id: "{{ main_bedroom_south_lights }}"

          # West top right:
          # Bedside Light ON
          - conditions:
              - condition: trigger
                id: west_top_right
            sequence:
              - action: homeassistant.turn_on
                target:
                  entity_id: "{{ bedside_light }}"

          # West bottom left:
          # Main Bedroom South Lights OFF
          # Main Bedroom North Lights OFF
          - conditions:
              - condition: trigger
                id: west_bottom_left
            sequence:
              - action: homeassistant.turn_off
                target:
                  entity_id:
                    - "{{ main_bedroom_south_lights }}"
                    - "{{ main_bedroom_north_lights }}"

          # West bottom right:
          # Bedside Light OFF
          - conditions:
              - condition: trigger
                id: west_bottom_right
            sequence:
              - action: homeassistant.turn_off
                target:
                  entity_id: "{{ bedside_light }}"

          #:##############################################################################:#
          # East Switch                                                                    #
          #:##############################################################################:#

          # East top left:
          # Bedside Light ON
          - conditions:
              - condition: trigger
                id: east_top_left
            sequence:
              - action: homeassistant.turn_on
                target:
                  entity_id: "{{ bedside_light }}"

          # East top right:
          # Main Bedroom South Lights ON
          - conditions:
              - condition: trigger
                id: east_top_right
            sequence:
              - action: homeassistant.turn_on
                target:
                  entity_id: "{{ main_bedroom_south_lights }}"

          # East bottom left:
          # Bedside Light OFF
          - conditions:
              - condition: trigger
                id: east_bottom_left
            sequence:
              - action: homeassistant.turn_off
                target:
                  entity_id: "{{ bedside_light }}"

          # East bottom right:
          # Main Bedroom South Lights OFF
          # Main Bedroom North Lights OFF
          - conditions:
              - condition: trigger
                id: east_bottom_right
            sequence:
              - action: homeassistant.turn_off
                target:
                  entity_id:
                    - "{{ main_bedroom_south_lights }}"
                    - "{{ main_bedroom_north_lights }}"
