AutomaTD.psm1

function GetTelldusAccessToken
{
    [cmdletbinding()]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", '', Scope="Function", Target="GetTelldusAccessToken")]
    Param(
        $RequestToken
    )

    BEGIN { }

    PROCESS {
        $RequestTokenJson = $RequestToken | ConvertTo-Json
        $TokenResponse = Invoke-RestMethod -Uri https://tl.p0wershell.com/api/GetAccessToken -Method Post -Body $RequestTokenJson   

        [PSCustomObject] @{
            Token = $TokenResponse.Token
            TokenSecret = (ConvertTo-SecureString -String $TokenResponse.TokenSecret -AsPlainText -Force -ErrorAction Stop)
        }

        Remove-Variable TokenResponse -ErrorAction SilentlyContinue
        [GC]::Collect()
    }

    END { }
}
function GetTelldusProperty
{
    [cmdletbinding()]
    Param(
        $Data
    )

    BEGIN {
        $TextFormat = (Get-Culture).TextInfo
    }

    PROCESS {
        foreach ($dataObj in $Data) {
            $Properties = @{}

            $KeyName = $textFormat.ToTitleCase($dataObj.Name.ToLower())

            # Resolve sensors types to friendly names if they are known
            $KeyName = switch ($KeyName) {
                'Temp'   { 'Temperature' }
                'rrate'  { 'RainRate' }
                'rtot'   { 'RainTotal' }
                'wdir'   { 'WindDirection' }
                'wavg'   { 'WindAverage' }
                'watt'   { 'Watt' }
                'lum'    { 'Luminance' }
                default  { $KeyName }
            }

            if ($Properties.ContainsKey($KeyName)) {
                Write-Warning "Property $KeyName already exists. It will be dropped."
            }
            else {
                $Properties.Add($TextFormat.ToTitleCase($KeyName), $dataObj.value)
            }

            $Properties
        }
    }

    END { }
}
function GetTelldusRequestToken
{
    Invoke-RestMethod -Uri https://tl.p0wershell.com/api/GetRequestToken
}
function InvokeTelldusAction
{
    Param($URI)

    BEGIN {
        $ApiUri = 'https://tl.p0wershell.com/api/InvokeAction'

        if (-not [Net.ServicePointManager]::SecurityProtocol.HasFlag([Net.SecurityProtocolType]::Tls12) -AND $EnableTls12) {
            [Net.ServicePointManager]::SecurityProtocol += [Net.SecurityProtocolType]::Tls12
        }
    }

    PROCESS {
        $Payload = @{
            Token = $Global:TelldusLiveAccessToken.Token
            TokenSecret = [System.Runtime.InteropServices.Marshal]::PtrToStringUni([System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($Global:TelldusLiveAccessToken.TokenSecret))
            URI = $URI
        } | ConvertTo-Json

        $Response = try {
            Invoke-RestMethod -Uri $ApiUri -Method Post -Body $Payload -ErrorAction Stop
        }
        catch {
            throw "Request failed with error: $($_.Exception.Message)"
        }

        if ($Response.error) {
            throw $Response.error
        }
        else {
            $Response
        }
    }

    END {
        Remove-Variable Payload -ErrorAction SilentlyContinue
        [GC]::Collect()
    }
}
function Connect-TelldusLive
{
    <#
    .SYNOPSIS
    Connects to Telldus Live!
 
    .DESCRIPTION
    This function connects to Telldus Live! by either using a saved access token or
    by creating a new one.
 
    .EXAMPLE
    Connect-TelldusLive
 
    .EXAMPLE
    Connect-TelldusLive -SaveCredential
 
    Will return a link which you need to authorize and then save that access token for later use.
 
    .PARAMETER Profile
    The name of the profile you use to connect. You can leave this to "Default" (default value)
    if you don't have more than one Telldus Live! account.
 
    .PARAMETER Credential
    If you have a known access token, you can specify it as a PSCredential object.
 
    Username should be the AccessToken
    Password Should be the AccessTokenSecret
 
    .PARAMETER AccessToken
    If you have a known access token, you can specify it here (not the AccessTokenSecret)
 
    .PARAMETER AccessTokenSecret
    If you have a known access TokenSecret, you can specify it here (as a secure string)
 
    .PARAMETER SaveCredential
    Will save and store the access token after it has been retieved so it can be reused later
     
    You can then connect by simply using the -UseSavedCredential switch. The credential is saved using your logon session.
 
    .PARAMETER UseSavedCredential
    Specify this switch to use a saved credential instead of specifying one.
 
    #>


    [cmdletbinding(DefaultParameterSetName='SpecifyCredential')]
    Param (
        [Parameter(Mandatory=$false)]
        $Profile = 'Default',

        [Parameter(Mandatory=$true, ParameterSetName='SpecifyAccessTokenAsCredential')]
        [PSCredential] $Credential,

        [Parameter(Mandatory=$true, ParameterSetName='SpecifyAccessToken')]
        $AccessToken,

        [Parameter(Mandatory=$true, ParameterSetName='SpecifyAccessToken')]
        [System.Security.SecureString] $AccessTokenSecret,

        [Parameter(Mandatory=$false)]
        [Switch] $SaveCredential,

        [Parameter(Mandatory=$false, ParameterSetName='SavedCredential')]
        [Switch] $UseSavedCredential
    )

    BEGIN { }

    PROCESS {
        $AccessTokenFolder = Join-Path -Path $($env:APPDATA) -ChildPath AutomaTD

        if ( -not (Test-Path -Path $AccessTokenFolder)) {
            $null = New-Item -Path $AccessTokenFolder -ItemType Directory -Force
        }

        $AccessTokenFilename = "TelldusAccessToken-$($Profile).json"
        $AccessTokenFilePath = Join-Path -Path $AccessTokenFolder -ChildPath $AccessTokenFilename

        if ($PSCmdlet.ParameterSetName -eq 'SavedCredential') {
            if (Test-Path -Path $AccessTokenFilePath) {
                $AccessTokenFromDisk = Get-Content $AccessTokenFilePath -Raw -Encoding UTF8 | ConvertFrom-Json

                $Token = $AccessTokenFromDisk.Token
                $TokenSecret = $AccessTokenFromDisk.TokenSecret | ConvertTo-SecureString

                # Build the token
                $AccessToken = [PSCustomObject] @{
                    Token = $Token
                    TokenSecret = $TokenSecret
                }
            }
            else {
                throw "Didn't locate any saved access tokens. Please run this command with the 'SaveCredential' switch first to store the credentials or verify which profile you choose."
            }
        }
        elseif ($PSCmdlet.ParameterSetName -eq 'SpecifyAccessToken') {
            # Build the credential
            $AccessToken = [PSCustomObject] @{
                Token = $AccessToken
                TokenSecret = $AccessTokenSecret
            }
        }
        elseif ($PSCmdlet.ParameterSetName -eq 'SpecifyAccessTokenAsCredential') {
            # Build the credential
            $AccessToken = [PSCustomObject] @{
                Token = $Credential.UserName
                TokenSecret = $Credential.Password
            }
        }
        else {
            $RequestToken = GetTelldusRequestToken
            Write-Output "Please go to the following URL to authenticate this module:`n$($RequestToken.AuthURL)"

            $PollingAttempts = 20
            Do {
                $PollingAttempts--
                $AuthFailed = $false

                try {
                    $AccessToken = GetTelldusAccessToken -RequestToken $RequestToken -ErrorAction Stop
                }
                catch {
                    $AuthFailed = $true

                    Start-Sleep -Seconds 15
                }
            }
            while ($AuthFailed -and $PollingAttempts -gt 0)

            if (-not $AccessToken) {
                throw "Authorization failed or timed out. Please try again."
            }

            if ($SaveCredential.IsPresent) {
                $ExportToken = @{
                    Token = $AccessToken.Token
                    TokenSecret = ConvertFrom-SecureString -SecureString $AccessToken.TokenSecret
                }

                $ExportToken | ConvertTo-Json -Compress | Out-File -FilePath $AccessTokenFilePath -Encoding utf8 -Force
            }
        }

        $Global:TelldusLiveAccessToken = $AccessToken

        try {
            $null = Get-TDClient -ErrorAction Stop
        }
        catch {
            throw "Failed to connect to Telldus Live! The error was: $($_.Exception.Message)"
        }
    }

    END {
    
    }
}
function ConvertTo-TDNormalizedOutput
{
    <#
    .SYNOPSIS
    Makes sure all objects have the same set of properties
 
    .DESCRIPTION
    Makes sure all objects have the same set of properties
 
    Makes exporting to for example CSV-files easiser since all sensors will have
    the same set of "columns" in the file (but blank values for those missing that
    sensor value type).
 
    .EXAMPLE
    Get-TDSensor | Get-TDSensorData | ConvertTo-TDNormalizedOutput
 
    Makes sure all objects have the same set of properties
 
    .EXAMPLE
    Get-TDSensor | Get-TDSensorData | ConvertTo-TDNormalizedOutput -PropertiesToAlwaysInclude CustomSensorData
 
    Makes sure all objects have the same set of properties, and "CustomSensorData" will always be a property
    of the objects even if it doesn't exist in the results.
 
    #>


    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        $InputObject,

        [Parameter(Mandatory=$false)]
        [string[]] $PropertiesToAlwaysInclude
    )

    begin {
        $Properties = New-Object System.Collections.Generic.HashSet[string]
        $Objects = New-Object System.Collections.Generic.List[System.Object]
    }

    process {
        $null = $InputObject.psobject.Properties.Name.foreach({$Properties.Add($_)})
        $Objects.Add($InputObject)
    }

    end {
        if ($PropertiesToAlwaysInclude) {
            foreach ($Property in $PropertiesToAlwaysInclude) {
                $null = $Properties.Add($Property)
            }
        }

        $Objects | Select-Object -Property @($Properties)
    }
}
function Get-TDAccessToken
{
    <#
    .SYNOPSIS
    Used to export the access token as a PSCredential or as plain text
 
    .DESCRIPTION
    Used to export the access token as a PSCredential or as plain text
 
    Useful if you want to set it up on a new computer where you wont be able to
    interact with the login command interactively or store the token.
 
    Be aware that exporting the access token as plain-text is a security risk!
 
    Only do this on systems that you trust.
 
    .EXAMPLE
    Get-TDAccessToken -ExportAsPSCredential
 
    #>


    [CmdletBinding(DefaultParameterSetName='AsPSCredential')]
    Param(
        [Parameter(Mandatory=$false, ParameterSetName='AsPSCredential')]
        [switch] $ExportAsPSCredential,

        [Parameter(Mandatory=$true, ParameterSetName='AsPlainText')]
        [switch] $ExportAsPlainText,

        [Parameter(Mandatory=$false, ParameterSetName='AsPlainText')]
        [switch] $Force
    )

    BEGIN {
        if ($TelldusLiveAccessToken -eq $null) {
            throw "You must first connect using the Connect-TelldusLive cmdlet to load the access token"
        }
    }

    PROCESS {
        if ($PSCmdlet.ParameterSetName -eq 'AsPSCredential') {
            New-Object System.Management.Automation.PSCredential ($Global:TelldusLiveAccessToken.Token, $Global:TelldusLiveAccessToken.TokenSecret)
        }
        elseif ($PSCmdlet.ParameterSetName -eq 'AsPlainText') {
            if ($Force.IsPresent) {
                [PSCustomObject] @{
                    Token = $Global:TelldusLiveAccessToken.Token
                    TokenSecret = [System.Runtime.InteropServices.Marshal]::PtrToStringUni([System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($Global:TelldusLiveAccessToken.TokenSecret))
                }
            }
            else {
                throw "The system cannot protect plain text output. To suppress this warning and output the access token as plain text anyway, reissue the command specifying the Force parameter."
            }
        }
    }

    END { }
}
function Get-TDClient
{
    <#
    .SYNOPSIS
    Retrieves all clients/locations associated with a Telldus Live! account.
 
    .DESCRIPTION
    Retrieves all clients/locations associated with a Telldus Live! account.
 
    .EXAMPLE
    Get-TDClient
 
    #>


    [CmdletBinding()]
    Param()

    BEGIN {
        if ($TelldusLiveAccessToken -eq $null) {
            throw "You must first connect using the Connect-TelldusLive cmdlet"
        }
    }

    PROCESS {
        $Clients = InvokeTelldusAction -URI "clients/list"

        foreach ($Client in $Clients.client) {
            $Client.online = [bool] $Client.online
            $Client.editable = [bool] $Client.editable
            $Client
        }
    }

    END { }
}
function Get-TDDevice
{
    <#
    .SYNOPSIS
    Retrieves all devices associated with a Telldus Live! account.
 
    .DESCRIPTION
    This command will list all devices associated with an Telldus Live!-account and their current status and other information.
 
    .EXAMPLE
    Get-TDDevice
 
    .EXAMPLE
    Get-TDDevice | Format-Table
 
    #>


    [CmdletBinding()]
    Param()

    BEGIN {
        if ($TelldusLiveAccessToken -eq $null) {
            throw "You must first connect using the Connect-TelldusLive cmdlet"
        }
    }

    PROCESS {

        $DeviceList = InvokeTelldusAction -URI 'devices/list?supportedMethods=19'

        foreach ($Device in $DeviceList.device) {

            $PropertiesToOutput = @{
                                 'Name' = $Device.name;
                                 'State' = switch ($Device.state)
                                           {
                                                 1 { "On" }
                                                 2 { "Off" }
                                                16 { "Dimmed" }
                                                default { "Unknown" }
                                           }
                                 'DeviceID' = $Device.id;
                             

                                 'Statevalue' = $Device.statevalue
                                 'Methods' = switch ($Device.methods)
                                             {
                                                 3 { "On/Off" }
                                                19 { "On/Off/Dim" }
                                                default { "Unknown" }
                                             }
                                 'Type' = $Device.type;
                                 'Client' = $Device.client;
                                 'ClientName' = $Device.clientName;
                                 'Online' = switch ($Device.online)
                                            {
                                                0 { $false }
                                                1 { $true }
                                            }
                                 }

            $returnObject = New-Object -TypeName PSObject -Property $PropertiesToOutput

            Write-Output $returnObject | Select-Object Name, DeviceID, State, Statevalue, Methods, Type, ClientName, Client, Online
        }
    }

    END { }
}
function Get-TDDeviceHistory
{
    <#
    .SYNOPSIS
    Retrieves all events associated with the specified device.
    .DESCRIPTION
    This command will list all events associated with the specified device
    .EXAMPLE
    Get-TDDeviceHistory
    .EXAMPLE
    Get-TDDeviceHistory | Format-Table
    #>


    [cmdletbinding()]
    param(
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [Alias('id')]
    [string] $DeviceID)

    BEGIN {
        if ($TelldusLiveAccessToken -eq $null) {
            throw "You must first connect using the Connect-TelldusLive cmdlet"
        }
    }

    PROCESS {
        $HistoryEvents = InvokeTelldusAction -URI "device/history`?id=$DeviceID"
        
        foreach ($HistoryEvent in $HistoryEvents.history)
        {
            $PropertiesToOutput = @{
                                 'DeviceID' = $DeviceID
                                 'State' = switch ($HistoryEvent.state)
                                           {
                                                 1 { "On" }
                                                 2 { "Off" }
                                                16 { "Dimmed" }
                                                default { "Unknown" }
                                           }
                                 'Statevalue' = $HistoryEvent.statevalue
                                 'Origin' = $HistoryEvent.Origin;
                                 'EventDate' = (Get-Date "1970-01-01 00:00:00").AddSeconds($HistoryEvent.ts)
                                 }

            $returnObject = New-Object -TypeName PSObject -Property $PropertiesToOutput

            Write-Output $returnObject | Select-Object DeviceID, EventDate, State, Statevalue, Origin
        }
    }

    END { }
}
function Get-TDEvent
{

    <#
    .SYNOPSIS
    List all events available in Telldus Live!
 
    .DESCRIPTION
    List all events available in Telldus Live!
 
    .EXAMPLE
    Get-TDEvent
 
    #>


    [CmdletBinding()]
    param()

    BEGIN {
        if ($TelldusLiveAccessToken -eq $null) {
            throw "You must first connect using the Connect-TelldusLive cmdlet"
        }
    }

    PROCESS {
        $EventHistory = InvokeTelldusAction -URI "events/list"

        $EventHistory.event
    }
}
function Get-TDSensor
{
    <#
    .SYNOPSIS
    Retrieves all sensors associated with a Telldus Live! account.
 
    .DESCRIPTION
    This command will list all sensors associated with an Telldus Live!-account and their current status and other information.
 
    .PARAMETER IncludeIgnored
    Returns hidden/ignored sensors as well
 
    .EXAMPLE
    Get-TDSensor
 
    .EXAMPLE
    Get-TDSensor | Format-Table
 
    #>

    [cmdletbinding()]
    Param(
        [switch] $IncludeIgnored
    )

    BEGIN {
        if ($TelldusLiveAccessToken -eq $null) {
            throw "You must first connect using the Connect-TelldusLive cmdlet"
        }
    }

    PROCESS {
        if ($IncludeIgnored.IsPresent) {
            $URI = "sensors/list?includeValues=1&includeIgnored=1"
        }
        else {
            $URI = "sensors/list?includeValues=1&includeIgnored=0"
        }

        $Response = InvokeTelldusAction -URI $URI

        $Sensors = $Response.sensor
        [datetime] $TelldusDate="1970-01-01 00:00:00"

        foreach ($Sensor in $Sensors) {
            $Sensor.lastUpdated = $TelldusDate.AddSeconds($Sensor.lastUpdated)
            $Sensor.Ignored = [bool] $Sensor.Ignored
            $Sensor.keepHistory = [bool] $Sensor.keepHistory
            $Sensor.Editable = [bool] $Sensor.Editable
            $Sensor.Online = [bool] $Sensor.Online
            Write-Output $Sensor
        }
    }

    END { }
}
function Get-TDSensorData
{
    <#
    .SYNOPSIS
    Retrieves the sensordata of specified sensor.
 
    .DESCRIPTION
    This command will retrieve the sensordata associated with the specified ID.
 
    .EXAMPLE
    Get-TDSensorData -DeviceID 123456
 
    .PARAMETER DeviceID
    The DeviceID of the sensor which data you want to retrieve.
 
    .PARAMETER HideRawData
    Specify this switch to hide the raw data response from Telldus Live!
 
    #>


    [CmdletBinding()]
    param(

      [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
      [Alias('id')]
      [string] $DeviceID,
      
      [Parameter(Mandatory=$false)]
      [switch] $HideRawData
    )

    BEGIN {
        if ($TelldusLiveAccessToken -eq $null) {
            throw "You must first connect using the Connect-TelldusLive cmdlet"
        }
    }

    PROCESS {
        $SensorData = InvokeTelldusAction -URI "sensor/info?id=$DeviceID"
        
        [datetime] $TelldusDate="1970-01-01 00:00:00"

        $PropertiesToOutput = @{
            DeviceId = $SensorData.id
            Name = $SensorData.name
            ClientName = $SensorData.clientName
            LastUpdated = $TelldusDate.AddSeconds($SensorData.lastUpdated)
            Ignored = [bool] $SensorData.Ignored
            Editable = [bool] $SensorData.editable
            Protocol = $SensorData.protocol
            SensorId = $SensorData.sensorId
            TimeZoneOffset = $SensorData.timezoneoffset
            Battery = $SensorData.battery
            KeepHistory = [bool] $SensorData.keepHistory
        }

        if (-not $HideRawData.IsPresent) {
            $PropertiesToOutput += @{ 'Data' = $SensorData.data }
        }

        $expandedProperties = GetTelldusProperty -Data $SensorData.data

        foreach ($expandedProperty in $expandedProperties) {
            $PropertiesToOutput += $expandedProperty
        }
        
        New-Object -TypeName PSObject -Property $PropertiesToOutput
    }

    END { }
}
function Get-TDSensorHistoryData
{
    <#
    .SYNOPSIS
    Retrieves sensor data history from Telldus Live!
     
    .DESCRIPTION
    This command will retrieve the sensor history data of the specified sensor.
     
    .PARAMETER DeviceID
    The DeviceID of the sensor which data you want to retrieve.
 
    .PARAMETER After
    Specify from which date you would like to retrieve sensor history.
 
    Always use UTC time.
 
    .PARAMETER Before
    Specify the "end date" of the data samples.
    Default value is current date.
 
    Always use UTC time.
 
    .PARAMETER PostRawData
    Specify this switch to post the raw data response from Telldus Live!
 
    .EXAMPLE
    Get-TDSensorHistoryData -DeviceID 123456
 
    .EXAMPLE
    Get-TDSensorHistoryData -DeviceID 123456 | Format-Table
 
    .EXAMPLE
    Get-TDSensorHistoryData -DeviceID 123456 -After (get-date).AddDays(-1)
     
    Get's the history from yesterday until today
 
    #>


    [cmdletbinding(DefaultParameterSetName='AllData')]
    param(
        [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName='AllData')]
        [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName='DateRange')]
        [Alias('id')]
        [string] $DeviceID,

        [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName='DateRange')]
        [DateTime] $After,

        [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, ParameterSetName='DateRange')]
        [DateTime] $Before,

        [Parameter(Mandatory=$false)]
        [switch] $PostRawData
    )

    BEGIN {
        if ($TelldusLiveAccessToken -eq $null) {
            throw "You must first connect using the Connect-TelldusLive cmdlet"
        }

        
    }

    PROCESS {
        $ApiEndpoint = "sensor/history`?id=$DeviceID"

        if ($PSCmdlet.ParameterSetName -eq 'DateRange') {
            if (-not $Before) {
                $Before = (Get-Date).ToUniversalTime()
            }

            if ($Before -gt $After) {
                $FromDateToPost = [Math]::Floor((New-TimeSpan -Start '1970-01-01' -End $After).TotalSeconds)
                $ToDateToPost = [Math]::Floor((New-TimeSpan -Start '1970-01-01' -End $Before).TotalSeconds)

                $ApiEndpoint = $ApiEndpoint + "&from=$FromDateToPost" + "&to=$ToDateToPost"
            }
            else {
                throw 'The value for Before must be greater than the value for After.'
            }
        }

        $HistoryDataPoints = InvokeTelldusAction -URI $ApiEndpoint


        foreach ($HistoryDataPoint in $HistoryDataPoints.history) {

            $PropertiesToOutput = @{
                                 'DeviceID' = $DeviceID
                                 'Date' = (Get-Date "1970-01-01 00:00:00").AddSeconds($HistoryDataPoint.ts)
                                 }

            $expandedProperties = GetTelldusProperty -Data $HistoryDataPoint.data

            foreach ($expandedProperty in $expandedProperties) {
                $PropertiesToOutput += $expandedProperty
            }

            if ($PostRawData.IsPresent) {
                $PropertiesToOutput += @{ 'Data' = $HistoryDataPoint.data }
            }

            $returnObject = New-Object -TypeName PSObject -Property $PropertiesToOutput

            Write-Output $returnObject
        }
    }

    END { }
}
function Rename-TDDevice
{

    <#
    .SYNOPSIS
    Renames a device in Telldus Live!
 
    .DESCRIPTION
    Renames a device in Telldus Live!
 
    .EXAMPLE
    Rename-TDDevice -DeviceID 123456 -NewName MyNewDeviceName
 
    .PARAMETER DeviceID
    The DeviceID of the device to rename
 
    .PARAMETER NewName
    The new name for that device
 
    #>


    [CmdletBinding()]
    param(

      [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
      [Alias('id')]
      [string] $DeviceID,

      [Parameter(Mandatory=$True)]
      [string] $NewName)


    BEGIN {
        if ($TelldusLiveAccessToken -eq $null) {
            throw "You must first connect using the Connect-TelldusLive cmdlet"
        }
    }

    PROCESS {
        $Response = InvokeTelldusAction -URI "device/setName`?id=$DeviceID&name=$([uri]::EscapeDataString($NewName))"

        Write-Verbose "Renamed device with id $DeviceID. Result: $($Response.status)."
    }
}

function Set-TDDevice
{

    <#
    .SYNOPSIS
    Turns a device on or off.
 
    .DESCRIPTION
    This command can set the state of a device to on or off through the Telldus Live! service.
 
    .EXAMPLE
    Set-TDDevice -DeviceID 123456 -Action turnOff
 
    .EXAMPLE
    Set-TDDevice -DeviceID 123456 -Action turnOn
 
    .EXAMPLE
    Set-TDDevice -DeviceID 123456 -Action bell
 
    .PARAMETER DeviceID
    The DeviceID of the device to turn off or on. (Pipelining possible)
 
    .PARAMETER Action
    What to do with that device. Possible values are "turnOff", "turnOn" and "bell".
 
    .NOTES
    Thank you Ispep (automatiserar.se) for fixing "bell" support!
 
    #>


    [CmdletBinding()]
    param(
      [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
      [Alias('id')]
      [string] $DeviceID,
      [Parameter(Mandatory=$True)]
      [ValidateSet("turnOff","turnOn", "bell", "down", "up", "stop")]
      [string] $Action)


    BEGIN {
        if ($TelldusLiveAccessToken -eq $null) {
            throw "You must first connect using the Connect-TelldusLive cmdlet"
        }
    }

    PROCESS {

        $Response = InvokeTelldusAction -URI "device/$Action`?id=$DeviceID"

        Write-Verbose "Doing action $Action on device $DeviceID. Result: $($Response.status)."
    }
}
function Set-TDDimmer
{
    <#
    .SYNOPSIS
    Dims a device to a certain level.
 
    .DESCRIPTION
    This command can set the dimming level of a device to through the Telldus Live! service.
 
    .EXAMPLE
    Set-TDDimmer -DeviceID 123456 -Level 89
 
    .EXAMPLE
    Set-TDDimmer -Level 180
 
    .PARAMETER DeviceID
    The DeviceID of the device to dim. (Pipelining possible)
 
    .PARAMETER Level
    What level to dim to. Possible values are 0 - 255.
 
    #>


    [CmdletBinding()]
    param(

      [Parameter(Mandatory=$True,
                 ValueFromPipeline=$true,
                 ValueFromPipelineByPropertyName=$true,
                 HelpMessage="Enter the DeviceID.")] [Alias('id')] [string] $DeviceID,

      [Parameter(Mandatory=$True,
                 HelpMessage="Enter the level to dim to between 0 and 255.")]
      [ValidateRange(0,255)]
      [int] $Level)


    BEGIN {
        if ($TelldusLiveAccessToken -eq $null) {
            throw "You must first connect using the Connect-TelldusLive cmdlet"
        }
    }

    PROCESS {

        $Response = InvokeTelldusAction -URI "device/dim`?id=$DeviceID&level=$Level"

        Write-Verbose "Dimming device $DeviceID to level $Level. Result: $($Response.status)."
    }
}
function Set-TDSensor
{

    <#
    .SYNOPSIS
    Sets/updates settings for sensors
 
    .DESCRIPTION
    Sets/updates settings for sensors
 
    It can for example enable history on the sensor, ignore/hide a sensor or
    rename a sensor
 
    .EXAMPLE
    Set-TDSensor -DeviceID 123456 -NewName Garage
 
    Changes the name of the sensor with id 123456 to Garage
 
    .EXAMPLE
    Set-TDSensor -DeviceID 123456 -KeepHistory $true -IgnoreSensor $false
 
    Enables history on sensor with id 123456 and unhides it
 
    .PARAMETER DeviceID
    The ID of the sensor which settings you wish to update
 
    #>


    [CmdletBinding()]
    param(
      [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
      [Alias('id')]
      [string] $DeviceID,
      [Parameter(Mandatory=$false)]
      [bool] $KeepHistory,
      [Parameter(Mandatory=$false)]
      [bool] $IgnoreSensor,
      [Parameter(Mandatory=$false)]
      [string] $NewName
    )


    BEGIN {
        if ($TelldusLiveAccessToken -eq $null) {
            throw "You must first connect using the Connect-TelldusLive cmdlet"
        }
    }

    PROCESS {
        if ($PSBoundParameters.ContainsKey('KeepHistory')) {
            if ($KeepHistory) {
                $HistorySetting = 1
            }
            else {
                $HistorySetting = 0
            }

            $Response = InvokeTelldusAction -URI "sensor/setKeepHistory?id=$DeviceID&keepHistory=$HistorySetting"
        }

        if ($PSBoundParameters.ContainsKey('IgnoreSensor')) {
            if ($IgnoreSensor) {
                $IgnoreSetting = 1
            }
            else {
                $IgnoreSetting = 0
            }

            $Response = InvokeTelldusAction -URI "sensor/setIgnore?id=$DeviceID&ignore=$IgnoreSetting"
        }

        if ($NewName) {
            $Response = InvokeTelldusAction -URI "sensor/setName?id=$DeviceID&name=$([uri]::EscapeDataString($NewName))"
        }
    }
}