Skip to content

Commit

Permalink
Merge pull request #5 from StartAutomating/BugFixesAndNewFixtures
Browse files Browse the repository at this point in the history
Bug fixes and new fixtures
  • Loading branch information
StartAutomating authored Mar 14, 2022
2 parents f05ff6a + b52055e commit 85ed730
Show file tree
Hide file tree
Showing 16 changed files with 859 additions and 50 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## 0.2:
Adding: Disconnect-HueBridge, Disconnect-NanoLeaf, Disconnect-Twinkly (#2)
Adding Pixoo commands: Connect/Disconnect/Get/Set-Pixoo (#4)

---
## 0.1:
Initial Release of LightScript: Smarter Lighting with PowerShell

Script your Hue Bridge, NanoLeaf, or Twinkly lights.
2 changes: 1 addition & 1 deletion Connect-NanoLeaf.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

$nanoLeafDataFile = Join-Path $lightScriptRoot ".$($nanoLeafInfo.serialNo).nanoleaf.clixml"

[PSCustomObject]@{IPAddress=$NanoLeafIP;NanoLeafToken=$authToken} |
[PSCustomObject]@{IPAddress=$NanoLeafIP;NanoLeafToken=$authToken;DeviceName=$nanoLeafInfo.DeviceName} |
Export-Clixml -Path $nanoLeafDataFile
}

Expand Down
78 changes: 78 additions & 0 deletions Connect-Pixoo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
function Connect-Pixoo
{
<#
.Synopsis
Connects to a Pixoo
.Description
Connects to a Pixoo over Wifi
.Example
Connect-Pixoo 1.2.3.4 -PassThru
.Link
Get-Pixoo
#>
[OutputType([Nullable], [PSObject])]
param(
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface.
[Parameter(Mandatory,Position=0,ValueFromPipelineByPropertyName)]
[Alias('PixooIPAddress')]
[IPAddress]
$IPAddress,

# If set, will output the connection information.
[switch]
$PassThru
)

begin {
if ($home) {
$lightScriptRoot = Join-Path $home -ChildPath LightScript
}
}

process {
#region Attempt to Contact the Device
$pixooConf = Invoke-RestMethod -Uri "http://$IPAddress/post" -Method POST -Body (
@{
Command="Channel/GetAllConf"
} | ConvertTo-Json
)
#endregion Attempt to Contact the Device


if ($pixooConf) {
$macAddress =
if ($PSVersionTable.Platform -like 'Win*' -or -not $PSVersionTable.Platform) {
Get-NetNeighbor | Where-Object IPAddress -eq $IPAddress | Select-Object -ExpandProperty LinkLayerAddress
} elseif ($ExecutionContext.SessionState.InvokeCommand.GetCommand('nmap','Application')) {
nmap -Pn "$ipAddress" |
Where-Object { $_ -like 'MAC Address:*'} |
ForEach-Object { @($_ -split ' ')[2] }
}

if (-not $macAddress) {
Write-Error "Unable to resolve MAC address for $ipAddress, will not save connection"
return
}
#region Save Device Information
if ($home) {
if (-not (Test-Path $lightScriptRoot)) {
$createLightScriptDir = New-Item -ItemType Directory -Path $lightScriptRoot
if (-not $createLightScriptDir) { return }
}


$pixooDataFile = Join-Path $lightScriptRoot ".$($macAddress).pixoo.clixml"
$pixooConf.pstypenames.clear()
$pixooConf.pstypenames.add('Pixoo')
$pixooConf |
Add-Member NoteProperty IPAddress $IPAddress -Force -PassThru |
Add-Member NoteProperty MACAddress $macAddress -Force -PassThru |
Export-Clixml -Path $pixooDataFile
}
#endregion Save Device Information
if ($PassThru) {
$pixooConf
}
}
}
}
49 changes: 49 additions & 0 deletions Disconnect-HueBridge.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function Disconnect-HueBridge
{
<#
.Synopsis
Disconnects a Hue Bridge.
.Description
Disconnects a new Hue Bridge and removes connection information.
.Link
Connect-HueBridge
.Example
Disconnect-HueBridge -HueBridgeIP 1.2.3.4
#>
[CmdletBinding(DefaultParameterSetName='ExistingConnection')]
[OutputType([Nullable], [PSObject])]
param(
# The IP Address of the Hue Bridge
[Parameter(Mandatory,ParameterSetName='NewConnection',ValueFromPipelineByPropertyName)]
[Alias('IPAddress')]
[IPAddress]
$HueBridgeIP
)

begin {
if ($home) {
$lightScriptRoot = Join-Path $home -ChildPath LightScript
}
}

process {
if (-not (Test-Path $lightScriptRoot)) {
$createLightScriptDir = New-Item -ItemType Directory -Path $lightScriptRoot
if (-not $createLightScriptDir) { return }
}


#region Find and Remove Connection
$files = @(Get-ChildItem -Path $lightScriptRoot -ErrorAction SilentlyContinue -Filter *.huebridge.clixml -Force)
$files |
ForEach-Object {
$file = $_
$fileInfo = Import-Clixml -LiteralPath $file.FullName
if ($fileInfo.IPAddress -eq $HueBridgeIP -and $PSCmdlet.ShouldProcess("Remove $($fileInfo.DeviceID)")) {
Remove-Item -LiteralPath $file.FullName -Force
}
}
#endregion Find and Remove Connection
}
}

49 changes: 49 additions & 0 deletions Disconnect-NanoLeaf.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function Disconnect-Nanoleaf
{
<#
.Synopsis
Disconnects a new Nanoleaf controller.
.Description
Disconnnects a new Nanoleaf controller and removes connection information.
.Example
Disconnect-NanoLeaf -IPAddress 1.2.3.4
.Link
Connect-NanoLeaf
#>
[CmdletBinding(SupportsShouldProcess,ConfirmImpact='High')]
[OutputType([Nullable], [PSObject])]
param(
# The IP Address of the Nanoleaf
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[Alias('IPAddress')]
[IPAddress]
$NanoLeafIP
)

begin {
if ($home) {
$lightScriptRoot = Join-Path $home -ChildPath LightScript
}
}

process {
if (-not (Test-Path $lightScriptRoot)) {
$createLightScriptDir = New-Item -ItemType Directory -Path $lightScriptRoot
if (-not $createLightScriptDir) { return }
}

#region Find and Remove Connection
$files = @(Get-ChildItem -Path $lightScriptRoot -ErrorAction SilentlyContinue -Filter *.nanoleaf.clixml -Force)
$files |
ForEach-Object {
$file = $_
$fileInfo = Import-Clixml -LiteralPath $file.FullName
if ($fileInfo.IPAddress -eq $NanoLeafIP -and $PSCmdlet.ShouldProcess("Remove $($fileInfo.DeviceName)")) {
Remove-Item -LiteralPath $file.FullName -Force
}
}


#endregion Find and Remove Connection
}
}
40 changes: 40 additions & 0 deletions Disconnect-Pixoo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function Disconnect-Pixoo
{
<#
.Synopsis
Disconnects a Pixoo
.Description
Disconnects a Pixoo, removing stored device info
.Example
Disconnect-Pixoo 1.2.3.4
.Link
Connect-Pixoo
#>
[OutputType([Nullable], [PSObject])]
[CmdletBinding(SupportsShouldProcess,ConfirmImpact='High')]
param(
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface.
[Parameter(Mandatory,Position=0,ValueFromPipelineByPropertyName)]
[Alias('PixooIPAddress')]
[IPAddress]
$IPAddress
)

begin {
if ($home) {
$lightScriptRoot = Join-Path $home -ChildPath LightScript
}
}

process {
@(Get-ChildItem -Filter *.pixoo.clixml -Path $lightScriptRoot) |
Foreach-Object {
$file = $_
$fileInfo = Import-Clixml -LiteralPath $file.FullName
if ($fileInfo.IPAddress -eq $IPAddress -and $PSCmdlet.ShouldProcess("Remove $($fileInfo.MACAddress)")) {
Remove-Item -LiteralPath $file.FullName -Force
}
}
}
}

49 changes: 49 additions & 0 deletions Disconnect-Twinkly.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function Disconnect-Twinkly
{
<#
.Synopsis
Disconnects a Twinkly light controller.
.Description
Disconnects a Twinkly light controller
.Example
Disconnect-Twinkly 192.168.0.144
.Link
Connect-Twinkly
#>
[OutputType([Nullable], [PSObject])]
param(
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface.
[Parameter(Mandatory,Position=0,ValueFromPipelineByPropertyName)]
[Alias('TwinklyIPAddress')]
[IPAddress]
$IPAddress,

# If set, will output the connection information.
[switch]
$PassThru
)

begin {
if ($home) {
$lightScriptRoot = Join-Path $home -ChildPath LightScript
}
}

process {
if (-not (Test-Path $lightScriptRoot)) {
$createLightScriptDir = New-Item -ItemType Directory -Path $lightScriptRoot
if (-not $createLightScriptDir) { return }
}

@(Get-ChildItem -Filter *.twinkly.clixml -Path $lightScriptRoot) |
Foreach-Object {
$file = $_
$fileInfo = Import-Clixml -LiteralPath $file.FullName
if ($fileInfo.IPAddress -eq $IPAddress -and $PSCmdlet.ShouldProcess("Remove $($fileInfo.uuid)")) {
Remove-Item -LiteralPath $file.FullName -Force
}
}

}
}

3 changes: 3 additions & 0 deletions Formatting/Pixoo.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Write-formatView -TypeName Pixoo -Property IPAddress, MACAddress, Brightness

Write-formatView -TypeName Pixoo -Property RotationFlag, GyrateAngle, MirrorFlag -Name Orientation
58 changes: 58 additions & 0 deletions Get-Pixoo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
function Get-Pixoo
{
<#
.Synopsis
Gets Pixoo Devices
.Description
Gets saved Pixoo Devices
.Example
Get-Pixoo
.LINK
Connect-Pixoo
.LINK
Set-Pixoo
#>
[CmdletBinding(DefaultParameterSetName="ListDevices")]
param(
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('PixooIPAddress')]
[IPAddress[]]
$IPAddress
)

begin {
if (-not $script:PixooCache) {
$script:PixooCache = @{}
}
if ($home) {
$lightScriptRoot = Join-Path $home -ChildPath LightScript
}
}

process {
#region Default to All Devices
if (-not $IPAddress) { # If no -IPAddress was passed
if ($home) {
# Read all .twinkly.clixml files beneath your LightScript directory.
Get-ChildItem -Path $lightScriptRoot -ErrorAction SilentlyContinue -Filter *.pixoo.clixml -Force |
Import-Clixml |
ForEach-Object {
if (-not $_) { return }
$pixooConnection = $_
$script:PixooCache["$($pixooConnection.IPAddress)"] = $pixooConnection
}

$IPAddress = $script:PixooCache.Keys # The keys of the device cache become the -IPAddress.
}
if (-not $IPAddress) { # If we still have no -IPAddress
return # return.
}
}
#endregion Default to All Devices

if ($PSCmdlet.ParameterSetName -eq 'ListDevices') {
return $script:PixooCache.Values
}
}
}
Loading

0 comments on commit 85ed730

Please sign in to comment.