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
406
407
408
409
410
411
412
413
414
415
// 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/.

//! Color response parsing for HSB color and color temperature.

use serde::Deserialize;

use crate::error::ParseError;
use crate::types::{ColorTemperature, HsbColor, PowerState};

/// Response from an `HSBColor` command.
///
/// Tasmota returns HSB color in JSON format like:
/// - `{"HSBColor": "180,100,75"}` for color-only response
/// - `{"HSBColor": "180,100,75", "Dimmer": 75, "POWER": "ON"}` with state
///
/// The HSB values are:
/// - Hue: 0-360 degrees on the color wheel
/// - Saturation: 0-100 percentage
/// - Brightness: 0-100 percentage
///
/// # Examples
///
/// ```
/// use tasmor_lib::response::HsbColorResponse;
///
/// let json = r#"{"HSBColor": "180,100,75", "POWER": "ON"}"#;
/// let response: HsbColorResponse = serde_json::from_str(json).unwrap();
/// assert_eq!(response.hue().unwrap(), 180);
/// assert_eq!(response.saturation().unwrap(), 100);
/// assert_eq!(response.brightness().unwrap(), 75);
/// ```
#[derive(Debug, Clone, Deserialize)]
pub struct HsbColorResponse {
    /// The HSB color as a comma-separated string (e.g., "180,100,75").
    #[serde(rename = "HSBColor")]
    hsb_color: String,

    /// Optional dimmer level included in the response.
    #[serde(rename = "Dimmer", default)]
    dimmer: Option<u8>,

    /// Optional power state included in the response.
    #[serde(rename = "POWER", default)]
    power: Option<String>,
}

impl HsbColorResponse {
    /// Parses the HSB color string into components.
    fn parse_hsb(&self) -> Result<(u16, u8, u8), ParseError> {
        let parts: Vec<&str> = self.hsb_color.split(',').collect();

        if parts.len() != 3 {
            return Err(ParseError::InvalidValue {
                field: "HSBColor".to_string(),
                message: format!("expected 3 comma-separated values, got: {}", self.hsb_color),
            });
        }

        let hue = parts[0]
            .parse::<u16>()
            .map_err(|_| ParseError::InvalidValue {
                field: "HSBColor.hue".to_string(),
                message: format!("invalid hue value: {}", parts[0]),
            })?;

        let saturation = parts[1]
            .parse::<u8>()
            .map_err(|_| ParseError::InvalidValue {
                field: "HSBColor.saturation".to_string(),
                message: format!("invalid saturation value: {}", parts[1]),
            })?;

        let brightness = parts[2]
            .parse::<u8>()
            .map_err(|_| ParseError::InvalidValue {
                field: "HSBColor.brightness".to_string(),
                message: format!("invalid brightness value: {}", parts[2]),
            })?;

        Ok((hue, saturation, brightness))
    }

    /// Returns the hue component (0-360).
    ///
    /// # Errors
    ///
    /// Returns `ParseError` if the HSB color string is malformed.
    pub fn hue(&self) -> Result<u16, ParseError> {
        self.parse_hsb().map(|(h, _, _)| h)
    }

    /// Returns the saturation component (0-100).
    ///
    /// # Errors
    ///
    /// Returns `ParseError` if the HSB color string is malformed.
    pub fn saturation(&self) -> Result<u8, ParseError> {
        self.parse_hsb().map(|(_, s, _)| s)
    }

    /// Returns the brightness component (0-100).
    ///
    /// # Errors
    ///
    /// Returns `ParseError` if the HSB color string is malformed.
    pub fn brightness(&self) -> Result<u8, ParseError> {
        self.parse_hsb().map(|(_, _, b)| b)
    }

    /// Returns all HSB components as a tuple (hue, saturation, brightness).
    ///
    /// # Errors
    ///
    /// Returns `ParseError` if the HSB color string is malformed.
    pub fn as_tuple(&self) -> Result<(u16, u8, u8), ParseError> {
        self.parse_hsb()
    }

    /// Returns the HSB color as an [`HsbColor`] type.
    ///
    /// # Errors
    ///
    /// Returns `ParseError` if the HSB color string is malformed or values are out of range.
    pub fn hsb_color(&self) -> Result<HsbColor, ParseError> {
        let (hue, saturation, brightness) = self.parse_hsb()?;
        HsbColor::new(hue, saturation, brightness).map_err(|e| ParseError::InvalidValue {
            field: "HSBColor".to_string(),
            message: e.to_string(),
        })
    }

    /// Returns the raw HSB color string.
    #[must_use]
    pub fn raw(&self) -> &str {
        &self.hsb_color
    }

