public void LightSwitch_FromDynamic_ShouldReturnExpectedResult(InstalledAppInstance installedApp) { var expectedResult = new LightSwitch() { Id = Guid.NewGuid().ToString(), Label = "MyDevice", CurrentState = SwitchState.Off }; var deviceJson = $@"{{ ""deviceId"": ""{expectedResult.Id}"", ""name"": ""NAME"", ""label"": ""{expectedResult.Label}"", ""locationId"": ""{installedApp.InstalledLocation.Id}"" }}"; var statusJson = $@"{{ ""deviceId"": ""{expectedResult.Id}"", ""name"": ""NAME"", ""label"": ""{expectedResult.Label}"", ""locationId"": ""{installedApp.InstalledLocation.Id}"", ""components"" : {{ ""main"": {{ ""switch"": {{ ""switch"": {{ ""value"": ""off"" }} }} }} }} }}"; dynamic device = JObject.Parse(deviceJson); dynamic status = JObject.Parse(statusJson); var result = LightSwitch.SwitchFromDynamic(device, status); Assert.Equal(expectedResult, result); expectedResult.CurrentState = SwitchState.On; statusJson = $@"{{ ""deviceId"": ""{expectedResult.Id}"", ""name"": ""NAME"", ""label"": ""{expectedResult.Label}"", ""locationId"": ""{installedApp.InstalledLocation.Id}"", ""components"" : {{ ""main"": {{ ""switch"": {{ ""switch"": {{ ""value"": ""on"" }} }} }} }} }}"; status = JObject.Parse(statusJson); result = LightSwitch.SwitchFromDynamic(device, status); Assert.Equal(expectedResult, result); expectedResult.CurrentState = SwitchState.Unknown; statusJson = $@"{{ ""deviceId"": ""{expectedResult.Id}"", ""name"": ""NAME"", ""label"": ""{expectedResult.Label}"", ""locationId"": ""{installedApp.InstalledLocation.Id}"", ""components"" : {{ ""main"": {{ ""switch"": {{ ""switch"": {{ ""value"": ""unknown"" }} }} }} }} }}"; status = JObject.Parse(statusJson); result = LightSwitch.SwitchFromDynamic(device, status); Assert.Equal(expectedResult, result); _ = expectedResult.GetHashCode(); _ = expectedResult.ToJson(); }
#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..."); }