Skip to content

Commit 8b74d6d

Browse files
committed
Implement transition support for legacy v1 api
1 parent 16d7725 commit 8b74d6d

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

crates/hue/src/api/grouped_light.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,27 @@ impl GroupedLight {
4747
}
4848
}
4949

50-
#[derive(Debug, Serialize, Deserialize, Clone)]
50+
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
5151
pub struct GroupedLightDynamicsUpdate {
5252
#[serde(skip_serializing_if = "Option::is_none")]
5353
pub duration: Option<u32>,
5454
}
5555

56+
impl GroupedLightDynamicsUpdate {
57+
#[must_use]
58+
pub fn new() -> Self {
59+
Self::default()
60+
}
61+
62+
#[must_use]
63+
pub fn with_duration(self, duration: Option<impl Into<u32>>) -> Self {
64+
Self {
65+
duration: duration.map(Into::into),
66+
..self
67+
}
68+
}
69+
}
70+
5671
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
5772
pub struct GroupedLightUpdate {
5873
#[serde(skip_serializing_if = "Option::is_none")]
@@ -111,6 +126,11 @@ impl GroupedLightUpdate {
111126
..self
112127
}
113128
}
129+
130+
#[must_use]
131+
pub const fn with_dynamics(self, dynamics: Option<GroupedLightDynamicsUpdate>) -> Self {
132+
Self { dynamics, ..self }
133+
}
114134
}
115135

116136
/* conversion from v1 api */
@@ -121,5 +141,9 @@ impl From<&ApiLightStateUpdate> for GroupedLightUpdate {
121141
.with_brightness(upd.bri.map(|b| f64::from(b) / 2.54))
122142
.with_color_xy(upd.xy.map(XY::from))
123143
.with_color_temperature(upd.ct)
144+
.with_dynamics(
145+
upd.transitiontime
146+
.map(|t| GroupedLightDynamicsUpdate::new().with_duration(Some(t * 100))),
147+
)
124148
}
125149
}

crates/hue/src/api/light.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,14 +449,29 @@ pub struct LightDynamics {
449449
pub speed_valid: bool,
450450
}
451451

452-
#[derive(Debug, Serialize, Deserialize, Clone)]
452+
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
453453
pub struct LightDynamicsUpdate {
454454
#[serde(skip_serializing_if = "Option::is_none")]
455455
pub speed: Option<f64>,
456456
#[serde(skip_serializing_if = "Option::is_none")]
457457
pub duration: Option<u32>,
458458
}
459459

460+
impl LightDynamicsUpdate {
461+
#[must_use]
462+
pub fn new() -> Self {
463+
Self::default()
464+
}
465+
466+
#[must_use]
467+
pub fn with_duration(self, duration: Option<impl Into<u32>>) -> Self {
468+
Self {
469+
duration: duration.map(Into::into),
470+
..self
471+
}
472+
}
473+
}
474+
460475
impl Default for LightDynamics {
461476
fn default() -> Self {
462477
Self {
@@ -684,6 +699,11 @@ impl LightUpdate {
684699
pub fn with_gradient(self, gradient: Option<LightGradientUpdate>) -> Self {
685700
Self { gradient, ..self }
686701
}
702+
703+
#[must_use]
704+
pub fn with_dynamics(self, dynamics: Option<LightDynamicsUpdate>) -> Self {
705+
Self { dynamics, ..self }
706+
}
687707
}
688708

689709
impl From<&ApiLightStateUpdate> for LightUpdate {
@@ -694,6 +714,10 @@ impl From<&ApiLightStateUpdate> for LightUpdate {
694714
.with_color_temperature(upd.ct)
695715
.with_color_hs(upd.hs.map(Into::into))
696716
.with_color_xy(upd.xy.map(Into::into))
717+
.with_dynamics(
718+
upd.transitiontime
719+
.map(|t| LightDynamicsUpdate::new().with_duration(Some(t * 100))),
720+
)
697721
}
698722
}
699723

crates/hue/src/api/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ impl<'a> V1Reply<'a> {
382382
self.add_option("on", upd.on)?
383383
.add_option("bri", upd.bri)?
384384
.add_option("xy", upd.xy)?
385-
.add_option("ct", upd.ct)
385+
.add_option("ct", upd.ct)?
386+
.add_option("transitiontime", upd.transitiontime)
386387
}
387388

388389
pub fn add<T: Serialize>(mut self, name: &'a str, value: T) -> HueResult<Self> {

crates/hue/src/legacy_api.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,8 @@ pub struct ApiLightStateUpdate {
484484
pub ct: Option<u16>,
485485
#[serde(skip_serializing_if = "Option::is_none", flatten)]
486486
pub hs: Option<RawHS>,
487+
#[serde(skip_serializing_if = "Option::is_none")]
488+
pub transitiontime: Option<u16>,
487489
}
488490

489491
#[derive(Debug, Serialize, Deserialize)]
@@ -519,6 +521,7 @@ impl From<api::SceneAction> for ApiLightStateUpdate {
519521
xy: action.color.map(|col| col.xy.into()),
520522
ct: action.color_temperature.and_then(|ct| ct.mirek),
521523
hs: None,
524+
transitiontime: None,
522525
}
523526
}
524527
}

0 commit comments

Comments
 (0)