lgtv-ip-control 0.1.1

A Rust crate for controlling LG TVs over the network
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
use crate::constants::errors::LgTvError;
use crate::constants::types::{
    AppDetails, Apps, ConnectionType, EnergySavingLevels, Inputs, Keys, PictureModes, PowerStates,
    ScreenMuteModes, VolumeLevel,
};
use crate::encryption::Encryption;
use crate::tcp_connection::{Connected, Disconnected, TcpConnection};
use regex::Regex;
use std::collections::HashMap;
use std::io::Error;
use tokio::time::{Duration, sleep};

#[trait_variant::make(CommandExecutor: Send)]
pub trait LocalCommandExecutor: Sized {
    async fn send_command(&mut self, command: Vec<u8>) -> Result<Vec<u8>, Error>;
    async fn wake_on_lan(&self) -> Result<(), Box<dyn std::error::Error>>;
    async fn disconnect(self) -> Result<Self, &'static str>;
    async fn reconnect(&mut self) -> Result<(), String>;
}

/// Client for controlling an LG TV over IP Control.
///
/// The client uses a typestate pattern:
///
/// - `LGTV<Disconnected>` represents a disconnected client.
/// - `LGTV<Connected>` represents a connected client.
///
/// Most control methods are only available after a successful connection.
pub struct LGTV<C, State = Disconnected> {
    executor: C,
    encryption: Encryption,
    _state: std::marker::PhantomData<State>,
}

impl LGTV<TcpConnection<Connected>, Disconnected> {
    /// Connects to an LG TV over IP Control.
    ///
    /// # Arguments
    ///
    /// - `tv_ip` - The IP address of the TV.
    /// - `mac_address` - The MAC address of the TV.
    /// - `key` - The LG IP Control key code.
    ///
    /// # Errors
    ///
    /// Returns [`LgTvError::MissingKeyCode`] if no key code is provided.
    /// Returns [`LgTvError::TcpConnectionError`] if the TCP connection fails.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use lgtv_ip_control::LGTV;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let tv = LGTV::connect_tcp(
    ///     "192.168.1.100",
    ///     "AA:BB:CC:DD:EE:FF",
    ///     Some("YOUR_KEY_CODE"),
    /// )
    /// .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn connect_tcp(
        tv_ip: &str,
        mac_address: &str,
        key: Option<&str>,
    ) -> Result<LGTV<TcpConnection<Connected>, Connected>, LgTvError> {
        let tcp_connection = match TcpConnection::new(tv_ip, mac_address).await {
            Ok(connection) => connection,
            Err(error) => return Err(LgTvError::TcpConnectionError(error.to_string())),
        };

        let lgtv_disconnected = Self::new(tcp_connection, key)?;

        Ok(lgtv_disconnected.transition_to_connected())
    }
}

impl<C: CommandExecutor> LGTV<C, Disconnected> {
    pub(crate) fn new(executor: C, key: Option<&str>) -> Result<Self, LgTvError> {
        let key_code = key
            .map(|k| k.to_string())
            .ok_or(LgTvError::MissingKeyCode)?;

        let encryption = Encryption::new(key_code);

        Ok(LGTV {
            executor,
            encryption,
            _state: std::marker::PhantomData,
        })
    }

    pub(crate) fn transition_to_connected(self) -> LGTV<C, Connected> {
        LGTV {
            executor: self.executor,
            encryption: self.encryption,
            _state: std::marker::PhantomData,
        }
    }
}

impl<C: CommandExecutor> LGTV<C, Connected> {
    /// Disconnects from the TV.
    ///
    /// Returns the client back in the disconnected state.
    pub async fn disconnect(self) -> Result<LGTV<C, Disconnected>, LgTvError> {
        let tcp_connection = match self.executor.disconnect().await {
            Ok(connection) => connection,
            Err(error) => return Err(LgTvError::TcpConnectionError(error.to_string())),
        };

        Ok(LGTV {
            executor: tcp_connection,
            encryption: self.encryption,
            _state: std::marker::PhantomData,
        })
    }

    /// Sends a Wake-on-LAN packet and waits for the TV to become available.
    ///
    /// If `retries` is `None`, a default retry count is used.
    pub async fn power_on(&mut self, retries: Option<u8>) -> Result<(), LgTvError> {
        let attempts_left = retries.unwrap_or(10);

        self.executor
            .wake_on_lan()
            .await
            .map_err(|error| LgTvError::WakeOnLan(error.to_string()))?;

        sleep(Duration::from_millis(500)).await;

        self.executor
            .reconnect()
            .await
            .map_err(LgTvError::TcpConnectionError)?;

        self.test_power_on(attempts_left).await
    }

