matc 0.1.3

Matter protocol library (controller side)
Documentation
//! Matter TLV encoders and decoders for Laundry Washer Controls Cluster
//! Cluster ID: 0x0053
//!
//! This file is automatically generated from LaundryWasherControls.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 NumberOfRinses {
    /// This laundry washer mode does not perform rinse cycles
    None = 0,
    /// This laundry washer mode performs normal rinse cycles determined by the manufacturer
    Normal = 1,
    /// This laundry washer mode performs an extra rinse cycle
    Extra = 2,
    /// This laundry washer mode performs the maximum number of rinse cycles determined by the manufacturer
    Max = 3,
}

impl NumberOfRinses {
    /// Convert from u8 value
    pub fn from_u8(value: u8) -> Option<Self> {
        match value {
            0 => Some(NumberOfRinses::None),
            1 => Some(NumberOfRinses::Normal),
            2 => Some(NumberOfRinses::Extra),
            3 => Some(NumberOfRinses::Max),
            _ => None,
        }
    }

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

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

// Attribute decoders

/// Decode SpinSpeeds attribute (0x0000)
pub fn decode_spin_speeds(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<String>> {
    let mut res = Vec::new();
    if let tlv::TlvItemValue::List(v) = inp {
        for item in v {
            if let tlv::TlvItemValue::String(s) = &item.value {
                res.push(s.clone());
            }
        }
    }
    Ok(res)
}

/// Decode SpinSpeedCurrent attribute (0x0001)
pub fn decode_spin_speed_current(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u8>> {
    if let tlv::TlvItemValue::Int(v) = inp {
        Ok(Some(*v as u8))
    } else {
        Ok(None)
    }
}

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

/// Decode SupportedRinses attribute (0x0003)
pub fn decode_supported_rinses(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<NumberOfRinses>> {
    let mut res = Vec::new();
    if let tlv::TlvItemValue::List(v) = inp {
        for item in v {
            if let tlv::TlvItemValue::Int(i) = &item.value {
                if let Some(enum_val) = NumberOfRinses::from_u8(*i as u8) {
                    res.push(enum_val);
                }
            }
        }
    }
    Ok(res)
}


// 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 != 0x0053 {
        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0053, got {}\"}}", cluster_id);
    }

    match attribute_id {
        0x0000 => {
            match decode_spin_speeds(tlv_value) {
                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
                Err(e) => format!("{{\"error\": \"{}\"}}", e),
            }
        }
        0x0001 => {
            match decode_spin_speed_current(tlv_value) {
                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
                Err(e) => format!("{{\"error\": \"{}\"}}", e),
            }
        }
        0x0002 => {
            match decode_number_of_rinses(tlv_value) {
                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
                Err(e) => format!("{{\"error\": \"{}\"}}", e),
            }
        }
        0x0003 => {
            match decode_supported_rinses(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, "SpinSpeeds"),
        (0x0001, "SpinSpeedCurrent"),
        (0x0002, "NumberOfRinses"),
        (0x0003, "SupportedRinses"),
    ]
}

// Typed facade (invokes + reads)

/// Read `SpinSpeeds` attribute from cluster `Laundry Washer Controls`.
pub async fn read_spin_speeds(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<String>> {
    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LAUNDRY_WASHER_CONTROLS, crate::clusters::defs::CLUSTER_LAUNDRY_WASHER_CONTROLS_ATTR_ID_SPINSPEEDS).await?;
    decode_spin_speeds(&tlv)
}

/// Read `SpinSpeedCurrent` attribute from cluster `Laundry Washer Controls`.
pub async fn read_spin_speed_current(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u8>> {
    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LAUNDRY_WASHER_CONTROLS, crate::clusters::defs::CLUSTER_LAUNDRY_WASHER_CONTROLS_ATTR_ID_SPINSPEEDCURRENT).await?;
    decode_spin_speed_current(&tlv)
}

/// Read `NumberOfRinses` attribute from cluster `Laundry Washer Controls`.
pub async fn read_number_of_rinses(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<NumberOfRinses> {
    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LAUNDRY_WASHER_CONTROLS, crate::clusters::defs::CLUSTER_LAUNDRY_WASHER_CONTROLS_ATTR_ID_NUMBEROFRINSES).await?;
    decode_number_of_rinses(&tlv)
}

/// Read `SupportedRinses` attribute from cluster `Laundry Washer Controls`.
pub async fn read_supported_rinses(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<NumberOfRinses>> {
    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LAUNDRY_WASHER_CONTROLS, crate::clusters::defs::CLUSTER_LAUNDRY_WASHER_CONTROLS_ATTR_ID_SUPPORTEDRINSES).await?;
    decode_supported_rinses(&tlv)
}