matter-clusters 0.1.0

Matter protocol cluster definitions (generated from the spec).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
//! WindowCovering cluster (0x0102).
//! @generated by `cargo xtask codegen` — do not edit.

#![allow(
    clippy::all,
    clippy::pedantic,
    dead_code,
    unreachable_pub,
    unused_imports
)]

use crate::datatypes::SemanticTagStruct;
use crate::error::ClusterError;
use crate::types::Nullable;
use matter_codec::{ContainerKind, Element, Tag, TlvReader, TlvWriter, Value};

/// Cluster ID.
pub const CLUSTER_ID: u32 = 0x0102;
/// Cluster revision.
pub const CLUSTER_REVISION: u16 = 8;

/// Command IDs (requests and responses).
pub mod command_id {
    /// `UpOrOpen` (request).
    pub const UP_OR_OPEN: u32 = 0x00;
    /// `DownOrClose` (request).
    pub const DOWN_OR_CLOSE: u32 = 0x01;
    /// `StopMotion` (request).
    pub const STOP_MOTION: u32 = 0x02;
    /// `GoToLiftPercentage` (request).
    pub const GO_TO_LIFT_PERCENTAGE: u32 = 0x05;
    /// `GoToTiltPercentage` (request).
    pub const GO_TO_TILT_PERCENTAGE: u32 = 0x08;
}

/// Attribute IDs (cluster-specific).
pub mod attribute_id {
    /// `Type`.
    pub const TYPE: u32 = 0x0000;
    /// `NumberOfActuationsLift`.
    pub const NUMBER_OF_ACTUATIONS_LIFT: u32 = 0x0005;
    /// `NumberOfActuationsTilt`.
    pub const NUMBER_OF_ACTUATIONS_TILT: u32 = 0x0006;
    /// `ConfigStatus`.
    pub const CONFIG_STATUS: u32 = 0x0007;
    /// `CurrentPositionLiftPercentage`.
    pub const CURRENT_POSITION_LIFT_PERCENTAGE: u32 = 0x0008;
    /// `CurrentPositionTiltPercentage`.
    pub const CURRENT_POSITION_TILT_PERCENTAGE: u32 = 0x0009;
    /// `OperationalStatus`.
    pub const OPERATIONAL_STATUS: u32 = 0x000A;
    /// `TargetPositionLiftPercent100ths`.
    pub const TARGET_POSITION_LIFT_PERCENT100THS: u32 = 0x000B;
    /// `TargetPositionTiltPercent100ths`.
    pub const TARGET_POSITION_TILT_PERCENT100THS: u32 = 0x000C;
    /// `EndProductType`.
    pub const END_PRODUCT_TYPE: u32 = 0x000D;
    /// `CurrentPositionLiftPercent100ths`.
    pub const CURRENT_POSITION_LIFT_PERCENT100THS: u32 = 0x000E;
    /// `CurrentPositionTiltPercent100ths`.
    pub const CURRENT_POSITION_TILT_PERCENT100THS: u32 = 0x000F;
    /// `Mode`.
    pub const MODE: u32 = 0x0017;
    /// `SafetyStatus`.
    pub const SAFETY_STATUS: u32 = 0x001A;
}

bitflags::bitflags! {
    /// `WindowCovering` feature bits (FeatureMap).
    #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
    pub struct Feature: u32 {
        /// Lift (LF).
        const LF = 1 << 0;
        /// Tilt (TL).
        const TL = 1 << 1;
        /// PositionAwareLift (PA_LF).
        const PA_LF = 1 << 2;
        /// PositionAwareTilt (PA_TL).
        const PA_TL = 1 << 4;
    }
}

bitflags::bitflags! {
    /// `ConfigStatusBitmap` (map8).
    #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
    pub struct ConfigStatusBitmap: u8 {
        /// Operational.
        const OPERATIONAL = 1 << 0;
        /// OnlineReserved.
        const ONLINE_RESERVED = 1 << 1;
        /// LiftMovementReversed.
        const LIFT_MOVEMENT_REVERSED = 1 << 2;
        /// LiftPositionAware.
        const LIFT_POSITION_AWARE = 1 << 3;
        /// TiltPositionAware.
        const TILT_POSITION_AWARE = 1 << 4;
        /// LiftEncoderControlled.
        const LIFT_ENCODER_CONTROLLED = 1 << 5;
        /// TiltEncoderControlled.
        const TILT_ENCODER_CONTROLLED = 1 << 6;
    }
}

