|
| 1 | +function Add-HueLight { |
| 2 | + |
| 3 | + [CmdletBinding(SupportsShouldProcess)] |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + param( |
| 8 | +# One or more Device Identifiers (serial numbers ). |
| 9 | + # Use this parameter when adding lights that have already been assigned to another bridge. |
| 10 | + [Alias('SerialNumber')] |
| 11 | + [string[]] |
| 12 | + $DeviceID |
| 13 | + ) |
| 14 | + begin { |
| 15 | + $hueBridge = Get-HueBridge |
| 16 | + $invokeParams = @{} |
| 17 | + $invokeParams.IPAddress = $hueBridge.IPAddress |
| 18 | + $invokeParams.HueUserName = $hueBridge.HueUserName |
| 19 | + |
| 20 | + function ConvertRestInput { |
| 21 | + param([Collections.IDictionary]$RestInput = @{}, [switch]$ToQueryString) |
| 22 | + foreach ($ri in @($RestInput.GetEnumerator())) { |
| 23 | + |
| 24 | + $restParameterValue = $ri.Value |
| 25 | + $restParameterValue = |
| 26 | + if ($restParameterValue -is [DateTime]) { |
| 27 | + $restParameterValue.Tostring('o') |
| 28 | + } |
| 29 | + elseif ($restParameterValue -is [switch]) { |
| 30 | + $restParameterValue -as [bool] |
| 31 | + } |
| 32 | + else { |
| 33 | + if ($ToQueryString -and |
| 34 | + $restParameterValue -is [Array] -and |
| 35 | + $JoinQueryValue) { |
| 36 | + $restParameterValue -join $JoinQueryValue |
| 37 | + } else { |
| 38 | + $restParameterValue |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + $RestInput[$ri.Key] = $restParameterValue |
| 43 | + } |
| 44 | + $RestInput |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | +process { |
| 50 | + $InvokeCommand = 'Send-HueBridge' |
| 51 | + $invokerCommandinfo = |
| 52 | + $ExecutionContext.SessionState.InvokeCommand.GetCommand('Send-HueBridge', 'All') |
| 53 | + $method = 'POST' |
| 54 | + $contentType = 'application/json' |
| 55 | + $bodyParameterNames = @('DeviceID') |
| 56 | + $queryParameterNames = @('') |
| 57 | + $joinQueryValue = '' |
| 58 | + $uriParameterNames = @('') |
| 59 | + $endpoints = @("lights") |
| 60 | + $ForEachOutput = { |
| 61 | + |
| 62 | + } |
| 63 | + if ($ForEachOutput -match '^\s{0,}$') { |
| 64 | + $ForEachOutput = $null |
| 65 | + } |
| 66 | + if (-not $invokerCommandinfo) { |
| 67 | + Write-Error "Unable to find invoker '$InvokeCommand'" |
| 68 | + return |
| 69 | + } |
| 70 | + if (-not $psParameterSet) { $psParameterSet = $psCmdlet.ParameterSetName} |
| 71 | + if ($psParameterSet -eq '__AllParameterSets') { $psParameterSet = $endpoints[0]} |
| 72 | + $uri = $endpoints[0] |
| 73 | + |
| 74 | + $invokeSplat = @{} |
| 75 | + $invokeSplat.Uri = $uri |
| 76 | + if ($method) { |
| 77 | + $invokeSplat.Method = $method |
| 78 | + } |
| 79 | + if ($ContentType -and $invokerCommandInfo.Parameters.ContentType) { |
| 80 | + $invokeSplat.ContentType = $ContentType |
| 81 | + } |
| 82 | + if ($InvokeParams -and $InvokeParams -is [Collections.IDictionary]) { |
| 83 | + $invokeSplat += $InvokeParams |
| 84 | + } |
| 85 | + $completeBody = [Ordered]@{} |
| 86 | + foreach ($bodyParameterName in $bodyParameterNames) { |
| 87 | + if ($bodyParameterName) { |
| 88 | + if ($PSBoundParameters.ContainsKey($bodyParameterName)) { |
| 89 | + $completeBody[$bodyParameterName] = $PSBoundParameters[$bodyParameterName] |
| 90 | + } else { |
| 91 | + $bodyDefault = $ExecutionContext.SessionState.PSVariable.Get($bodyParameterName).Value |
| 92 | + if ($null -ne $bodyDefault) { |
| 93 | + $completeBody[$bodyParameterName] = $bodyDefault |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + $completeBody = ConvertRestInput $completeBody |
| 99 | + $bodyContent = |
| 100 | + if ($ContentType -match 'x-www-form-urlencoded') { |
| 101 | + @(foreach ($bodyPart in $completeBody.GetEnumerator()) { |
| 102 | + "$($bodyPart.Key.ToString().ToLower())=$([Web.HttpUtility]::UrlEncode($bodyPart.Value))" |
| 103 | + }) -join '&' |
| 104 | + } elseif ($ContentType -match 'json' -or -not $ContentType) { |
| 105 | + ConvertTo-Json $completeBody |
| 106 | + } |
| 107 | + if ($bodyContent -and $method -ne 'get') { |
| 108 | + $invokeSplat.Body = $bodyContent |
| 109 | + } |
| 110 | + Write-Verbose "$($invokeSplat.Uri)" |
| 111 | + if ($ForEachOutput) { |
| 112 | + if ($ForEachOutput.Ast.ProcessBlock) { |
| 113 | + & $invokerCommandinfo @invokeSplat | & $ForEachOutput |
| 114 | + } else { |
| 115 | + & $invokerCommandinfo @invokeSplat | ForEach-Object -Process $ForEachOutput |
| 116 | + } |
| 117 | + } else { |
| 118 | + & $invokerCommandinfo @invokeSplat |
| 119 | + } |
| 120 | +} |
| 121 | +} |
| 122 | + |
0 commit comments