public/Set-DaikinAirCon.ps1

<#PSScriptInfo
    .VERSION 1.0.0.0
    .GUID 6746c383-3590-4a42-b8e0-1a4134e6f216
    .FILENAME Set-DaikinAirCon.ps1
    .AUTHOR Hannes Palmquist
    .AUTHOREMAIL hannes.palmquist@outlook.com
    .CREATEDDATE 2020-10-03
    .COMPANYNAME Personal
    .COPYRIGHT (c) 2020, , All Rights Reserved
#>

function Set-DaikinAirCon
{
    <#
    .DESCRIPTION
        Cmdlet allows to configure the aircon device to the desired setting.
    .PARAMETER Hostname
        Hostname or IP of the Daikin AirCon device.
    .PARAMETER PowerOn
        Set to $true or $false to specify if the master power should be on or off. This does not the affect the connectivity of the control surfice.
    .PARAMETER Temp
        Specified the target temperature in celcius.
    .PARAMETER Mode
        Specifies the operating mode of the aircon device. Allowed values are;
        AUTO : Switches between heat and cold depending on the current and target temperature.
        DRY : Sets the device lower the humidity of the air.
        COLD : Sets the device chill the air if needed. No heat will be provided.
        HEAT : Sets the device heat the air if needed. No chilling of air will be provided.
        FAN : Sets the device to only circulate air without affecting temperature or humidity.
    .PARAMETER FanSpeed
        Specifies the strenght of the fan. Allowed values are;
        AUTO : Sets the device to manage fan speed to keep the target climate.
        SILENT : Sets the fan speed to minimize noise.
        Level_1 -> Level_5 : Sets the fan speed to the target level.
    .PARAMETER FanDirection
        Specifies how the direction of airflow is controlled. Allowed values are
        Stopped, VerticalSwing, HorizontalSwing and BothSwing
    .EXAMPLE
        Set-DaikinAirCon -HostName daikin.local.network -PowerOn:$true -Temp 19 -Mode AUTO -FanSpeed AUTO -FanDirection Stopped
         
        This example configures the aircon device to the specified parameter values.
    #>


    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidInvokingEmptyMembers', '', Justification = 'asd')]
    [CmdletBinding()] # Enabled advanced function support
    param(
        $HostName,
        [boolean]$PowerOn,
        [ValidateRange(10, 41)][int]$Temp,
        [ValidateSet('AUTO', 'DRY', 'COLD', 'HEAT', 'FAN')]$Mode,
        [ValidateSet('AUTO', 'SILENT', 'Level_1', 'Level_2', 'Level_3', 'Level_4', 'Level_5')]$FanSpeed,
        [ValidateSet('Stopped', 'VerticalSwing', 'HorizontalSwing', 'BothSwing')]$FanDirection
    )

    BEGIN
    {
        $ModeTranslation = @{
            'AUTO' = '1'
            'DRY'  = '2'
            'COLD' = '3'
            'HEAT' = '4'
            'FAN'  = '6'
        }
        $FanSpeedTranslation = @{
            'AUTO'    = 'A'
            'SILENT'  = 'B'
            'Level_1' = 'lvl_1'
            'Level_2' = 'lvl_2'
            'Level_3' = 'lvl_3'
            'Level_4' = 'lvl_4'
            'Level_5' = 'lvl_5'

        } 
        $FanDirectionTranslation = @{
            'Stopped'         = '0'
            'VerticalSwing'   = '1'
            'HorizontalSwing' = '2'
            'BothSwing'       = '3'
        }

        $CurrentSettings = Get-DaikinControlInfo -Hostname:$Hostname -Raw
        $NewSettings = [ordered]@{
            'pow'    = $CurrentSettings.pow
            'mode'   = $CurrentSettings.mode
            'stemp'  = $CurrentSettings.stemp
            'shum'   = $CurrentSettings.shum
            'f_rate' = $CurrentSettings.f_rate
            'f_dir'  = $CurrentSettings.f_dir
        }
    }

    PROCESS
    {
        foreach ($Key in $PSBoundParameters.Keys)
        {
            if ($Key -eq 'HostName') 
            {
                continue 
            }
            switch ($Key)
            {
                'Temp'
                { 
                    $NewSettings.stemp = $PSBoundParameters.$Key 
                }
                'PowerOn'
                { 
                    $NewSettings.pow = $PSBoundParameters.$Key 
                }
                'Mode'
                { 
                    $NewSettings.mode = $ModeTranslation.($PSBoundParameters.$Key) 
                }
                'FanSpeed'
                { 
                    $NewSettings.f_rate = $FanSpeedTranslation.($PSBoundParameters.$Key) 
                }
                'FanDirection'
                { 
                    $NewSettings.f_dir = $FanDirectionTranslation.($PSBoundParameters.$Key) 
                }
            }
        }
        if ($NewSettings.stemp -eq '--')
        {
            $NewSettings.stemp = $CurrentSettings.('dt{0}' -f $NewSettings.Mode)
        }
        if ($NewSettings.shum -eq '--')
        {
            $NewSettings.shum = $CurrentSettings.('dh{0}' -f $NewSettings.Mode)
        }
    }

    END
    {
        $String = @()
        foreach ($Key in $NewSettings.Keys)
        {
            $String += ('{0}={1}' -f $Key, $NewSettings.$Key)
        } 
        $PropertyString = $String -join '&'
    
        $URI = ('http://{0}/aircon/set_control_info?{1}' -f $HostName, $PropertyString)
        $Result = Invoke-RestMethod -Uri $uri -Method post
        $Result = Convert-DaikinResponse -String $Result

        switch ($Result.ret)
        {
            'OK'
            { 
                Write-Host -Object 'Success' -ForegroundColor Green
            }
            default
            { 
                Write-Error -Message ('Unknown message returned: {0}' -f $PSItem) -ErrorAction Stop
            }
        } 
    }

}
#endregion



# SIG # Begin signature block
# MIIR2AYJKoZIhvcNAQcCoIIRyTCCEcUCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUkinPCku8MbifR0czuzlXU6dm
# EICggg0/MIIDBDCCAeygAwIBAgIQXaH43Bl75ZhMB4kpXyZ2qjANBgkqhkiG9w0B
# AQUFADAaMRgwFgYDVQQDDA9IYW5uZXNQYWxtcXVpc3QwHhcNMjAxMDIyMDczMDU3
# WhcNMjUxMDIyMDc0MDU3WjAaMRgwFgYDVQQDDA9IYW5uZXNQYWxtcXVpc3QwggEi
# MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQClDvznsRz3hBOHS8rZ5ZlNB/ac
# GrKUQyoH0Pk6JONmIBeQPLwurne3ulSwb+6cbwwgp87eSnGoq4YbAFfqTbwytKLn
# YmIoHGiCrwxZb5YU6ijOrW6Sywa+H+/uKsZqJfXFRn1vGnC5tZwa5rSngLaow1qV
# tvyRQGRGNpI02hUwtChneJJmwk2B8dtY1ECH6Ob9LFlWETcETy5T5RKSS1sRWATk
# K9EQsZ65AHbGKGkpDv8y/+g7hg3KKU+m8f3ahMscMB5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