matc/clusters/codec/
mode_microwave_oven.rs

1//! Matter TLV encoders and decoders for Microwave Oven Mode Cluster
2//! Cluster ID: 0x005E
3//!
4//! This file is automatically generated from Mode_MicrowaveOven.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    Normal = 16384,
29    Defrost = 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::Normal),
52            16385 => Some(ModeTag::Defrost),
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// Command encoders
75
76// Attribute decoders
77
78/// Decode SupportedModes attribute (0x0000)
79pub fn decode_supported_modes(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
80    if let tlv::TlvItemValue::Int(v) = inp {
81        Ok(*v as u8)
82    } else {
83        Err(anyhow::anyhow!("Expected UInt8"))
84    }
85}
86
87/// Decode CurrentMode attribute (0x0001)
88pub fn decode_current_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
89    if let tlv::TlvItemValue::Int(v) = inp {
90        Ok(*v as u8)
91    } else {
92        Err(anyhow::anyhow!("Expected UInt8"))
93    }
94}
95
96/// Decode StartUpMode attribute (0x0002)
97pub fn decode_start_up_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
98    if let tlv::TlvItemValue::Int(v) = inp {
99        Ok(*v as u8)
100    } else {
101        Err(anyhow::anyhow!("Expected UInt8"))
102    }
103}
104
105/// Decode OnMode attribute (0x0003)
106pub fn decode_on_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
107    if let tlv::TlvItemValue::Int(v) = inp {
108        Ok(*v as u8)
109    } else {
110        Err(anyhow::anyhow!("Expected UInt8"))
111    }
112}
113
114
115// JSON dispatcher function
116
117/// Decode attribute value and return as JSON string
118///
119/// # Parameters
120/// * `cluster_id` - The cluster identifier
121/// * `attribute_id` - The attribute identifier
122/// * `tlv_value` - The TLV value to decode
123///
124/// # Returns
125/// JSON string representation of the decoded value or error
126pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
127    // Verify this is the correct cluster
128    if cluster_id != 0x005E {
129        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x005E, got {}\"}}", cluster_id);
130    }
131
132    match attribute_id {
133        0x0000 => {
134            match decode_supported_modes(tlv_value) {
135                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
136                Err(e) => format!("{{\"error\": \"{}\"}}", e),
137            }
138        }
139        0x0001 => {
140            match decode_current_mode(tlv_value) {
141                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
142                Err(e) => format!("{{\"error\": \"{}\"}}", e),
143            }
144        }
145        0x0002 => {
146            match decode_start_up_mode(tlv_value) {
147                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
148                Err(e) => format!("{{\"error\": \"{}\"}}", e),
149            }
150        }
151        0x0003 => {
152            match decode_on_mode(tlv_value) {
153                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
154                Err(e) => format!("{{\"error\": \"{}\"}}", e),
155            }
156        }
157        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
158    }
159}
160
161/// Get list of all attributes supported by this cluster
162///
163/// # Returns
164/// Vector of tuples containing (attribute_id, attribute_name)
165pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
166    vec![
167        (0x0000, "SupportedModes"),
168        (0x0001, "CurrentMode"),
169        (0x0002, "StartUpMode"),
170        (0x0003, "OnMode"),
171    ]
172}
173
174// Command listing
175
176pub fn get_command_list() -> Vec<(u32, &'static str)> {
177    vec![
178        (0x00, "ChangeToMode"),
179        (0x01, "ChangeToModeResponse"),
180    ]
181}
182
183pub fn get_command_name(cmd_id: u32) -> Option<&'static str> {
184    match cmd_id {
185        0x00 => Some("ChangeToMode"),
186        0x01 => Some("ChangeToModeResponse"),
187        _ => None,
188    }
189}
190
191pub fn get_command_schema(cmd_id: u32) -> Option<Vec<crate::clusters::codec::CommandField>> {
192    match cmd_id {
193        0x00 => Some(vec![]),
194        0x01 => Some(vec![]),
195        _ => None,
196    }
197}
198
199pub fn encode_command_json(cmd_id: u32, _args: &serde_json::Value) -> anyhow::Result<Vec<u8>> {
200    match cmd_id {
201        0x00 => Ok(vec![]),
202        0x01 => Ok(vec![]),
203        _ => Err(anyhow::anyhow!("unknown command ID: 0x{:02X}", cmd_id)),
204    }
205}
206
207// Typed facade (invokes + reads)
208
209/// Invoke `ChangeToMode` command on cluster `Microwave Oven Mode`.
210pub async fn change_to_mode(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<()> {
211    conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_MICROWAVE_OVEN_MODE, crate::clusters::defs::CLUSTER_MICROWAVE_OVEN_MODE_CMD_ID_CHANGETOMODE, &[]).await?;
212    Ok(())
213}
214
215/// Invoke `ChangeToModeResponse` command on cluster `Microwave Oven Mode`.
216pub async fn change_to_mode_response(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<()> {
217    conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_MICROWAVE_OVEN_MODE, crate::clusters::defs::CLUSTER_MICROWAVE_OVEN_MODE_CMD_ID_CHANGETOMODERESPONSE, &[]).await?;
218    Ok(())
219}
220
221/// Read `SupportedModes` attribute from cluster `Microwave Oven Mode`.
222pub async fn read_supported_modes(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
223    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_MICROWAVE_OVEN_MODE, crate::clusters::defs::CLUSTER_MICROWAVE_OVEN_MODE_ATTR_ID_SUPPORTEDMODES).await?;
224    decode_supported_modes(&tlv)
225}
226
227/// Read `CurrentMode` attribute from cluster `Microwave Oven Mode`.
228pub async fn read_current_mode(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
229    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_MICROWAVE_OVEN_MODE, crate::clusters::defs::CLUSTER_MICROWAVE_OVEN_MODE_ATTR_ID_CURRENTMODE).await?;
230    decode_current_mode(&tlv)
231}
232
233/// Read `StartUpMode` attribute from cluster `Microwave Oven Mode`.
234pub async fn read_start_up_mode(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
235    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_MICROWAVE_OVEN_MODE, crate::clusters::defs::CLUSTER_MICROWAVE_OVEN_MODE_ATTR_ID_STARTUPMODE).await?;
236    decode_start_up_mode(&tlv)
237}
238
239/// Read `OnMode` attribute from cluster `Microwave Oven Mode`.
240pub async fn read_on_mode(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
241    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_MICROWAVE_OVEN_MODE, crate::clusters::defs::CLUSTER_MICROWAVE_OVEN_MODE_ATTR_ID_ONMODE).await?;
242    decode_on_mode(&tlv)
243}
244