michielpost / Q42.HueApi

C# helper library to talk to the Philips Hue bridge
MIT License
411 stars 114 forks source link

Switching lights off after Entertainment Stream #234

Closed beyondwatts closed 3 years ago

beyondwatts commented 3 years ago

Great library! I'm successfull using an entertainment stream to control multiple lights. What is the recommended way of turning the lights off when the entertainment stream has finished? Is there a way to do this through the stream or should I create a new connection with a standard client? Thanks!

michielpost commented 3 years ago

That's great to hear!

If you're using the StreamingHueClient with AutoUpdate, you should give it a CancellationToken, example:

var cts = new CancellationTokenSource();
client.AutoUpdate(stream, cts.Token, 50, onlySendDirtyStates: false);
cts.Cancel();

Call client.Close(); to manually close down the streaming connection to the Hue Bridge. The streaming connection is automaticallys stopped by the bridge if it does not receive any updates.

When streaming is stopped, the lights will keep their last streaming state. So if your last entertainment state was all lights off, they will stay off.

The streaming client also has access to the standard client. So after closing the entertainment stream, you can also do this:

 await streamingClient.LocalHueClient.SendGroupCommandAsync(new LightCommand().TurnOff());
beyondwatts commented 3 years ago

Thanks Michael. Closing the stream and sending the TurnOff command the local client works as you have described