matc/clusters/codec/
laundry_dryer_controls.rs

1//! Matter TLV encoders and decoders for Laundry Dryer Controls Cluster
2//! Cluster ID: 0x004A
3//!
4//! This file is automatically generated from LaundryDryerControls.xml
5
6#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13// Enum definitions
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
16#[repr(u8)]
17pub enum DrynessLevel {
18    /// Provides a low dryness level for the selected mode
19    Low = 0,
20    /// Provides the normal level of dryness for the selected mode
21    Normal = 1,
22    /// Provides an extra dryness level for the selected mode
23    Extra = 2,
24    /// Provides the max dryness level for the selected mode
25    Max = 3,
26}
27
28impl DrynessLevel {
29    /// Convert from u8 value
30    pub fn from_u8(value: u8) -> Option<Self> {
31        match value {
32            0 => Some(DrynessLevel::Low),
33            1 => Some(DrynessLevel::Normal),
34            2 => Some(DrynessLevel::Extra),
35            3 => Some(DrynessLevel::Max),
36            _ => None,
37        }
38    }
39
40    /// Convert to u8 value
41    pub fn to_u8(self) -> u8 {
42        self as u8
43    }
44}
45
46impl From<DrynessLevel> for u8 {
47    fn from(val: DrynessLevel) -> Self {
48        val as u8
49    }
50}
51
52// Attribute decoders
53
54/// Decode SupportedDrynessLevels attribute (0x0000)
55pub fn decode_supported_dryness_levels(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<DrynessLevel>> {
56    let mut res = Vec::new();
57    if let tlv::TlvItemValue::List(v) = inp {
58        for item in v {
59            if let tlv::TlvItemValue::Int(i) = &item.value {
60                if let Some(enum_val) = DrynessLevel::from_u8(*i as u8) {
61                    res.push(enum_val);
62                }
63            }
64        }
65    }
66    Ok(res)
67}
68
69/// Decode SelectedDrynessLevel attribute (0x0001)
70pub fn decode_selected_dryness_level(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<DrynessLevel>> {
71    if let tlv::TlvItemValue::Int(v) = inp {
72        Ok(DrynessLevel::from_u8(*v as u8))
73    } else {
74        Ok(None)
75    }
76}
77
78
79// JSON dispatcher function
80
81/// Decode attribute value and return as JSON string
82///
83/// # Parameters
84/// * `cluster_id` - The cluster identifier
85/// * `attribute_id` - The attribute identifier
86/// * `tlv_value` - The TLV value to decode
87///
88/// # Returns
89/// JSON string representation of the decoded value or error
90pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
91    // Verify this is the correct cluster
92    if cluster_id != 0x004A {
93        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x004A, got {}\"}}", cluster_id);
94    }
95
96    match attribute_id {
97        0x0000 => {
98            match decode_supported_dryness_levels(tlv_value) {
99                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
100                Err(e) => format!("{{\"error\": \"{}\"}}", e),
101            }
102        }
103        0x0001 => {
104            match decode_selected_dryness_level(tlv_value) {
105                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
106                Err(e) => format!("{{\"error\": \"{}\"}}", e),
107            }
108        }
109        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
110    }
111}
112
113/// Get list of all attributes supported by this cluster
114///
115/// # Returns
116/// Vector of tuples containing (attribute_id, attribute_name)
117pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
118    vec![
119        (0x0000, "SupportedDrynessLevels"),
120        (0x0001, "SelectedDrynessLevel"),
121    ]
122}
123
124// Typed facade (invokes + reads)
125
126/// Read `SupportedDrynessLevels` attribute from cluster `Laundry Dryer Controls`.
127pub async fn read_supported_dryness_levels(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<DrynessLevel>> {
128    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LAUNDRY_DRYER_CONTROLS, crate::clusters::defs::CLUSTER_LAUNDRY_DRYER_CONTROLS_ATTR_ID_SUPPORTEDDRYNESSLEVELS).await?;
129    decode_supported_dryness_levels(&tlv)
130}
131
132/// Read `SelectedDrynessLevel` attribute from cluster `Laundry Dryer Controls`.
133pub async fn read_selected_dryness_level(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<DrynessLevel>> {
134    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LAUNDRY_DRYER_CONTROLS, crate::clusters::defs::CLUSTER_LAUNDRY_DRYER_CONTROLS_ATTR_ID_SELECTEDDRYNESSLEVEL).await?;
135    decode_selected_dryness_level(&tlv)
136}
137