From 133c466faf2fdb7e879a62258cee931924cc5453 Mon Sep 17 00:00:00 2001 From: WhySoBad <49595640+WhySoBad@users.noreply.github.com> Date: Sat, 21 Dec 2024 17:17:25 +0100 Subject: [PATCH 1/2] chore: public accessible color config --- tapo/src/requests/set_device_info/color.rs | 98 ++++++++++--------- .../requests/set_device_info/color_light.rs | 14 +-- 2 files changed, 60 insertions(+), 52 deletions(-) diff --git a/tapo/src/requests/set_device_info/color.rs b/tapo/src/requests/set_device_info/color.rs index 5f230e1..f419e9b 100644 --- a/tapo/src/requests/set_device_info/color.rs +++ b/tapo/src/requests/set_device_info/color.rs @@ -51,52 +51,60 @@ pub enum Color { ForestGreen, } -type ColorConfig = (Option, Option, Option); +impl Color { + /// Get the [`crate::requests::ColorConfig`] for the color + pub fn get_color_config(&self) -> Option { + COLOR_MAP.get(self).cloned() + } +} + +/// Triple-Tuple of hue, saturation and color temperature of a predefined color +pub type ColorConfig = (u16, u8, u16); lazy_static! { - pub(crate) static ref COLOR_MAP: HashMap = { - let mut map = HashMap::new(); - 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))); + static ref COLOR_MAP: HashMap = { + let mut map = HashMap::::new(); + map.insert(Color::CoolWhite, (0, 100, 4000)); + map.insert(Color::Daylight, (0, 100, 5000)); + map.insert(Color::Ivory, (0, 100, 6000)); + map.insert(Color::WarmWhite, (0, 100, 3000)); + map.insert(Color::Incandescent, (0, 100, 2700)); + map.insert(Color::Candlelight, (0, 100, 2500)); + map.insert(Color::Snow, (0, 100, 6500)); + map.insert(Color::GhostWhite, (0, 100, 6500)); + map.insert(Color::AliceBlue, (208, 5, 0)); + map.insert(Color::LightGoldenrod, (54, 28, 0)); + map.insert(Color::LemonChiffon, (54, 19, 0)); + map.insert(Color::AntiqueWhite, (0, 100, 5500)); + map.insert(Color::Gold, (50, 100, 0)); + map.insert(Color::Peru, (29, 69, 0)); + map.insert(Color::Chocolate, (30, 100, 0)); + map.insert(Color::SandyBrown, (27, 60, 0)); + map.insert(Color::Coral, (16, 68, 0)); + map.insert(Color::Pumpkin, (24, 90, 0)); + map.insert(Color::Tomato, (9, 72, 0)); + map.insert(Color::Vermilion, (4, 77, 0)); + map.insert(Color::OrangeRed, (16, 100, 0)); + map.insert(Color::Pink, (349, 24, 0)); + map.insert(Color::Crimson, (348, 90, 0)); + map.insert(Color::DarkRed, (0, 100, 0)); + map.insert(Color::HotPink, (330, 58, 0)); + map.insert(Color::Smitten, (329, 67, 0)); + map.insert(Color::MediumPurple, (259, 48, 0)); + map.insert(Color::BlueViolet, (271, 80, 0)); + map.insert(Color::Indigo, (274, 100, 0)); + map.insert(Color::LightSkyBlue, (202, 46, 0)); + map.insert(Color::CornflowerBlue, (218, 57, 0)); + map.insert(Color::Ultramarine, (254, 100, 0)); + map.insert(Color::DeepSkyBlue, (195, 100, 0)); + map.insert(Color::Azure, (210, 100, 0)); + map.insert(Color::NavyBlue, (240, 100, 0)); + map.insert(Color::LightTurquoise, (180, 26, 0)); + map.insert(Color::Aquamarine, (159, 50, 0)); + map.insert(Color::Turquoise, (174, 71, 0)); + map.insert(Color::LightGreen, (120, 39, 0)); + map.insert(Color::Lime, (75, 100, 0)); + map.insert(Color::ForestGreen, (120, 75, 0)); map }; -} +} \ No newline at end of file diff --git a/tapo/src/requests/set_device_info/color_light.rs b/tapo/src/requests/set_device_info/color_light.rs index 33d70c5..35410a4 100644 --- a/tapo/src/requests/set_device_info/color_light.rs +++ b/tapo/src/requests/set_device_info/color_light.rs @@ -3,7 +3,7 @@ use std::ops::RangeInclusive; use serde::Serialize; use crate::error::Error; -use crate::requests::{Color, COLOR_MAP}; +use crate::requests::Color; use crate::HandlerExt; /// Builder that is used by the [`crate::ColorLightHandler::set`] API to set multiple properties in a single request. @@ -53,13 +53,13 @@ impl ColorLightSetDeviceInfoParams { /// /// * `color` - one of [crate::requests::Color] pub fn color(mut self, color: Color) -> Self { - let (hue, saturation, color_temperature) = *COLOR_MAP - .get(&color) - .unwrap_or_else(|| panic!("Failed to find the color definition for {color:?}")); + let (hue, saturation, color_temperature) = color + .get_color_config() + .expect(format!("Failed to find the color definition for {color:?}").as_str()); - self.hue = hue; - self.saturation = saturation; - self.color_temperature = color_temperature; + self.hue = Some(hue); + self.saturation = Some(saturation); + self.color_temperature = Some(color_temperature); self } From 8c00e614cd590c490c3bd25200c0842a12fe697c Mon Sep 17 00:00:00 2001 From: Mihai Dinculescu Date: Sun, 22 Dec 2024 11:23:44 +0000 Subject: [PATCH 2/2] Tweaks and Python support --- CHANGELOG.md | 5 +++++ .../tapo/requests/set_device_info/color.pyi | 4 ++++ tapo/src/requests/set_device_info/color.rs | 14 +++++++++----- tapo/src/requests/set_device_info/color_light.rs | 4 +--- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 097a793..9e0c9b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ file. This change log follows the conventions of #### Added - Added functionality for controlling the alarm on the H100 hub via the `play_alarm` and `stop_alarm` methods in the `H100Handler`. Additionally, `get_supported_ringtone_list` is available to retrieve the list of supported ringtones for debugging purposes. (thanks to @kay) +- Added the ability to retrieve the color configuration (`hue`, `saturation`, `color_temperature`) for the `Color` enum values through the `get_color_config` method. (thanks to @WhySoBad) #### Changed @@ -19,6 +20,10 @@ file. This change log follows the conventions of ### Python +#### Added + +- Added the ability to retrieve the color configuration (`hue`, `saturation`, `color_temperature`) for the `Color` enum values through the `get_color_config` method. (thanks to @WhySoBad) + #### Changed - The internal implementation of `H100Handler`'s `get_child_device_list` has been updated to fetch all pages, not just the first one. diff --git a/tapo-py/tapo-py/tapo/requests/set_device_info/color.pyi b/tapo-py/tapo-py/tapo/requests/set_device_info/color.pyi index 04bdf1d..e78ea15 100644 --- a/tapo-py/tapo-py/tapo/requests/set_device_info/color.pyi +++ b/tapo-py/tapo-py/tapo/requests/set_device_info/color.pyi @@ -1,4 +1,5 @@ from enum import Enum +from typing import Tuple class Color(str, Enum): """List of preset colors as defined in the Google Home app.""" @@ -44,3 +45,6 @@ class Color(str, Enum): LightGreen = "LightGreen" Lime = "Lime" ForestGreen = "ForestGreen" + + def get_color_config(self) -> Tuple[int, int, int]: + """Get the `hue`, `saturation`, and `color_temperature` of the color.""" diff --git a/tapo/src/requests/set_device_info/color.rs b/tapo/src/requests/set_device_info/color.rs index f419e9b..bf8c26b 100644 --- a/tapo/src/requests/set_device_info/color.rs +++ b/tapo/src/requests/set_device_info/color.rs @@ -51,14 +51,18 @@ pub enum Color { ForestGreen, } +#[cfg_attr(feature = "python", pyo3::pymethods)] impl Color { - /// Get the [`crate::requests::ColorConfig`] for the color - pub fn get_color_config(&self) -> Option { - COLOR_MAP.get(self).cloned() + /// Get the [`crate::requests::ColorConfig`] of the color. + pub fn get_color_config(&self) -> ColorConfig { + COLOR_MAP + .get(self) + .cloned() + .unwrap_or_else(|| panic!("Failed to find the color definition of {self:?}")) } } -/// Triple-Tuple of hue, saturation and color temperature of a predefined color +/// Triple-tuple containing the `hue`, `saturation`, and `color_temperature` of a color. pub type ColorConfig = (u16, u8, u16); lazy_static! { @@ -107,4 +111,4 @@ lazy_static! { map.insert(Color::ForestGreen, (120, 75, 0)); map }; -} \ No newline at end of file +} diff --git a/tapo/src/requests/set_device_info/color_light.rs b/tapo/src/requests/set_device_info/color_light.rs index 35410a4..5de2247 100644 --- a/tapo/src/requests/set_device_info/color_light.rs +++ b/tapo/src/requests/set_device_info/color_light.rs @@ -53,9 +53,7 @@ impl ColorLightSetDeviceInfoParams { /// /// * `color` - one of [crate::requests::Color] pub fn color(mut self, color: Color) -> Self { - let (hue, saturation, color_temperature) = color - .get_color_config() - .expect(format!("Failed to find the color definition for {color:?}").as_str()); + let (hue, saturation, color_temperature) = color.get_color_config(); self.hue = Some(hue); self.saturation = Some(saturation);