    /// Attempts to power off the TV.
    ///
    /// If `retries` is `None`, a default retry count is used.
    pub async fn power_off(&mut self, retries: Option<u8>) -> Result<(), LgTvError> {
        let mut attempts_left = retries.unwrap_or(10);

        loop {
            if attempts_left == 0 {
                return Err(LgTvError::PowerOff);
            }

            // Send the command once per iteration of the main retry loop
            self.send_command("POWER off").await?;
            sleep(Duration::from_secs(1)).await;

            match self.test_power_on(1).await {
                Ok(_) => {
                    // TV is still on, decrement attempts and try again on the next loop cycle
                    attempts_left -= 1;
                }
                Err(_) => {
                    // TV failed the "power on" test, meaning it successfully turned off!
                    return Ok(());
                }
            }
        }
    }

    /// Returns information about the currently active app/input.
    ///
    /// # Errors
    ///
    /// Returns [`LgTvError::PowerOff`] if the TV returns an empty response.
    pub async fn get_current_app(&mut self) -> Result<AppDetails, LgTvError> {
        let result = self.send_command("CURRENT_APP").await?;

        match result.as_str() {
            "" => Err(LgTvError::PowerOff),
            _ => {
                let mut pairs: HashMap<String, String> = HashMap::new();
                let re = match Regex::new(r"([\w\s]+):(\S+)") {
                    Ok(re) => re,
                    Err(error) => return Err(LgTvError::RegExpression(error.to_string())),
                };

                for captures in re.captures_iter(&result) {
                    pairs.insert(captures[1].trim().to_string(), captures[2].to_string());
                }

                Ok(AppDetails {
                    app: pairs.get("APP").cloned().unwrap_or_default(),
                    hot_plug: pairs.get("Hot plug").cloned().unwrap_or_default(),
                    signal: pairs.get("Signal").cloned().unwrap_or_default(),
                    hdcp_version: pairs.get("HDCP").cloned().unwrap_or_default(),
                    hdcp_status: pairs.get("HDCP Status").cloned().unwrap_or_default(),
                })
            }
        }
    }

    /// Returns the current volume level.
    ///
    /// The returned value is parsed from a TV response such as `VOL:25`.
    pub async fn get_current_volume(&mut self) -> Result<u8, LgTvError> {
        let result = self.send_command("CURRENT_VOL").await?;

        let re = match Regex::new(r"^VOL:(\d+)$") {
            Ok(re) => re,
            Err(error) => return Err(LgTvError::RegExpression(error.to_string())),
        };

        let capture = re.captures(&result);

        match capture {
            Some(capture) => Ok(capture[1].parse::<u8>()?),
            None => Err(LgTvError::ParseVolumeError),
        }
    }

    /// Returns whether IP Control is enabled on the TV.
    pub async fn get_ip_control_state(&mut self) -> Result<bool, LgTvError> {
        let result = self.send_command("GET_IPCONTROL_STATE").await?;

        match result.as_str() {
            "ON" => Ok(true),
            _ => Ok(false),
        }
    }