/// `EndProductTypeEnum` (enum8).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum EndProductTypeEnum {
    /// RollerShade = 0.
    RollerShade,
    /// RomanShade = 1.
    RomanShade,
    /// BalloonShade = 2.
    BalloonShade,
    /// WovenWood = 3.
    WovenWood,
    /// PleatedShade = 4.
    PleatedShade,
    /// CellularShade = 5.
    CellularShade,
    /// LayeredShade = 6.
    LayeredShade,
    /// LayeredShade2D = 7.
    LayeredShade2D,
    /// SheerShade = 8.
    SheerShade,
    /// TiltOnlyInteriorBlind = 9.
    TiltOnlyInteriorBlind,
    /// InteriorBlind = 10.
    InteriorBlind,
    /// VerticalBlindStripCurtain = 11.
    VerticalBlindStripCurtain,
    /// InteriorVenetianBlind = 12.
    InteriorVenetianBlind,
    /// ExteriorVenetianBlind = 13.
    ExteriorVenetianBlind,
    /// LateralLeftCurtain = 14.
    LateralLeftCurtain,
    /// LateralRightCurtain = 15.
    LateralRightCurtain,
    /// CentralCurtain = 16.
    CentralCurtain,
    /// RollerShutter = 17.
    RollerShutter,
    /// ExteriorVerticalScreen = 18.
    ExteriorVerticalScreen,
    /// AwningTerracePatio = 19.
    AwningTerracePatio,
    /// AwningVerticalScreen = 20.
    AwningVerticalScreen,
    /// TiltOnlyPergola = 21.
    TiltOnlyPergola,
    /// SwingingShutter = 22.
    SwingingShutter,
    /// SlidingShutter = 23.
    SlidingShutter,
    /// Unknown = 255.
    Unknown,
    /// A value not known to this codegen revision.
    Unrecognized(u8),
}

impl EndProductTypeEnum {
    /// Decode from its raw discriminant (unknown → `Unrecognized`).
    #[must_use]
    pub fn from_raw(v: u8) -> Self {
        match v {
            0 => Self::RollerShade,
            1 => Self::RomanShade,
            2 => Self::BalloonShade,
            3 => Self::WovenWood,
            4 => Self::PleatedShade,
            5 => Self::CellularShade,
            6 => Self::LayeredShade,
            7 => Self::LayeredShade2D,
            8 => Self::SheerShade,
            9 => Self::TiltOnlyInteriorBlind,
            10 => Self::InteriorBlind,
            11 => Self::VerticalBlindStripCurtain,
            12 => Self::InteriorVenetianBlind,
            13 => Self::ExteriorVenetianBlind,
            14 => Self::LateralLeftCurtain,
            15 => Self::LateralRightCurtain,
            16 => Self::CentralCurtain,
            17 => Self::RollerShutter,
            18 => Self::ExteriorVerticalScreen,
            19 => Self::AwningTerracePatio,
            20 => Self::AwningVerticalScreen,
            21 => Self::TiltOnlyPergola,
            22 => Self::SwingingShutter,
            23 => Self::SlidingShutter,
            255 => Self::Unknown,
            other => Self::Unrecognized(other),
        }
    }
    /// The raw discriminant.
    #[must_use]
    pub fn to_raw(self) -> u8 {
        match self {
            Self::RollerShade => 0,
            Self::RomanShade => 1,
            Self::BalloonShade => 2,
            Self::WovenWood => 3,
            Self::PleatedShade => 4,
            Self::CellularShade => 5,
            Self::LayeredShade => 6,
            Self::LayeredShade2D => 7,
            Self::SheerShade => 8,
            Self::TiltOnlyInteriorBlind => 9,
            Self::InteriorBlind => 10,
            Self::VerticalBlindStripCurtain => 11,
            Self::InteriorVenetianBlind => 12,
            Self::ExteriorVenetianBlind => 13,
            Self::LateralLeftCurtain => 14,
            Self::LateralRightCurtain => 15,
            Self::CentralCurtain => 16,
            Self::RollerShutter => 17,
            Self::ExteriorVerticalScreen => 18,
            Self::AwningTerracePatio => 19,
            Self::AwningVerticalScreen => 20,
            Self::TiltOnlyPergola => 21,
            Self::SwingingShutter => 22,
            Self::SlidingShutter => 23,
            Self::Unknown => 255,
            Self::Unrecognized(v) => v,
        }
    }
}

