use lifxi::http::prelude::*;
type Result = lifxi::http::ClientResult;
#[derive(Debug, Deserialize, Eq, PartialEq)]
#[serde(tag = "type")]
pub enum Type {
#[serde(rename = "lifx")]
LifxBulb { selector: Selector },
}
#[derive(Deserialize)]
pub struct Device {
#[serde(flatten)]
pub r#type: Type,
pub name: String,
pub alternatives: Option<Vec<String>>,
}
impl Device {
pub fn power(&self, on: bool, fast: bool) -> Result {
match &self.r#type {
Type::LifxBulb { selector } => crate::config::LIFX_CLIENT
.select(selector.clone())
.set_state()
.power(on)
.fast(fast)
.send(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deserialize() {
let device: Device =
toml::from_str("type = \"lifx\"\nselector = \"label:Foo\"\nname = \"foo\"").unwrap();
assert_eq!(
device.r#type,
Type::LifxBulb {
selector: Selector::Label("Foo".to_owned())
}
);
assert!(toml::from_str::<Device>("").is_err());
assert!(toml::from_str::<Device>("type = \"lifx\"").is_err());
}
}