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
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
749
750
751
752
use std::collections::HashMap;
use std::sync::RwLock;
use std::time::Duration;

use async_trait::async_trait;
use reqwest::Client;
use tracing::{debug, instrument, warn};

use super::GoveeBackend;
use crate::error::{GoveeError, Result};
use crate::types::{BackendType, Color, Device, DeviceId, DeviceState};

/// Default base URL for the Govee cloud API.
const DEFAULT_BASE_URL: &str = "https://developer-api.govee.com";

/// Default request timeout (covers the entire request lifecycle).
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);

/// Default connection timeout (TCP + TLS handshake).
const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(10);

/// Maximum retry-after delay honored from a 429 response (RT-M07-02).
/// Prevents a server from blocking the client indefinitely.
const MAX_RETRY_AFTER_SECS: u64 = 300;

/// Maximum number of retries for transient errors and rate limiting.
const MAX_RETRIES: u32 = 3;

/// User-Agent header identifying the library and version.
fn user_agent() -> String {
    format!("govee/{}", env!("CARGO_PKG_VERSION"))
}

/// Check if a URL points to a loopback address (127.0.0.1, ::1, localhost).
///
/// This allows HTTP for local test servers (e.g., wiremock) while enforcing
/// HTTPS for all remote hosts. Callers should not rely on this for production
/// configurations — if a production config accidentally resolves to localhost,
/// API keys would be sent over plaintext.
fn is_loopback(url: &reqwest::Url) -> bool {
    match url.host_str() {
        Some("localhost") | Some("127.0.0.1") | Some("[::1]") => true,
        // Url::host_str() returns IPv6 in brackets (e.g., "[::1]"),
        // but IpAddr::parse expects bare addresses. Strip brackets.
        Some(host) => host
            .strip_prefix('[')
            .and_then(|h| h.strip_suffix(']'))
            .unwrap_or(host)
            .parse::<std::net::IpAddr>()
            .is_ok_and(|ip| ip.is_loopback()),
        None => false,
    }
}

/// Build a configured `reqwest::Client` with timeouts and User-Agent.
fn build_client(custom_ua: Option<&str>) -> std::result::Result<Client, reqwest::Error> {
    let ua = custom_ua.map(|s| s.to_string()).unwrap_or_else(user_agent);
    Client::builder()
        .timeout(DEFAULT_TIMEOUT)
        .connect_timeout(DEFAULT_CONNECT_TIMEOUT)
        .user_agent(ua)
        .build()
}

/// Cloud API backend using the Govee v1 REST API.
///
/// Authenticates via `Govee-API-Key` header. Base URL defaults to
/// `https://developer-api.govee.com` but can be overridden for testing.
///
/// # Security
///
/// - **MITM risk:** Uses the system CA bundle for TLS verification
///   (no certificate pinning). A corporate MITM proxy or CA-installing
///   malware can intercept all traffic, capturing the API key. The
///   Govee API does not provide key rotation or revocation. (RT-08)
///
/// # Resource lifecycle
///
/// - **HTTP client:** A single `reqwest::Client` is created at construction
///   time and shared across all requests. `reqwest` pools connections
///   internally — no manual connection management needed.
/// - **Device model cache:** `std::sync::RwLock<HashMap>` holding device→model
///   mappings. Populated by `list_devices`, read by `get_state`/`send_control`.
///   Lock is held only for brief lookups/swaps and never across `.await` points.
pub struct CloudBackend {
    client: Client,
    base_url: reqwest::Url,
    api_key: String,
    /// Device ID → model mapping, populated by `list_devices`.
    /// Required because `GET /v1/devices/state` needs both `device` and `model`.
    device_models: RwLock<HashMap<DeviceId, String>>,
    /// Single-flight guard for auto-refresh of device_models cache (RT-M07-04).
    refresh_guard: tokio::sync::Mutex<()>,
}

