{
  "pluginAlias": "PhilipsAmbilightTV",
  "pluginType": "platform",
  "singular": true,
  "strictValidation": false,
  "customUi": true,
  "schema": {
    "type": "object",
    "required": [
      "devices"
    ],
    "additionalProperties": false,
    "properties": {
      "devices": {
        "title": "TV Devices",
        "type": "array",
        "minItems": 1,
        "items": {
          "type": "object",
          "required": [
            "name",
            "mac",
            "ip",
            "username",
            "password"
          ],
          "additionalProperties": false,
          "properties": {
            "name": {
              "title": "TV Name",
              "type": "string",
              "default": "My Philips TV",
              "description": "The name of the TV as it will appear in HomeKit."
            },
            "mac": {
              "title": "MAC Address",
              "type": "string",
              "pattern": "^([A-Fa-f0-9]{2}:){5}[A-Fa-f0-9]{2}$",
              "placeholder": "AA:BB:CC:DD:EE:FF",
              "description": "The MAC address of the TV (required for Wake-on-LAN)."
            },
            "ip": {
              "title": "IP Address",
              "type": "string",
              "format": "ipv4",
              "placeholder": "192.168.1.100",
              "description": "The IP address of the TV on the local network."
            },
            "username": {
              "title": "Username",
              "type": "string",
              "description": "The username obtained during TV pairing."
            },
            "password": {
              "title": "Password",
              "type": "string",
              "description": "The password (auth key) obtained during TV pairing."
            },
            "pollingInterval": {
              "title": "Polling Interval (ms)",
              "type": "integer",
              "default": 10000,
              "minimum": 1000,
              "maximum": 60000,
              "description": "How often to poll the TV for state updates (in milliseconds)."
            },
            "wakeOnLanEnabled": {
              "title": "Wake-on-LAN",
              "type": "boolean",
              "default": true,
              "description": "Enable Wake-on-LAN to turn on the TV from standby."
            },
            "ambilightMode": {
              "title": "Ambilight Mode",
              "type": "string",
              "default": "FOLLOW_VIDEO/NATURAL",
              "description": "The ambilight mode to activate when turning on via HomeKit.",
              "oneOf": [
                { "title": "Follow Video - Standard", "enum": ["FOLLOW_VIDEO/STANDARD"] },
                { "title": "Follow Video - Natural", "enum": ["FOLLOW_VIDEO/NATURAL"] },
                { "title": "Follow Video - Vivid", "enum": ["FOLLOW_VIDEO/VIVID"] },
                { "title": "Follow Video - Game", "enum": ["FOLLOW_VIDEO/GAME"] },
                { "title": "Follow Video - Comfort", "enum": ["FOLLOW_VIDEO/COMFORT"] },
                { "title": "Follow Video - Relax", "enum": ["FOLLOW_VIDEO/RELAX"] },
                { "title": "Follow Video - Football", "enum": ["FOLLOW_VIDEO/FOOTBALL"] },
                { "title": "Follow Audio - Energy Brightness", "enum": ["FOLLOW_AUDIO/ENERGY_ADAPTIVE_BRIGHTNESS"] },
                { "title": "Follow Audio - Energy Colors", "enum": ["FOLLOW_AUDIO/ENERGY_ADAPTIVE_COLORS"] },
                { "title": "Follow Audio - VU Meter", "enum": ["FOLLOW_AUDIO/VU_METER"] },
                { "title": "Follow Audio - Spectrum", "enum": ["FOLLOW_AUDIO/SPECTRUM_ANALYZER"] },
                { "title": "Follow Audio - Party", "enum": ["FOLLOW_AUDIO/PARTY"] },
                { "title": "Lounge Light", "enum": ["Lounge light"] }
              ]
            },
            "ambilightOnStart": {
              "title": "Start Ambilight when TV turns on",
              "type": "boolean",
              "default": false,
              "description": "Automatically turn Ambilight on (in the mode above) whenever the TV powers on."
            },
            "infoButtonKey": {
              "title": "Info Button Key",
              "type": "string",
              "default": "Source",
              "description": "The TV key that the HomeKit remote's information button sends.",
              "oneOf": [
                { "title": "Source (input picker)", "enum": ["Source"] },
                { "title": "Info (program details)", "enum": ["Info"] },
                { "title": "Options (context menu)", "enum": ["Options"] },
                { "title": "Find (search)", "enum": ["Find"] }
              ]
            },
            "backButtonKey": {
              "title": "Back Button Key",
              "type": "string",
              "default": "Back",
              "description": "The TV key that the HomeKit remote's back button sends.",
              "oneOf": [
                { "title": "Back", "enum": ["Back"] },
                { "title": "Home", "enum": ["Home"] },
                { "title": "Source (input picker)", "enum": ["Source"] },
                { "title": "Options (context menu)", "enum": ["Options"] }
              ]
            },
            "playPauseButtonKey": {
              "title": "Play/Pause Button Key",
              "type": "string",
              "default": "PlayPause",
              "description": "The TV key that the HomeKit remote's play/pause button sends.",
              "oneOf": [
                { "title": "Play/Pause", "enum": ["PlayPause"] },
                { "title": "Home", "enum": ["Home"] },
                { "title": "Source (input picker)", "enum": ["Source"] },
                { "title": "Online (smart TV portal)", "enum": ["Online"] }
              ]
            },
            "inputs": {
              "title": "Custom Input Sources",
              "type": "array",
              "description": "Custom input sources to display in HomeKit. If not specified, apps will be fetched from the TV.",
              "items": {
                "type": "object",
                "required": [
                  "name",
                  "type",
                  "identifier"
                ],
                "properties": {
                  "name": {
                    "title": "Name",
                    "type": "string",
                    "description": "The display name for this input source."
                  },
                  "type": {
                    "title": "Type",
                    "type": "string",
                    "enum": [
                      "app",
                      "source",
                      "channel"
                    ],
                    "description": "The type of input source."
                  },
                  "identifier": {
                    "title": "Identifier",
                    "type": "string",
                    "description": "The app package name, source ID, or channel number."
                  },
                  "displayOrder": {
                    "title": "Display Order",
                    "type": "integer",
                    "minimum": 0,
                    "description": "The order in which this input appears in HomeKit."
                  }
                }
              }
            },
            "sourceSwitches": {
              "title": "Source Switches",
              "type": "boolean",
              "default": false,
              "description": "Expose each input source as a separate Switch accessory for use in HomeKit automations and scenes."
            },
            "ambilightHueSwitch": {
              "title": "Ambilight + Hue Switch",
              "type": "boolean",
              "default": false,
              "description": "Expose the Ambilight + Hue integration (Philips Hue lamps following Ambilight) as a separate Switch, toggleable independently of the main Ambilight controls."
            },
            "customApps": {
              "title": "Custom Apps",
              "type": "array",
              "description": "Apps the TV does not report via its app list (e.g. sideloaded apps). Added in addition to auto-discovered apps. Tip: open the app on your TV and use \"Detect from TV\" to fill these in automatically.",
              "items": {
                "type": "object",
                "properties": {
                  "name": {
                    "title": "Name",
                    "type": "string",
                    "description": "Display name shown in HomeKit."
                  },
                  "packageName": {
                    "title": "Package Name",
                    "type": "string",
                    "description": "Android package name, e.g. com.ug.eon.android.tv"
                  },
                  "className": {
                    "title": "Launch Activity",
                    "type": "string",
                    "description": "Launch activity class name. Required for reliable launching."
                  },
                  "action": {
                    "title": "Intent Action",
                    "type": "string",
                    "description": "Intent action (defaults to android.intent.action.MAIN)."
                  }
                },
                "required": [
                  "name",
                  "packageName"
                ]
              }
            },
            "stateSensors": {
              "title": "State Sensors",
              "type": "array",
              "uniqueItems": true,
              "description": "Expose TV states as MotionSensor services for HomeKit automations. Motion is detected when the state is active.",
              "items": {
                "type": "string",
                "enum": [
                  "power",
                  "ambilight",
                  "mute"
                ]
              }
            },
            "sources": {
              "title": "Source Configuration",
              "type": "array",
              "description": "Source order and visibility settings (managed via the Sources UI).",
              "items": {
                "type": "object",
                "properties": {
                  "id": {
                    "title": "Source ID",
                    "type": "string",
                    "description": "The unique identifier for this source."
                  },
                  "order": {
                    "title": "Display Order",
                    "type": "integer",
                    "minimum": 0,
                    "description": "The order in which this source appears in HomeKit."
                  },
                  "visible": {
                    "title": "Visible",
                    "type": "boolean",
                    "default": true,
                    "description": "Whether this source is visible in HomeKit."
                  },
                  "customName": {
                    "title": "Custom Name",
                    "type": "string",
                    "description": "Optional custom name to display instead of the default."
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "layout": [
    {
      "type": "fieldset",
      "title": "TV Devices",
      "expandable": true,
      "expanded": true,
      "items": [
        {
          "key": "devices",
          "type": "array",
          "orderable": true,
          "buttonText": "Add TV",
          "items": [
            "devices[].name",
            "devices[].ip",
            "devices[].mac",
            "devices[].username",
            "devices[].password",
            {
              "key": "devices[].pollingInterval",
              "description": "Polling interval in milliseconds (default: 10000)"
            },
            "devices[].wakeOnLanEnabled",
            "devices[].ambilightMode",
            "devices[].ambilightOnStart",
            "devices[].infoButtonKey",
            "devices[].backButtonKey",
            "devices[].playPauseButtonKey",
            "devices[].sourceSwitches",
            "devices[].ambilightHueSwitch",
            {
              "key": "devices[].stateSensors",
              "type": "checkboxes",
              "title": "State Sensors (for HomeKit automations)",
              "description": "Select which states to expose as MotionSensor services. Motion is detected when the state is active (e.g., TV is on, Ambilight is active).",
              "titleMap": {
                "power": "Power (TV On/Off)",
                "ambilight": "Ambilight (Active/Inactive)",
                "mute": "Muted (Yes/No)"
              }
            },
            {
              "key": "devices[].inputs",
              "type": "array",
              "title": "Custom Input Sources (Optional)",
              "expandable": true,
              "expanded": false,
              "buttonText": "Add Input",
              "items": [
                "devices[].inputs[].name",
                "devices[].inputs[].type",
                "devices[].inputs[].identifier",
                "devices[].inputs[].displayOrder"
              ]
            },
            {
              "key": "devices[].customApps",
              "type": "array",
              "title": "Custom Apps (Optional)",
              "expandable": true,
              "expanded": false,
              "buttonText": "Add App",
              "items": [
                "devices[].customApps[].name",
                "devices[].customApps[].packageName",
                "devices[].customApps[].className",
                "devices[].customApps[].action"
              ]
            }
          ]
        }
      ]
    }
  ]
}
