{
  "type": "object",
  "additionalProperties": false,
  "required": [
    "revision",
    "selected_source",
    "double_click",
    "lanes"
  ],
  "properties": {
    "revision": {
      "type": "integer",
      "minimum": 0,
      "maximum": 2147483647
    },
    "selected_source": {
      "enum": [
        "original",
        "user"
      ]
    },
    "double_click": {
      "enum": [
        "none",
        "switch_display"
      ]
    },
    "lanes": {
      "type": "array",
      "minItems": 2,
      "maxItems": 2,
      "items": {
        "$ref": "#/$defs/lane"
      },
      "allOf": [
        {
          "contains": {
            "properties": {
              "slot": {
                "const": 0
              }
            },
            "required": [
              "slot"
            ]
          },
          "minContains": 1,
          "maxContains": 1
        },
        {
          "contains": {
            "properties": {
              "slot": {
                "const": 1
              }
            },
            "required": [
              "slot"
            ]
          },
          "minContains": 1,
          "maxContains": 1
        }
      ]
    }
  },
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://phicomm-io.github.io/iotcloud-community-protocols/contracts/schemas/m1-display-profile-set-v3.schema.json",
  "title": "M1 display_profile.set complete formatted display configuration",
  "$defs": {
    "lane": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "slot",
        "source",
        "device_id",
        "metric",
        "unit",
        "ttl_seconds",
        "color",
        "display"
      ],
      "properties": {
        "slot": {
          "type": "integer",
          "minimum": 0,
          "maximum": 1
        },
        "source": {
          "enum": [
            "none",
            "local",
            "mqtt",
            "api"
          ]
        },
        "device_id": {
          "type": "string"
        },
        "metric": {
          "enum": [
            "temperature",
            "humidity",
            "pm25",
            "formaldehyde",
            "co2",
            "pressure",
            "power",
            "energy",
            "tvoc",
            "pm10",
            "custom",
            "fan_speed_percent"
          ]
        },
        "unit": {
          "enum": [
            "",
            "°C",
            "%",
            "µg/m³",
            "mg/m³",
            "ppm",
            "Pa",
            "W",
            "kWh",
            "ppb"
          ]
        },
        "ttl_seconds": {
          "type": "integer",
          "minimum": 1,
          "maximum": 60
        },
        "color": {
          "oneOf": [
            {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "mode",
                "lamps"
              ],
              "properties": {
                "mode": {
                  "const": "fixed"
                },
                "lamps": {
                  "$ref": "#/$defs/lamps"
                }
              }
            },
            {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "mode",
                "low",
                "high"
              ],
              "properties": {
                "mode": {
                  "const": "thresholds"
                },
                "low": {
                  "type": "number"
                },
                "high": {
                  "type": "number"
                }
              },
              "x-orderedFields": [
                "low",
                "high"
              ],
              "x-float32OrderedFields": [
                "low",
                "high"
              ]
            }
          ]
        },
        "display": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "mode",
            "show_label",
            "show_unit"
          ],
          "properties": {
            "mode": {
              "enum": [
                "integer",
                "auto_kilo",
                "decimal_2",
                "decimal_1",
                "milli"
              ]
            },
            "show_label": {
              "type": "boolean"
            },
            "show_unit": {
              "type": "boolean"
            }
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "source": {
                "const": "mqtt"
              }
            },
            "required": [
              "source"
            ]
          },
          "then": {
            "properties": {
              "device_id": {
                "type": "string",
                "oneOf": [
                  {
                    "pattern": "^[0-9A-Fa-f]{12}$",
                    "minLength": 12,
                    "maxLength": 12
                  },
                  {
                    "pattern": "^(?:[0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$",
                    "minLength": 17,
                    "maxLength": 17
                  }
                ]
              },
              "metric": {
                "enum": [
                  "temperature",
                  "humidity",
                  "pm25",
                  "formaldehyde",
                  "co2",
                  "pressure",
                  "power",
                  "energy",
                  "tvoc",
                  "pm10",
                  "fan_speed_percent"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "source": {
                "enum": [
                  "none",
                  "local",
                  "api"
                ]
              }
            },
            "required": [
              "source"
            ]
          },
          "then": {
            "properties": {
              "device_id": {
                "const": ""
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "source": {
                "const": "none"
              }
            },
            "required": [
              "source"
            ]
          },
          "then": {
            "properties": {
              "metric": {
                "const": "custom"
              },
              "unit": {
                "const": ""
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "source": {
                "const": "local"
              }
            },
            "required": [
              "source"
            ]
          },
          "then": {
            "properties": {
              "metric": {
                "enum": [
                  "temperature",
                  "humidity",
                  "pm25",
                  "formaldehyde",
                  "co2",
                  "pressure"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "custom"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "source": {
                "enum": [
                  "none",
                  "api"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "temperature"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "°C"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "humidity"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "%"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "pm25"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "µg/m³"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "formaldehyde"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "mg/m³"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "co2"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "ppm"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "pressure"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "Pa"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "power"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "W"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "energy"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "kWh"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "tvoc"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "ppb"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "pm10"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "µg/m³"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "fan_speed_percent"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "%"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "slot": {
                "const": 1
              }
            },
            "required": [
              "slot"
            ]
          },
          "then": {
            "properties": {
              "display": {
                "properties": {
                  "mode": {
                    "const": "integer"
                  }
                }
              }
            }
          }
        }
      ]
    },
    "lamps": {
      "type": "array",
      "minItems": 3,
      "maxItems": 3,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "red",
          "green",
          "yellow"
        ],
        "properties": {
          "red": {
            "type": "boolean"
          },
          "green": {
            "type": "boolean"
          },
          "yellow": {
            "type": "boolean"
          }
        }
      }
    }
  },
  "x-defaults": {
    "selected_source": "original",
    "double_click": "none",
    "lane": {
      "source": "none",
      "device_id": "",
      "metric": "custom",
      "unit": "",
      "ttl_seconds": 20,
      "color": {
        "mode": "fixed",
        "lamps": [
          {
            "red": false,
            "green": false,
            "yellow": false
          },
          {
            "red": false,
            "green": false,
            "yellow": false
          },
          {
            "red": false,
            "green": false,
            "yellow": false
          }
        ]
      },
      "display": {
        "mode": "integer",
        "show_label": false,
        "show_unit": false
      }
    }
  }
}