    /// Returns the dimmer level if included in the response.
    #[must_use]
    pub fn dimmer(&self) -> Option<u8> {
        self.dimmer
    }

    /// Returns the power state if included in the response.
    ///
    /// # Errors
    ///
    /// Returns `ParseError` if the power state string is invalid.
    pub fn power_state(&self) -> Result<Option<PowerState>, ParseError> {
        match &self.power {
            Some(s) => s
                .parse::<PowerState>()
                .map(Some)
                .map_err(|_| ParseError::InvalidValue {
                    field: "POWER".to_string(),
                    message: format!("invalid power state: {s}"),
                }),
            None => Ok(None),
        }
    }

    /// Returns `true` if the device is on according to the response.
    ///
    /// Returns `None` if power state was not included in the response.
    #[must_use]
    pub fn is_on(&self) -> Option<bool> {
        self.power.as_ref().map(|s| s == "ON")
    }
}

/// Response from a `CT` (Color Temperature) command.
///
/// Tasmota returns color temperature in JSON format like:
/// - `{"CT": 250}` for CT-only response
/// - `{"CT": 250, "POWER": "ON"}` with power state
///
/// Color temperature is measured in mireds (micro reciprocal degrees):
/// - 153 = coldest (6500K, daylight)
/// - 500 = warmest (2000K, candlelight)
///
/// # Examples
///
/// ```
/// use tasmor_lib::response::ColorTemperatureResponse;
///
/// let json = r#"{"CT": 326, "POWER": "ON"}"#;
/// let response: ColorTemperatureResponse = serde_json::from_str(json).unwrap();
/// assert_eq!(response.color_temperature(), 326);
/// ```
#[derive(Debug, Clone, Deserialize)]
pub struct ColorTemperatureResponse {
    /// The color temperature in mireds (153-500).
    #[serde(rename = "CT")]
    ct: u16,

    /// Optional power state included in the response.
    #[serde(rename = "POWER", default)]
    power: Option<String>,
}

impl ColorTemperatureResponse {
    /// Returns the color temperature in mireds (153-500).
    #[must_use]
    pub fn color_temperature(&self) -> u16 {
        self.ct
    }

    /// Returns the approximate Kelvin temperature.
    ///
    /// Calculated as 1,000,000 / mireds.
    #[must_use]
    pub fn to_kelvin(&self) -> u32 {
        if self.ct == 0 {
            0
        } else {
            1_000_000 / u32::from(self.ct)
        }
    }

    /// Returns the power state if included in the response.
    ///
    /// # Errors
    ///
    /// Returns `ParseError` if the power state string is invalid.
    pub fn power_state(&self) -> Result<Option<PowerState>, ParseError> {
        match &self.power {
            Some(s) => s
                .parse::<PowerState>()
                .map(Some)
                .map_err(|_| ParseError::InvalidValue {
                    field: "POWER".to_string(),
                    message: format!("invalid power state: {s}"),
                }),
            None => Ok(None),
        }
    }

    /// Returns `true` if the device is on according to the response.
    ///
    /// Returns `None` if power state was not included in the response.
    #[must_use]
    pub fn is_on(&self) -> Option<bool> {
        self.power.as_ref().map(|s| s == "ON")
    }

