Advertisement
c9h13no3

Telldus Module

May 30th, 2013
1,322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-TDDevice
  2. {
  3.     <#
  4.     .SYNOPSIS
  5.     Retrieves all devices associated with a Telldus Live! account.
  6.  
  7.     .DESCRIPTION
  8.     This command will list all devices associated with an Telldus Live!-account and their current status and other information.
  9.  
  10.     .EXAMPLE
  11.     Get-TDDevice -Username myemail@telldus.com -Password MyTelldusPassword
  12.  
  13.     .EXAMPLE
  14.     Get-TDDevice -Username myemail@telldus.com -Password MyTelldusPassword | Format-Table
  15.  
  16.     .PARAMETER Username
  17.     The username (e-mail) used to log into Telldus Live!
  18.  
  19.     .PARAMETER Password
  20.     The password used to log into Telldus Live!
  21.  
  22.     #>
  23.  
  24.     param(
  25.       [Parameter(Mandatory=$True)] [string] $Username,
  26.       [Parameter(Mandatory=$True)] [string] $Password
  27.     )
  28.  
  29.     $LoginPostURI="https://login.telldus.com/openid/server?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.mode=checkid_setup&openid.return_to=http%3A%2F%2Fapi.telldus.com%2Fexplore%2Fclients%2Flist&openid.realm=http%3A%2F%2Fapi.telldus.com&openid.ns.sreg=http%3A%2F%2Fopenid.net%2Fextensions%2Fsreg%2F1.1&openid.sreg.required=email&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select#"
  30.     $turnOffURI="http://api.telldus.com/explore/device/turnOff"
  31.     $deviceListURI="http://api.telldus.com/explore/devices/list"
  32.     $PostActionURI="http://api.telldus.com/explore/doCall"
  33.     $Action='list'
  34.     $SupportedMethods=19
  35.     $TelldusWEB = Invoke-WebRequest $turnOffURI -SessionVariable Telldus
  36.  
  37.     $form = $TelldusWEB.Forms[0]
  38.     $form.Fields["email"] = $Username
  39.     $form.Fields["password"] = $Password
  40.  
  41.     $TelldusWEB = Invoke-WebRequest -Uri $LoginPostURI -WebSession $Telldus -Method POST -Body $form.Fields
  42.  
  43.     $request = @{'group'='devices';'method'= $Action;'param[supportedMethods]'= $SupportedMethods;'responseAsXml'='xml'}
  44.  
  45.     [xml] $ActionResults=Invoke-WebRequest -Uri $PostActionURI -WebSession $Telldus -Method POST -Body $request
  46.  
  47.     $Results=$ActionResults.devices.ChildNodes
  48.  
  49.     foreach ($Result in $Results)
  50.     {
  51.         $PropertiesToOutput = @{
  52.                              'Name' = $Result.name;
  53.                              'State' = switch ($Result.state)
  54.                                        {
  55.                                              1 { "On" }
  56.                                              2 { "Off" }
  57.                                             16 { "Dimmed" }
  58.                                             default { "Unknown" }
  59.                                        }
  60.                              'DeviceID' = $Result.id;
  61.                              
  62.  
  63.                              'Statevalue' = $Result.statevalue
  64.                              'Methods' = switch ($Result.methods)
  65.                                          {
  66.                                              3 { "On/Off" }
  67.                                             19 { "On/Off/Dim" }
  68.                                             default { "Unknown" }
  69.                                          }
  70.                              'Type' = $Result.type;
  71.                              'Client' = $Result.client;
  72.                              'ClientName' = $Result.clientName;
  73.                              'Online' = switch ($Result.online)
  74.                                         {
  75.                                             0 { $false }
  76.                                             1 { $true }
  77.                                         }
  78.                              }
  79.  
  80.         $returnObject = New-Object -TypeName PSObject -Property $PropertiesToOutput
  81.         Write-Output $returnObject | Select-Object Name, DeviceID, State, Statevalue, Methods, Type, ClientName, Client, Online
  82.     }
  83. }
  84.  
  85. function Set-TDDevice
  86. {
  87.  
  88.     <#
  89.     .SYNOPSIS
  90.     Turns a device on or off.
  91.  
  92.     .DESCRIPTION
  93.     This command can set the state of a device to on or off through the Telldus Live! service.
  94.  
  95.     .EXAMPLE
  96.     Set-TDDevice -Username myemail@telldus.com -Password MyTelldusPassword -DeviceID 123456 -Action turnOff
  97.  
  98.     .EXAMPLE
  99.     Set-TDDevice -Username myemail@telldus.com -Password MyTelldusPassword -DeviceID 123456 -Action turnOn
  100.  
  101.     .PARAMETER Username
  102.     The username (e-mail) used to log into Telldus Live!
  103.  
  104.     .PARAMETER Password
  105.     The password used to log into Telldus Live!
  106.  
  107.     .PARAMETER DeviceID
  108.     The DeviceID of the device to turn off or on. (Pipelining possible)
  109.  
  110.     .PARAMETER Action
  111.     What to do with that device. Possible values are "turnOff" or "turnOn".
  112.  
  113.     #>
  114.  
  115.     [CmdletBinding()]
  116.     param(
  117.  
  118.       [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
  119.       [Alias('id')]
  120.       [string] $DeviceID,
  121.       [Parameter(Mandatory=$True)]
  122.       [ValidateSet("turnOff","turnOn")]
  123.       [string] $Action,
  124.       [Parameter(Mandatory=$True)] [string] $Username,
  125.       [Parameter(Mandatory=$True)] [string] $Password
  126.     )
  127.  
  128.  
  129.     BEGIN {
  130.         $LoginPostURI="https://login.telldus.com/openid/server?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.mode=checkid_setup&openid.return_to=http%3A%2F%2Fapi.telldus.com%2Fexplore%2Fclients%2Flist&openid.realm=http%3A%2F%2Fapi.telldus.com&openid.ns.sreg=http%3A%2F%2Fopenid.net%2Fextensions%2Fsreg%2F1.1&openid.sreg.required=email&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select#"
  131.         $turnOffURI="http://api.telldus.com/explore/device/turnOff"
  132.         $PostActionURI="http://api.telldus.com/explore/doCall"
  133.  
  134.         $TelldusWEB = Invoke-WebRequest $turnOffURI -SessionVariable Telldus
  135.  
  136.         $form = $TelldusWEB.Forms[0]
  137.         $form.Fields["email"] = $Username
  138.         $form.Fields["password"] = $Password
  139.  
  140.         $TelldusWEB = Invoke-WebRequest -Uri $LoginPostURI -WebSession $Telldus -Method POST -Body $form.Fields
  141.     }
  142.  
  143.     PROCESS {
  144.         $request = @{'group'='device';'method'= $Action;'param[id]'= $DeviceID;'responseAsXml'='xml'}
  145.  
  146.         [xml] $ActionResults=Invoke-WebRequest -Uri $PostActionURI -WebSession $Telldus -Method POST -Body $request
  147.  
  148.         $Results=$ActionResults.device.status -replace "\s"
  149.  
  150.         $returnObject = New-Object System.Object
  151.         $returnObject | Add-Member -Type NoteProperty -Name DeviceID -Value $DeviceID
  152.         $returnObject | Add-Member -Type NoteProperty -Name Action -Value $Action
  153.         $returnObject | Add-Member -Type NoteProperty -Name Results -Value $Results
  154.  
  155.         Write-Output $returnObject
  156.     }
  157. }
  158.  
  159. function Get-TDSensor
  160. {
  161.     <#
  162.     .SYNOPSIS
  163.     Retrieves all sensors associated with a Telldus Live! account.
  164.  
  165.     .DESCRIPTION
  166.     This command will list all sensors associated with an Telldus Live!-account and their current status and other information.
  167.  
  168.     .EXAMPLE
  169.     Get-TDSensor -Username myemail@telldus.com -Password MyTelldusPassword
  170.  
  171.     .EXAMPLE
  172.     Get-TDSensor -Username myemail@telldus.com -Password MyTelldusPassword | Format-Table
  173.  
  174.     .PARAMETER Username
  175.     The username (e-mail) used to log into Telldus Live!
  176.  
  177.     .PARAMETER Password
  178.     The password used to log into Telldus Live!
  179.  
  180.     #>
  181.  
  182.     param(
  183.       [Parameter(Mandatory=$True)] [string] $Username,
  184.       [Parameter(Mandatory=$True)] [string] $Password
  185.     )
  186.  
  187.     $LoginPostURI="https://login.telldus.com/openid/server?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.mode=checkid_setup&openid.return_to=http%3A%2F%2Fapi.telldus.com%2Fexplore%2Fclients%2Flist&openid.realm=http%3A%2F%2Fapi.telldus.com&openid.ns.sreg=http%3A%2F%2Fopenid.net%2Fextensions%2Fsreg%2F1.1&openid.sreg.required=email&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select#"
  188.     $turnOffURI="http://api.telldus.com/explore/device/turnOff"
  189.     $sensorListURI="http://api.telldus.com/explore/sensors/list"
  190.     $PostActionURI="http://api.telldus.com/explore/doCall"
  191.  
  192.     $TelldusWEB = Invoke-WebRequest $turnOffURI -SessionVariable Telldus
  193.  
  194.     $form = $TelldusWEB.Forms[0]
  195.     $form.Fields["email"] = $Username
  196.     $form.Fields["password"] = $Password
  197.  
  198.     $TelldusWEB = Invoke-WebRequest -Uri $LoginPostURI -WebSession $Telldus -Method POST -Body $form.Fields
  199.  
  200.     $SensorList=Invoke-WebRequest -Uri $sensorListURI -WebSession $Telldus
  201.     $SensorListForm=$SensorList.Forms
  202.  
  203.     $ActionResults=$null
  204.  
  205.     [xml] $ActionResults=Invoke-WebRequest -Uri $PostActionURI -WebSession $Telldus -Method POST -Body $SensorListForm.Fields
  206.     [datetime] $TelldusDate="1970-01-01 00:00:00"
  207.  
  208.     $TheResults=$ActionResults.sensors.ChildNodes
  209.  
  210.     foreach ($Result in $TheResults) {
  211.         $SensorInfo=$Result
  212.  
  213.         $DeviceID=$SensorInfo.id.trim()
  214.         $SensorName=$SensorInfo.name.trim()
  215.         $SensorLastUpdated=$SensorInfo.lastupdated.trim()
  216.         $SensorLastUpdatedDate=$TelldusDate.AddSeconds($SensorLastUpdated)
  217.         $clientid=$SensorInfo.client.trim()
  218.         $clientName=$SensorInfo.clientname.trim()
  219.         $sensoronline=$SensorInfo.online.trim()
  220.  
  221.         $returnObject = New-Object System.Object
  222.         $returnObject | Add-Member -Type NoteProperty -Name DeviceID -Value $DeviceID
  223.         $returnObject | Add-Member -Type NoteProperty -Name Name -Value $SensorName
  224.         $returnObject | Add-Member -Type NoteProperty -Name LocationID -Value $clientid
  225.         $returnObject | Add-Member -Type NoteProperty -Name LocationName -Value $clientName
  226.         $returnObject | Add-Member -Type NoteProperty -Name LastUpdate -Value $SensorLastUpdatedDate
  227.         $returnObject | Add-Member -Type NoteProperty -Name Online -Value $sensoronline
  228.  
  229.         Write-Output $returnObject
  230.     }
  231. }
  232.  
  233. function Get-TDSensorData
  234. {
  235.     <#
  236.     .SYNOPSIS
  237.     Retrieves the sensordata of specified sensor.
  238.  
  239.     .DESCRIPTION
  240.     This command will retrieve the sensordata associated with the specified ID.
  241.  
  242.     .EXAMPLE
  243.     Get-TDSensorData -Username myemail@telldus.com -Password MyTelldusPassword -DeviceID 123456
  244.  
  245.     .PARAMETER DeviceID
  246.     The DeviceID of the sensor which data you want to retrieve.
  247.  
  248.     .PARAMETER Username
  249.     The username (e-mail) used to log into Telldus Live!
  250.  
  251.     .PARAMETER Password
  252.     The password used to log into Telldus Live!
  253.  
  254.     #>
  255.  
  256.     [CmdletBinding()]
  257.     param(
  258.  
  259.       [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [Alias('id')] [string] $DeviceID,
  260.       [Parameter(Mandatory=$True)] [string] $Username,
  261.       [Parameter(Mandatory=$True)] [string] $Password
  262.     )
  263.  
  264.     BEGIN {
  265.         $LoginPostURI="https://login.telldus.com/openid/server?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.mode=checkid_setup&openid.return_to=http%3A%2F%2Fapi.telldus.com%2Fexplore%2Fclients%2Flist&openid.realm=http%3A%2F%2Fapi.telldus.com&openid.ns.sreg=http%3A%2F%2Fopenid.net%2Fextensions%2Fsreg%2F1.1&openid.sreg.required=email&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select#"
  266.         $turnOffURI="http://api.telldus.com/explore/device/turnOff"
  267.         $sensorDataURI="http://api.telldus.com/explore/sensor/info"
  268.         $PostActionURI="http://api.telldus.com/explore/doCall"
  269.  
  270.         $TelldusWEB = Invoke-WebRequest $turnOffURI -SessionVariable Telldus
  271.  
  272.         $form = $TelldusWEB.Forms[0]
  273.         $form.Fields["email"] = $Username
  274.         $form.Fields["password"] = $Password
  275.  
  276.         $TelldusWEB = Invoke-WebRequest -Uri $LoginPostURI -WebSession $Telldus -Method POST -Body $form.Fields
  277.     }
  278.  
  279.     PROCESS {
  280.         $request = @{'group'='sensor';'method'= 'info';'param[id]'= $DeviceID;'responseAsXml'='xml'}
  281.  
  282.         [xml] $ActionResults=Invoke-WebRequest -Uri $PostActionURI -WebSession $Telldus -Method POST -Body $request
  283.         [datetime] $TelldusDate="1970-01-01 00:00:00"
  284.  
  285.         $SensorInfo=$ActionResults.sensor
  286.         $SensorData=$ActionResults.sensor.data
  287.  
  288.         $SensorName=$SensorInfo.name.trim()
  289.         $SensorLastUpdated=$SensorInfo.lastupdated.trim()
  290.         $SensorLastUpdatedDate=$TelldusDate.AddSeconds($SensorLastUpdated)
  291.         $clientName=$SensorInfo.clientname.trim()
  292.         $SensorTemp=($SensorData | ? name -eq "temp").value | select -First 1
  293.         $SensorHumidity=($SensorData | ? name -eq "humidity").value | select -First 1
  294.  
  295.         $returnObject = New-Object System.Object
  296.         $returnObject | Add-Member -Type NoteProperty -Name DeviceID -Value $DeviceID
  297.         $returnObject | Add-Member -Type NoteProperty -Name Name -Value $SensorName
  298.         $returnObject | Add-Member -Type NoteProperty -Name LocationName -Value $clientName
  299.         $returnObject | Add-Member -Type NoteProperty -Name Temperature -Value $SensorTemp
  300.         $returnObject | Add-Member -Type NoteProperty -Name Humidity -Value $SensorHumidity
  301.         $returnObject | Add-Member -Type NoteProperty -Name LastUpdate -Value $SensorLastUpdatedDate
  302.  
  303.         Write-Output $returnObject
  304.     }
  305. }
  306.  
  307. function Set-TDDimmer
  308. {
  309.     <#
  310.     .SYNOPSIS
  311.     Dims a device to a certain level.
  312.  
  313.     .DESCRIPTION
  314.     This command can set the dimming level of a device to through the Telldus Live! service.
  315.  
  316.     .EXAMPLE
  317.     Set-TDDimmer -Username myemail@telldus.com -Password MyTelldusPassword -DeviceID 123456 -Level 89
  318.  
  319.     .EXAMPLE
  320.     Set-TDDimmer -Username myemail@telldus.com -Password MyTelldusPassword -DeviceID 123456 -Level 180
  321.  
  322.     .PARAMETER Username
  323.     The username (e-mail) used to log into Telldus Live!
  324.  
  325.     .PARAMETER Password
  326.     The password used to log into Telldus Live!
  327.  
  328.     .PARAMETER DeviceID
  329.     The DeviceID of the device to dim. (Pipelining possible)
  330.  
  331.     .PARAMETER Level
  332.     What level to dim to. Possible values are 0 - 255.
  333.  
  334.     #>
  335.  
  336.     [CmdletBinding()]
  337.     param(
  338.  
  339.       [Parameter(Mandatory=$True,
  340.                  ValueFromPipeline=$true,
  341.                  ValueFromPipelineByPropertyName=$true,
  342.                  HelpMessage="Enter the DeviceID.")] [Alias('id')] [string] $DeviceID,
  343.  
  344.       [Parameter(Mandatory=$True,
  345.                  HelpMessage="Enter the level to dim to between 0 and 255.")]
  346.       [ValidateRange(0,255)]
  347.       [int] $Level,
  348.  
  349.       [Parameter(Mandatory=$True)] [string] $Username,
  350.       [Parameter(Mandatory=$True)] [string] $Password
  351.     )
  352.  
  353.  
  354.     BEGIN {
  355.  
  356.         $LoginPostURI="https://login.telldus.com/openid/server?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.mode=checkid_setup&openid.return_to=http%3A%2F%2Fapi.telldus.com%2Fexplore%2Fclients%2Flist&openid.realm=http%3A%2F%2Fapi.telldus.com&openid.ns.sreg=http%3A%2F%2Fopenid.net%2Fextensions%2Fsreg%2F1.1&openid.sreg.required=email&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select#"
  357.         $turnOffURI="http://api.telldus.com/explore/device/turnOff"
  358.         $PostActionURI="http://api.telldus.com/explore/doCall"
  359.  
  360.         $TelldusWEB = Invoke-WebRequest $turnOffURI -SessionVariable Telldus
  361.  
  362.         $form = $TelldusWEB.Forms[0]
  363.         $form.Fields["email"] = $Username
  364.         $form.Fields["password"] = $Password
  365.  
  366.         $TelldusWEB = Invoke-WebRequest -Uri $LoginPostURI -WebSession $Telldus -Method POST -Body $form.Fields
  367.  
  368.     }
  369.  
  370.     PROCESS {
  371.         $Action='dim'
  372.  
  373.         $request = @{'group'='device';'method'= $Action;'param[id]'= $DeviceID;'param[level]'= $Level;'responseAsXml'='xml'}
  374.  
  375.         [xml] $ActionResults=Invoke-WebRequest -Uri $PostActionURI -WebSession $Telldus -Method POST -Body $request
  376.  
  377.         $Results=$ActionResults.device.status -replace "\s"
  378.  
  379.         $returnObject = New-Object System.Object
  380.         $returnObject | Add-Member -Type NoteProperty -Name DeviceID -Value $DeviceID
  381.         $returnObject | Add-Member -Type NoteProperty -Name Action -Value $Action
  382.         $returnObject | Add-Member -Type NoteProperty -Name Results -Value $Results
  383.  
  384.         Write-Output $returnObject
  385.     }
  386. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement