govee 0.7.2

Async Rust library for controlling Govee smart lighting devices via cloud and local LAN APIs
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
use std::collections::HashMap;

use crate::config::SceneConfig;
use crate::error::{GoveeError, Result};
use crate::types::{Color, DeviceId};

/// Target specification for applying a scene.
#[derive(Debug, Clone)]
pub enum SceneTarget {
    /// A specific device by ID.
    Device(DeviceId),
    /// A device resolved by name or alias.
    DeviceName(String),
    /// All devices in a named group.
    Group(String),
    /// Every registered device.
    All,
}

/// The color component of a scene: either an RGB value or a color temperature.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SceneColor {
    /// An RGB color value.
    Rgb(Color),
    /// A color temperature in Kelvin (1–10000).
    Temp(u32),
}

/// A named lighting preset with brightness and color.
#[derive(Debug, Clone)]
pub struct Scene {
    name: String,
    brightness: u8,
    color: SceneColor,
}

impl Scene {
    /// Create a new scene, validating all fields.
    ///
    /// - `brightness` must be 0–100.
    /// - `SceneColor::Temp` value must be 1–10000.
    /// - `name` must be non-empty and contain only alphanumeric characters, `-`, or `_`.
    pub fn new(name: &str, brightness: u8, color: SceneColor) -> Result<Self> {
        if brightness > 100 {
            return Err(GoveeError::InvalidBrightness(brightness));
        }

        if let SceneColor::Temp(temp) = &color
            && (*temp == 0 || *temp > 10000)
        {
            return Err(GoveeError::InvalidConfig(
                "color temp must be 1-10000".to_string(),
            ));
        }

        if name.is_empty()
            || !name
                .chars()
                .all(|c| c.is_alphanumeric() || c == '-' || c == '_')
        {
            return Err(GoveeError::InvalidConfig(
                "scene name must contain only alphanumeric, '-', '_' characters".to_string(),
            ));
        }

        Ok(Self {
            name: name.to_string(),
            brightness,
            color,
        })
    }

    /// Return the scene name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Return the scene brightness (0–100).
    pub fn brightness(&self) -> u8 {
        self.brightness
    }

    /// Return a reference to the scene color.
    pub fn color(&self) -> &SceneColor {
        &self.color
    }
}

/// A registry of named lighting scenes with case-insensitive lookup.
#[derive(Debug)]
pub struct SceneRegistry {
    scenes: HashMap<String, Scene>,
}

impl SceneRegistry {
    /// Create a new registry populated with built-in scenes.
    ///
    /// Built-ins are constructed directly (no validation) since their
    /// values are compile-time constants known to be valid.
    pub fn new() -> Self {
        let builtins = [
            Scene {
                name: "warm".into(),
                brightness: 40,
                color: SceneColor::Temp(2700),
            },
            Scene {
                name: "focus".into(),
                brightness: 80,
                color: SceneColor::Temp(5500),
            },
            Scene {
                name: "night".into(),
                brightness: 10,
                color: SceneColor::Rgb(Color::new(255, 0, 0)),
            },
            Scene {
                name: "movie".into(),
                brightness: 20,
                color: SceneColor::Temp(2200),
            },
            Scene {
                name: "bright".into(),
                brightness: 100,
                color: SceneColor::Temp(6500),
            },
        ];

        let mut scenes = HashMap::new();
        for scene in builtins {
            scenes.insert(scene.name().to_lowercase(), scene);
        }

        Self { scenes }
    }

    /// Look up a scene by name (case-insensitive).
    pub fn get(&self, name: &str) -> Result<&Scene> {
        self.scenes
            .get(&name.to_lowercase())
            .ok_or_else(|| GoveeError::DeviceNotFound(format!("scene: {name}")))
    }

    /// Return all registered scenes, sorted by name.
    pub fn list(&self) -> Vec<&Scene> {
        let mut scenes: Vec<_> = self.scenes.values().collect();
        scenes.sort_by_key(|s| s.name());
        scenes
    }

