From d363a017c8adddaab83577a21afee5526b0f5c25 Mon Sep 17 00:00:00 2001 From: James Brundage <@github.com> Date: Sun, 2 Oct 2022 22:27:25 -0700 Subject: [PATCH] Adding Rename-HueSensor (Fixes #26). Adding [Alias('ID')] to -OldName (Fixes #27) --- Rename-HueLight.ps1 | 5 ++-- Rename-HueSensor.ps1 | 55 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 Rename-HueSensor.ps1 diff --git a/Rename-HueLight.ps1 b/Rename-HueLight.ps1 index e0a90f1..45b5cc1 100644 --- a/Rename-HueLight.ps1 +++ b/Rename-HueLight.ps1 @@ -15,12 +15,13 @@ function Rename-HueLight [OutputType([PSObject])] param( # The old name of the light. This can be a wildcard or regular expression. - [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)] + [Parameter(Mandatory,ValueFromPipelineByPropertyName)] + [Alias('ID')] [string] $OldName, # The new name of the light. A number sign will be replaced with the match number. - [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)] + [Parameter(Mandatory,ValueFromPipelineByPropertyName)] [string] $NewName ) diff --git a/Rename-HueSensor.ps1 b/Rename-HueSensor.ps1 new file mode 100644 index 0000000..b654070 --- /dev/null +++ b/Rename-HueSensor.ps1 @@ -0,0 +1,55 @@ +function Rename-HueSensor +{ + <# + .Synopsis + Renames Hue Sensors + .Description + Renames one or more Hue Sensors. + .Example + Rename-HueSensor + .Link + Get-HueBridge + .Link + Get-HueSensor + #> + [OutputType([PSObject])] + param( + # The old name of the Sensor. This can be a wildcard or regular expression. + [Parameter(Mandatory,ValueFromPipelineByPropertyName)] + [Alias('ID')] + [string] + $OldName, + + # The new name of the Sensor. A number sign will be replaced with the match number. + [Parameter(Mandatory,ValueFromPipelineByPropertyName)] + [string] + $NewName + ) + + begin { + $Sensors = Get-HueSensor + $bridges = Get-HueBridge + } + + process { + $Sensors | + Where-Object { + #region Find matching Sensors + ($_.Id -eq $oldName) -or + ($_.Name -like $OldName) -or + ($oldName -as [regex] -and $_.Name -match $oldName) + #endregion Find matching Sensors + } | + ForEach-Object -Begin { + $matchCount = 0 + } -Process { + #region Rename the Sensors + $MatchCount++ + $realNewName = $NewName -replace '#', $MatchCount + $SensorToRename = $_ + $bridges | Send-HueBridge -Command "sensors/$($SensorToRename.id)" -Method PUT -Data @{name=$realNewName} + #endregion Rename the Sensors + } + } +} +