matc/clusters/codec/
mode_device_energy_management.rs

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