Skip to content

Commit 8c8a547

Browse files
committed
Save Philips Hue effect state
1 parent fd77b4e commit 8c8a547

3 files changed

Lines changed: 58 additions & 14 deletions

File tree

crates/hue/src/api/light.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,20 @@ impl AddAssign<&LightUpdate> for Light {
224224
grad.points.clone_from(&grupd.points);
225225
}
226226
}
227+
228+
if let Some(upd) = &upd.effects_v2 {
229+
if let Some(effects) = &mut self.effects_v2 {
230+
*effects += upd;
231+
}
232+
if let Some(effects) = &mut self.effects {
233+
let light_effect = upd
234+
.action
235+
.as_ref()
236+
.and_then(|a| a.effect)
237+
.unwrap_or(LightEffect::NoEffect);
238+
effects.status = light_effect;
239+
}
240+
}
227241
}
228242
}
229243

@@ -537,7 +551,7 @@ impl LightEffects {
537551
}
538552
}
539553

540-
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
554+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
541555
pub struct LightEffectsV2 {
542556
pub action: LightEffectValues,
543557
pub status: LightEffectStatus,
@@ -575,6 +589,19 @@ pub struct LightEffectsV2Update {
575589
pub status: Option<Value>,
576590
}
577591

592+
impl AddAssign<&LightEffectsV2Update> for LightEffectsV2 {
593+
fn add_assign(&mut self, upd: &LightEffectsV2Update) {
594+
let light_effect = upd
595+
.action
596+
.as_ref()
597+
.and_then(|a| a.effect)
598+
.unwrap_or(LightEffect::NoEffect);
599+
600+
self.status.effect = light_effect;
601+
self.status.parameters = upd.action.as_ref().map(|a| a.parameters.clone());
602+
}
603+
}
604+
578605
#[derive(Debug, Serialize, Deserialize, Clone)]
579606
#[serde(deny_unknown_fields)]
580607
pub struct LightEffectActionUpdate {
@@ -583,12 +610,12 @@ pub struct LightEffectActionUpdate {
583610
pub parameters: LightEffectParameters,
584611
}
585612

586-
#[derive(Debug, Serialize, Deserialize, Clone)]
613+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
587614
#[serde(deny_unknown_fields)]
588615
pub struct LightEffectParameters {
589-
#[serde(default)]
616+
#[serde(default, skip_serializing_if = "Option::is_none")]
590617
pub color: Option<ColorUpdate>,
591-
#[serde(default)]
618+
#[serde(default, skip_serializing_if = "Option::is_none")]
592619
pub color_temperature: Option<ColorTemperatureUpdate>,
593620
pub speed: Option<f32>,
594621
}
@@ -598,12 +625,12 @@ pub struct LightEffectValues {
598625
pub effect_values: Vec<LightEffect>,
599626
}
600627

601-
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
628+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
602629
pub struct LightEffectStatus {
603630
pub effect: LightEffect,
604631
pub effect_values: Vec<LightEffect>,
605632
#[serde(default, skip_serializing_if = "Option::is_none")]
606-
pub parameters: Option<Value>,
633+
pub parameters: Option<LightEffectParameters>,
607634
}
608635

609636
#[derive(Debug, Default, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]

crates/hue/src/api/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ pub use light::{
3838
DimmingDeltaAction, DimmingDeltaUpdate, DimmingUpdate, GamutType, Light, LightAlert,
3939
LightColor, LightDynamics, LightDynamicsStatus, LightDynamicsUpdate, LightEffect,
4040
LightEffectActionUpdate, LightEffectParameters, LightEffectStatus, LightEffectValues,
41-
LightEffects, LightEffectsV2, LightEffectsV2Update, LightFunction, LightGradient,
42-
LightGradientMode, LightGradientPoint, LightGradientUpdate, LightMetadata, LightMode,
43-
LightPowerup, LightPowerupColor, LightPowerupDimming, LightPowerupOn, LightPowerupPreset,
44-
LightProductData, LightSignal, LightSignaling, LightTimedEffect, LightTimedEffects,
45-
LightTimedEffectsUpdate, LightUpdate, MirekSchema, On,
41+
LightEffects, LightEffectsUpdate, LightEffectsV2, LightEffectsV2Update, LightFunction,
42+
LightGradient, LightGradientMode, LightGradientPoint, LightGradientUpdate, LightMetadata,
43+
LightMode, LightPowerup, LightPowerupColor, LightPowerupDimming, LightPowerupOn,
44+
LightPowerupPreset, LightProductData, LightSignal, LightSignaling, LightTimedEffect,
45+
LightTimedEffects, LightTimedEffectsUpdate, LightUpdate, MirekSchema, On,
4646
};
4747
pub use resource::{RType, ResourceLink, ResourceRecord};
4848
pub use room::{Room, RoomArchetype, RoomMetadata, RoomMetadataUpdate, RoomUpdate};

src/backend/z2m/backend_event.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ use uuid::Uuid;
1010
use bifrost_api::backend::BackendRequest;
1111
use hue::api::{
1212
BridgeHome, ColorTemperatureUpdate, DimmingDeltaAction, Entertainment,
13-
EntertainmentConfiguration, GroupedLight, GroupedLightUpdate, Light, LightEffectsV2Update,
14-
LightGradientMode, LightUpdate, RType, Resource, ResourceLink, Room, RoomUpdate, Scene,
15-
SceneActive, SceneStatus, SceneStatusEnum, SceneUpdate, ZigbeeDeviceDiscoveryUpdate,
13+
EntertainmentConfiguration, GroupedLight, GroupedLightUpdate, Light, LightEffect,
14+
LightEffectsV2Update, LightGradientMode, LightUpdate, RType, Resource, ResourceLink, Room,
15+
RoomUpdate, Scene, SceneActive, SceneStatus, SceneStatusEnum, SceneUpdate,
16+
ZigbeeDeviceDiscoveryUpdate,
1617
};
1718
use hue::error::HueError;
1819
use hue::stream::HueStreamLightsV2;
@@ -116,6 +117,22 @@ impl Z2mBackend {
116117
}
117118
})?;
118119
}
120+
// Effect state is currently not retrieved from backend updates either
121+
if let Some(upd) = upd.effects_v2.as_ref() {
122+
lock.update::<Light>(&link.rid, |light| {
123+
if let Some(effects_v2) = &mut light.effects_v2 {
124+
*effects_v2 += upd;
125+
}
126+
if let Some(effects) = &mut light.effects {
127+
let light_effect = upd
128+
.action
129+
.as_ref()
130+
.and_then(|a| a.effect)
131+
.unwrap_or(LightEffect::NoEffect);
132+
effects.status = light_effect;
133+
}
134+
})?;
135+
}
119136
let hue_effects = lock.get::<Light>(link)?.effects.is_some();
120137
drop(lock);
121138

0 commit comments

Comments
 (0)