    /// Merge user-defined scenes from config into this registry.
    ///
    /// - Converts each `SceneConfig` to a `Scene` via `Scene::new()`.
    /// - Keys are lowercased for case-insensitive storage.
    /// - On name collision with a built-in, the user scene wins (logged at debug).
    /// - On case-insensitive collision between user scenes, last-wins (logged at warn).
    pub fn with_user_scenes(mut self, user: &HashMap<String, SceneConfig>) -> Result<Self> {
        // Track built-in keys to distinguish overrides from user/user collisions.
        let builtin_keys: std::collections::HashSet<String> = self.scenes.keys().cloned().collect();
        // Track keys inserted from user scenes in this merge.
        let mut user_keys = std::collections::HashSet::new();

        // Sort user scene names for deterministic iteration order.
        let mut sorted_names: Vec<&String> = user.keys().collect();
        sorted_names.sort();

        for name in sorted_names {
            let sc = &user[name];
            let color = match (&sc.color, sc.color_temp) {
                (Some(c), None) => SceneColor::Rgb(*c),
                (None, Some(temp)) => SceneColor::Temp(temp),
                _ => {
                    return Err(GoveeError::InvalidConfig(format!(
                        "scene \"{name}\": must set exactly one of color or color_temp"
                    )));
                }
            };

            let scene = Scene::new(name, sc.brightness, color)
                .map_err(|e| GoveeError::InvalidConfig(format!("scene \"{name}\": {e}")))?;
            let key = name.to_lowercase();

            if self.scenes.contains_key(&key) {
                if user_keys.contains(&key) {
                    // Collision between two user scenes (case-insensitive).
                    tracing::warn!(scene = %name, "case-insensitive collision with existing user scene");
                } else if builtin_keys.contains(&key) {
                    // User scene overriding a built-in.
                    tracing::debug!(scene = %name, "user scene overrides built-in");
                }
            }

            user_keys.insert(key.clone());
            self.scenes.insert(key, scene);
        }

        Ok(self)
    }
}

impl Default for SceneRegistry {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn builtins_all_present_with_values() {
        let registry = SceneRegistry::new();
        let scenes = registry.list();
        assert_eq!(scenes.len(), 5);

        // list() is sorted by name.
        assert_eq!(scenes[0].name(), "bright");
        assert_eq!(scenes[0].brightness(), 100);
        assert_eq!(*scenes[0].color(), SceneColor::Temp(6500));

        assert_eq!(scenes[1].name(), "focus");
        assert_eq!(scenes[1].brightness(), 80);
        assert_eq!(*scenes[1].color(), SceneColor::Temp(5500));

        assert_eq!(scenes[2].name(), "movie");
        assert_eq!(scenes[2].brightness(), 20);
        assert_eq!(*scenes[2].color(), SceneColor::Temp(2200));

        assert_eq!(scenes[3].name(), "night");
        assert_eq!(scenes[3].brightness(), 10);
        assert_eq!(*scenes[3].color(), SceneColor::Rgb(Color::new(255, 0, 0)));

        assert_eq!(scenes[4].name(), "warm");
        assert_eq!(scenes[4].brightness(), 40);
        assert_eq!(*scenes[4].color(), SceneColor::Temp(2700));
    }

    #[test]
    fn lookup_exact_name() {
        let registry = SceneRegistry::new();
        let scene = registry.get("warm").unwrap();
        assert_eq!(scene.name(), "warm");
        assert_eq!(scene.brightness(), 40);
        assert_eq!(*scene.color(), SceneColor::Temp(2700));
    }

    #[test]
    fn lookup_case_insensitive() {
        let registry = SceneRegistry::new();
        assert!(registry.get("WARM").is_ok());
        assert!(registry.get("Warm").is_ok());
        assert!(registry.get("wArM").is_ok());
        assert_eq!(registry.get("FOCUS").unwrap().name(), "focus");
    }

    #[test]
    fn lookup_unknown_scene() {
        let registry = SceneRegistry::new();
        let err = registry.get("nonexistent").unwrap_err();
        assert!(matches!(err, GoveeError::DeviceNotFound(_)));
    }

    #[test]
    fn reject_brightness_over_100() {
        let result = Scene::new("test", 101, SceneColor::Temp(3000));
        assert!(matches!(result, Err(GoveeError::InvalidBrightness(101))));
    }

    #[test]
    fn reject_temp_zero() {
        let result = Scene::new("test", 50, SceneColor::Temp(0));
        assert!(matches!(result, Err(GoveeError::InvalidConfig(_))));
    }

    #[test]
    fn reject_temp_over_10000() {
        let result = Scene::new("test", 50, SceneColor::Temp(10001));
        assert!(matches!(result, Err(GoveeError::InvalidConfig(_))));
    }

    #[test]
    fn reject_name_with_newline() {
        let result = Scene::new("bad\nname", 50, SceneColor::Temp(3000));
        assert!(matches!(result, Err(GoveeError::InvalidConfig(_))));
    }

