private async Task SubscribeToDeviceEventAsync(InstalledAppInstance installedApp, dynamic deviceConfig) { _ = installedApp ?? throw new ArgumentNullException(nameof(installedApp)); _ = deviceConfig ?? throw new ArgumentNullException(nameof(deviceConfig)); var resp = await SmartThingsAPIHelper.SubscribeToDeviceEventAsync( installedApp, deviceConfig); var body = await resp.Content.ReadAsStringAsync(); dynamic subscriptionResp = JObject.Parse(body); _ = subscriptionResp.id ?? throw new InvalidOperationException("subscriptionResp.id is null!"); }
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously private async Task LoadLightSwitchesAsync(InstalledAppInstance installedApp, #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously MyState state, dynamic data, bool shouldSubscribeToEvents = true) { Logger.LogInformation("Loading lightSwitches..."); state.LightSwitches = new List <LightSwitch>(); _ = installedApp ?? throw new ArgumentNullException(nameof(installedApp)); _ = state ?? throw new ArgumentNullException(nameof(state)); _ = data ?? throw new ArgumentNullException(nameof(data)); var lightSwitches = data.installedApp.config.switches; if (lightSwitches != null) { var index = 0; foreach (var device in lightSwitches) { dynamic deviceConfig = device.deviceConfig; var deviceId = deviceConfig.deviceId.Value; deviceConfig.capability = "switch"; deviceConfig.attribute = "switch"; deviceConfig.stateChangeOnly = true; deviceConfig.value = "*"; deviceConfig.subscriptionName = $"MySwitches{index}"; index++; var deviceTasks = new Task <dynamic>[] { SmartThingsAPIHelper.GetDeviceDetailsAsync( installedApp, deviceId), SmartThingsAPIHelper.GetDeviceStatusAsync( installedApp, deviceId) }; Task.WaitAll(deviceTasks); var ls = LightSwitch.SwitchFromDynamic( deviceTasks[0].Result, deviceTasks[1].Result); state.LightSwitches.Add(ls); if (shouldSubscribeToEvents) { #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed Task.Run(() => SubscribeToDeviceEventAsync(installedApp, deviceConfig).ConfigureAwait(false)); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed } } } Logger.LogInformation($"Loaded {state.LightSwitches.Count} lightSwitches..."); }