impl CloudBackend {
    /// Create a new `CloudBackend`.
    ///
    /// Returns `GoveeError::InvalidConfig` if `base_url` is not a valid URL
    /// or does not use HTTPS (unless the host is a loopback address, which
    /// allows HTTP for local testing with wiremock).
    ///
    /// # Security
    ///
    /// `base_url` is a privileged parameter. If an attacker controls it,
    /// all API calls (including the API key) are sent to the attacker's
    /// endpoint. Callers must never derive `base_url` from untrusted
    /// input. (RT-09)
    pub fn new(
        api_key: String,
        base_url: Option<String>,
        user_agent: Option<String>,
    ) -> Result<Self> {
        // Validate user_agent: reject control characters (bytes < 0x20 or DEL 0x7F).
        if let Some(ref ua) = user_agent
            && ua.bytes().any(|b| b < 0x20 || b == 0x7f)
        {
            return Err(GoveeError::InvalidConfig(
                "user_agent contains invalid characters".into(),
            ));
        }

        let raw = base_url.unwrap_or_else(|| DEFAULT_BASE_URL.to_string());
        let parsed = reqwest::Url::parse(&raw)
            .map_err(|e| GoveeError::InvalidConfig(format!("invalid base URL \"{raw}\": {e}")))?;
        if parsed.scheme() != "https" && !is_loopback(&parsed) {
            return Err(GoveeError::InvalidConfig(format!(
                "base URL must use HTTPS (HTTP is only allowed for loopback addresses), got: {raw}"
            )));
        }
        let client = build_client(user_agent.as_deref())
            .map_err(|e| GoveeError::InvalidConfig(format!("failed to build HTTP client: {e}")))?;
        Ok(Self {
            client,
            base_url: parsed,
            api_key,
            device_models: RwLock::new(HashMap::new()),
            refresh_guard: tokio::sync::Mutex::new(()),
        })
    }

    /// Look up the model for a device ID from the internal cache.
    ///
    /// Returns `DeviceNotFound` if the device is not cached.
    /// Call `list_devices` first to populate the cache.
    fn get_model(&self, id: &DeviceId) -> Result<String> {
        let models = self
            .device_models
            .read()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        models.get(id).cloned().ok_or_else(|| {
            GoveeError::DeviceNotFound(format!(
                "{} (call list_devices first to populate the device cache)",
                id
            ))
        })
    }

    /// Send a control command to a device via `PUT /v1/devices/control`.
    ///
    /// Parses the response body for API-level errors (HTTP 200 with
    /// `code != 200` in the JSON envelope).
    async fn send_control(
        &self,
        id: &DeviceId,
        cmd_name: &str,
        cmd_value: serde_json::Value,
    ) -> Result<()> {
        let model = self.get_model(id)?;
        let url = self
            .base_url
            .join("v1/devices/control")
            .map_err(|e| GoveeError::InvalidConfig(format!("failed to build URL: {e}")))?;

        let payload = serde_json::json!({
            "device": id.as_str(),
            "model": model,
            "cmd": {
                "name": cmd_name,
                "value": cmd_value,
            }
        });

        for attempt in 0..=MAX_RETRIES {
            let result = self
                .client
                .put(url.clone())
                .header("Govee-API-Key", &self.api_key)
                .json(&payload)
                .send()
                .await;

            let response = match result {
                Ok(r) => r,
                Err(e) => {
                    let err = GoveeError::Request(e);
                    if let Some(delay) = Self::retry_delay(&err, attempt)
                        && attempt < MAX_RETRIES
                    {
                        debug!(attempt, ?delay, "retrying after request error");
                        tokio::time::sleep(delay).await;
                        continue;
                    }
                    return Err(err);
                }
            };

            match self.check_response(response).await {
                Ok(response) => {
                    let body: V1ControlResponse = response.json().await?;
                    if body.code != 200 {
                        return Err(GoveeError::Api {
                            code: body.code,
                            message: body.message,
                        });
                    }
                    debug!(device = %id, cmd = cmd_name, "sent control command");
                    return Ok(());
                }
                Err(err) => {
                    if let Some(delay) = Self::retry_delay(&err, attempt)
                        && attempt < MAX_RETRIES
                    {
                        debug!(attempt, ?delay, "retrying after error");
                        tokio::time::sleep(delay).await;
                        continue;
                    }
                    return Err(err);
                }
            }
        }

        unreachable!("retry loop always returns on the final attempt")
    }