bitflags::bitflags! {
    /// `ModeBitmap` (map8).
    #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
    pub struct ModeBitmap: u8 {
        /// MotorDirectionReversed.
        const MOTOR_DIRECTION_REVERSED = 1 << 0;
        /// CalibrationMode.
        const CALIBRATION_MODE = 1 << 1;
        /// MaintenanceMode.
        const MAINTENANCE_MODE = 1 << 2;
        /// LedFeedback.
        const LED_FEEDBACK = 1 << 3;
    }
}

/// `MovementStatus` (enum8).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum MovementStatus {
    /// Stopped = 0.
    Stopped,
    /// Opening = 1.
    Opening,
    /// Closing = 2.
    Closing,
    /// A value not known to this codegen revision.
    Unknown(u8),
}

impl MovementStatus {
    /// Decode from its raw discriminant (unknown → `Unknown`).
    #[must_use]
    pub fn from_raw(v: u8) -> Self {
        match v {
            0 => Self::Stopped,
            1 => Self::Opening,
            2 => Self::Closing,
            other => Self::Unknown(other),
        }
    }
    /// The raw discriminant.
    #[must_use]
    pub fn to_raw(self) -> u8 {
        match self {
            Self::Stopped => 0,
            Self::Opening => 1,
            Self::Closing => 2,
            Self::Unknown(v) => v,
        }
    }
}

bitflags::bitflags! {
    /// `OperationalStatusBitmap` (map8).
    #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
    pub struct OperationalStatusBitmap: u8 {
    }
}

bitflags::bitflags! {
    /// `SafetyStatusBitmap` (map16).
    #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
    pub struct SafetyStatusBitmap: u16 {
        /// RemoteLockout.
        const REMOTE_LOCKOUT = 1 << 0;
        /// TamperDetection.
        const TAMPER_DETECTION = 1 << 1;
        /// FailedCommunication.
        const FAILED_COMMUNICATION = 1 << 2;
        /// PositionFailure.
        const POSITION_FAILURE = 1 << 3;
        /// ThermalProtection.
        const THERMAL_PROTECTION = 1 << 4;
        /// ObstacleDetected.
        const OBSTACLE_DETECTED = 1 << 5;
        /// Power.
        const POWER = 1 << 6;
        /// StopInput.
        const STOP_INPUT = 1 << 7;
        /// MotorJammed.
        const MOTOR_JAMMED = 1 << 8;
        /// HardwareFailure.
        const HARDWARE_FAILURE = 1 << 9;
        /// ManualOperation.
        const MANUAL_OPERATION = 1 << 10;
        /// Protection.
        const PROTECTION = 1 << 11;
    }
}

/// `TypeEnum` (enum8).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum TypeEnum {
    /// Rollershade = 0.
    Rollershade,
    /// Rollershade2Motor = 1.
    Rollershade2Motor,
    /// RollershadeExterior = 2.
    RollershadeExterior,
    /// RollershadeExterior2Motor = 3.
    RollershadeExterior2Motor,
    /// Drapery = 4.
    Drapery,
    /// Awning = 5.
    Awning,
    /// Shutter = 6.
    Shutter,
    /// TiltBlindTiltOnly = 7.
    TiltBlindTiltOnly,
    /// TiltBlindLift = 8.
    TiltBlindLift,
    /// ProjectorScreen = 9.
    ProjectorScreen,
    /// Unknown = 255.
    Unknown,
    /// A value not known to this codegen revision.
    Unrecognized(u8),
}

impl TypeEnum {
    /// Decode from its raw discriminant (unknown → `Unrecognized`).
    #[must_use]
    pub fn from_raw(v: u8) -> Self {
        match v {
            0 => Self::Rollershade,
            1 => Self::Rollershade2Motor,
            2 => Self::RollershadeExterior,
            3 => Self::RollershadeExterior2Motor,
            4 => Self::Drapery,
            5 => Self::Awning,
            6 => Self::Shutter,
            7 => Self::TiltBlindTiltOnly,
            8 => Self::TiltBlindLift,
            9 => Self::ProjectorScreen,
            255 => Self::Unknown,
            other => Self::Unrecognized(other),
        }
    }
    /// The raw discriminant.
    #[must_use]
    pub fn to_raw(self) -> u8 {
        match self {
            Self::Rollershade => 0,
            Self::Rollershade2Motor => 1,
            Self::RollershadeExterior => 2,
            Self::RollershadeExterior2Motor => 3,
            Self::Drapery => 4,
            Self::Awning => 5,
            Self::Shutter => 6,
            Self::TiltBlindTiltOnly => 7,
            Self::TiltBlindLift => 8,
            Self::ProjectorScreen => 9,
            Self::Unknown => 255,
            Self::Unrecognized(v) => v,
        }
    }
}

