#![allow(unused_imports)]
use crate::dm::conformance::{Clause, Condition, Conform, Conformance};
use crate::dm::meta::{EventPriority, Reporting};
use crate::dm::spec::{Attribute, Cluster, Command, Event, Feature};
use crate::dm::{Access, AccessQualities, AttributeQualities, Privilege};
use crate::error::Result;
use crate::im::{AttributeId, ClusterId, CommandId, EventId};
use crate::tlv::{
ContainerKind, Element, FromTlv, Nullable, Tag, TlvList, TlvReader, TlvWriter, ToTlv, Value,
};
pub const ID: ClusterId = 0x0094;
pub const REVISION: u16 = 2;
pub const PICS: &str = "EWATERHTR";
pub mod feature {
pub const ENERGY_MANAGEMENT: u32 = 1 << 0;
pub const TANK_PERCENT: u32 = 1 << 1;
}
pub const FEATURES: &[Feature] = &[
Feature {
bit: 0,
code: "EM",
name: "EnergyManagement",
conform: Conform::OPTIONAL,
},
Feature {
bit: 1,
code: "TP",
name: "TankPercent",
conform: Conform::OPTIONAL,
},
];
pub mod attribute {
use crate::im::AttributeId;
pub const HEATER_TYPES: AttributeId = 0x00;
pub const HEAT_DEMAND: AttributeId = 0x01;
pub const TANK_VOLUME: AttributeId = 0x02;
pub const ESTIMATED_HEAT_REQUIRED: AttributeId = 0x03;
pub const TANK_PERCENTAGE: AttributeId = 0x04;
pub const BOOST_STATE: AttributeId = 0x05;
}
pub mod command {
use crate::im::CommandId;
pub const BOOST: CommandId = 0x00;
pub const CANCEL_BOOST: CommandId = 0x01;
}
pub mod event {
use crate::im::EventId;
pub const BOOST_STARTED: EventId = 0x00;
pub const BOOST_ENDED: EventId = 0x01;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum BoostStateEnum {
Inactive = 0,
Active = 1,
}
impl BoostStateEnum {
#[must_use]
pub const fn from_value(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Inactive),
1 => Some(Self::Active),
_ => None,
}
}
#[must_use]
pub const fn value(self) -> u8 {
self as u8
}
}
impl ToTlv for BoostStateEnum {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
self.value().to_tlv(w, tag)
}
}
impl<'a> FromTlv<'a> for BoostStateEnum {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
}
}
bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct WaterHeaterHeatSourceBitmap: u8 {
const IMMERSION_ELEMENT1 = 1 << 0;
const IMMERSION_ELEMENT2 = 1 << 1;
const HEAT_PUMP = 1 << 2;
const BOILER = 1 << 3;
const OTHER = 1 << 4;
}
}
impl ToTlv for WaterHeaterHeatSourceBitmap {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
self.bits().to_tlv(w, tag)
}
}
impl<'a> FromTlv<'a> for WaterHeaterHeatSourceBitmap {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
let bits = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
Self::from_bits(bits).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct WaterHeaterBoostInfoStruct {
pub duration: u32,
pub one_shot: Option<bool>,
pub emergency_boost: Option<bool>,
pub temporary_setpoint: Option<i16>,
pub target_percentage: Option<u8>,
pub target_reheat: Option<u8>,
}
impl ToTlv for WaterHeaterBoostInfoStruct {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.duration.to_tlv(w, Tag::Context(0))?;
if let Some(value) = &self.one_shot {
value.to_tlv(w, Tag::Context(1))?;
}
if let Some(value) = &self.emergency_boost {
value.to_tlv(w, Tag::Context(2))?;
}
if let Some(value) = &self.temporary_setpoint {
value.to_tlv(w, Tag::Context(3))?;
}
if let Some(value) = &self.target_percentage {
value.to_tlv(w, Tag::Context(4))?;
}
if let Some(value) = &self.target_reheat {
value.to_tlv(w, Tag::Context(5))?;
}
w.end_container()
}
}
impl<'a> FromTlv<'a> for WaterHeaterBoostInfoStruct {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut duration = None;
let mut one_shot = None;
let mut emergency_boost = None;
let mut temporary_setpoint = None;
let mut target_percentage = None;
let mut target_reheat = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => duration = Some(<u32>::from_tlv(reader, &field)?),
Some(1) => one_shot = Some(<bool>::from_tlv(reader, &field)?),
Some(2) => emergency_boost = Some(<bool>::from_tlv(reader, &field)?),
Some(3) => temporary_setpoint = Some(<i16>::from_tlv(reader, &field)?),
Some(4) => target_percentage = Some(<u8>::from_tlv(reader, &field)?),
Some(5) => target_reheat = Some(<u8>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
duration: duration.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
one_shot,
emergency_boost,
temporary_setpoint,
target_percentage,
target_reheat,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BoostFields {
pub boost_info: WaterHeaterBoostInfoStruct,
}
impl ToTlv for BoostFields {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.boost_info.to_tlv(w, Tag::Context(0))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for BoostFields {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut boost_info = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => {
boost_info = Some(<WaterHeaterBoostInfoStruct>::from_tlv(reader, &field)?)
}
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
boost_info: boost_info.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
pub const ATTRIBUTES: &[Attribute] = &[
Attribute {
id: 0x0000,
name: "HeaterTypes",
kind: "WaterHeaterHeatSourceBitmap",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::FIXED,
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0001,
name: "HeatDemand",
kind: "WaterHeaterHeatSourceBitmap",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0002,
name: "TankVolume",
kind: "uint16",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Feature(0),
}],
},
},
Attribute {
id: 0x0003,
name: "EstimatedHeatRequired",
kind: "energy-mWh",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Feature(0),
}],
},
},
Attribute {
id: 0x0004,
name: "TankPercentage",
kind: "percent",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Feature(1),
}],
},
},
Attribute {
id: 0x0005,
name: "BoostState",
kind: "BoostStateEnum",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
];
pub const COMMANDS: &[Command] = &[
Command {
id: 0x00,
name: "Boost",
to_server: true,
response: None,
access: Access::invoke(Privilege::Manage),
conform: Conform::MANDATORY,
},
Command {
id: 0x01,
name: "CancelBoost",
to_server: true,
response: None,
access: Access::invoke(Privilege::Manage),
conform: Conform::MANDATORY,
},
];
pub const EVENTS: &[Event] = &[
Event {
id: 0x00,
name: "BoostStarted",
priority: EventPriority::Info,
access: Access::read_only(Privilege::View),
conform: Conform::MANDATORY,
},
Event {
id: 0x01,
name: "BoostEnded",
priority: EventPriority::Info,
access: Access::read_only(Privilege::View),
conform: Conform::MANDATORY,
},
];
pub const CLUSTER: Cluster = Cluster {
id: ID,
name: "Water Heater Management",
revision: REVISION,
pics: "EWATERHTR",
derived_from: None,
features: FEATURES,
attributes: ATTRIBUTES,
commands: COMMANDS,
events: EVENTS,
};