    /// Check an HTTP response for rate limiting and error status codes.
    ///
    /// Returns the response unchanged on success (2xx). For 429, returns
    /// `RateLimited`. For other non-2xx, returns `Api` with the response body.
    async fn check_response(&self, response: reqwest::Response) -> Result<reqwest::Response> {
        let status = response.status();
        if status.as_u16() == 429 {
            let retry_after_secs = parse_retry_after(&response);
            warn!(retry_after_secs, "rate limited by Govee API");
            return Err(GoveeError::RateLimited { retry_after_secs });
        }
        if !status.is_success() {
            let body = response
                .text()
                .await
                .unwrap_or_else(|e| format!("<failed to read response body: {e}>"));
            return Err(GoveeError::Api {
                code: status.as_u16(),
                message: body,
            });
        }
        Ok(response)
    }

    /// Compute retry delay for a failed request.
    ///
    /// Returns `Some(duration)` if the error is retryable, `None` otherwise.
    fn retry_delay(err: &GoveeError, attempt: u32) -> Option<Duration> {
        match err {
            GoveeError::RateLimited { retry_after_secs } => {
                let capped = (*retry_after_secs).min(MAX_RETRY_AFTER_SECS);
                Some(Duration::from_secs(capped))
            }
            GoveeError::Request(_) | GoveeError::Api { code: 500.., .. } => {
                // Exponential backoff: 1s, 2s, 4s (deterministic, no randomness)
                let delay_ms = 1000u64 * 2u64.pow(attempt);
                Some(Duration::from_millis(delay_ms))
            }
            _ => None,
        }
    }
}

// --- v1 API response types (internal) ---

/// Top-level response envelope from `GET /v1/devices`.
#[derive(serde::Deserialize)]
struct V1DevicesResponse {
    data: V1DevicesData,
    code: u16,
    message: String,
}

/// The `data` field inside a v1 devices response.
#[derive(serde::Deserialize)]
struct V1DevicesData {
    devices: Vec<V1Device>,
}

/// A single device as returned by the v1 API.
///
/// Only fields we use are declared; extra API fields (`retrievable`,
/// `supportCmds`, etc.) are silently ignored by serde.
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct V1Device {
    device: String,
    model: String,
    device_name: String,
}

impl V1Device {
    /// Convert the API device into our domain `Device`.
    ///
    /// Returns an error if the MAC address is invalid.
    fn into_domain(self) -> Result<Device> {
        let id = DeviceId::new(&self.device)?;
        Ok(Device {
            id,
            model: self.model,
            name: self.device_name,
            alias: None,
            backend: BackendType::Cloud,
        })
    }
}

/// Top-level response envelope from `GET /v1/devices/state`.
#[derive(serde::Deserialize)]
struct V1StateResponse {
    data: V1StateData,
    code: u16,
    message: String,
}

/// The `data` field inside a v1 state response.
///
/// Only `properties` is used; `device` and `model` echo back the request
/// params and are ignored by serde's default permissive parsing.
#[derive(serde::Deserialize)]
struct V1StateData {
    properties: Vec<serde_json::Value>,
}

/// Build a `DeviceState` from the v1 property array.
///
/// The v1 API returns state as `[{"online": true}, {"powerState": "on"}, ...]`
/// — each element is a JSON object with a single key. We parse each as a
/// `serde_json::Value` map and extract known keys.
///
/// Values are clamped to valid ranges before construction:
/// - brightness: clamped to 0–100 on the u64 before cast
/// - color components: clamped to 0–255 on the u64 before cast
/// - colorTem: clamped to u32::MAX via saturating conversion
fn build_state_from_properties(properties: Vec<serde_json::Value>) -> Result<DeviceState> {
    let mut on = false;
    let mut brightness: u8 = 0;
    let mut color = Color::new(0, 0, 0);
    let mut color_temp: Option<u32> = None;
    let mut online = true;

    for prop in properties {
        if let Some(v) = prop.get("online").and_then(|v| v.as_bool()) {
            online = v;
        }
        if let Some(v) = prop.get("powerState").and_then(|v| v.as_str()) {
            on = v == "on";
        }
        if let Some(v) = prop.get("brightness").and_then(|v| v.as_u64()) {
            brightness = v.min(100) as u8;
        }
        if let Some(obj) = prop.get("color").and_then(|v| v.as_object()) {
            let r = obj.get("r").and_then(|v| v.as_u64()).unwrap_or(0).min(255) as u8;
            let g = obj.get("g").and_then(|v| v.as_u64()).unwrap_or(0).min(255) as u8;
            let b = obj.get("b").and_then(|v| v.as_u64()).unwrap_or(0).min(255) as u8;
            color = Color::new(r, g, b);
        }
        if let Some(v) = prop.get("colorTem").and_then(|v| v.as_u64()) {
            color_temp = Some(u32::try_from(v).unwrap_or(u32::MAX));
        }
    }

    DeviceState::new(on, brightness, color, color_temp, !online)
}

