matc/clusters/codec/
laundry_washer_controls.rs

1//! Matter TLV encoders and decoders for Laundry Washer Controls Cluster
2//! Cluster ID: 0x0053
3//!
4//! This file is automatically generated from LaundryWasherControls.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 NumberOfRinses {
18    /// This laundry washer mode does not perform rinse cycles
19    None = 0,
20    /// This laundry washer mode performs normal rinse cycles determined by the manufacturer
21    Normal = 1,
22    /// This laundry washer mode performs an extra rinse cycle
23    Extra = 2,
24    /// This laundry washer mode performs the maximum number of rinse cycles determined by the manufacturer
25    Max = 3,
26}
27
28impl NumberOfRinses {
29    /// Convert from u8 value
30    pub fn from_u8(value: u8) -> Option<Self> {
31        match value {
32            0 => Some(NumberOfRinses::None),
33            1 => Some(NumberOfRinses::Normal),
34            2 => Some(NumberOfRinses::Extra),
35            3 => Some(NumberOfRinses::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<NumberOfRinses> for u8 {
47    fn from(val: NumberOfRinses) -> Self {
48        val as u8
49    }
50}
51
52// Attribute decoders
53
54/// Decode SpinSpeeds attribute (0x0000)
55pub fn decode_spin_speeds(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<String>> {
56    let mut res = Vec::new();
57    if let tlv::TlvItemValue::List(v) = inp {
58        for item in v {
59            if let tlv::TlvItemValue::String(s) = &item.value {
60                res.push(s.clone());
61            }
62        }
63    }
64    Ok(res)
65}
66
67/// Decode SpinSpeedCurrent attribute (0x0001)
68pub fn decode_spin_speed_current(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u8>> {
69    if let tlv::TlvItemValue::Int(v) = inp {
70        Ok(Some(*v as u8))
71    } else {
72        Ok(None)
73    }
74}
75
76/// Decode NumberOfRinses attribute (0x0002)
77pub fn decode_number_of_rinses(inp: &tlv::TlvItemValue) -> anyhow::Result<NumberOfRinses> {
78    if let tlv::TlvItemValue::Int(v) = inp {
79        NumberOfRinses::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
80    } else {
81        Err(anyhow::anyhow!("Expected Integer"))
82    }
83}
84
85/// Decode SupportedRinses attribute (0x0003)
86pub fn decode_supported_rinses(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<NumberOfRinses>> {
87    let mut res = Vec::new();
88    if let tlv::TlvItemValue::List(v) = inp {
89        for item in v {
90            if let tlv::TlvItemValue::Int(i) = &item.value {
91                if let Some(enum_val) = NumberOfRinses::from_u8(*i as u8) {
92                    res.push(enum_val);
93                }
94            }
95        }
96    }
97    Ok(res)
98}
99
100
101// JSON dispatcher function
102
103/// Decode attribute value and return as JSON string
104///
105/// # Parameters
106/// * `cluster_id` - The cluster identifier
107/// * `attribute_id` - The attribute identifier
108/// * `tlv_value` - The TLV value to decode
109///
110/// # Returns
111/// JSON string representation of the decoded value or error
112pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
113    // Verify this is the correct cluster
114    if cluster_id != 0x0053 {
115        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0053, got {}\"}}", cluster_id);
116    }
117
118    match attribute_id {
119        0x0000 => {
120            match decode_spin_speeds(tlv_value) {
121                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
122                Err(e) => format!("{{\"error\": \"{}\"}}", e),
123            }
124        }
125        0x0001 => {
126            match decode_spin_speed_current(tlv_value) {
127                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
128                Err(e) => format!("{{\"error\": \"{}\"}}", e),
129            }
130        }
131        0x0002 => {
132            match decode_number_of_rinses(tlv_value) {
133                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
134                Err(e) => format!("{{\"error\": \"{}\"}}", e),
135            }
136        }
137        0x0003 => {
138            match decode_supported_rinses(tlv_value) {
139                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
140                Err(e) => format!("{{\"error\": \"{}\"}}", e),
141            }
142        }
143        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
144    }
145}
146
147/// Get list of all attributes supported by this cluster
148///
149/// # Returns
150/// Vector of tuples containing (attribute_id, attribute_name)
151pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
152    vec![
153        (0x0000, "SpinSpeeds"),
154        (0x0001, "SpinSpeedCurrent"),
155        (0x0002, "NumberOfRinses"),
156        (0x0003, "SupportedRinses"),
157    ]
158}
159
160// Typed facade (invokes + reads)
161
162/// Read `SpinSpeeds` attribute from cluster `Laundry Washer Controls`.
163pub async fn read_spin_speeds(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<String>> {
164    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?;
165    decode_spin_speeds(&tlv)
166}
167
168/// Read `SpinSpeedCurrent` attribute from cluster `Laundry Washer Controls`.
169pub async fn read_spin_speed_current(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u8>> {
170    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?;
171    decode_spin_speed_current(&tlv)
172}
173
174/// Read `NumberOfRinses` attribute from cluster `Laundry Washer Controls`.
175pub async fn read_number_of_rinses(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<NumberOfRinses> {
176    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?;
177    decode_number_of_rinses(&tlv)
178}
179
180/// Read `SupportedRinses` attribute from cluster `Laundry Washer Controls`.
181pub async fn read_supported_rinses(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<NumberOfRinses>> {
182    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?;
183    decode_supported_rinses(&tlv)
184}
185