Skip to content

Commit

Permalink
Fix the regression introduced in a61741a
Browse files Browse the repository at this point in the history
Addresses #266.
  • Loading branch information
mihai-dinculescu committed Sep 23, 2024
1 parent 4db3aab commit 56b8ac4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 52 deletions.
10 changes: 5 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ file. This change log follows the conventions of

### Added

- Added full support for the S200B switches though the `S200BHandler` handler.
- Added full support for the T100 sensors though the `T100Handler` handler.
- Added full support for the T110 sensors though the `T110Handler` handler.
- Added full support for the T300 sensors though the `T300Handler` handler.
- Added full support for the T310 and T315 sensors though the `T31XHandler` handler.
- Added full support for the S200B switches through the `S200BHandler` handler.
- Added full support for the T100 sensors through the `T100Handler` handler.
- Added full support for the T110 sensors through the `T110Handler` handler.
- Added full support for the T300 sensors through the `T300Handler` handler.
- Added full support for the T310 and T315 sensors through the `T31XHandler` handler.

### Changed

Expand Down
82 changes: 41 additions & 41 deletions tapo/src/requests/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,47 +56,47 @@ type ColorConfig = (Option<u16>, Option<u8>, Option<u16>);
lazy_static! {
pub(crate) static ref COLOR_MAP: HashMap<Color, ColorConfig> = {
let mut map = HashMap::new();
map.insert(Color::CoolWhite, (None, None, Some(4000)));
map.insert(Color::Daylight, (None, None, Some(5000)));
map.insert(Color::Ivory, (None, None, Some(6000)));
map.insert(Color::WarmWhite, (None, None, Some(3000)));
map.insert(Color::Incandescent, (None, None, Some(2700)));
map.insert(Color::Candlelight, (None, None, Some(2500)));
map.insert(Color::Snow, (None, None, Some(6500)));
map.insert(Color::GhostWhite, (None, None, Some(6500)));
map.insert(Color::AliceBlue, (Some(208), Some(5), None));
map.insert(Color::LightGoldenrod, (Some(54), Some(28), None));
map.insert(Color::LemonChiffon, (Some(54), Some(19), None));
map.insert(Color::AntiqueWhite, (None, None, Some(5500)));
map.insert(Color::Gold, (Some(50), Some(100), None));
map.insert(Color::Peru, (Some(29), Some(69), None));
map.insert(Color::Chocolate, (Some(30), Some(100), None));
map.insert(Color::SandyBrown, (Some(27), Some(60), None));
map.insert(Color::Coral, (Some(16), Some(68), None));
map.insert(Color::Pumpkin, (Some(24), Some(90), None));
map.insert(Color::Tomato, (Some(9), Some(72), None));
map.insert(Color::Vermilion, (Some(4), Some(77), None));
map.insert(Color::OrangeRed, (Some(16), Some(100), None));
map.insert(Color::Pink, (Some(349), Some(24), None));
map.insert(Color::Crimson, (Some(348), Some(90), None));
map.insert(Color::DarkRed, (Some(0), Some(100), None));
map.insert(Color::HotPink, (Some(330), Some(58), None));
map.insert(Color::Smitten, (Some(329), Some(67), None));
map.insert(Color::MediumPurple, (Some(259), Some(48), None));
map.insert(Color::BlueViolet, (Some(271), Some(80), None));
map.insert(Color::Indigo, (Some(274), Some(100), None));
map.insert(Color::LightSkyBlue, (Some(202), Some(46), None));
map.insert(Color::CornflowerBlue, (Some(218), Some(57), None));
map.insert(Color::Ultramarine, (Some(254), Some(100), None));
map.insert(Color::DeepSkyBlue, (Some(195), Some(100), None));
map.insert(Color::Azure, (Some(210), Some(100), None));
map.insert(Color::NavyBlue, (Some(240), Some(100), None));
map.insert(Color::LightTurquoise, (Some(180), Some(26), None));
map.insert(Color::Aquamarine, (Some(159), Some(50), None));
map.insert(Color::Turquoise, (Some(174), Some(71), None));
map.insert(Color::LightGreen, (Some(120), Some(39), None));
map.insert(Color::Lime, (Some(75), Some(100), None));
map.insert(Color::ForestGreen, (Some(120), Some(75), None));
map.insert(Color::CoolWhite, (Some(0), Some(100), Some(4000)));
map.insert(Color::Daylight, (Some(0), Some(100), Some(5000)));
map.insert(Color::Ivory, (Some(0), Some(100), Some(6000)));
map.insert(Color::WarmWhite, (Some(0), Some(100), Some(3000)));
map.insert(Color::Incandescent, (Some(0), Some(100), Some(2700)));
map.insert(Color::Candlelight, (Some(0), Some(100), Some(2500)));
map.insert(Color::Snow, (Some(0), Some(100), Some(6500)));
map.insert(Color::GhostWhite, (Some(0), Some(100), Some(6500)));
map.insert(Color::AliceBlue, (Some(208), Some(5), Some(0)));
map.insert(Color::LightGoldenrod, (Some(54), Some(28), Some(0)));
map.insert(Color::LemonChiffon, (Some(54), Some(19), Some(0)));
map.insert(Color::AntiqueWhite, (Some(0), Some(100), Some(5500)));
map.insert(Color::Gold, (Some(50), Some(100), Some(0)));
map.insert(Color::Peru, (Some(29), Some(69), Some(0)));
map.insert(Color::Chocolate, (Some(30), Some(100), Some(0)));
map.insert(Color::SandyBrown, (Some(27), Some(60), Some(0)));
map.insert(Color::Coral, (Some(16), Some(68), Some(0)));
map.insert(Color::Pumpkin, (Some(24), Some(90), Some(0)));
map.insert(Color::Tomato, (Some(9), Some(72), Some(0)));
map.insert(Color::Vermilion, (Some(4), Some(77), Some(0)));
map.insert(Color::OrangeRed, (Some(16), Some(100), Some(0)));
map.insert(Color::Pink, (Some(349), Some(24), Some(0)));
map.insert(Color::Crimson, (Some(348), Some(90), Some(0)));
map.insert(Color::DarkRed, (Some(0), Some(100), Some(0)));
map.insert(Color::HotPink, (Some(330), Some(58), Some(0)));
map.insert(Color::Smitten, (Some(329), Some(67), Some(0)));
map.insert(Color::MediumPurple, (Some(259), Some(48), Some(0)));
map.insert(Color::BlueViolet, (Some(271), Some(80), Some(0)));
map.insert(Color::Indigo, (Some(274), Some(100), Some(0)));
map.insert(Color::LightSkyBlue, (Some(202), Some(46), Some(0)));
map.insert(Color::CornflowerBlue, (Some(218), Some(57), Some(0)));
map.insert(Color::Ultramarine, (Some(254), Some(100), Some(0)));
map.insert(Color::DeepSkyBlue, (Some(195), Some(100), Some(0)));
map.insert(Color::Azure, (Some(210), Some(100), Some(0)));
map.insert(Color::NavyBlue, (Some(240), Some(100), Some(0)));
map.insert(Color::LightTurquoise, (Some(180), Some(26), Some(0)));
map.insert(Color::Aquamarine, (Some(159), Some(50), Some(0)));
map.insert(Color::Turquoise, (Some(174), Some(71), Some(0)));
map.insert(Color::LightGreen, (Some(120), Some(39), Some(0)));
map.insert(Color::Lime, (Some(75), Some(100), Some(0)));
map.insert(Color::ForestGreen, (Some(120), Some(75), Some(0)));
map
};
}
29 changes: 23 additions & 6 deletions tapo/src/requests/set_device_info/color_light.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::RangeInclusive;

