Skip to content

Commit

Permalink
Merge pull request #22 from StartAutomating/LightScriptingHueFixes
Browse files Browse the repository at this point in the history
Light scripting hue fixes
  • Loading branch information
StartAutomating authored Jul 24, 2022
2 parents a8a439d + b30eaa1 commit 6f3dd18
Show file tree
Hide file tree
Showing 22 changed files with 378 additions and 49 deletions.
17 changes: 0 additions & 17 deletions .github/workflows/RunEZOut.yml

This file was deleted.

18 changes: 16 additions & 2 deletions .github/workflows/TestAndPublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,9 @@ jobs:
if (-not $imported) { return }
$foundModule = try { Find-Module -Name $imported.Name -ErrorAction SilentlyContinue } catch {}
$foundModule = try { Find-Module -Name $imported.Name -ErrorAction SilentlyContinue} catch {}
if ($foundModule -and $foundModule.Version -ge $imported.Version) {
if ($foundModule -and (([Version]$foundModule.Version) -ge ([Version]$imported.Version))) {
"::warning::Gallery Version of $moduleName is more recent ($($foundModule.Version) >= $($imported.Version))" | Out-Host
} else {
Expand Down Expand Up @@ -485,4 +485,18 @@ jobs:
}
}
} @Parameters
BuildModule:
runs-on: ubuntu-latest
if: ${{ success() }}
steps:
- name: Check out repository
uses: actions/checkout@v2
- name: BuildPipeScript
uses: StartAutomating/PipeScript@main
- name: UseEZOut
uses: StartAutomating/EZOut@master
- name: UsePiecemeal
uses: StartAutomating/Piecemeal@main
- name: UseHelpOut
uses: StartAutomating/HelpOut@master

14 changes: 0 additions & 14 deletions .github/workflows/UpdateDocs.yml

This file was deleted.

122 changes: 122 additions & 0 deletions Add-HueLight.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
function Add-HueLight {

[CmdletBinding(SupportsShouldProcess)]



param(
# One or more Device Identifiers (serial numbers ).
# Use this parameter when adding lights that have already been assigned to another bridge.
[Alias('SerialNumber')]
[string[]]
$DeviceID
)
begin {
$hueBridge = Get-HueBridge
$invokeParams = @{}
$invokeParams.IPAddress = $hueBridge.IPAddress
$invokeParams.HueUserName = $hueBridge.HueUserName

function ConvertRestInput {
param([Collections.IDictionary]$RestInput = @{}, [switch]$ToQueryString)
foreach ($ri in @($RestInput.GetEnumerator())) {

$restParameterValue = $ri.Value
$restParameterValue =
if ($restParameterValue -is [DateTime]) {
$restParameterValue.Tostring('o')
}
elseif ($restParameterValue -is [switch]) {
$restParameterValue -as [bool]
}
else {
if ($ToQueryString -and
$restParameterValue -is [Array] -and
$JoinQueryValue) {
$restParameterValue -join $JoinQueryValue
} else {
$restParameterValue
}
}

$RestInput[$ri.Key] = $restParameterValue
}
$RestInput

}

}
process {
$InvokeCommand = 'Send-HueBridge'
$invokerCommandinfo =
$ExecutionContext.SessionState.InvokeCommand.GetCommand('Send-HueBridge', 'All')
$method = 'POST'
$contentType = 'application/json'
$bodyParameterNames = @('DeviceID')
$queryParameterNames = @('')
$joinQueryValue = ''
$uriParameterNames = @('')
$endpoints = @("lights")
$ForEachOutput = {

}
if ($ForEachOutput -match '^\s{0,}$') {
$ForEachOutput = $null
}
if (-not $invokerCommandinfo) {
Write-Error "Unable to find invoker '$InvokeCommand'"
return
}
if (-not $psParameterSet) { $psParameterSet = $psCmdlet.ParameterSetName}
if ($psParameterSet -eq '__AllParameterSets') { $psParameterSet = $endpoints[0]}
$uri = $endpoints[0]

$invokeSplat = @{}
$invokeSplat.Uri = $uri
if ($method) {
$invokeSplat.Method = $method
}
if ($ContentType -and $invokerCommandInfo.Parameters.ContentType) {
$invokeSplat.ContentType = $ContentType
}
if ($InvokeParams -and $InvokeParams -is [Collections.IDictionary]) {
$invokeSplat += $InvokeParams
}
$completeBody = [Ordered]@{}
foreach ($bodyParameterName in $bodyParameterNames) {
if ($bodyParameterName) {
if ($PSBoundParameters.ContainsKey($bodyParameterName)) {
$completeBody[$bodyParameterName] = $PSBoundParameters[$bodyParameterName]
} else {
$bodyDefault = $ExecutionContext.SessionState.PSVariable.Get($bodyParameterName).Value
if ($null -ne $bodyDefault) {
$completeBody[$bodyParameterName] = $bodyDefault
}
}
}
}
$completeBody = ConvertRestInput $completeBody
$bodyContent =
if ($ContentType -match 'x-www-form-urlencoded') {
@(foreach ($bodyPart in $completeBody.GetEnumerator()) {
"$($bodyPart.Key.ToString().ToLower())=$([Web.HttpUtility]::UrlEncode($bodyPart.Value))"
}) -join '&'
} elseif ($ContentType -match 'json' -or -not $ContentType) {
ConvertTo-Json $completeBody
}
if ($bodyContent -and $method -ne 'get') {
$invokeSplat.Body = $bodyContent
}
Write-Verbose "$($invokeSplat.Uri)"
if ($ForEachOutput) {
if ($ForEachOutput.Ast.ProcessBlock) {
& $invokerCommandinfo @invokeSplat | & $ForEachOutput
} else {
& $invokerCommandinfo @invokeSplat | ForEach-Object -Process $ForEachOutput
}
} else {
& $invokerCommandinfo @invokeSplat
}
}
}