    /// Returns the color temperature as a [`ColorTemperature`] type.
    ///
    /// # Errors
    ///
    /// Returns `ParseError` if the color temperature value is out of range.
    pub fn to_color_temperature(&self) -> Result<ColorTemperature, ParseError> {
        ColorTemperature::new(self.ct).map_err(|e| ParseError::InvalidValue {
            field: "CT".to_string(),
            message: e.to_string(),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ========================================================================
    // HsbColorResponse tests
    // ========================================================================

    #[test]
    fn parse_hsb_color_only() {
        let json = r#"{"HSBColor": "180,100,75"}"#;
        let response: HsbColorResponse = serde_json::from_str(json).unwrap();

        assert_eq!(response.hue().unwrap(), 180);
        assert_eq!(response.saturation().unwrap(), 100);
        assert_eq!(response.brightness().unwrap(), 75);
        assert_eq!(response.as_tuple().unwrap(), (180, 100, 75));
        assert!(response.power_state().unwrap().is_none());
    }

    #[test]
    fn parse_hsb_color_with_power() {
        let json = r#"{"HSBColor": "0,100,100", "POWER": "ON"}"#;
        let response: HsbColorResponse = serde_json::from_str(json).unwrap();

        assert_eq!(response.hue().unwrap(), 0);
        assert_eq!(response.power_state().unwrap().unwrap(), PowerState::On);
        assert_eq!(response.is_on(), Some(true));
    }

    #[test]
    fn parse_hsb_color_with_dimmer() {
        let json = r#"{"HSBColor": "120,80,50", "Dimmer": 50}"#;
        let response: HsbColorResponse = serde_json::from_str(json).unwrap();

        assert_eq!(response.hue().unwrap(), 120);
        assert_eq!(response.dimmer(), Some(50));
    }

    #[test]
    fn parse_hsb_color_full_response() {
        let json = r#"{
            "HSBColor": "240,50,75",
            "Dimmer": 75,
            "POWER": "ON",
            "Color": "5959BF",
            "White": 0,
            "CT": 327
        }"#;
        let response: HsbColorResponse = serde_json::from_str(json).unwrap();

        assert_eq!(response.as_tuple().unwrap(), (240, 50, 75));
        assert_eq!(response.dimmer(), Some(75));
        assert_eq!(response.power_state().unwrap().unwrap(), PowerState::On);
    }

    #[test]
    fn parse_hsb_color_red() {
        let json = r#"{"HSBColor": "0,100,100"}"#;
        let response: HsbColorResponse = serde_json::from_str(json).unwrap();
        assert_eq!(response.as_tuple().unwrap(), (0, 100, 100));
    }

    #[test]
    fn parse_hsb_color_green() {
        let json = r#"{"HSBColor": "120,100,100"}"#;
        let response: HsbColorResponse = serde_json::from_str(json).unwrap();
        assert_eq!(response.as_tuple().unwrap(), (120, 100, 100));
    }

    #[test]
    fn parse_hsb_color_blue() {
        let json = r#"{"HSBColor": "240,100,100"}"#;
        let response: HsbColorResponse = serde_json::from_str(json).unwrap();
        assert_eq!(response.as_tuple().unwrap(), (240, 100, 100));
    }

    #[test]
    fn parse_hsb_color_max_hue() {
        let json = r#"{"HSBColor": "360,100,100"}"#;
        let response: HsbColorResponse = serde_json::from_str(json).unwrap();
        assert_eq!(response.hue().unwrap(), 360);
    }

    #[test]
    fn parse_hsb_invalid_format() {
        let json = r#"{"HSBColor": "180,100"}"#; // Missing brightness
        let response: HsbColorResponse = serde_json::from_str(json).unwrap();
        assert!(response.as_tuple().is_err());
    }

    // ========================================================================
    // ColorTemperatureResponse tests
    // ========================================================================

    #[test]
    fn parse_ct_only() {
        let json = r#"{"CT": 326}"#;
        let response: ColorTemperatureResponse = serde_json::from_str(json).unwrap();

        assert_eq!(response.color_temperature(), 326);
        assert!(response.power_state().unwrap().is_none());
    }

    #[test]
    fn parse_ct_with_power() {
        let json = r#"{"CT": 250, "POWER": "ON"}"#;
        let response: ColorTemperatureResponse = serde_json::from_str(json).unwrap();

        assert_eq!(response.color_temperature(), 250);
        assert_eq!(response.power_state().unwrap().unwrap(), PowerState::On);
    }

    #[test]
    fn parse_ct_coldest() {
        let json = r#"{"CT": 153}"#;
        let response: ColorTemperatureResponse = serde_json::from_str(json).unwrap();

        assert_eq!(response.color_temperature(), 153);
        assert_eq!(response.to_kelvin(), 6535); // ~6500K
    }

    #[test]
    fn parse_ct_warmest() {
        let json = r#"{"CT": 500}"#;
        let response: ColorTemperatureResponse = serde_json::from_str(json).unwrap();

        assert_eq!(response.color_temperature(), 500);
        assert_eq!(response.to_kelvin(), 2000); // 2000K
    }

    #[test]
    fn parse_ct_neutral() {
        let json = r#"{"CT": 326}"#;
        let response: ColorTemperatureResponse = serde_json::from_str(json).unwrap();

        assert_eq!(response.color_temperature(), 326);
        assert_eq!(response.to_kelvin(), 3067); // ~3000K neutral
    }

    #[test]
    fn parse_ct_with_additional_fields() {
        let json = r#"{
            "CT": 327,
            "POWER": "ON",
            "Dimmer": 100,
            "Color": "FFFFFF"
        }"#;
        let response: ColorTemperatureResponse = serde_json::from_str(json).unwrap();

        assert_eq!(response.color_temperature(), 327);
        assert_eq!(response.power_state().unwrap().unwrap(), PowerState::On);
    }
}