{
  "name": "Chromecast Webhook - Cast from Website (Multi-Device)",
  "nodes": [
    {
      "parameters": {
        "jsCode": "// Extract data from webhook body\nconst body = $input.first().json.body;\nconst url = body.fileUrl || body.url || body.media_url || '';\nconst type = body.fileType || body.type || 'auto';\nconst mimeType = body.mimeType || '';\nconst deviceKey = (body.device || 'space_tv').toLowerCase().replace(/\\s+/g, '_');\n\n// Device mapping - update entity_ids to match your Home Assistant\nconst devices = {\n  'gaming_tv': {\n    name: 'GamingTV TV',\n    entity_id: 'media_player.gamingtv_tv'\n  },\n  'smart_tv': {\n    name: 'SmartTV 4K',\n    entity_id: 'media_player.smarttv_4k'\n  },\n  'my_room': {\n    name: 'My room Display',\n    entity_id: 'media_player.my_room_display'\n  },\n  'space_tv': {\n    name: 'Space TV',\n    entity_id: 'media_player.chromecast4772'\n  }\n};\n\n// Find the selected device\nconst selectedDevice = devices[deviceKey];\nif (!selectedDevice) {\n  throw new Error(`Unknown device: ${deviceKey}. Available: ${Object.keys(devices).join(', ')}`);\n}\n\nlet contentId = '';\nlet contentType = '';\n\n// Auto-detect or use specified type\nif (url.includes('youtube.com') || url.includes('youtu.be')) {\n  // YouTube video ONLY\n  let videoId = '';\n  if (url.includes('youtube.com/watch?v=')) {\n    videoId = url.split('v=')[1].split('&')[0];\n  } else if (url.includes('youtu.be/')) {\n    videoId = url.split('youtu.be/')[1].split('?')[0];\n  }\n  contentId = `{ \"app_name\": \"youtube\", \"media_id\": \"${videoId}\" }`;\n  contentType = 'cast';\n} else if (type === 'image' || url.match(/\\.(jpg|jpeg|png|gif|webp)$/i)) {\n  contentId = url;\n  contentType = mimeType || 'image/jpeg';\n} else {\n  // Everything else: mp4, direct video URLs, etc.\n  contentId = url;\n  contentType = mimeType || 'video/mp4';\n}\n\nreturn {\n  json: {\n    original_url: url,\n    content_id: contentId,\n    content_type: contentType,\n    type: type,\n    fileName: body.fileName || '',\n    device_key: deviceKey,\n    device_name: selectedDevice.name,\n    entity_id: selectedDevice.entity_id\n  }\n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        112,
        352
      ],
      "id": "ac4faf90-bd6a-4ab4-9880-f42ec99ff3ec",
      "name": "Format Media"
    },
    {
      "parameters": {
        "resource": "service",
        "operation": "call",
        "domain": "media_player",
        "service": "play_media",
        "serviceAttributes": {
          "attributes": [
            {
              "name": "entity_id",
              "value": "={{ $json.entity_id }}"
            },
            {
              "name": "media_content_id",
              "value": "={{ $json.content_id }}"
            },
            {
              "name": "media_content_type",
              "value": "={{ $json.content_type }}"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.homeAssistant",
      "typeVersion": 1,
      "position": [
        336,
        352
      ],
      "id": "fcd7eda8-0331-4fd9-bd9b-b496be59e7d2",
      "name": "Cast to Chromecast",
      "credentials": {
        "homeAssistantApi": {
          "id": "1LrjflGF3O53K02s",
          "name": "Home Assistant account"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ success: true, message: 'Cast successful', url: $('Format Media').item.json.original_url, type: $('Format Media').item.json.type, device: $('Format Media').item.json.device_name }) }}",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Access-Control-Allow-Origin",
                "value": "*"
              },
              {
                "name": "Content-Type",
                "value": "application/json"
              }
            ]
          }
        }
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        560,
        352
      ],
      "id": "7188fdc8-91cd-4be5-8ded-5b7b6e351c70",
      "name": "Respond to Webhook"
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "castc35483a5",
        "responseMode": "responseNode",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -112,
        352
      ],
      "id": "ec25bb49-33dd-44a1-8411-fa4f214b30ca",
      "name": "Webhook - Cast",
      "webhookId": "chromecast-cast"
    }
  ],
  "pinData": {
    "Webhook - Cast": [
      {
        "json": {
          "headers": {
            "host": "n8n.arcelliteserver.com",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0",
            "content-length": "186",
            "accept": "*/*",
            "accept-encoding": "gzip, br",
            "accept-language": "en-US,en;q=0.9",
            "cdn-loop": "cloudflare; loops=1",
            "cf-connecting-ip": "75.188.196.234",
            "cf-ipcountry": "US",
            "cf-ray": "9cd7e5ef2bb72780-CMH",
            "cf-visitor": "{\"scheme\":\"https\"}",
            "cf-warp-tag-id": "b861dcfd-df25-4980-b9ec-9db28afcfe1e",
            "connection": "keep-alive",
            "content-type": "application/json",
            "origin": "http://192.168.5.0:3007",
            "priority": "u=1, i",
            "referer": "http://192.168.5.0:3007/",
            "sec-ch-ua": "\"Not(A:Brand\";v=\"8\", \"Chromium\";v=\"144\", \"Microsoft Edge\";v=\"144\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "cross-site",
            "x-forwarded-for": "75.188.196.234",
            "x-forwarded-proto": "https"
          },
          "params": {},
          "query": {},
          "body": {
            "fileName": "Image_fx (56).jpg",
            "fileType": "image",
            "fileUrl": "http://192.168.5.0:3007/api/files/serve?category=media&path=Image_fx%20(56).jpg",
            "mimeType": "image/jpeg",
            "device": "my_room"
          },
          "webhookUrl": "https://n8n.arcelliteserver.com/webhook/castc35483a5",
          "executionMode": "production"
        },
        "pairedItem": {
          "item": 0
        }
      }
    ]
  },
  "connections": {
    "Format Media": {
      "main": [
        [
          {
            "node": "Cast to Chromecast",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Cast to Chromecast": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook - Cast": {
      "main": [
        [
          {
            "node": "Format Media",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "availableInMCP": false
  },
  "versionId": "0c311b82-b079-44f1-a29b-957777fa094e",
  "meta": {
    "instanceId": "de60ded3609ed675dd3435a79fde884038d2348f816754f1f978c92a10e2da20"
  },
  "id": "5Z13vNuon9vVBiND1VlLh",
  "tags": []
}