/// Response envelope from `PUT /v1/devices/control`.
#[derive(serde::Deserialize)]
struct V1ControlResponse {
    code: u16,
    message: String,
}

/// Parse the `Retry-After` header value as seconds.
fn parse_retry_after(response: &reqwest::Response) -> u64 {
    response
        .headers()
        .get("retry-after")
        .and_then(|v| v.to_str().ok())
        .and_then(|v| v.parse::<u64>().ok())
        .unwrap_or(60)
}

#[async_trait]
impl GoveeBackend for CloudBackend {
    #[instrument(skip(self), fields(backend = "cloud"))]
    async fn list_devices(&self) -> Result<Vec<Device>> {
        let url = self
            .base_url
            .join("v1/devices")
            .map_err(|e| GoveeError::InvalidConfig(format!("failed to build URL: {e}")))?;
        let response = self
            .client
            .get(url)
            .header("Govee-API-Key", &self.api_key)
            .send()
            .await?;

        let response = self.check_response(response).await?;

        let body: V1DevicesResponse = response.json().await?;
        if body.code != 200 {
            return Err(GoveeError::Api {
                code: body.code,
                message: body.message,
            });
        }

        let devices: Vec<Device> = body
            .data
            .devices
            .into_iter()
            .map(V1Device::into_domain)
            .collect::<Result<Vec<_>>>()?;

        // Cache device→model mappings for get_state (atomic swap).
        {
            let new_map: HashMap<DeviceId, String> = devices
                .iter()
                .map(|d| (d.id.clone(), d.model.clone()))
                .collect();
            let mut models = self
                .device_models
                .write()
                .unwrap_or_else(|poisoned| poisoned.into_inner());
            *models = new_map;
        }

        debug!(count = devices.len(), "listed cloud devices");
        Ok(devices)
    }

    /// Query the current state of a device.
    ///
    /// Uses an internal device→model cache and will automatically refresh
    /// the cache with `list_devices` on a cache miss. Returns
    /// `DeviceNotFound` if the device is still unknown after refreshing.
    #[instrument(skip(self), fields(backend = "cloud", device = %id))]
    async fn get_state(&self, id: &DeviceId) -> Result<DeviceState> {
        // Auto-refresh device cache on miss (single-flight guard).
        let model = match self.get_model(id) {
            Ok(m) => m,
            Err(_) => {
                let _guard = self.refresh_guard.lock().await;
                // Re-check after acquiring lock (another task may have refreshed).
                match self.get_model(id) {
                    Ok(m) => m,
                    Err(_) => {
                        debug!(device = %id, "model cache miss, refreshing device list");
                        self.list_devices().await?;
                        self.get_model(id)?
                    }
                }
            }
        };
        let mut url = self
            .base_url
            .join("v1/devices/state")
            .map_err(|e| GoveeError::InvalidConfig(format!("failed to build URL: {e}")))?;
        url.query_pairs_mut()
            .append_pair("device", id.as_str())
            .append_pair("model", &model);

        let response = self
            .client
            .get(url)
            .header("Govee-API-Key", &self.api_key)
            .send()
            .await?;

        let response = self.check_response(response).await?;

        let body: V1StateResponse = response.json().await?;
        if body.code != 200 {
            return Err(GoveeError::Api {
                code: body.code,
                message: body.message,
            });
        }

        let state = build_state_from_properties(body.data.properties)?;
        debug!(device = %id, stale = state.stale, "queried device state");
        Ok(state)
    }

