matc/clusters/codec/
mode_refrigerator.rs

1//! Matter TLV encoders and decoders for Refrigerator And Temperature Controlled Cabinet Mode Cluster
2//! Cluster ID: 0x0052
3//!
4//! This file is automatically generated from Mode_Refrigerator.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(u16)]
17pub enum ModeTag {
18    Auto = 0,
19    Quick = 1,
20    Quiet = 2,
21    Lownoise = 3,
22    Lowenergy = 4,
23    Vacation = 5,
24    Min = 6,
25    Max = 7,
26    Night = 8,
27    Day = 9,
28    Rapidcool = 16384,
29    Rapidfreeze = 16385,
30}
31
32impl ModeTag {
33    /// Convert from u8 value (promoted to u16)
34    pub fn from_u8(value: u8) -> Option<Self> {
35        Self::from_u16(value as u16)
36    }
37
38    /// Convert from u16 value
39    pub fn from_u16(value: u16) -> Option<Self> {
40        match value {
41            0 => Some(ModeTag::Auto),
42            1 => Some(ModeTag::Quick),
43            2 => Some(ModeTag::Quiet),
44            3 => Some(ModeTag::Lownoise),
45            4 => Some(ModeTag::Lowenergy),
46            5 => Some(ModeTag::Vacation),
47            6 => Some(ModeTag::Min),
48            7 => Some(ModeTag::Max),
49            8 => Some(ModeTag::Night),
50            9 => Some(ModeTag::Day),
51            16384 => Some(ModeTag::Rapidcool),
52            16385 => Some(ModeTag::Rapidfreeze),
53            _ => None,
54        }
55    }
56
57    /// Convert to u8 value (truncated if value > 255)
58    pub fn to_u8(self) -> u8 {
59        self as u8
60    }
61
62    /// Convert to u16 value
63    pub fn to_u16(self) -> u16 {
64        self as u16
65    }
66}
67
68impl From<ModeTag> for u16 {
69    fn from(val: ModeTag) -> Self {
70        val as u16
71    }
72}
73
74// Struct definitions
75
76#[derive(Debug, serde::Serialize)]
77pub struct ModeOption {
78    pub label: Option<u8>,
79    pub mode: Option<u8>,
80    pub mode_tags: Option<u8>,
81}
82
83// Attribute decoders
84
85/// Decode SupportedModes attribute (0x0000)
86pub fn decode_supported_modes(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
87    if let tlv::TlvItemValue::Int(v) = inp {
88        Ok(*v as u8)
89    } else {
90        Err(anyhow::anyhow!("Expected UInt8"))
91    }
92}
93
94/// Decode CurrentMode attribute (0x0001)
95pub fn decode_current_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
96    if let tlv::TlvItemValue::Int(v) = inp {
97        Ok(*v as u8)
98    } else {
99        Err(anyhow::anyhow!("Expected UInt8"))
100    }
101}
102
103/// Decode StartUpMode attribute (0x0002)
104pub fn decode_start_up_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
105    if let tlv::TlvItemValue::Int(v) = inp {
106        Ok(*v as u8)
107    } else {
108        Err(anyhow::anyhow!("Expected UInt8"))
109    }
110}
111
112/// Decode OnMode attribute (0x0003)
113pub fn decode_on_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
114    if let tlv::TlvItemValue::Int(v) = inp {
115        Ok(*v as u8)
116    } else {
117        Err(anyhow::anyhow!("Expected UInt8"))
118    }
119}
120
121
122// JSON dispatcher function
123
124/// Decode attribute value and return as JSON string
125///
126/// # Parameters
127/// * `cluster_id` - The cluster identifier
128/// * `attribute_id` - The attribute identifier
129/// * `tlv_value` - The TLV value to decode
130///
131/// # Returns
132/// JSON string representation of the decoded value or error
133pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
134    // Verify this is the correct cluster
135    if cluster_id != 0x0052 {
136        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0052, got {}\"}}", cluster_id);
137    }
138
139    match attribute_id {
140        0x0000 => {
141            match decode_supported_modes(tlv_value) {
142                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
143                Err(e) => format!("{{\"error\": \"{}\"}}", e),
144            }
145        }
146        0x0001 => {
147            match decode_current_mode(tlv_value) {
148                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
149                Err(e) => format!("{{\"error\": \"{}\"}}", e),
150            }
151        }
152        0x0002 => {
153            match decode_start_up_mode(tlv_value) {
154                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
155                Err(e) => format!("{{\"error\": \"{}\"}}", e),
156            }
157        }
158        0x0003 => {
159            match decode_on_mode(tlv_value) {
160                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
161                Err(e) => format!("{{\"error\": \"{}\"}}", e),
162            }
163        }
164        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
165    }
166}
167
168/// Get list of all attributes supported by this cluster
169///
170/// # Returns
171/// Vector of tuples containing (attribute_id, attribute_name)
172pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
173    vec![
174        (0x0000, "SupportedModes"),
175        (0x0001, "CurrentMode"),
176        (0x0002, "StartUpMode"),
177        (0x0003, "OnMode"),
178    ]
179}
180
181// Typed facade (invokes + reads)
182
183/// Read `SupportedModes` attribute from cluster `Refrigerator And Temperature Controlled Cabinet Mode`.
184pub async fn read_supported_modes(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
185    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_REFRIGERATOR_AND_TEMPERATURE_CONTROLLED_CABINET_MODE, crate::clusters::defs::CLUSTER_REFRIGERATOR_AND_TEMPERATURE_CONTROLLED_CABINET_MODE_ATTR_ID_SUPPORTEDMODES).await?;
186    decode_supported_modes(&tlv)
187}
188
189/// Read `CurrentMode` attribute from cluster `Refrigerator And Temperature Controlled Cabinet Mode`.
190pub async fn read_current_mode(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
191    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_REFRIGERATOR_AND_TEMPERATURE_CONTROLLED_CABINET_MODE, crate::clusters::defs::CLUSTER_REFRIGERATOR_AND_TEMPERATURE_CONTROLLED_CABINET_MODE_ATTR_ID_CURRENTMODE).await?;
192    decode_current_mode(&tlv)
193}
194
195/// Read `StartUpMode` attribute from cluster `Refrigerator And Temperature Controlled Cabinet Mode`.
196pub async fn read_start_up_mode(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
197    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_REFRIGERATOR_AND_TEMPERATURE_CONTROLLED_CABINET_MODE, crate::clusters::defs::CLUSTER_REFRIGERATOR_AND_TEMPERATURE_CONTROLLED_CABINET_MODE_ATTR_ID_STARTUPMODE).await?;
198    decode_start_up_mode(&tlv)
199}
200
201/// Read `OnMode` attribute from cluster `Refrigerator And Temperature Controlled Cabinet Mode`.
202pub async fn read_on_mode(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
203    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_REFRIGERATOR_AND_TEMPERATURE_CONTROLLED_CABINET_MODE, crate::clusters::defs::CLUSTER_REFRIGERATOR_AND_TEMPERATURE_CONTROLLED_CABINET_MODE_ATTR_ID_ONMODE).await?;
204    decode_on_mode(&tlv)
205}
206