/// <summary> /// /// </summary> /// <param name="client"></param> /// <param name="lightsToSet"></param> /// <param name="colorTemperature"></param> internal async Task ModifyFluxSwitchScenes( HueClient client, IEnumerable <Light> lights, Primitives.ColorTemperature colorTemperature, Primitives.Brightness newBrightness) { Dictionary <string, int> scenesModified = new Dictionary <string, int>(); IEnumerable <Scene> scenes = null; try { scenes = await client.GetScenesAsync(); } catch (Exception e) { Log.Debug($"'{nameof(ModifyFluxSwitchScenes)}' exception attempting to get all scenes from client. '{e.Message}' '{e.InnerException}'."); return; } scenes = scenes.Where(x => x.Name.ToLowerInvariant().Contains("flux") && (!x.Recycle.HasValue || !x.Recycle.Value)); // Scenes to update foreach (Scene sceneId in scenes) { Scene scene = null; try { scene = await client.GetSceneAsync(sceneId.Id); } catch (Exception e) { Log.Debug($"'{nameof(ModifyFluxSwitchScenes)}' exception attempting to get scene info from client. '{e.Message}' '{e.InnerException}'."); } if (null != scene) { Dictionary <string, State> lightStates = scene.LightStates; // Update scenes to use the new color temperature and brightness foreach (string lightId in scene.LightStates.Keys) { Light light = lights.Get(lightId); if (lightStates[lightId].On && light.IsFluxControlled()) { LightCommand lightCommand = new LightCommand() { On = true, }; if (light.ControlTemperature()) { lightCommand.ColorTemperature = colorTemperature.NormalizeColorForAllowedColorRange(light.GetLightType()); } if (light.ControlBrightness() && LastBrightnesses.Contains(lightStates[lightId].Brightness)) { lightCommand.Brightness = newBrightness; } else { lightCommand.Brightness = lightStates[lightId].Brightness; } try { if ((lightCommand.ColorTemperature.HasValue && lightStates[lightId].ColorTemperature != lightCommand.ColorTemperature.Value) || (lightCommand.Brightness.HasValue && lightStates[lightId].Brightness != lightCommand.Brightness.Value)) { HueResults result = await client.ModifySceneAsync(sceneId.Id, lightId, lightCommand); // Increment the number of lights modified per scene if (scenesModified.ContainsKey(scene.Name)) { scenesModified[scene.Name]++; } else { scenesModified.Add(scene.Name, 1); } // Limit hub requests await Task.Delay(TimeSpan.FromMilliseconds(15)); } } catch (Exception exception) { Log.Error($"Exception: '{nameof(ModifyFluxSwitchScenes)}' unable to modify scene ID '{sceneId.Id}' named '{scene.Name}' for light id '{lightId}' with color temperature set to '{lightCommand.ColorTemperature}' and brightness '{lightCommand.Brightness}'. {exception.Message}"); // Limit hub requests await Task.Delay(TimeSpan.FromMilliseconds(100)); } } } } } Log.Info($"'{nameof(ModifyFluxSwitchScenes)}' modified '{scenesModified.Count()}' scenes to color temperature '{colorTemperature}' and brightness '{newBrightness.ToString()}'."); }