    #[instrument(skip(self), fields(backend = "cloud", device = %id))]
    async fn set_power(&self, id: &DeviceId, on: bool) -> Result<()> {
        let value = if on { "on" } else { "off" };
        self.send_control(id, "turn", serde_json::json!(value))
            .await
    }

    #[instrument(skip(self), fields(backend = "cloud", device = %id))]
    async fn set_brightness(&self, id: &DeviceId, value: u8) -> Result<()> {
        if value > 100 {
            return Err(GoveeError::InvalidBrightness(value));
        }
        self.send_control(id, "brightness", serde_json::json!(value))
            .await
    }

    #[instrument(skip(self, color), fields(backend = "cloud", device = %id))]
    async fn set_color(&self, id: &DeviceId, color: Color) -> Result<()> {
        self.send_control(
            id,
            "color",
            serde_json::json!({"r": color.r, "g": color.g, "b": color.b}),
        )
        .await
    }

    /// Set color temperature in Kelvin (1-10000).
    #[instrument(skip(self), fields(backend = "cloud", device = %id))]
    async fn set_color_temp(&self, id: &DeviceId, kelvin: u32) -> Result<()> {
        if kelvin == 0 || kelvin > 10000 {
            return Err(GoveeError::InvalidConfig(
                "color temperature must be 1-10000K".into(),
            ));
        }
        self.send_control(id, "colorTem", serde_json::json!(kelvin))
            .await
    }

    fn backend_type(&self) -> BackendType {
        BackendType::Cloud
    }
}