27 changes: 27 additions & 0 deletions Add-HueLight.ps1.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function Add-HueLight
{
[Rest("lights",
Invoker="Send-HueBridge",
Method='POST',
ContentType='application/json',
BodyParameter={
{
param(
# One or more Device Identifiers (serial numbers ).
# Use this parameter when adding lights that have already been assigned to another bridge.
[Alias('SerialNumber')]
[string[]]
$DeviceID
)
}
})]
[CmdletBinding(SupportsShouldProcess)]
param()

begin {
$hueBridge = Get-HueBridge
$invokeParams = @{}
$invokeParams.IPAddress = $hueBridge.IPAddress
$invokeParams.HueUserName = $hueBridge.HueUserName
}
}
Binary file modified Add-HueRoom.ps1
Binary file not shown.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.2.4
* Adding Add-HueLight (#18)
* Adding Get-HueLight -New (#21)
* Fixing Rename-HueLight (#19)
* Building LightScript with PipeScript (#20)
---

## 0.2.3
* Set-NanoLeaf: Fixing #17
* Send-HueBridge: Adding logging
Expand Down
Binary file modified Get-HueLight.ps1
Binary file not shown.
20 changes: 20 additions & 0 deletions LightScript.ps1.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[include("*-*.ps1")]$PSScriptRoot

if ($home) {
$Script:KnownResources =
@(
Get-HueLight
Get-HueRoom
Get-NanoLeaf
)

foreach ($resource in $Script:KnownResources) {
if ($resource.pstypenames -contains 'Hue.Light') {
Set-Alias -Name ($resource.Name -replace '\s') -Value Set-HueLight
}
if ($resource.pstypenames -contains 'Hue.Group' -or
$resource.pstypenames -contains 'Hue.LightGroup') {
Set-Alias -Name ($resource.Name -replace '\s') -Value Set-HueLight
}
}
}
9 changes: 8 additions & 1 deletion LightScript.psd1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@{
ModuleVersion = '0.2.3'
ModuleVersion = '0.2.4'
RootModule = 'LightScript.psm1'
Description = 'Smarter Lighting with PowerShell'
FormatsToProcess = 'LightScript.format.ps1xml'
Expand All @@ -14,6 +14,13 @@
LicenseURI = 'https://github.com/StartAutomating/LightScript/blob/main/LICENSE'
IconURI = 'https://github.com/StartAutomating/LightScript/blob/main/Assets/LightScript.png'
ReleaseNotes = @'
## 0.2.4
* Adding Add-HueLight (#18)
* Adding Get-HueLight -New (#21)
* Fixing Rename-HueLight (#19)
* Building LightScript with PipeScript (#20)
---
## 0.2.3
* Set-NanoLeaf: Fixing #17
* Send-HueBridge: Adding logging
Expand Down
Binary file modified LightScript.psm1
Binary file not shown.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ Add-HueSchedule -In '00:10:00' -Command (Set-HueLight -Alert lselect -OutputInpu
~~~


You can also add new lights to a bridge:

~~~PowerShell
Add-HueLight
Get-HueLight -New
~~~

### Hue Devices and Smart Aliases

Set-HueLight supports smart aliasing.
Expand Down
Binary file modified Rename-HueLight.ps1
Binary file not shown.
16 changes: 9 additions & 7 deletions Send-HueBridge.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function Send-HueBridge
Get-HueBridge
#>
[OutputType([PSObject])]
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Low')]
[CmdletBinding(SupportsShouldProcess,ConfirmImpact='Low')]
param(
# The IP address of the hue bridge.
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
Expand All @@ -26,14 +26,16 @@ function Send-HueBridge

# The command being sent to the bridge. This is a partial URI.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('Uri','Url')]
[string]
$Command,

# The data being sent to the Hue Bridge.
# If this data is not a string, it will be converted to JSON.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('Data')]
[PSObject]
$Data,
$Body,

# The HTTP method. By default, Get.
[Parameter(ValueFromPipelineByPropertyName)]
Expand Down Expand Up @@ -67,17 +69,17 @@ function Send-HueBridge
method = $Method
}

if ($Data) {
if ($data -is [string]){
$splat.body = $data
if ($Body) {
if ($Body -is [string]){
$splat.body = $Body
} else {
$splat.body = ConvertTo-Json -Compress -Depth 100 -InputObject $Data
$splat.body = ConvertTo-Json -Compress -Depth 100 -InputObject $Body
}
}
#endregion Construct the Message Body

if ($OutputInput -or $WhatIfPreference) { # If -OutputInput or -WhatIf was passed
if ($Data) { $splat.body = $Data }
if ($Body) { $splat.body = $Body }
$splat.address = ([uri]$splat.uri).LocalPath # Make an address property with a relative URI
$splat.Remove('uri') # and remove URI
$splat
Expand Down
2 changes: 2 additions & 0 deletions Set-NanoLeaf.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
delayTime = 1
transTime = 1
}
.Example
Set-NanoLeaf -Palette "#123456", "#654321", "#abcdef", "#fedcba" -PluginType Rhythm -PluginName 'Dancing Duo'
.Link
Get-NanoLeaf
.Link
Expand Down
54 changes: 54 additions & 0 deletions docs/Add-HueLight.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

Add-HueLight
------------
### Synopsis

Add-HueLight [[-DeviceID] <string[]>] [-WhatIf] [-Confirm] [<CommonParameters>]


---
### Description
---
### Parameters
#### **Confirm**
-Confirm is an automatic variable that is created when a command has ```[CmdletBinding(SupportsShouldProcess)]```.
-Confirm is used to -Confirm each operation.

If you pass ```-Confirm:$false``` you will not be prompted.


If the command sets a ```[ConfirmImpact("Medium")]``` which is lower than ```$confirmImpactPreference```, you will not be prompted unless -Confirm is passed.

#### **DeviceID**

|Type |Requried|Postion|PipelineInput|
|----------------|--------|-------|-------------|
|```[string[]]```|false |0 |false |
---
#### **WhatIf**
-WhatIf is an automatic variable that is created when a command has ```[CmdletBinding(SupportsShouldProcess)]```.
-WhatIf is used to see what would happen, or return operations without executing them
---
### Inputs
None


---
### Outputs
System.Object


---
### Syntax
```PowerShell
syntaxItem
```
```PowerShell
----------
```
```PowerShell
{@{name=Add-HueLight; CommonParameters=True; parameter=System.Object[]}}
```
---


Loading

0 comments on commit 6f3dd18

Please sign in to comment.