{
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema",
    "revision",
    "selected_source",
    "double_click",
    "lanes",
    "runtime"
  ],
  "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
        }
      ]
    },
    "runtime": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "profile",
        "selected_source",
        "mcu_supported",
        "confirmed",
        "confirmation",
        "lanes"
      ],
      "properties": {
        "profile": {
          "enum": [
            "legacy",
            "dual",
            "formatted"
          ]
        },
        "selected_source": {
          "enum": [
            "original",
            "user"
          ]
        },
        "mcu_supported": {
          "type": "boolean"
        },
        "confirmed": {
          "type": "boolean"
        },
        "confirmation": {
          "enum": [
            "mcu_readback",
            "pending_mcu_readback",
            "requires_mcu_upgrade"
          ]
        },
        "lanes": {
          "type": "array",
          "minItems": 2,
          "maxItems": 2,
          "items": {
            "$ref": "#/$defs/runtime_lane"
          },
          "allOf": [
            {
              "contains": {
                "properties": {
                  "slot": {
                    "const": 0
                  }
                },
                "required": [
                  "slot"
                ]
              },
              "minContains": 1,
              "maxContains": 1
            },
            {
              "contains": {
                "properties": {
                  "slot": {
                    "const": 1
                  }
                },
                "required": [
                  "slot"
                ]
              },
              "minContains": 1,
              "maxContains": 1
            }
          ]
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "mcu_supported": {
                "const": false
              }
            },
            "required": [
              "mcu_supported"
            ]
          },
          "then": {
            "properties": {
              "confirmed": {
                "const": false
              },
              "confirmation": {
                "const": "requires_mcu_upgrade"
              },
              "lanes": {
                "items": {
                  "properties": {
                    "effective": {
                      "const": false
                    },
                    "confirmed": {
                      "const": false
                    }
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "confirmed": {
                "const": true
              }
            },
            "required": [
              "confirmed"
            ]
          },
          "then": {
            "properties": {
              "profile": {
                "const": "formatted"
              },
              "mcu_supported": {
                "const": true
              },
              "confirmation": {
                "const": "mcu_readback"
              },
              "lanes": {
                "items": {
                  "properties": {
                    "confirmed": {
                      "const": true
                    }
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "confirmation": {
                "const": "mcu_readback"
              }
            },
            "required": [
              "confirmation"
            ]
          },
          "then": {
            "properties": {
              "confirmed": {
                "const": true
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "mcu_supported": {
                "const": true
              },
              "confirmed": {
                "const": false
              }
            },
            "required": [
              "mcu_supported",
              "confirmed"
            ]
          },
          "then": {
            "properties": {
              "confirmation": {
                "const": "pending_mcu_readback"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "profile": {
                "const": "formatted"
              },
              "mcu_supported": {
                "const": true
              },
              "lanes": {
                "items": {
                  "properties": {
                    "confirmed": {
                      "const": true
                    }
                  }
                }
              }
            },
            "required": [
              "profile",
              "mcu_supported",
              "lanes"
            ]
          },
          "then": {
            "properties": {
              "confirmed": {
                "const": true
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "profile": {
                "not": {
                  "const": "formatted"
                }
              }
            },
            "required": [
              "profile"
            ]
          },
          "then": {
            "properties": {
              "confirmed": {
                "const": false
              },
              "lanes": {
                "items": {
                  "properties": {
                    "confirmed": {
                      "const": false
                    },
                    "effective": {
                      "const": false
                    }
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "selected_source": {
                "const": "original"
              }
            },
            "required": [
              "selected_source"
            ]
          },
          "then": {
            "properties": {
              "lanes": {
                "items": {
                  "properties": {
                    "effective": {
                      "const": false
                    }
                  }
                }
              }
            }
          }
        }
      ]
    },
    "schema": {
      "type": "integer",
      "const": 4
    }
  },
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://phicomm-io.github.io/iotcloud-community-protocols/contracts/schemas/m1-display-profile-state-v4.schema.json",
  "title": "M1 display_profile.get schema 4 configuration and separate runtime",
  "$defs": {
    "lane": {
      "oneOf": [
        {
          "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",
                "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",
                    "pattern": "^[0-9a-f]{12}$",
                    "minLength": 12,
                    "maxLength": 12
                  },
                  "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"
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "slot",
            "source",
            "device_id",
            "field_id",
            "type",
            "unit",
            "display",
            "ttl_seconds",
            "color"
          ],
          "properties": {
            "slot": {
              "type": "integer",
              "minimum": 0,
              "maximum": 1
            },
            "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"
                }
              }
            },
            "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"
                  ]
                }
              ]
            },
            "source": {
              "const": "mqtt"
            },
            "device_id": {
              "type": "string",
              "pattern": "^[0-9a-f]{12}$",
              "minLength": 12,
              "maxLength": 12
            },
            "field_id": {
              "type": "string",
              "pattern": "^[a-z][a-z0-9_]{0,31}$",
              "minLength": 1,
              "maxLength": 32
            },
            "type": {
              "const": "number"
            },
            "unit": {
              "type": "string",
              "pattern": "^[^\\u0000-\\u001f\\u007f]*$",
              "x-utf8Bytes": {
                "minimum": 0,
                "maximum": 24
              }
            }
          },
          "allOf": [
            {
              "if": {
                "properties": {
                  "slot": {
                    "const": 1
                  }
                },
                "required": [
                  "slot"
                ]
              },
              "then": {
                "properties": {
                  "display": {
                    "properties": {
                      "mode": {
                        "const": "integer"
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      ]
    },
    "runtime_lane": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "slot",
        "valid",
        "value",
        "unit",
        "age_ms",
        "remaining_ttl_seconds",
        "effective",
        "reason",
        "number",
        "format",
        "lamps",
        "confirmed"
      ],
      "properties": {
        "slot": {
          "type": "integer",
          "minimum": 0,
          "maximum": 1
        },
        "valid": {
          "type": "boolean"
        },
        "value": {
          "type": [
            "number",
            "null"
          ],
          "minimum": 0,
          "maximum": 999999
        },
        "unit": {
          "type": "string",
          "pattern": "^[^\\u0000-\\u001f\\u007f]*$",
          "x-utf8Bytes": {
            "minimum": 0,
            "maximum": 24
          }
        },
        "age_ms": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 0,
          "maximum": 4294967295
        },
        "remaining_ttl_seconds": {
          "type": "integer",
          "minimum": 0,
          "maximum": 60
        },
        "effective": {
          "type": "boolean"
        },
        "reason": {
          "enum": [
            "ok",
            "disabled",
            "missing",
            "stale",
            "offline",
            "invalid",
            "out_of_range",
            "unsupported"
          ]
        },
        "number": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 0,
          "maximum": 999
        },
        "format": {
          "type": [
            "integer",
            "null"
          ],
          "enum": [
            null,
            0,
            1,
            2,
            3,
            4,
            5,
            6,
            7
          ]
        },
        "confirmed": {
          "type": "boolean"
        },
        "lamps": {
          "oneOf": [
            {
              "$ref": "#/$defs/lamps"
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "effective": {
                "const": true
              }
            },
            "required": [
              "effective"
            ]
          },
          "then": {
            "properties": {
              "valid": {
                "const": true
              },
              "confirmed": {
                "const": true
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "valid": {
                "const": true
              }
            },
            "required": [
              "valid"
            ]
          },
          "then": {
            "properties": {
              "value": {
                "type": "number",
                "minimum": 0,
                "maximum": 999999
              },
              "age_ms": {
                "type": "integer"
              },
              "remaining_ttl_seconds": {
                "minimum": 1
              },
              "reason": {
                "const": "ok"
              },
              "number": {
                "type": "integer"
              },
              "format": {
                "enum": [
                  0,
                  1,
                  2,
                  3,
                  4,
                  5,
                  6,
                  7
                ]
              },
              "lamps": {
                "$ref": "#/$defs/lamps"
              }
            }
          },
          "else": {
            "properties": {
              "value": {
                "const": null
              },
              "remaining_ttl_seconds": {
                "const": 0
              },
              "effective": {
                "const": false
              },
              "reason": {
                "not": {
                  "const": "ok"
                }
              },
              "number": {
                "const": null
              },
              "format": {
                "const": null
              },
              "lamps": {
                "const": null
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "slot": {
                "const": 1
              }
            },
            "required": [
              "slot"
            ]
          },
          "then": {
            "properties": {
              "format": {
                "enum": [
                  null,
                  0,
                  2,
                  4,
                  6
                ]
              }
            }
          }
        }
      ]
    },
    "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"
          }
        }
      }
    }
  }
}
