//! The light's own effects cluster: a manufacturer-specific cluster through //! which a coordinator selects a scene and tunes it. //! //! The cluster is not part of the Zigbee Cluster Library, so nothing speaks it //! without help: Home Assistant needs the quirk in `ha/zha_quirks`, which //! defines the same identifiers. //! //! Scenes are not an on/off thing. They play in the light's own colour and //! brightness, so the standard controls keep working while one runs. use zigbee::zcl::frame::Status; use zigbee::zcl::server::ClusterServer; use zigbee::zcl::types::descriptors::AttrInfo; use zigbee::zcl::types::descriptors::Attribute; use zigbee::zcl::types::descriptors::Cluster; use zigbee::zcl::types::descriptors::ReadWrite; use zigbee::zcl::types::enums::Enum8; use zigbee::zcl::types::enums::ZclEnum8; use zigbee::zcl::types::ids::AttributeId; use zigbee::zcl::types::ids::ClusterId; use zigbee::zcl::types::ids::ManufacturerCode; use zigbee::zcl::types::ids::TypeId; use zigbee::zcl::types::integers::Uint8; use zigbee::zdo::ClusterReply; use zigbee::zdo::ClusterRequest; use zigbee::zdo::ClusterRequestHandler; use crate::light; use crate::scene; use crate::scene::Scene; /// A development code: this device has no manufacturer code of its own. Not /// assigned to anyone in zigbee-herdsman's list of known codes, but unregistered. pub const MANUFACTURER_CODE: u16 = 0xc0de; /// Manufacturer-specific cluster identifiers run from 0xfc00 to 0xffff. const CLUSTER_ID_VALUE: u16 = 0xfd00; pub const CLUSTER: Cluster = Cluster::manufacturer_specific( ClusterId(CLUSTER_ID_VALUE), ManufacturerCode(MANUFACTURER_CODE), "SmartLightEffects", ); /// Cluster identifier, for the endpoint's cluster list. pub const CLUSTER_ID: u16 = CLUSTER.id().0; /// The scene playing: 0 none, 1 rainbow, 2 pulse, 3 comet. pub const EFFECT: Attribute, ReadWrite> = CLUSTER.attribute(AttributeId(0x0000), "Effect"); /// How fast the scene moves, 0 slowest to 255 fastest. pub const SPEED: Attribute = CLUSTER.attribute(AttributeId(0x0001), "Speed"); /// How pronounced the scene is: rainbow repeats, pulse depth, comet tail. pub const INTENSITY: Attribute = CLUSTER.attribute(AttributeId(0x0002), "Intensity"); const ATTRIBUTES: &[AttrInfo] = &[EFFECT.attr_info(), SPEED.attr_info(), INTENSITY.attr_info()]; impl ZclEnum8 for Scene { fn from_raw(raw: u8) -> Option { Scene::from_raw(raw) } fn into_raw(self) -> u8 { self as u8 } } /// Serves the effects cluster; the state lives in [`crate::scene`]. pub struct EffectsServer; impl ClusterServer for EffectsServer { fn cluster(&self) -> Cluster { CLUSTER } fn attributes(&self) -> &'static [AttrInfo] { ATTRIBUTES } fn encode_value(&self, id: AttributeId, out: &mut [u8], offset: &mut usize) -> Status { let encoded = match id.0 { 0x0000 => EFFECT.encode(scene::scene(), out, offset), 0x0001 => SPEED.encode(Uint8(scene::speed()), out, offset), 0x0002 => INTENSITY.encode(Uint8(scene::intensity()), out, offset), _ => return Status::UnsupportedAttribute, }; encoded.map_or(Status::InsufficientSpace, |()| Status::Success) } fn decode_value( &self, id: AttributeId, type_id: TypeId, bytes: &[u8], offset: &mut usize, ) -> Status { match id.0 { 0x0000 => match EFFECT.decode(type_id, bytes, offset) { Ok(scene) => { light::set_scene(scene); Status::Success } // the right type but not one of the scenes, or the wrong type Err(_) if type_id == EFFECT.type_id() => Status::InvalidValue, Err(_) => Status::InvalidDataType, }, 0x0001 => match SPEED.decode(type_id, bytes, offset) { Ok(Uint8(speed)) => { scene::set_speed(speed); Status::Success } Err(_) => Status::InvalidDataType, }, 0x0002 => match INTENSITY.decode(type_id, bytes, offset) { Ok(Uint8(intensity)) => { scene::set_intensity(intensity); Status::Success } Err(_) => Status::InvalidDataType, }, _ => Status::UnsupportedAttribute, } } } impl ClusterRequestHandler for EffectsServer { fn handle(&self, request: &ClusterRequest<'_>, out: &mut [u8]) -> Option { self.handle_request(request, out) } }