matc 0.1.3

Matter protocol library (controller side)
Documentation
//! Matter TLV encoders and decoders for Thermostat User Interface Configuration Cluster
//! Cluster ID: 0x0204
//!
//! This file is automatically generated from ThermostatUserInterfaceConfiguration.xml

#![allow(clippy::too_many_arguments)]

use crate::tlv;
use anyhow;
use serde_json;


// Enum definitions

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum KeypadLockout {
    /// All functionality available to the user
    Nolockout = 0,
    /// Level 1 reduced functionality
    Lockout1 = 1,
    /// Level 2 reduced functionality
    Lockout2 = 2,
    /// Level 3 reduced functionality
    Lockout3 = 3,
    /// Level 4 reduced functionality
    Lockout4 = 4,
    /// Least functionality available to the user
    Lockout5 = 5,
}

impl KeypadLockout {
    /// Convert from u8 value
    pub fn from_u8(value: u8) -> Option<Self> {
        match value {
            0 => Some(KeypadLockout::Nolockout),
            1 => Some(KeypadLockout::Lockout1),
            2 => Some(KeypadLockout::Lockout2),
            3 => Some(KeypadLockout::Lockout3),
            4 => Some(KeypadLockout::Lockout4),
            5 => Some(KeypadLockout::Lockout5),
            _ => None,
        }
    }

    /// Convert to u8 value
    pub fn to_u8(self) -> u8 {
        self as u8
    }
}

impl From<KeypadLockout> for u8 {
    fn from(val: KeypadLockout) -> Self {
        val as u8
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum ScheduleProgrammingVisibility {
    /// Local schedule programming functionality is enabled at the thermostat
    Scheduleprogrammingpermitted = 0,
    /// Local schedule programming functionality is disabled at the thermostat
    Scheduleprogrammingdenied = 1,
}

impl ScheduleProgrammingVisibility {
    /// Convert from u8 value
    pub fn from_u8(value: u8) -> Option<Self> {
        match value {
            0 => Some(ScheduleProgrammingVisibility::Scheduleprogrammingpermitted),
            1 => Some(ScheduleProgrammingVisibility::Scheduleprogrammingdenied),
            _ => None,
        }
    }

    /// Convert to u8 value
    pub fn to_u8(self) -> u8 {
        self as u8
    }
}

impl From<ScheduleProgrammingVisibility> for u8 {
    fn from(val: ScheduleProgrammingVisibility) -> Self {
        val as u8
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum TemperatureDisplayMode {
    /// Temperature displayed in °C
    Celsius = 0,
    /// Temperature displayed in °F
    Fahrenheit = 1,
}

impl TemperatureDisplayMode {
    /// Convert from u8 value
    pub fn from_u8(value: u8) -> Option<Self> {
        match value {
            0 => Some(TemperatureDisplayMode::Celsius),
            1 => Some(TemperatureDisplayMode::Fahrenheit),
            _ => None,
        }
    }

    /// Convert to u8 value
    pub fn to_u8(self) -> u8 {
        self as u8
    }
}

impl From<TemperatureDisplayMode> for u8 {
    fn from(val: TemperatureDisplayMode) -> Self {
        val as u8
    }
}

// Attribute decoders

/// Decode TemperatureDisplayMode attribute (0x0000)
pub fn decode_temperature_display_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<TemperatureDisplayMode> {
    if let tlv::TlvItemValue::Int(v) = inp {
        TemperatureDisplayMode::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
    } else {
        Err(anyhow::anyhow!("Expected Integer"))
    }
}

/// Decode KeypadLockout attribute (0x0001)
pub fn decode_keypad_lockout(inp: &tlv::TlvItemValue) -> anyhow::Result<KeypadLockout> {
    if let tlv::TlvItemValue::Int(v) = inp {
        KeypadLockout::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
    } else {
        Err(anyhow::anyhow!("Expected Integer"))
    }
}

/// Decode ScheduleProgrammingVisibility attribute (0x0002)
pub fn decode_schedule_programming_visibility(inp: &tlv::TlvItemValue) -> anyhow::Result<ScheduleProgrammingVisibility> {
    if let tlv::TlvItemValue::Int(v) = inp {
        ScheduleProgrammingVisibility::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
    } else {
        Err(anyhow::anyhow!("Expected Integer"))
    }
}


// JSON dispatcher function

/// Decode attribute value and return as JSON string
///
/// # Parameters
/// * `cluster_id` - The cluster identifier
/// * `attribute_id` - The attribute identifier
/// * `tlv_value` - The TLV value to decode
///
/// # Returns
/// JSON string representation of the decoded value or error
pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
    // Verify this is the correct cluster
    if cluster_id != 0x0204 {
        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0204, got {}\"}}", cluster_id);
    }

    match attribute_id {
        0x0000 => {
            match decode_temperature_display_mode(tlv_value) {
                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
                Err(e) => format!("{{\"error\": \"{}\"}}", e),
            }
        }
        0x0001 => {
            match decode_keypad_lockout(tlv_value) {
                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
                Err(e) => format!("{{\"error\": \"{}\"}}", e),
            }
        }
        0x0002 => {
            match decode_schedule_programming_visibility(tlv_value) {
                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
                Err(e) => format!("{{\"error\": \"{}\"}}", e),
            }
        }
        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
    }
}

/// Get list of all attributes supported by this cluster
///
/// # Returns
/// Vector of tuples containing (attribute_id, attribute_name)
pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
    vec![
        (0x0000, "TemperatureDisplayMode"),
        (0x0001, "KeypadLockout"),
        (0x0002, "ScheduleProgrammingVisibility"),
    ]
}

// Typed facade (invokes + reads)

/// Read `TemperatureDisplayMode` attribute from cluster `Thermostat User Interface Configuration`.
pub async fn read_temperature_display_mode(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<TemperatureDisplayMode> {
    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_THERMOSTAT_USER_INTERFACE_CONFIGURATION, crate::clusters::defs::CLUSTER_THERMOSTAT_USER_INTERFACE_CONFIGURATION_ATTR_ID_TEMPERATUREDISPLAYMODE).await?;
    decode_temperature_display_mode(&tlv)
}

/// Read `KeypadLockout` attribute from cluster `Thermostat User Interface Configuration`.
pub async fn read_keypad_lockout(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<KeypadLockout> {
    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_THERMOSTAT_USER_INTERFACE_CONFIGURATION, crate::clusters::defs::CLUSTER_THERMOSTAT_USER_INTERFACE_CONFIGURATION_ATTR_ID_KEYPADLOCKOUT).await?;
    decode_keypad_lockout(&tlv)
}

/// Read `ScheduleProgrammingVisibility` attribute from cluster `Thermostat User Interface Configuration`.
pub async fn read_schedule_programming_visibility(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<ScheduleProgrammingVisibility> {
    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_THERMOSTAT_USER_INTERFACE_CONFIGURATION, crate::clusters::defs::CLUSTER_THERMOSTAT_USER_INTERFACE_CONFIGURATION_ATTR_ID_SCHEDULEPROGRAMMINGVISIBILITY).await?;
    decode_schedule_programming_visibility(&tlv)
}