functions/public/Get-KlippyPowerDevice.ps1
|
function Get-KlippyPowerDevice { <# .SYNOPSIS Gets power devices from a Klipper printer. .DESCRIPTION Retrieves configured power devices (smart plugs, relays, GPIO, etc.) and their current state. .PARAMETER Id The unique identifier of the printer. .PARAMETER PrinterName The friendly name of the printer. .PARAMETER InputObject A printer object from pipeline input. .PARAMETER DeviceName Filter by specific device name. .EXAMPLE Get-KlippyPowerDevice Gets all power devices from the default printer. .EXAMPLE Get-KlippyPowerDevice -DeviceName "printer_power" Gets a specific power device. .EXAMPLE Get-KlippyPrinter | Get-KlippyPowerDevice Gets power devices from all registered printers. .OUTPUTS KlippyCLI.PowerDevice objects. #> [CmdletBinding(DefaultParameterSetName = 'Default')] [OutputType([PSCustomObject])] param( [Parameter(ParameterSetName = 'ById')] [ValidateNotNullOrEmpty()] [string]$Id, [Parameter(ParameterSetName = 'ByName', Position = 0)] [ValidateNotNullOrEmpty()] [string]$PrinterName, [Parameter(ParameterSetName = 'ByObject', ValueFromPipeline = $true)] [PSCustomObject]$InputObject, [Parameter()] [string]$DeviceName ) process { # Resolve printer $resolveParams = @{} switch ($PSCmdlet.ParameterSetName) { 'ById' { $resolveParams['Id'] = $Id } 'ByName' { $resolveParams['PrinterName'] = $PrinterName } 'ByObject' { $resolveParams['InputObject'] = $InputObject } } $printer = Resolve-KlippyPrinterTarget @resolveParams try { $response = Invoke-KlippyJsonRpc -Printer $printer -Method "machine/device_power/devices" -NoNormalize if (-not $response -or -not $response.devices) { Write-Verbose "No power devices found on '$($printer.PrinterName)'." return } foreach ($device in $response.devices) { # Filter by name if specified if ($DeviceName -and $device.device -ne $DeviceName) { continue } [PSCustomObject]@{ PSTypeName = 'KlippyCLI.PowerDevice' PrinterId = $printer.Id PrinterName = $printer.PrinterName DeviceName = $device.device Status = $device.status LockedWhilePrinting = $device.locked_while_printing Type = $device.type IsOn = $device.status -eq 'on' } } } catch { Write-Error "Failed to get power devices from '$($printer.PrinterName)': $_" } } } |