    #[test]
    fn reject_name_with_space() {
        let result = Scene::new("bad name", 50, SceneColor::Temp(3000));
        assert!(matches!(result, Err(GoveeError::InvalidConfig(_))));
    }

    #[test]
    fn reject_empty_name() {
        let result = Scene::new("", 50, SceneColor::Temp(3000));
        assert!(matches!(result, Err(GoveeError::InvalidConfig(_))));
    }

    #[test]
    fn accept_valid_name_chars() {
        let result = Scene::new("my-Scene_01", 50, SceneColor::Temp(3000));
        assert!(result.is_ok());
        assert_eq!(result.unwrap().name(), "my-Scene_01");
    }

    #[test]
    fn user_scene_loaded_via_with_user_scenes() {
        let mut user = HashMap::new();
        user.insert(
            "cozy".to_string(),
            SceneConfig {
                brightness: 30,
                color: Some(Color::new(255, 200, 100)),
                color_temp: None,
            },
        );

        let registry = SceneRegistry::new().with_user_scenes(&user).unwrap();
        let scene = registry.get("cozy").unwrap();
        assert_eq!(scene.name(), "cozy");
        assert_eq!(scene.brightness(), 30);
        assert_eq!(*scene.color(), SceneColor::Rgb(Color::new(255, 200, 100)));
    }

    #[test]
    fn user_scene_neither_color_nor_temp_rejected() {
        let mut user = HashMap::new();
        user.insert(
            "bad".to_string(),
            SceneConfig {
                brightness: 50,
                color: None,
                color_temp: None,
            },
        );
        let err = SceneRegistry::new().with_user_scenes(&user).unwrap_err();
        assert!(matches!(err, crate::error::GoveeError::InvalidConfig(_)));
    }

    #[test]
    fn user_scene_collision_between_two_user_scenes() {
        let mut user = HashMap::new();
        user.insert(
            "Cozy".to_string(),
            SceneConfig {
                brightness: 30,
                color: Some(Color::new(255, 200, 100)),
                color_temp: None,
            },
        );
        user.insert(
            "cozy".to_string(),
            SceneConfig {
                brightness: 80,
                color: Some(Color::new(100, 100, 255)),
                color_temp: None,
            },
        );
        // Both entries are valid; last-wins for case-insensitive collision.
        let registry = SceneRegistry::new().with_user_scenes(&user).unwrap();
        assert!(registry.get("cozy").is_ok());
    }

    #[test]
    fn user_scene_overrides_builtin() {
        let mut user = HashMap::new();
        user.insert(
            "warm".to_string(),
            SceneConfig {
                brightness: 80,
                color: None,
                color_temp: Some(3000),
            },
        );

        let registry = SceneRegistry::new().with_user_scenes(&user).unwrap();
        let scene = registry.get("warm").unwrap();
        assert_eq!(scene.brightness(), 80);
        assert_eq!(*scene.color(), SceneColor::Temp(3000));
    }

    #[test]
    fn user_color_temp_scene() {
        let mut user = HashMap::new();
        user.insert(
            "daylight".to_string(),
            SceneConfig {
                brightness: 100,
                color: None,
                color_temp: Some(6500),
            },
        );

        let registry = SceneRegistry::new().with_user_scenes(&user).unwrap();
        let scene = registry.get("daylight").unwrap();
        assert_eq!(scene.brightness(), 100);
        assert_eq!(*scene.color(), SceneColor::Temp(6500));
    }

    #[test]
    fn user_scene_case_insensitive_collision_last_wins() {
        // Two user scenes differing only by case. Sorted iteration
        // means "Cozy" comes before "cozy" — "cozy" wins.
        let mut user = HashMap::new();
        user.insert(
            "Cozy".to_string(),
            SceneConfig {
                brightness: 30,
                color: None,
                color_temp: Some(2700),
            },
        );
        user.insert(
            "cozy".to_string(),
            SceneConfig {
                brightness: 50,
                color: None,
                color_temp: Some(3000),
            },
        );

        let registry = SceneRegistry::new().with_user_scenes(&user).unwrap();
        let scene = registry.get("cozy").unwrap();
        // "cozy" (lowercase) sorts after "Cozy" (uppercase), so it wins.
        assert_eq!(scene.brightness(), 50);
        assert_eq!(*scene.color(), SceneColor::Temp(3000));
    }
}