tasmor_lib 0.6.0

Rust library to control Tasmota devices via MQTT and HTTP
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
// SPDX-License-Identifier: MPL-2.0
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! State change representation.
//!
//! State changes are the fundamental building blocks for updating device state.
//! They represent discrete changes that can be applied to a [`DeviceState`](super::DeviceState),
//! either from command responses or telemetry updates.
//!
//! # Change Types
//!
//! - [`StateChange::Power`] - Relay state changes (on/off)
//! - [`StateChange::Dimmer`] - Brightness level changes
//! - [`StateChange::HsbColor`] - RGB color changes in HSB format
//! - [`StateChange::ColorTemperature`] - White color temperature changes
//! - [`StateChange::Scheme`] - Light scheme/effect changes
//! - [`StateChange::WakeupDuration`] - Wakeup effect duration changes
//! - [`StateChange::FadeEnabled`] - Fade transition enable/disable
//! - [`StateChange::FadeDuration`] - Fade transition speed changes
//! - [`StateChange::Energy`] - Energy monitoring updates
//! - [`StateChange::Batch`] - Multiple changes grouped together
//!
//! # Examples
//!
//! ## Creating individual changes
//!
//! ```
//! use tasmor_lib::state::StateChange;
//! use tasmor_lib::types::{PowerState, Dimmer, HsbColor, ColorTemperature};
//!
//! // Power state change
//! let power_on = StateChange::power(1, PowerState::On);
//!
//! // Light control changes
//! let dim = StateChange::dimmer(Dimmer::new(75).unwrap());
//! let color = StateChange::hsb_color(HsbColor::red());
//! let warm = StateChange::color_temperature(ColorTemperature::WARM);
//! ```
//!
//! ## Applying changes to device state
//!
//! ```
//! use tasmor_lib::state::{DeviceState, StateChange};
//! use tasmor_lib::types::PowerState;
//!
//! let mut state = DeviceState::new();
//!
//! // Apply returns true if state actually changed
//! let changed = state.apply(&StateChange::power_on());
//! assert!(changed);
//!
//! // Applying same change again returns false
//! let changed = state.apply(&StateChange::power_on());
//! assert!(!changed);
//! ```

use crate::types::{
    ColorTemperature, Dimmer, FadeDuration, HsbColor, PowerState, Scheme, TasmotaDateTime,
    WakeupDuration,
};

/// Represents a change in device state.
///
/// State changes are used to update [`DeviceState`](super::DeviceState) and
/// to emit events when the device state changes. Each variant represents
/// a specific type of state change.
///
/// # Examples
///
/// ```
/// use tasmor_lib::state::StateChange;
/// use tasmor_lib::types::PowerState;
///
/// let change = StateChange::Power { index: 1, state: PowerState::On };
/// ```
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum StateChange {
    /// Power state changed for a specific relay.
    Power {
        /// The relay index (1-8).
        index: u8,
        /// The new power state.
        state: PowerState,
    },

    /// Dimmer level changed.
    Dimmer(Dimmer),

    /// HSB color changed.
    HsbColor(HsbColor),

    /// Color temperature changed.
    ColorTemperature(ColorTemperature),

    /// Light scheme/effect changed.
    Scheme(Scheme),

    /// Wakeup duration changed.
    WakeupDuration(WakeupDuration),

    /// Fade enabled/disabled.
    FadeEnabled(bool),

    /// Fade duration changed.
    FadeDuration(FadeDuration),

    /// Energy monitoring data updated.
    ///
    /// Contains all energy-related readings. All fields are optional
    /// to allow partial updates from different telemetry sources.
    Energy {
        /// Power consumption in Watts.
        power: Option<f32>,
        /// Voltage in Volts.
        voltage: Option<f32>,
        /// Current in Amperes.
        current: Option<f32>,
        /// Apparent power in VA.
        apparent_power: Option<f32>,
        /// Reactive power in `VAr`.
        reactive_power: Option<f32>,
        /// Power factor (0-1).
        power_factor: Option<f32>,
        /// Energy consumed today in kWh.
        energy_today: Option<f32>,
        /// Energy consumed yesterday in kWh.
        energy_yesterday: Option<f32>,
        /// Total energy consumed in kWh.
        energy_total: Option<f32>,
        /// Timestamp when total energy counting started.
        ///
        /// Contains both naive datetime and timezone-aware datetime if available.
        total_start_time: Option<TasmotaDateTime>,
        /// AC frequency in Hz. `None` for DC monitors or devices that do not report it.
        frequency: Option<f32>,
    },

    /// Multiple changes at once.
    ///
    /// Used when a status refresh returns multiple values.
    Batch(Vec<StateChange>),
}

impl StateChange {
    /// Creates a power state change.
    #[must_use]
    pub fn power(index: u8, state: PowerState) -> Self {
        Self::Power { index, state }
    }

    /// Creates a power-on change for relay 1.
    #[must_use]
    pub fn power_on() -> Self {
        Self::Power {
            index: 1,
            state: PowerState::On,
        }
    }

    /// Creates a power-off change for relay 1.
    #[must_use]
    pub fn power_off() -> Self {
        Self::Power {
            index: 1,
            state: PowerState::Off,
        }
    }

    /// Creates a dimmer change.
    #[must_use]
    pub fn dimmer(value: Dimmer) -> Self {
        Self::Dimmer(value)
    }

    /// Creates an HSB color change.
    #[must_use]
    pub fn hsb_color(color: HsbColor) -> Self {
        Self::HsbColor(color)
    }

    /// Creates a color temperature change.
    #[must_use]
    pub fn color_temperature(ct: ColorTemperature) -> Self {
        Self::ColorTemperature(ct)
    }