/// Decode the `Type` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_type(tlv: &[u8]) -> Result<TypeEnum, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(TypeEnum::from_raw(
            u8::try_from(v).map_err(|_| ClusterError::InvalidLength("Type"))?,
        )),
        _ => Err(ClusterError::UnexpectedType { context: "Type" }),
    }
}

/// Decode the `NumberOfActuationsLift` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_number_of_actuations_lift(tlv: &[u8]) -> Result<u16, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => {
            Ok(u16::try_from(v)
                .map_err(|_| ClusterError::InvalidLength("NumberOfActuationsLift"))?)
        }
        _ => Err(ClusterError::UnexpectedType {
            context: "NumberOfActuationsLift",
        }),
    }
}

/// Decode the `NumberOfActuationsTilt` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_number_of_actuations_tilt(tlv: &[u8]) -> Result<u16, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => {
            Ok(u16::try_from(v)
                .map_err(|_| ClusterError::InvalidLength("NumberOfActuationsTilt"))?)
        }
        _ => Err(ClusterError::UnexpectedType {
            context: "NumberOfActuationsTilt",
        }),
    }
}

/// Decode the `ConfigStatus` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_config_status(tlv: &[u8]) -> Result<ConfigStatusBitmap, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(ConfigStatusBitmap::from_bits_retain(
            u8::try_from(v).map_err(|_| ClusterError::InvalidLength("ConfigStatus"))?,
        )),
        _ => Err(ClusterError::UnexpectedType {
            context: "ConfigStatus",
        }),
    }
}

/// Decode the `CurrentPositionLiftPercentage` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_current_position_lift_percentage(tlv: &[u8]) -> Result<Nullable<u8>, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Null, ..
        }) => Ok(Nullable::Null),
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(Nullable::Value(u8::try_from(v).map_err(|_| {
            ClusterError::InvalidLength("CurrentPositionLiftPercentage")
        })?)),
        _ => Err(ClusterError::UnexpectedType {
            context: "CurrentPositionLiftPercentage",
        }),
    }
}

/// Decode the `CurrentPositionTiltPercentage` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_current_position_tilt_percentage(tlv: &[u8]) -> Result<Nullable<u8>, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Null, ..
        }) => Ok(Nullable::Null),
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(Nullable::Value(u8::try_from(v).map_err(|_| {
            ClusterError::InvalidLength("CurrentPositionTiltPercentage")
        })?)),
        _ => Err(ClusterError::UnexpectedType {
            context: "CurrentPositionTiltPercentage",
        }),
    }
}

/// Decode the `OperationalStatus` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_operational_status(tlv: &[u8]) -> Result<OperationalStatusBitmap, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(OperationalStatusBitmap::from_bits_retain(
            u8::try_from(v).map_err(|_| ClusterError::InvalidLength("OperationalStatus"))?,
        )),
        _ => Err(ClusterError::UnexpectedType {
            context: "OperationalStatus",
        }),
    }
}

/// Decode the `TargetPositionLiftPercent100ths` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_target_position_lift_percent100ths(
    tlv: &[u8],
) -> Result<Nullable<u16>, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Null, ..
        }) => Ok(Nullable::Null),
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(Nullable::Value(u16::try_from(v).map_err(|_| {
            ClusterError::InvalidLength("TargetPositionLiftPercent100ths")
        })?)),
        _ => Err(ClusterError::UnexpectedType {
            context: "TargetPositionLiftPercent100ths",
        }),
    }
}

/// Decode the `TargetPositionTiltPercent100ths` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_target_position_tilt_percent100ths(
    tlv: &[u8],
) -> Result<Nullable<u16>, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Null, ..
        }) => Ok(Nullable::Null),
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(Nullable::Value(u16::try_from(v).map_err(|_| {
            ClusterError::InvalidLength("TargetPositionTiltPercent100ths")
        })?)),
        _ => Err(ClusterError::UnexpectedType {
            context: "TargetPositionTiltPercent100ths",
        }),
    }
}

/// Decode the `EndProductType` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_end_product_type(tlv: &[u8]) -> Result<EndProductTypeEnum, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(EndProductTypeEnum::from_raw(
            u8::try_from(v).map_err(|_| ClusterError::InvalidLength("EndProductType"))?,
        )),
        _ => Err(ClusterError::UnexpectedType {
            context: "EndProductType",
        }),
    }
}