use serde::Serialize;

use crate::error::Error;
Expand Down Expand Up @@ -72,7 +74,7 @@ impl ColorLightSetDeviceInfoParams {
pub fn hue_saturation(mut self, hue: u16, saturation: u8) -> Self {
self.hue = Some(hue);
self.saturation = Some(saturation);
self.color_temperature = None;
self.color_temperature = Some(0);

self
}
Expand Down Expand Up @@ -154,10 +156,11 @@ impl ColorLightSetDeviceInfoParams {
});
}

const COLOR_TEMPERATURE_RANGE: RangeInclusive<u16> = 2500..=6500;
if let Some(color_temperature) = self.color_temperature {
if self.hue.unwrap_or_default() == 0
&& self.saturation.unwrap_or(100) == 100
&& !(2500..=6500).contains(&color_temperature)
if self.hue.is_none()
&& self.saturation.is_none()
&& !COLOR_TEMPERATURE_RANGE.contains(&color_temperature)
{
return Err(Error::Validation {
field: "color_temperature".to_string(),
Expand Down Expand Up @@ -206,7 +209,7 @@ mod tests {

assert_eq!(params.hue, Some(50));
assert_eq!(params.saturation, Some(50));
assert_eq!(params.color_temperature, None);
assert_eq!(params.color_temperature, Some(0));

assert!(params.send(&MockHandler).await.is_ok())
}
Expand Down Expand Up @@ -280,19 +283,33 @@ mod tests {
}

#[tokio::test]
async fn color_temperature_validation() {
async fn color_temperature_validation_low() {
let params: ColorLightSetDeviceInfoParams = ColorLightSetDeviceInfoParams::new();
let result = params.color_temperature(2499).send(&MockHandler).await;
assert!(matches!(
result.err(),
Some(Error::Validation { field, message }) if field == "color_temperature" && message == "must be between 2500 and 6500"
));
}

#[tokio::test]
async fn color_temperature_validation_high() {
let params = ColorLightSetDeviceInfoParams::new();
let result = params.color_temperature(6501).send(&MockHandler).await;
assert!(matches!(
result.err(),
Some(Error::Validation { field, message }) if field == "color_temperature" && message == "must be between 2500 and 6500"
));
}

#[tokio::test]
async fn color_temperature_validation_default_hue_saturation() {
let params: ColorLightSetDeviceInfoParams = ColorLightSetDeviceInfoParams::new();
let result = params
.color_temperature(2500)
.hue_saturation(0, 100)
.send(&MockHandler)
.await;
assert!(result.is_ok());
}
}

0 comments on commit 56b8ac4

Please sign in to comment.