matc/clusters/codec/
mode_water_heater.rs

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