Skip to main content

enphase_api/
models.rs

1//! # Models for the Enphase API client
2//!
3//! This module contains data models used by the Enphase API client.
4
5use serde::Deserialize;
6
7/// Power state for an inverter or device.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9#[non_exhaustive]
10pub enum PowerState {
11    /// Power is ON.
12    On,
13    /// Power is OFF.
14    Off,
15}
16
17/// Response structure for getting the power status.
18#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
19#[non_exhaustive]
20#[serde(rename_all = "camelCase")]
21pub struct PowerStatusResponse {
22    /// Whether power is forced off.
23    pub power_forced_off: bool,
24}
25
26impl PowerState {
27    /// Get the payload array value for this power state.
28    pub(crate) fn payload_value(self) -> u8 {
29        match self {
30            PowerState::On => 0,
31            PowerState::Off => 1,
32        }
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use pretty_assertions::assert_eq;
40
41    #[test]
42    fn power_state_on_payload() {
43        let state = PowerState::On;
44        assert_eq!(
45            state.payload_value(),
46            0,
47            "PowerState::On should have payload value 0"
48        );
49    }
50
51    #[test]
52    fn power_state_off_payload() {
53        let state = PowerState::Off;
54        assert_eq!(
55            state.payload_value(),
56            1,
57            "PowerState::Off should have payload value 1"
58        );
59    }
60
61    #[test]
62    fn deserialize_power_forced_off_true() {
63        let json = r#"{"powerForcedOff": true}"#;
64        let response: PowerStatusResponse =
65            serde_json::from_str(json).expect("Should deserialize successfully");
66
67        assert!(response.power_forced_off, "powerForcedOff should be true");
68    }
69
70    #[test]
71    fn deserialize_power_forced_off_false() {
72        let json = r#"{"powerForcedOff": false}"#;
73        let response: PowerStatusResponse =
74            serde_json::from_str(json).expect("Should deserialize successfully");
75
76        assert!(!response.power_forced_off, "powerForcedOff should be false");
77    }
78}