Skip to content

Commit

Permalink
Change UsageByPeriodResult's fields to be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
mihai-dinculescu committed Oct 23, 2024
1 parent 740d793 commit 0a65575
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ file. This change log follows the conventions of
- The `openssl` dependency has been replaced with native Rust alternatives to expand cross-compilation options, such as for Android, and to decrease build times (thanks to @rbock44).
- `PlugPowerStripHandler` has been renamed to `PowerStripPlugHandler` to be consistent with the rest of the library.
- `PlugPowerStripResult` has been renamed to `PowerStripPlugResult` to be consistent with the rest of the library.
- The `UsageByPeriodResult` fields `today`, `past7`, and `past30` have been updated to `Option<u64>` to handle cases where the API returns negative values, which will be represented as `None`.

### Fixed

Expand All @@ -30,6 +31,7 @@ file. This change log follows the conventions of
### Changed

- The `openssl` dependency has been replaced with native Rust alternatives to expand cross-compilation options, such as for Android, and to decrease build times (thanks to @rbock44).
- The `UsageByPeriodResult` fields `today`, `past7`, and `past30` have been updated to `Optional[int]` to handle cases where the API returns negative values, which will be represented as `null`.

### Fixed

Expand Down
8 changes: 5 additions & 3 deletions tapo-py/tapo-py/tapo/responses/device_usage_result.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from typing import Optional

class UsageByPeriodResult:
"""Usage by period result for today, the past 7 days, and the past 30 days."""

today: int
today: Optional[int]
"""Today."""
past7: int
past7: Optional[int]
"""Past 7 days."""
past30: int
past30: Optional[int]
"""Past 30 days."""

class DeviceUsageResult:
Expand Down
2 changes: 1 addition & 1 deletion tapo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
mod api;
mod error;
mod tapo_date_format;
mod utils;

#[cfg(feature = "python")]
pub mod python;
Expand Down
10 changes: 7 additions & 3 deletions tapo/src/responses/device_usage_result.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize};

use crate::responses::TapoResponseExt;
use crate::utils::ok_or_default;

/// Contains the time usage.
#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -28,9 +29,12 @@ impl DeviceUsageResult {
#[cfg_attr(feature = "python", pyo3::prelude::pyclass(get_all))]
pub struct UsageByPeriodResult {
/// Today.
pub today: u64,
#[serde(deserialize_with = "ok_or_default")]
pub today: Option<u64>,
/// Past 7 days.
pub past7: u64,
#[serde(deserialize_with = "ok_or_default")]
pub past7: Option<u64>,
/// Past 30 days.
pub past30: u64,
#[serde(deserialize_with = "ok_or_default")]
pub past30: Option<u64>,
}
2 changes: 1 addition & 1 deletion tapo/src/responses/energy_data_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};

use crate::responses::TapoResponseExt;
use crate::tapo_date_format::der_tapo_datetime_format;
use crate::utils::der_tapo_datetime_format;

/// Energy data for the requested [`crate::requests::EnergyDataInterval`].
#[derive(Debug, Serialize, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion tapo/src/responses/energy_usage_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};

use crate::responses::TapoResponseExt;
use crate::tapo_date_format::der_tapo_datetime_format;
use crate::utils::der_tapo_datetime_format;

/// Contains local time, current power and the energy usage and runtime for today and for the current month.
#[derive(Debug, Serialize, Deserialize)]
Expand Down
8 changes: 8 additions & 0 deletions tapo/src/tapo_date_format.rs → tapo/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ where

Ok(value)
}

pub fn ok_or_default<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: Deserialize<'de> + Default,
D: Deserializer<'de>,
{
Ok(Deserialize::deserialize(deserializer).unwrap_or_default())
}

0 comments on commit 0a65575

Please sign in to comment.