    /// Launches an app on the TV.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use lgtv_ip_control::{Apps, LGTV};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut tv = LGTV::new(
    ///     "192.168.1.100",
    ///     "AA:BB:CC:DD:EE:FF",
    ///     Some("YOUR_KEY_CODE"),
    /// )
    /// .await?;
    ///
    /// tv.launch_app(Apps::Youtube).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_mac_address(
        &mut self,
        connection_type: ConnectionType,
    ) -> Result<String, LgTvError> {
        let command = format!("GET_MACADDRESS {:?}", connection_type);
        let result = self.send_command(&command).await?;
        Ok(result)
    }

    pub async fn get_mute_state(&mut self) -> Result<bool, LgTvError> {
        let result = self.send_command("MUTE_STATE").await?;
        println!("{}", result);
        let re = match Regex::new(r"^MUTE:(on|off)$") {
            Ok(re) => re,
            Err(error) => return Err(LgTvError::RegExpression(error.to_string())),
        };

        if let Some(captures) = re.captures(&result) {
            match &captures[1] {
                "on" => Ok(true),
                "off" => Ok(false),
                _ => Err(LgTvError::UnknownMuteState),
            }
        } else {
            Err(LgTvError::UnableToParseMuteState)
        }
    }

    pub async fn get_power_state(&mut self) -> PowerStates {
        match self.test_power_on(5).await {
            Ok(_) => PowerStates::On,
            Err(_) => PowerStates::Off,
        }
    }

    pub async fn launch_app(&mut self, app: Apps) -> Result<(), LgTvError> {
        let command = format!("APP_LAUNCH {}", app.as_str());
        self.send_command(&command).await?;
        Ok(())
    }

    pub async fn set_picture_mode(&mut self, mode: PictureModes) -> Result<(), LgTvError> {
        let command = format!("PICTURE_MODE {}", mode.as_str());
        self.send_command(&command).await?;
        Ok(())
    }

    pub async fn send_key(&mut self, key: Keys) -> Result<(), LgTvError> {
        let command = format!("KEY_ACTION {}", key.as_str());
        self.send_command(&command).await?;
        Ok(())
    }

    pub async fn set_screen_mute(&mut self, mode: ScreenMuteModes) -> Result<(), LgTvError> {
        let command = format!("SCREEN_MUTE {}", mode.as_str());
        self.send_command(&command).await?;
        Ok(())
    }

    pub async fn set_energy_saving(&mut self, level: EnergySavingLevels) -> Result<(), LgTvError> {
        let command = format!("ENERGY_SAVING {}", level.as_str());
        self.send_command(&command).await?;
        Ok(())
    }

    pub async fn set_input(&mut self, input: Inputs) -> Result<(), LgTvError> {
        let command = format!("INPUT_SELECT {}", input.as_str());
        self.send_command(&command).await?;
        Ok(())
    }

    pub async fn set_volume(
        &mut self,
        level: VolumeLevel,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let command = format!("VOLUME_CONTROL {}", level.value());
        self.send_command(&command).await?;
        Ok(())
    }

    pub async fn set_volume_mute(&mut self, is_muted: bool) -> Result<(), LgTvError> {
        let action = if is_muted { "on" } else { "off" };
        let command = format!("VOLUME_MUTE {action}");
        self.send_command(&command).await?;
        Ok(())
    }

    async fn send_command(&mut self, command: &str) -> Result<String, LgTvError> {
        let encrypted_command = match self.encryption.encrypt(command) {
            Ok(encrypted_command) => encrypted_command,
            Err(error) => return Err(LgTvError::EncryptionError(error.to_string())),
        };

        let result = match self.executor.send_command(encrypted_command).await {
            Ok(result) => result,
            Err(error) => return Err(LgTvError::SendCommand(error.to_string())),
        };

        match self.encryption.decrypt(&result) {
            Ok(decrypted_result) => Ok(decrypted_result),
            Err(error) => Err(LgTvError::DecryptionError(error.to_string())),
        }
    }

    async fn test_power_on(&mut self, retries: u8) -> Result<(), LgTvError> {
        let mut attempts_left = retries;
        loop {
            match self.get_current_app().await {
                Ok(app_details) if !app_details.app.is_empty() => return Ok(()),
                _ => {
                    if attempts_left == 0 {
                        return Err(LgTvError::PowerOff);
                    }
                    attempts_left -= 1;
                    sleep(Duration::from_secs(1)).await;
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Arc, Mutex};

    // =========================================================================
    // 1. Define the Mock Executor
    // =========================================================================
    /// A mock network layer tracking sent packets, state flags, and mock responses.
    #[derive(Clone)]
    struct MockExecutor {
        // Shared mutable state across the async trait methods
        inner: Arc<Mutex<MockInner>>,
    }

    struct MockInner {
        mock_response: String,
        wake_on_lan_called: bool,
        reconnect_called: bool,
        disconnect_called: bool,
        encryption_key: String,
        commands_sent: Vec<String>, // Tracks all decrypted commands passed down
        inject_error: Option<String>,
    }

    impl MockExecutor {
        fn new(initial_response: &str, encryption_key: &str) -> Self {
            Self {
                inner: Arc::new(Mutex::new(MockInner {
                    mock_response: initial_response.to_string(),
                    wake_on_lan_called: false,
                    reconnect_called: false,
                    disconnect_called: false,
                    encryption_key: encryption_key.to_string(),
                    commands_sent: Vec::new(),
                    inject_error: None,
                })),
            }
        }

        fn set_response(&self, new_response: &str) {
            if let Ok(mut inner) = self.inner.lock() {
                inner.mock_response = new_response.to_string();
            }
        }

        fn inject_error(&self, error_msg: &str) {
            if let Ok(mut inner) = self.inner.lock() {
                inner.inject_error = Some(error_msg.to_string());
            }
        }

        fn get_last_command(&self) -> Option<String> {
            let inner = self.inner.lock().unwrap();
            inner.commands_sent.last().cloned()
        }
    }

    // =========================================================================
    // 2. Implement CommandExecutor Contract for MockExecutor
    // =========================================================================
    impl CommandExecutor for MockExecutor {
        async fn send_command(&mut self, command: Vec<u8>) -> Result<Vec<u8>, Error> {
            let mut inner = self.inner.lock().unwrap();

            if let Some(ref err_msg) = inner.inject_error {
                return Err(std::io::Error::other(err_msg.clone()));
            }

            let mock_encryption = Encryption::new(inner.encryption_key.clone());
            let decrypted_command = mock_encryption.decrypt((command).as_ref()).unwrap();

            inner.commands_sent.push(decrypted_command);

            let encrypted_response = mock_encryption.encrypt(&inner.mock_response).unwrap();
            Ok(encrypted_response)
        }

        async fn wake_on_lan(&self) -> Result<(), Box<dyn std::error::Error>> {
            let mut inner = self.inner.lock().unwrap();
            if let Some(ref err_msg) = inner.inject_error {
                return Err(err_msg.clone().into());
            }
            inner.wake_on_lan_called = true;
            Ok(())
        }

        async fn disconnect(self) -> Result<Self, &'static str> {
            let inner_clone = self.inner.clone();
            let mut inner = inner_clone.lock().unwrap();
            if let Some(ref _err_msg) = inner.inject_error {
                return Err("Failed to disconnect");
            }
            inner.disconnect_called = true;
            Ok(self)
        }

        async fn reconnect(&mut self) -> Result<(), String> {
            let mut inner = self.inner.lock().unwrap();
            if let Some(ref err_msg) = inner.inject_error {
                return Err(err_msg.clone());
            }
            inner.reconnect_called = true;
            Ok(())
        }
    }

    // =========================================================================
    // Helper function to build connected TV instance cleanly
    // =========================================================================
    fn setup_connected_tv(mock: MockExecutor, key: &str) -> LGTV<MockExecutor, Connected> {
        let lgtv_disconnected = LGTV::new(mock, Some(key)).unwrap();
        lgtv_disconnected.transition_to_connected()
    }

    const TEST_KEY: &str = "TESTKEY123";

    // =========================================================================
    // Method Group: LGTV::new & Initialization Edge Cases
    // =========================================================================
    #[test]
    fn test_new_missing_key_code_error() {
        let mock_executor = MockExecutor::new("", TEST_KEY);
        let result = LGTV::new(mock_executor, None);

        assert!(matches!(result, Err(LgTvError::MissingKeyCode)));
    }

    // =========================================================================
    // Method Group: disconnect()
    // =========================================================================
    #[tokio::test]
    async fn test_disconnect_success() {
        let mock = MockExecutor::new("", TEST_KEY);
        let tracker = mock.clone();
        let tv = setup_connected_tv(mock, TEST_KEY);

        let _disconnected_tv = tv.disconnect().await.unwrap();
        assert!(tracker.inner.lock().unwrap().disconnect_called);
    }

    #[tokio::test]
    async fn test_disconnect_failure_error_mapping() {
        let mock = MockExecutor::new("", TEST_KEY);
        mock.inject_error("Network Dropped");
        let tv = setup_connected_tv(mock, TEST_KEY);

        let result = tv.disconnect().await;
        assert!(matches!(result, Err(LgTvError::TcpConnectionError(_))));
    }

    // =========================================================================
    // Method Group: power_on()
    // =========================================================================
    #[tokio::test]
    async fn test_power_on_success() {
        let mock = MockExecutor::new("APP:live_tv", TEST_KEY);
        let tracker = mock.clone();
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        let result = tv.power_on(Some(2)).await;
        assert!(result.is_ok());

        let inner = tracker.inner.lock().unwrap();
        assert!(inner.wake_on_lan_called);
        assert!(inner.reconnect_called);
    }

    #[tokio::test]
    async fn test_power_on_wol_fails() {
        let mock = MockExecutor::new("APP:live_tv", TEST_KEY);
        mock.inject_error("UDP socket failure");
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        let result = tv.power_on(Some(1)).await;
        assert!(matches!(result, Err(LgTvError::WakeOnLan(_))));
    }

    #[tokio::test]
    async fn test_power_on_timeout_exhausted() {
        // TV keeps returning blank data indicating it hasn't booted up yet
        let mock = MockExecutor::new("APP:", TEST_KEY);
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        let result = tv.power_on(Some(1)).await; // Limit to 1 retry cycle
        assert!(matches!(result, Err(LgTvError::PowerOff)));
    }

    // =========================================================================
    // Method Group: power_off()
    // =========================================================================
    #[tokio::test]
    async fn test_power_off_immediate_success() {
        // The first test loop check fails to find an app (meaning TV shut off immediately)
        let mock = MockExecutor::new("OFF", TEST_KEY);
        let tracker = mock.clone();
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        let result = tv.power_off(Some(3)).await;
        assert!(result.is_ok());
        assert_eq!(tracker.get_last_command().unwrap(), "CURRENT_APP");
    }

    #[tokio::test]
    async fn test_power_off_retries_exhausted() {
        // TV remains permanently on, continually returning active app
        let mock = MockExecutor::new("APP:hdmi1", TEST_KEY);
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        let result = tv.power_off(Some(2)).await;
        assert!(matches!(result, Err(LgTvError::PowerOff)));
    }

    // =========================================================================
    // Method Group: get_current_app()
    // =========================================================================
    #[tokio::test]
    async fn test_get_current_app_malformed_response() {
        // Corrupted response configuration missing structural colons
        let mock = MockExecutor::new("APP netflix HotPlug true", TEST_KEY);
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        let details = tv.get_current_app().await.unwrap();
        // Values default to empty strings if regex fails to match key-value formats
        assert_eq!(details.app, "");
    }

    #[tokio::test]
    async fn test_get_current_app_empty_string_tv_off() {
        let mock = MockExecutor::new("", TEST_KEY);
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        let result = tv.get_current_app().await;
        assert!(matches!(result, Err(LgTvError::PowerOff)));
    }

    // =========================================================================
    // Method Group: get_current_volume()
    // =========================================================================
    #[tokio::test]
    async fn test_get_current_volume_parse_error() {
        // Volume value contains letters, preventing numerical extraction
        let mock = MockExecutor::new("VOL:abc", TEST_KEY);
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        let result = tv.get_current_volume().await;
        assert!(matches!(result, Err(LgTvError::ParseVolumeError)));
    }

    // =========================================================================
    // Method Group: get_ip_control_state()
    // =========================================================================
    #[tokio::test]
    async fn test_get_ip_control_state_variants() {
        let mock = MockExecutor::new("ON", TEST_KEY);
        let mut tv = setup_connected_tv(mock.clone(), TEST_KEY);
        assert!(tv.get_ip_control_state().await.unwrap());

        mock.set_response("OFF");
        assert!(!tv.get_ip_control_state().await.unwrap());
    }

    // =========================================================================
    // Method Group: get_mac_address()
    // =========================================================================
    #[tokio::test]
    async fn test_get_mac_address_command_formatting() {
        let mock = MockExecutor::new("AA:BB:CC:DD:EE:FF", TEST_KEY);
        let tracker = mock.clone();
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        let mac = tv.get_mac_address(ConnectionType::Wired).await.unwrap();
        assert_eq!(mac, "AA:BB:CC:DD:EE:FF");
        assert_eq!(tracker.get_last_command().unwrap(), "GET_MACADDRESS Wired");
    }

    // =========================================================================
    // Method Group: get_mute_state()
    // =========================================================================
    #[tokio::test]
    async fn test_get_mute_state_variants() {
        let mock = MockExecutor::new("MUTE:on", TEST_KEY);
        let mut tv = setup_connected_tv(mock.clone(), TEST_KEY);
        assert!(tv.get_mute_state().await.unwrap());

        mock.set_response("MUTE:off");
        assert!(!tv.get_mute_state().await.unwrap());
    }

    #[tokio::test]
    async fn test_get_mute_state_invalid_payload_error() {
        let mock = MockExecutor::new("MUTE:unknown", TEST_KEY);
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        let result = tv.get_mute_state().await;

        assert!(matches!(result, Err(LgTvError::UnableToParseMuteState)));
    }

    #[tokio::test]
    async fn test_get_mute_state_regex_miss_error() {
        let mock = MockExecutor::new("INVALID_MUTE_FORMAT", TEST_KEY);
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        let result = tv.get_mute_state().await;
        assert!(matches!(result, Err(LgTvError::UnableToParseMuteState)));
    }

    // =========================================================================
    // Method Group: get_power_state()
    // =========================================================================
    #[tokio::test]
    async fn test_get_power_state_variants() {
        let mock = MockExecutor::new("APP:hdmi2", TEST_KEY);
        let mut tv = setup_connected_tv(mock.clone(), TEST_KEY);

        assert!(matches!(tv.get_power_state().await, PowerStates::On));

        mock.set_response("OFF");
        assert!(matches!(tv.get_power_state().await, PowerStates::Off));
    }

    // =========================================================================
    // Method Group: Setters, Actions, and State Modification Vectors
    // =========================================================================
    #[tokio::test]
    async fn test_launch_app_payload() {
        let mock = MockExecutor::new("OK", TEST_KEY);
        let tracker = mock.clone();
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        tv.launch_app(Apps::Netflix).await.unwrap();
        assert_eq!(tracker.get_last_command().unwrap(), "APP_LAUNCH netflix");
    }

    #[tokio::test]
    async fn test_set_picture_mode_payload() {
        let mock = MockExecutor::new("OK", TEST_KEY);
        let tracker = mock.clone();
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        tv.set_picture_mode(PictureModes::Cinema).await.unwrap();
        assert_eq!(tracker.get_last_command().unwrap(), "PICTURE_MODE cinema");
    }

    #[tokio::test]
    async fn test_send_key_payload() {
        let mock = MockExecutor::new("OK", TEST_KEY);
        let tracker = mock.clone();
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        tv.send_key(Keys::VolumeMute).await.unwrap();
        assert_eq!(tracker.get_last_command().unwrap(), "KEY_ACTION volumemute");
    }

    #[tokio::test]
    async fn test_set_screen_mute_payload() {
        let mock = MockExecutor::new("OK", TEST_KEY);
        let tracker = mock.clone();
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        tv.set_screen_mute(ScreenMuteModes::ScreenMuteOn)
            .await
            .unwrap();
        assert_eq!(
            tracker.get_last_command().unwrap(),
            "SCREEN_MUTE screenmuteon"
        );
    }

    #[tokio::test]
    async fn test_set_energy_saving_payload() {
        let mock = MockExecutor::new("OK", TEST_KEY);
        let tracker = mock.clone();
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        tv.set_energy_saving(EnergySavingLevels::Maximum)
            .await
            .unwrap();
        assert_eq!(tracker.get_last_command().unwrap(), "ENERGY_SAVING maximum");
    }

    #[tokio::test]
    async fn test_set_input_payload() {
        let mock = MockExecutor::new("OK", TEST_KEY);
        let tracker = mock.clone();
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        tv.set_input(Inputs::Hdmi1).await.unwrap();
        assert_eq!(tracker.get_last_command().unwrap(), "INPUT_SELECT hdmi1");
    }

    #[tokio::test]
    async fn test_set_volume_payload() {
        let mock = MockExecutor::new("OK", TEST_KEY);
        let tracker = mock.clone();
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        tv.set_volume(VolumeLevel::try_from(12).unwrap())
            .await
            .unwrap();
        assert_eq!(tracker.get_last_command().unwrap(), "VOLUME_CONTROL 12");
    }

    #[tokio::test]
    async fn test_set_volume_mute_payload() {
        let mock = MockExecutor::new("OK", TEST_KEY);
        let tracker = mock.clone();
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        tv.set_volume_mute(true).await.unwrap();
        assert_eq!(tracker.get_last_command().unwrap().trim(), "VOLUME_MUTE on");

        tv.set_volume_mute(false).await.unwrap();
        assert_eq!(
            tracker.get_last_command().unwrap().trim(),
            "VOLUME_MUTE off"
        );
    }

    #[tokio::test]
    async fn test_send_command_underlying_network_error_propagation() {
        let mock = MockExecutor::new("OK", TEST_KEY);
        mock.inject_error("Broken Pipe");
        let mut tv = setup_connected_tv(mock, TEST_KEY);

        let result = tv.launch_app(Apps::Youtube).await;
        assert!(matches!(result, Err(LgTvError::SendCommand(_))));
    }
}