From 0a6557554005eb51b9ef99b1ba14a64293b03cd3 Mon Sep 17 00:00:00 2001 From: Mihai Dinculescu Date: Wed, 23 Oct 2024 21:40:43 +0100 Subject: [PATCH] Change UsageByPeriodResult's fields to be optional --- CHANGELOG.md | 2 ++ tapo-py/tapo-py/tapo/responses/device_usage_result.pyi | 8 +++++--- tapo/src/lib.rs | 2 +- tapo/src/responses/device_usage_result.rs | 10 +++++++--- tapo/src/responses/energy_data_result.rs | 2 +- tapo/src/responses/energy_usage_result.rs | 2 +- tapo/src/{tapo_date_format.rs => utils.rs} | 8 ++++++++ 7 files changed, 25 insertions(+), 9 deletions(-) rename tapo/src/{tapo_date_format.rs => utils.rs} (67%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a8e542..0b06298 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` to handle cases where the API returns negative values, which will be represented as `None`. ### Fixed @@ -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 diff --git a/tapo-py/tapo-py/tapo/responses/device_usage_result.pyi b/tapo-py/tapo-py/tapo/responses/device_usage_result.pyi index fc942ce..e09d0b0 100644 --- a/tapo-py/tapo-py/tapo/responses/device_usage_result.pyi +++ b/tapo-py/tapo-py/tapo/responses/device_usage_result.pyi @@ -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: diff --git a/tapo/src/lib.rs b/tapo/src/lib.rs index aae7a59..ee4fa2f 100644 --- a/tapo/src/lib.rs +++ b/tapo/src/lib.rs @@ -74,7 +74,7 @@ mod api; mod error; -mod tapo_date_format; +mod utils; #[cfg(feature = "python")] pub mod python; diff --git a/tapo/src/responses/device_usage_result.rs b/tapo/src/responses/device_usage_result.rs index 7197252..264517d 100644 --- a/tapo/src/responses/device_usage_result.rs +++ b/tapo/src/responses/device_usage_result.rs @@ -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)] @@ -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, /// Past 7 days. - pub past7: u64, + #[serde(deserialize_with = "ok_or_default")] + pub past7: Option, /// Past 30 days. - pub past30: u64, + #[serde(deserialize_with = "ok_or_default")] + pub past30: Option, } diff --git a/tapo/src/responses/energy_data_result.rs b/tapo/src/responses/energy_data_result.rs index d3165ec..1d43c83 100644 --- a/tapo/src/responses/energy_data_result.rs +++ b/tapo/src/responses/energy_data_result.rs @@ -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)] diff --git a/tapo/src/responses/energy_usage_result.rs b/tapo/src/responses/energy_usage_result.rs index 36c942d..1ac0a97 100644 --- a/tapo/src/responses/energy_usage_result.rs +++ b/tapo/src/responses/energy_usage_result.rs @@ -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)] diff --git a/tapo/src/tapo_date_format.rs b/tapo/src/utils.rs similarity index 67% rename from tapo/src/tapo_date_format.rs rename to tapo/src/utils.rs index 3984a73..a753856 100644 --- a/tapo/src/tapo_date_format.rs +++ b/tapo/src/utils.rs @@ -15,3 +15,11 @@ where Ok(value) } + +pub fn ok_or_default<'de, T, D>(deserializer: D) -> Result +where + T: Deserialize<'de> + Default, + D: Deserializer<'de>, +{ + Ok(Deserialize::deserialize(deserializer).unwrap_or_default()) +}