/// Decode the `CurrentPositionLiftPercent100ths` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_current_position_lift_percent100ths(
    tlv: &[u8],
) -> Result<Nullable<u16>, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Null, ..
        }) => Ok(Nullable::Null),
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(Nullable::Value(u16::try_from(v).map_err(|_| {
            ClusterError::InvalidLength("CurrentPositionLiftPercent100ths")
        })?)),
        _ => Err(ClusterError::UnexpectedType {
            context: "CurrentPositionLiftPercent100ths",
        }),
    }
}

/// Decode the `CurrentPositionTiltPercent100ths` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_current_position_tilt_percent100ths(
    tlv: &[u8],
) -> Result<Nullable<u16>, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Null, ..
        }) => Ok(Nullable::Null),
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(Nullable::Value(u16::try_from(v).map_err(|_| {
            ClusterError::InvalidLength("CurrentPositionTiltPercent100ths")
        })?)),
        _ => Err(ClusterError::UnexpectedType {
            context: "CurrentPositionTiltPercent100ths",
        }),
    }
}

/// Decode the `Mode` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_mode(tlv: &[u8]) -> Result<ModeBitmap, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(ModeBitmap::from_bits_retain(
            u8::try_from(v).map_err(|_| ClusterError::InvalidLength("Mode"))?,
        )),
        _ => Err(ClusterError::UnexpectedType { context: "Mode" }),
    }
}

/// Encode the `Mode` attribute value as a standalone TLV element.
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] // Vec-backed TlvWriter is infallible.
pub fn encode_mode(value: ModeBitmap) -> Vec<u8> {
    let mut buf = Vec::new();
    let mut w = TlvWriter::new(&mut buf);
    w.put_uint(Tag::Anonymous, u64::from(value.bits()))
        .expect("infallible: vec writer");
    buf
}

/// Decode the `SafetyStatus` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_safety_status(tlv: &[u8]) -> Result<SafetyStatusBitmap, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(SafetyStatusBitmap::from_bits_retain(
            u16::try_from(v).map_err(|_| ClusterError::InvalidLength("SafetyStatus"))?,
        )),
        _ => Err(ClusterError::UnexpectedType {
            context: "SafetyStatus",
        }),
    }
}

/// Encode the `UpOrOpen` command request payload.
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] // Vec-backed TlvWriter is infallible.
pub fn encode_up_or_open() -> Vec<u8> {
    let mut buf = Vec::new();
    let mut w = TlvWriter::new(&mut buf);
    w.start_structure(Tag::Anonymous)
        .expect("infallible: vec writer");
    w.end_container().expect("infallible: vec writer");
    buf
}

/// Encode the `DownOrClose` command request payload.
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] // Vec-backed TlvWriter is infallible.
pub fn encode_down_or_close() -> Vec<u8> {
    let mut buf = Vec::new();
    let mut w = TlvWriter::new(&mut buf);
    w.start_structure(Tag::Anonymous)
        .expect("infallible: vec writer");
    w.end_container().expect("infallible: vec writer");
    buf
}

/// Encode the `StopMotion` command request payload.
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] // Vec-backed TlvWriter is infallible.
pub fn encode_stop_motion() -> Vec<u8> {
    let mut buf = Vec::new();
    let mut w = TlvWriter::new(&mut buf);
    w.start_structure(Tag::Anonymous)
        .expect("infallible: vec writer");
    w.end_container().expect("infallible: vec writer");
    buf
}

/// Encode the `GoToLiftPercentage` command request payload.
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] // Vec-backed TlvWriter is infallible.
pub fn encode_go_to_lift_percentage(lift_percent100ths_value: u16) -> Vec<u8> {
    let mut buf = Vec::new();
    let mut w = TlvWriter::new(&mut buf);
    w.start_structure(Tag::Anonymous)
        .expect("infallible: vec writer");
    w.put_uint(Tag::Context(0), u64::from(lift_percent100ths_value))
        .expect("infallible: vec writer");
    w.end_container().expect("infallible: vec writer");
    buf
}

/// Encode the `GoToTiltPercentage` command request payload.
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] // Vec-backed TlvWriter is infallible.
pub fn encode_go_to_tilt_percentage(tilt_percent100ths_value: u16) -> Vec<u8> {
    let mut buf = Vec::new();
    let mut w = TlvWriter::new(&mut buf);
    w.start_structure(Tag::Anonymous)
        .expect("infallible: vec writer");
    w.put_uint(Tag::Context(0), u64::from(tilt_percent100ths_value))
        .expect("infallible: vec writer");
    w.end_container().expect("infallible: vec writer");
    buf
}