public/Set-YeelightColor.ps1

<#
.SYNOPSIS
    Changes Yeelight Lightstrip color
.DESCRIPTION
    The function changes Yeelight Lightstrip color
.EXAMPLE
    PS C:\> Set-YeelightColor -Name Biuro -Hex FDF6E3 -PassThru
 
.EXAMPLE
    PS C:\> Set-YeelightColor -Name Biuro -Color Red -PassThru
 
.NOTES
    Author: Mateusz Nadobnik
    Link: akademiapowershell.pl
 
    Date: 14-11-2020
    Version: 0.0.1
    Keywords: color, yeelight, xiaomi
    Notes:
    Changelog:
#>

function Set-YeelightColor
{
    [cmdletbinding()]
    param(
        [Parameter(Mandatory, Position = 0)]
        [string]$Name,
        [Parameter(Mandatory, ParameterSetName = 'Color', Position = 1)]
        [ValidateSet('Red', 'Blue', 'Green', 'Yellow', 'White', 'Grey')]
        [string]$Color,
        [ValidateScript( { $_ -match '[A-Fa-f0-9]{6}' })]
        [Parameter(Mandatory, ParameterSetName = 'Hex', Position = 1)]
        [string]$HexColor,
        [switch]$PassThru
    )

    try
    {

        switch ($PsCmdlet.ParameterSetName)
        {
            'Color'
            {
                Write-Verbose $Color
                $RgbColor = switch ($Color)
                {
                    'Red'
                    {
                        "255,0,0"
                    }
                    'Blue'
                    {
                        "0,128,255"
                    }
                    'Green'
                    {
                        "0,255,0"
                    }
                    'Yellow'
                    {
                        "255,255,0"
                    }
                    'White'
                    {
                        "255,255,255"
                    }
                    'Grey'
                    {
                        "128,128,128"
                    }
                }
            }
            'Hex'
            {
                Write-Verbose $HexColor
                $RgbColor = Convert-Color -HEX $HexColor
            }
        }

        $Device = $script:connections | Where-Object Name -eq $Name

        if ($null -ne $IpDevice)
        {
            $Device = $script:connections | Where-Object Hostname -eq $IpDevice
        }

        if ($null -ne $Device)
        {
            $Red, $Green, $Blue = $RgbColor -split ','
            $Device.SetRGBColor($Red, $Green, $Blue, 1).Wait()

            if ($PassThru.IsPresent)
            {
                [PSCustomObject]@{
                    Name        = $Device.Name
                    Hostname    = $Device.Hostname
                    IsConnected = $Device.IsConnected
                    RGB         = $Device.Properties['rgb']
                }
            }
        }
        else
        {
            Write-Warning 'The connection not exist. Use Connect-Yeelight function and try again.'
        }
    }
    catch
    {
        Write-Warning $_
    }
}