#![allow(clippy::too_many_arguments)]
use crate::tlv;
use anyhow;
use serde_json;
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum AirflowDirection {
Forward = 0,
Reverse = 1,
}
impl AirflowDirection {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(AirflowDirection::Forward),
1 => Some(AirflowDirection::Reverse),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<AirflowDirection> for u8 {
fn from(val: AirflowDirection) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum FanMode {
Off = 0,
Low = 1,
Medium = 2,
High = 3,
On = 4,
Auto = 5,
Smart = 6,
}
impl FanMode {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(FanMode::Off),
1 => Some(FanMode::Low),
2 => Some(FanMode::Medium),
3 => Some(FanMode::High),
4 => Some(FanMode::On),
5 => Some(FanMode::Auto),
6 => Some(FanMode::Smart),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<FanMode> for u8 {
fn from(val: FanMode) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum FanModeSequence {
Offlowmedhigh = 0,
Offlowhigh = 1,
Offlowmedhighauto = 2,
Offlowhighauto = 3,
Offhighauto = 4,
Offhigh = 5,
}
impl FanModeSequence {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(FanModeSequence::Offlowmedhigh),
1 => Some(FanModeSequence::Offlowhigh),
2 => Some(FanModeSequence::Offlowmedhighauto),
3 => Some(FanModeSequence::Offlowhighauto),
4 => Some(FanModeSequence::Offhighauto),
5 => Some(FanModeSequence::Offhigh),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<FanModeSequence> for u8 {
fn from(val: FanModeSequence) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum StepDirection {
Increase = 0,
Decrease = 1,
}
impl StepDirection {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(StepDirection::Increase),
1 => Some(StepDirection::Decrease),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<StepDirection> for u8 {
fn from(val: StepDirection) -> Self {
val as u8
}
}
pub type Rock = u8;
pub mod rock {
pub const ROCK_LEFT_RIGHT: u8 = 0x01;
pub const ROCK_UP_DOWN: u8 = 0x02;
pub const ROCK_ROUND: u8 = 0x04;
}
pub type Wind = u8;
pub mod wind {
pub const SLEEP_WIND: u8 = 0x01;
pub const NATURAL_WIND: u8 = 0x02;
}
pub fn encode_step(direction: StepDirection, wrap: Option<bool>, lowest_off: Option<bool>) -> anyhow::Result<Vec<u8>> {
let mut tlv_fields: Vec<tlv::TlvItemEnc> = Vec::new();
tlv_fields.push((0, tlv::TlvItemValueEnc::UInt8(direction.to_u8())).into());
if let Some(x) = wrap { tlv_fields.push((1, tlv::TlvItemValueEnc::Bool(x)).into()); }
if let Some(x) = lowest_off { tlv_fields.push((2, tlv::TlvItemValueEnc::Bool(x)).into()); }
let tlv = tlv::TlvItemEnc {
tag: 0,
value: tlv::TlvItemValueEnc::StructInvisible(tlv_fields),
};
Ok(tlv.encode()?)
}
pub fn decode_fan_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<FanMode> {
if let tlv::TlvItemValue::Int(v) = inp {
FanMode::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_fan_mode_sequence(inp: &tlv::TlvItemValue) -> anyhow::Result<FanModeSequence> {
if let tlv::TlvItemValue::Int(v) = inp {
FanModeSequence::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_percent_setting(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u8>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u8))
} else {
Ok(None)
}
}
pub fn decode_percent_current(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u8)
} else {
Err(anyhow::anyhow!("Expected UInt8"))
}
}
pub fn decode_speed_max(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u8)
} else {
Err(anyhow::anyhow!("Expected UInt8"))
}
}
pub fn decode_speed_setting(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u8>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u8))
} else {
Ok(None)
}
}
pub fn decode_speed_current(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u8)
} else {
Err(anyhow::anyhow!("Expected UInt8"))
}
}
pub fn decode_rock_support(inp: &tlv::TlvItemValue) -> anyhow::Result<Rock> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u8)
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_rock_setting(inp: &tlv::TlvItemValue) -> anyhow::Result<Rock> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u8)
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_wind_support(inp: &tlv::TlvItemValue) -> anyhow::Result<Wind> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u8)
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_wind_setting(inp: &tlv::TlvItemValue) -> anyhow::Result<Wind> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u8)
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_airflow_direction(inp: &tlv::TlvItemValue) -> anyhow::Result<AirflowDirection> {
if let tlv::TlvItemValue::Int(v) = inp {
AirflowDirection::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
if cluster_id != 0x0202 {
return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0202, got {}\"}}", cluster_id);
}
match attribute_id {
0x0000 => {
match decode_fan_mode(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0001 => {
match decode_fan_mode_sequence(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0002 => {
match decode_percent_setting(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0003 => {
match decode_percent_current(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0004 => {
match decode_speed_max(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0005 => {
match decode_speed_setting(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0006 => {
match decode_speed_current(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0007 => {
match decode_rock_support(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0008 => {
match decode_rock_setting(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0009 => {
match decode_wind_support(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x000A => {
match decode_wind_setting(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x000B => {
match decode_airflow_direction(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
_ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
}
}
pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
vec![
(0x0000, "FanMode"),
(0x0001, "FanModeSequence"),
(0x0002, "PercentSetting"),
(0x0003, "PercentCurrent"),
(0x0004, "SpeedMax"),
(0x0005, "SpeedSetting"),
(0x0006, "SpeedCurrent"),
(0x0007, "RockSupport"),
(0x0008, "RockSetting"),
(0x0009, "WindSupport"),
(0x000A, "WindSetting"),
(0x000B, "AirflowDirection"),
]
}
pub fn get_command_list() -> Vec<(u32, &'static str)> {
vec![
(0x00, "Step"),
]
}
pub fn get_command_name(cmd_id: u32) -> Option<&'static str> {
match cmd_id {
0x00 => Some("Step"),
_ => None,
}
}
pub fn get_command_schema(cmd_id: u32) -> Option<Vec<crate::clusters::codec::CommandField>> {
match cmd_id {
0x00 => Some(vec![
crate::clusters::codec::CommandField { tag: 0, name: "direction", kind: crate::clusters::codec::FieldKind::Enum { name: "StepDirection", variants: &[(0, "Increase"), (1, "Decrease")] }, optional: false, nullable: false },
crate::clusters::codec::CommandField { tag: 1, name: "wrap", kind: crate::clusters::codec::FieldKind::Bool, optional: true, nullable: false },
crate::clusters::codec::CommandField { tag: 2, name: "lowest_off", kind: crate::clusters::codec::FieldKind::Bool, optional: true, nullable: false },
]),
_ => None,
}
}
pub fn encode_command_json(cmd_id: u32, args: &serde_json::Value) -> anyhow::Result<Vec<u8>> {
match cmd_id {
0x00 => {
let direction = {
let n = crate::clusters::codec::json_util::get_u64(args, "direction")?;
StepDirection::from_u8(n as u8).ok_or_else(|| anyhow::anyhow!("invalid StepDirection: {}", n))?
};
let wrap = crate::clusters::codec::json_util::get_opt_bool(args, "wrap")?;
let lowest_off = crate::clusters::codec::json_util::get_opt_bool(args, "lowest_off")?;
encode_step(direction, wrap, lowest_off)
}
_ => Err(anyhow::anyhow!("unknown command ID: 0x{:02X}", cmd_id)),
}
}
pub async fn step(conn: &crate::controller::Connection, endpoint: u16, direction: StepDirection, wrap: Option<bool>, lowest_off: Option<bool>) -> anyhow::Result<()> {
conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_FAN_CONTROL, crate::clusters::defs::CLUSTER_FAN_CONTROL_CMD_ID_STEP, &encode_step(direction, wrap, lowest_off)?).await?;
Ok(())
}
pub async fn read_fan_mode(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<FanMode> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_FAN_CONTROL, crate::clusters::defs::CLUSTER_FAN_CONTROL_ATTR_ID_FANMODE).await?;
decode_fan_mode(&tlv)
}
pub async fn read_fan_mode_sequence(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<FanModeSequence> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_FAN_CONTROL, crate::clusters::defs::CLUSTER_FAN_CONTROL_ATTR_ID_FANMODESEQUENCE).await?;
decode_fan_mode_sequence(&tlv)
}
pub async fn read_percent_setting(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u8>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_FAN_CONTROL, crate::clusters::defs::CLUSTER_FAN_CONTROL_ATTR_ID_PERCENTSETTING).await?;
decode_percent_setting(&tlv)
}
pub async fn read_percent_current(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_FAN_CONTROL, crate::clusters::defs::CLUSTER_FAN_CONTROL_ATTR_ID_PERCENTCURRENT).await?;
decode_percent_current(&tlv)
}
pub async fn read_speed_max(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_FAN_CONTROL, crate::clusters::defs::CLUSTER_FAN_CONTROL_ATTR_ID_SPEEDMAX).await?;
decode_speed_max(&tlv)
}
pub async fn read_speed_setting(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u8>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_FAN_CONTROL, crate::clusters::defs::CLUSTER_FAN_CONTROL_ATTR_ID_SPEEDSETTING).await?;
decode_speed_setting(&tlv)
}
pub async fn read_speed_current(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_FAN_CONTROL, crate::clusters::defs::CLUSTER_FAN_CONTROL_ATTR_ID_SPEEDCURRENT).await?;
decode_speed_current(&tlv)
}
pub async fn read_rock_support(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Rock> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_FAN_CONTROL, crate::clusters::defs::CLUSTER_FAN_CONTROL_ATTR_ID_ROCKSUPPORT).await?;
decode_rock_support(&tlv)
}
pub async fn read_rock_setting(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Rock> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_FAN_CONTROL, crate::clusters::defs::CLUSTER_FAN_CONTROL_ATTR_ID_ROCKSETTING).await?;
decode_rock_setting(&tlv)
}
pub async fn read_wind_support(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Wind> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_FAN_CONTROL, crate::clusters::defs::CLUSTER_FAN_CONTROL_ATTR_ID_WINDSUPPORT).await?;
decode_wind_support(&tlv)
}
pub async fn read_wind_setting(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Wind> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_FAN_CONTROL, crate::clusters::defs::CLUSTER_FAN_CONTROL_ATTR_ID_WINDSETTING).await?;
decode_wind_setting(&tlv)
}
pub async fn read_airflow_direction(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<AirflowDirection> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_FAN_CONTROL, crate::clusters::defs::CLUSTER_FAN_CONTROL_ATTR_ID_AIRFLOWDIRECTION).await?;
decode_airflow_direction(&tlv)
}