    /// Creates a scheme change.
    #[must_use]
    pub fn scheme(scheme: Scheme) -> Self {
        Self::Scheme(scheme)
    }

    /// Creates a wakeup duration change.
    #[must_use]
    pub fn wakeup_duration(duration: WakeupDuration) -> Self {
        Self::WakeupDuration(duration)
    }

    /// Creates a fade enabled change.
    #[must_use]
    pub fn fade_enabled(enabled: bool) -> Self {
        Self::FadeEnabled(enabled)
    }

    /// Creates a fade duration change.
    #[must_use]
    pub fn fade_duration(duration: FadeDuration) -> Self {
        Self::FadeDuration(duration)
    }

    /// Creates an energy reading change with basic power data.
    #[must_use]
    pub fn energy(power: f32, voltage: f32, current: f32) -> Self {
        Self::Energy {
            power: Some(power),
            voltage: Some(voltage),
            current: Some(current),
            apparent_power: None,
            reactive_power: None,
            power_factor: None,
            energy_today: None,
            energy_yesterday: None,
            energy_total: None,
            total_start_time: None,
            frequency: None,
        }
    }

    /// Creates an energy reading change with all fields.
    #[must_use]
    #[allow(clippy::too_many_arguments)]
    pub fn energy_full(
        power: Option<f32>,
        voltage: Option<f32>,
        current: Option<f32>,
        apparent_power: Option<f32>,
        reactive_power: Option<f32>,
        power_factor: Option<f32>,
        energy_today: Option<f32>,
        energy_yesterday: Option<f32>,
        energy_total: Option<f32>,
        total_start_time: Option<TasmotaDateTime>,
        frequency: Option<f32>,
    ) -> Self {
        Self::Energy {
            power,
            voltage,
            current,
            apparent_power,
            reactive_power,
            power_factor,
            energy_today,
            energy_yesterday,
            energy_total,
            total_start_time,
            frequency,
        }
    }

    /// Creates a batch of changes.
    #[must_use]
    pub fn batch(changes: Vec<StateChange>) -> Self {
        Self::Batch(changes)
    }

    /// Returns `true` if this is a power state change.
    #[must_use]
    pub fn is_power(&self) -> bool {
        matches!(self, Self::Power { .. })
    }

    /// Returns `true` if this is a light-related change (dimmer, color, CT, scheme, fade).
    #[must_use]
    pub fn is_light(&self) -> bool {
        matches!(
            self,
            Self::Dimmer(_)
                | Self::HsbColor(_)
                | Self::ColorTemperature(_)
                | Self::Scheme(_)
                | Self::WakeupDuration(_)
                | Self::FadeEnabled(_)
                | Self::FadeDuration(_)
        )
    }

    /// Returns `true` if this is a scheme change.
    #[must_use]
    pub fn is_scheme(&self) -> bool {
        matches!(self, Self::Scheme(_))
    }

    /// Returns `true` if this is an energy-related change.
    #[must_use]
    pub fn is_energy(&self) -> bool {
        matches!(self, Self::Energy { .. })
    }

    /// Returns `true` if this is a batch of changes.
    #[must_use]
    pub fn is_batch(&self) -> bool {
        matches!(self, Self::Batch(_))
    }

    /// Returns the number of individual changes.
    ///
    /// For batch changes, returns the total count of nested changes.
    #[must_use]
    pub fn change_count(&self) -> usize {
        match self {
            Self::Batch(changes) => changes.iter().map(Self::change_count).sum(),
            _ => 1,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use super::*;

    #[test]
    fn power_change_constructors() {
        let change = StateChange::power(1, PowerState::On);
        assert!(matches!(
            change,
            StateChange::Power {
                index: 1,
                state: PowerState::On
            }
        ));

        let on = StateChange::power_on();
        assert!(matches!(
            on,
            StateChange::Power {
                index: 1,
                state: PowerState::On
            }
        ));

        let off = StateChange::power_off();
        assert!(matches!(
            off,
            StateChange::Power {
                index: 1,
                state: PowerState::Off
            }
        ));
    }

    #[test]
    fn is_power() {
        assert!(StateChange::power_on().is_power());
        assert!(!StateChange::Dimmer(Dimmer::MAX).is_power());
    }

    #[test]
    fn is_light() {
        assert!(StateChange::Dimmer(Dimmer::MAX).is_light());
        assert!(StateChange::fade_enabled(true).is_light());
        assert!(
            StateChange::fade_duration(FadeDuration::new(Duration::from_secs(10)).unwrap())
                .is_light()
        );
        assert!(!StateChange::power_on().is_light());
    }

    #[test]
    fn fade_constructors() {
        let enabled = StateChange::fade_enabled(true);
        assert!(matches!(enabled, StateChange::FadeEnabled(true)));

        let disabled = StateChange::fade_enabled(false);
        assert!(matches!(disabled, StateChange::FadeEnabled(false)));

        let duration =
            StateChange::fade_duration(FadeDuration::new(Duration::from_millis(500)).unwrap());
        assert!(matches!(duration, StateChange::FadeDuration(_)));
    }

    #[test]
    fn is_energy() {
        assert!(StateChange::energy(100.0, 230.0, 0.5).is_energy());
        assert!(!StateChange::power_on().is_energy());
    }

    #[test]
    fn change_count() {
        assert_eq!(StateChange::power_on().change_count(), 1);

        let batch = StateChange::batch(vec![
            StateChange::power_on(),
            StateChange::Dimmer(Dimmer::MAX),
        ]);
        assert_eq!(batch.change_count(), 2);

        // Nested batch
        let nested = StateChange::batch(vec![batch, StateChange::power_off()]);
        assert_eq!(nested.change_count(), 3);
    }
}