impl std::fmt::Debug for CloudBackend {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let cached = self.device_models.read().map(|m| m.len()).unwrap_or(0);
        f.debug_struct("CloudBackend")
            .field("base_url", &self.base_url.as_str())
            .field("api_key", &"[REDACTED]")
            .field("cached_devices", &cached)
            .finish()
    }
}

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

    #[test]
    fn rejects_http_non_loopback() {
        let result = CloudBackend::new("key".into(), Some("http://example.com".into()), None);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, GoveeError::InvalidConfig(_)));
        assert!(err.to_string().contains("HTTPS"));
    }

    #[test]
    fn allows_http_loopback() {
        assert!(
            CloudBackend::new("key".into(), Some("http://127.0.0.1:8080".into()), None).is_ok()
        );
        assert!(
            CloudBackend::new("key".into(), Some("http://localhost:8080".into()), None).is_ok()
        );
        assert!(CloudBackend::new("key".into(), Some("http://[::1]:8080".into()), None).is_ok());
    }

    #[test]
    fn rejects_invalid_url() {
        let result = CloudBackend::new("key".into(), Some("not a url".into()), None);
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), GoveeError::InvalidConfig(_)));
    }

    #[test]
    fn accepts_https_base_url() {
        let result = CloudBackend::new("key".into(), Some("https://example.com".into()), None);
        assert!(result.is_ok());
    }

    #[test]
    fn default_base_url_is_https() {
        let backend = CloudBackend::new("key".into(), None, None).unwrap();
        assert_eq!(backend.base_url.scheme(), "https");
    }

    #[test]
    fn trailing_slash_normalized() {
        let backend =
            CloudBackend::new("key".into(), Some("https://example.com/".into()), None).unwrap();
        let url = backend.base_url.join("v1/devices").unwrap();
        assert_eq!(url.path(), "/v1/devices");
    }

    #[test]
    fn debug_redacts_api_key() {
        let backend = CloudBackend::new("super-secret-key".into(), None, None).unwrap();
        let debug = format!("{:?}", backend);
        assert!(!debug.contains("super-secret-key"));
        assert!(debug.contains("[REDACTED]"));
    }

    #[test]
    fn v1_device_into_domain() {
        let v1 = V1Device {
            device: "AA:BB:CC:DD:EE:FF".into(),
            model: "H6076".into(),
            device_name: "Kitchen Light".into(),
        };
        let device = v1.into_domain().unwrap();
        assert_eq!(device.id.as_str(), "AA:BB:CC:DD:EE:FF");
        assert_eq!(device.model, "H6076");
        assert_eq!(device.name, "Kitchen Light");
        assert_eq!(device.backend, BackendType::Cloud);
        assert!(device.alias.is_none());
    }

    #[test]
    fn v1_device_invalid_mac_returns_error() {
        let v1 = V1Device {
            device: "not-a-mac".into(),
            model: "H6076".into(),
            device_name: "Bad Device".into(),
        };
        assert!(v1.into_domain().is_err());
    }

    #[test]
    fn build_state_all_properties() {
        let props: Vec<serde_json::Value> = serde_json::from_str(
            r#"[
                {"online": true},
                {"powerState": "on"},
                {"brightness": 75},
                {"color": {"r": 255, "g": 128, "b": 0}},
                {"colorTem": 5000}
            ]"#,
        )
        .unwrap();
        let state = build_state_from_properties(props).unwrap();
        assert!(state.on);
        assert_eq!(state.brightness, 75);
        assert_eq!(state.color, Color::new(255, 128, 0));
        assert_eq!(state.color_temp_kelvin, Some(5000));
        assert!(!state.stale);
    }

    #[test]
    fn build_state_offline_is_stale() {
        let props: Vec<serde_json::Value> = serde_json::from_str(
            r#"[{"online": false}, {"powerState": "off"}, {"brightness": 50}]"#,
        )
        .unwrap();
        let state = build_state_from_properties(props).unwrap();
        assert!(state.stale);
        assert!(!state.on);
    }

    #[test]
    fn build_state_clamps_brightness() {
        let props: Vec<serde_json::Value> =
            serde_json::from_str(r#"[{"brightness": 200}]"#).unwrap();
        let state = build_state_from_properties(props).unwrap();
        assert_eq!(state.brightness, 100);
    }

    #[test]
    fn build_state_clamps_brightness_above_255() {
        let props: Vec<serde_json::Value> =
            serde_json::from_str(r#"[{"brightness": 300}]"#).unwrap();
        let state = build_state_from_properties(props).unwrap();
        assert_eq!(state.brightness, 100);
    }

    #[test]
    fn build_state_clamps_color_above_255() {
        let props: Vec<serde_json::Value> =
            serde_json::from_str(r#"[{"color": {"r": 300, "g": 500, "b": 1000}}]"#).unwrap();
        let state = build_state_from_properties(props).unwrap();
        assert_eq!(state.color, Color::new(255, 255, 255));
    }

    #[test]
    fn build_state_unknown_properties_ignored() {
        let props: Vec<serde_json::Value> =
            serde_json::from_str(r#"[{"unknownProp": 42}]"#).unwrap();
        let state = build_state_from_properties(props).unwrap();
        assert!(!state.on);
        assert_eq!(state.brightness, 0);
    }

    #[test]
    fn user_agent_contains_version() {
        let ua = user_agent();
        assert!(ua.starts_with("govee/"));
        assert!(ua.contains(env!("CARGO_PKG_VERSION")));
    }

    #[test]
    fn custom_user_agent_accepted() {
        let result = CloudBackend::new("key".into(), None, Some("my-app/1.0".into()));
        assert!(result.is_ok());
    }

    #[test]
    fn user_agent_crlf_rejected() {
        let result = CloudBackend::new("key".into(), None, Some("foo\r\nbar".into()));
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, GoveeError::InvalidConfig(_)));
        assert!(
            err.to_string()
                .contains("user_agent contains invalid characters")
        );
    }

    #[test]
    fn user_agent_null_byte_rejected() {
        let result = CloudBackend::new("key".into(), None, Some("foo\0bar".into()));
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), GoveeError::InvalidConfig(_)));
    }

    #[tokio::test]
    async fn set_color_temp_kelvin_zero_rejected() {
        let backend = CloudBackend::new("key".into(), None, None).unwrap();
        let id = DeviceId::new("AA:BB:CC:DD:EE:FF").unwrap();
        // Validation runs before cache lookup, so no need to populate cache.
        let result = backend.set_color_temp(&id, 0).await;
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), GoveeError::InvalidConfig(_)));
    }

    #[tokio::test]
    async fn set_color_temp_kelvin_above_10000_rejected() {
        let backend = CloudBackend::new("key".into(), None, None).unwrap();
        let id = DeviceId::new("AA:BB:CC:DD:EE:FF").unwrap();
        let result = backend.set_color_temp(&id, 10001).await;
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), GoveeError::InvalidConfig(_)));
    }
}