Skip to content

Commit

Permalink
Adding Rename-HueSensor (Fixes #26). Adding [Alias('ID')] to -OldName (
Browse files Browse the repository at this point in the history
…Fixes #27)
  • Loading branch information
James Brundage committed Oct 3, 2022
1 parent dd6f320 commit d363a01
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Rename-HueLight.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
55 changes: 55 additions & 0 deletions Rename-HueSensor.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit d363a01

Please sign in to comment.