feat: add light effects driven over Zigbee
Add animated effects to the light, controlled from Home Assistant. * Identify Trigger Effect (ZCL 3.5.2.3.3): the light plays Blink, Breathe, Okay and Channel change as overlays on its current state, and returns to exactly what it was showing (src/effect.rs). Home Assistant's flash option uses this. * Scenes: Rainbow, Pulse and Comet run continuously and use the light's own colour and brightness, so on/off, level and colour keep working while one plays (src/scene.rs). The strip is redrawn every 20 ms while anything animates. * A manufacturer-specific effects cluster (0xfd00) selects the scene and tunes its speed and intensity (src/effects_cluster.rs). It uses 0xc0de as a development manufacturer code, which is also what the node descriptor now reports instead of the NXP code it had borrowed. * A ZHA quirk exposes the cluster as a select and two number entities (ha/zha_quirks/smart_light.py). Copy it to a folder set as ZHA's custom_quirks_path. * Ignore Python bytecode. vendor/zigbee-rs is not vendored here: it stays a submodule, now tracking feature/lighting-effects-integration on the fork. That branch adds the manufacturer-specific cluster support and Identify Trigger Effect on top of the RGB colour control work, each of which is also on its own branch for upstream. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
1 parent
cb4b38fc01
commit
115f245484
10 files changed
+665
-13
No files matched your search
@@ -0,0 +1,133 @@
|
||||
//! 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<Enum8<Scene>, ReadWrite> =
|
||||
CLUSTER.attribute(AttributeId(0x0000), "Effect");
|
||||
|
||||
/// How fast the scene moves, 0 slowest to 255 fastest.
|
||||
pub const SPEED: Attribute<Uint8, ReadWrite> = CLUSTER.attribute(AttributeId(0x0001), "Speed");
|
||||
|
||||
/// How pronounced the scene is: rainbow repeats, pulse depth, comet tail.
|
||||
pub const INTENSITY: Attribute<Uint8, ReadWrite> =
|
||||
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<Self> {
|
||||
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<ClusterReply> {
|
||||
self.handle_request(request, out)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user