SHARE
TWEET

Telldus Live PowerShell Module

mariussm Aug 1st, 2015 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Install DevDefined.OAuth by using nuget.exe (see http://goodworkaround.com/node/73 for hint)
  2. Add-Type -Path "C:\graphapi\DevDefined.OAuth.0.2\lib\DevDefined.OAuth.dll"
  3.  
  4. # Get the following info from http://api.telldus.com/keys/index
  5. $Global:TelldusOAuthToken = @{
  6.     ConsumerKey = "FEH.....................NU"
  7.     ConsumerSecret = "ZUX....................EF"
  8.     Token = "52c423.............................3b906"
  9.     TokenSecret = "65e4........................716"
  10. }
  11.  
  12.  
  13. <#
  14. .Synopsis
  15.    Uses the $Global:TelldusOAuthToken hashmap to create OAuthSession
  16. .DESCRIPTION
  17.    $Global:TelldusOAuthToken contains the ConsumerKey, ConsumerSecret, Token and TokenSecret. These are used to create a OAuthSession.
  18. .EXAMPLE
  19.    Get-TelldusConsumerRequest
  20. #>
  21. function Get-TelldusConsumerRequest
  22. {
  23.     [CmdletBinding()]
  24.     [OutputType([DevDefined.OAuth.Consumer.ConsumerRequest])]
  25.     Param
  26.     (
  27.     )
  28.  
  29.     Begin
  30.     {
  31.     }
  32.     Process
  33.     {
  34.         # Create the oauth consumer
  35.         $Consumer = New-Object devdefined.oauth.consumer.oauthconsumercontext
  36.         $Consumer.ConsumerKey = $Global:TelldusOAuthToken.ConsumerKey
  37.         $Consumer.ConsumerSecret = $Global:TelldusOAuthToken.ConsumerSecret
  38.         $Consumer.SignatureMethod = [devdefined.oauth.framework.signaturemethod]::HmacSha1
  39.  
  40.         # Create the access token
  41.         $accessToken = new-object DevDefined.OAuth.Framework.TokenBase
  42.         $accessToken.ConsumerKey = $Global:TelldusOAuthToken.ConsumerKey
  43.         $accessToken.Token = $Global:TelldusOAuthToken.Token
  44.         $accessToken.TokenSecret = $Global:TelldusOAuthToken.TokenSecret
  45.  
  46.         $session = new-object DevDefined.OAuth.Consumer.OAuthSession $Consumer, $null, $null, $null
  47.         return $session.Request($accessToken)
  48.     }
  49.     End
  50.     {
  51.     }
  52. }
  53.  
  54.  
  55. <#
  56. .Synopsis
  57.    Returns all devices from Telldus Live
  58. .DESCRIPTION
  59.    Returns all devices from Telldus Live
  60. .EXAMPLE
  61.    Get-TelldusLiveDevice
  62. #>
  63. function Get-TelldusLiveDevice
  64. {
  65.     [CmdletBinding()]
  66.     Param
  67.     (
  68.     )
  69.  
  70.     Begin
  71.     {
  72.     }
  73.     Process
  74.     {
  75.         $request = Get-TelldusConsumerRequest
  76.  
  77.         $request.Context.RequestMethod = 'GET'
  78.         $request.Context.RawUri = [Uri]'http://api.telldus.com/xml/devices/list?supportedMethods=19'
  79.        
  80.         $result = [xml][DevDefined.OAuth.Consumer.ConsumerRequestExtensions]::ReadBody($request)
  81.  
  82.         foreach($Device in $result.devices.device) {
  83.             New-Object -TypeName PSCustomObject -Property @{
  84.                 Name = $Device.name
  85.                 State = switch ($Device.state) {
  86.                         1 { "On" }
  87.                         2 { "Off" }
  88.                     16 { "Dimmed" }
  89.                     default { "Unknown" }
  90.                 }
  91.                 ID = $Device.ID
  92.                 StateValue = $Device.StateValue
  93.                 Methods = switch ($Device.methods){
  94.                     3 { "On/Off" }
  95.                     19 { "On/Off/Dim" }
  96.                     default { "Unknown" }
  97.                 }
  98.                 Type = $Device.Type
  99.                 Client = $Device.Client
  100.                 ClientName = $Device.ClientName
  101.                 Online = switch ($Device.online){
  102.                     0 { $false }
  103.                     1 { $true }
  104.                 }
  105.             }
  106.         }
  107.     }
  108.     End
  109.     {
  110.     }
  111. }
  112.  
  113.  
  114.  
  115.  
  116. <#
  117. .Synopsis
  118.    Sends turn on/off request to Telldus Live device based on the ID of the device
  119. .DESCRIPTION
  120.    Sends turn on/off request to Telldus Live device based on the ID of the device
  121. .EXAMPLE
  122.    Set-TelldusLiveDevice -ID "838822" -Action "turnOn"
  123. .EXAMPLE
  124.    Get-TelldusLiveDevice | where{$_.Methods -eq "On/off"} | Set-TelldusLiveDevice -Action turnOn
  125. #>
  126. function Set-TelldusLiveDevice
  127. {
  128.     [CmdletBinding()]
  129.     [Alias()]
  130.     [OutputType([int])]
  131.     param(
  132.           [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
  133.           [string] $ID,
  134.  
  135.           [Parameter(Mandatory=$True)]
  136.           [ValidateSet("turnOff","turnOn")]
  137.           [string] $Action
  138.     )
  139.  
  140.     Begin
  141.     {
  142.     }
  143.     Process
  144.     {
  145.         $request = Get-TelldusConsumerRequest
  146.  
  147.         $request.Context.RequestMethod = 'GET'
  148.  
  149.         $request.Context.RawUri = [Uri]('http://api.telldus.com/xml/device/{0}?id={1}' -f $Action, $ID)
  150.        
  151.         $result = [xml][DevDefined.OAuth.Consumer.ConsumerRequestExtensions]::ReadBody($request)
  152.  
  153.         New-Object -TypeName PSCustomObject -Property @{
  154.             ID = $ID
  155.             Action = $Action
  156.             Result = $result.device.status.Trim()
  157.         }
  158.     }
  159.     End
  160.     {
  161.     }
  162. }
  163.  
  164.  
  165. <#
  166. .Synopsis
  167.    Sets the dimming value of a Telldus Live device
  168. .DESCRIPTION
  169.    Sets the dimming value of a Telldus Live device
  170. .EXAMPLE
  171.    Set-TelldusLiveDimmer -ID "838818" -Level 255
  172. .EXAMPLE
  173.    Get-TelldusLiveDevice | where{$_.Methods -eq "On/Off/Dim"} | Set-TelldusLiveDimmer -Level 128
  174. #>
  175. function Set-TelldusLiveDimmer
  176. {
  177.     [CmdletBinding()]
  178.     param(
  179.           [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
  180.           [string] $ID,
  181.  
  182.           [Parameter(Mandatory=$True)]
  183.           [ValidateRange(0,255)]
  184.           [int] $Level
  185.     )
  186.  
  187.     Begin
  188.     {
  189.     }
  190.     Process
  191.     {
  192.         $request = Get-TelldusConsumerRequest
  193.  
  194.         $request.Context.RequestMethod = 'GET'
  195.  
  196.         $request.Context.RawUri = [Uri]('http://api.telldus.com/xml/device/dim?id={1}&level={0}' -f $Level, $ID)
  197.        
  198.         $result = [xml][DevDefined.OAuth.Consumer.ConsumerRequestExtensions]::ReadBody($request)
  199.  
  200.         New-Object -TypeName PSCustomObject -Property @{
  201.             ID = $ID
  202.             Level = $Level
  203.             Result = $result.device.status.Trim()
  204.         }
  205.     }
  206.     End
  207.     {
  208.     }
  209. }
  210.  
  211.  
  212.  
  213.  
  214. <#
  215. .Synopsis
  216.    Returns all sensors from Telldus Live
  217. .DESCRIPTION
  218.    Returns all sensors from Telldus Live
  219. .EXAMPLE
  220.    Get-TelldusLiveSensor
  221. #>
  222. function Get-TelldusLiveSensor
  223. {
  224.     [CmdletBinding()]
  225.     Param
  226.     (
  227.     )
  228.  
  229.     Begin
  230.     {
  231.     }
  232.     Process
  233.     {
  234.         $request = Get-TelldusConsumerRequest
  235.  
  236.         $request.Context.RequestMethod = 'GET'
  237.         $request.Context.RawUri = [Uri]'http://api.telldus.com/xml/sensors/list'
  238.        
  239.  
  240.         $result = [xml][DevDefined.OAuth.Consumer.ConsumerRequestExtensions]::ReadBody($request)
  241.  
  242.         foreach($Sensor in $result.sensors.sensor) {
  243.             New-Object -TypeName PSCustomObject -Property @{
  244.                 Name = $Sensor.name
  245.                 Ignored = switch ($Sensor.Ignored){
  246.                     0 { $false }
  247.                     1 { $true }
  248.                 }
  249.                 Client = $Sensor.Client
  250.                 ClientName = $Sensor.ClientName
  251.                 ID = $Sensor.ID
  252.                 Battery = $Sensor.Battery
  253.                 Protocol = $Sensor.protocol
  254.                 keepHistory = switch ($Sensor.keepHistory){
  255.                     0 { $false }
  256.                     1 { $true }
  257.                 }
  258.                 Editable = switch ($Sensor.editable){
  259.                     0 { $false }
  260.                     1 { $true }
  261.                 }
  262.                 Online = switch ($Sensor.online){
  263.                     0 { $false }
  264.                     1 { $true }
  265.                 }
  266.                 Model = $Sensor.model
  267.                 SensorId = $Sensor.sensorId
  268.                 LastUpdated = ([datetime] "1970-01-01 00:00:00").AddSeconds([int] $Sensor.lastUpdated)
  269.             }
  270.         }
  271.     }
  272.     End
  273.     {
  274.     }
  275. }
  276.  
  277.  
  278.  
  279.  
  280. <#
  281. .Synopsis
  282.    Returns the sensordata for a Telldus Live sensor
  283. .DESCRIPTION
  284.    Returns the sensordata for a Telldus Live sensor
  285. .EXAMPLE
  286.    Get-TelldusLiveSensorData -ID "4049354"
  287. .EXAMPLE
  288.    Get-TelldusLiveSensor | Get-TelldusLiveSensorData
  289. #>
  290. function Get-TelldusLiveSensorData
  291. {
  292.     [CmdletBinding()]
  293.     [Alias()]
  294.     param(
  295.           [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
  296.           [string] $ID
  297.     )
  298.  
  299.     Begin
  300.     {
  301.     }
  302.     Process
  303.     {
  304.  
  305.         $request = Get-TelldusConsumerRequest
  306.  
  307.         $request.Context.RequestMethod = 'GET'
  308.  
  309.         $request.Context.RawUri = [Uri]('http://api.telldus.com/xml/sensor/info?id={0}' -f $ID)
  310.        
  311.         $result = [xml][DevDefined.OAuth.Consumer.ConsumerRequestExtensions]::ReadBody($request)
  312.         $Sensor = $result.sensor
  313.        
  314.         $props = @{
  315.             ID = $Sensor.ID.Trim()
  316.         }
  317.  
  318.         $temperature = $Sensor.data | where{$_.name -eq "temp"}
  319.         if($temperature.value)
  320.         {
  321.             $props["Temperature"] = [Double] $temperature.value    
  322.         }
  323.         else
  324.         {
  325.             $props["Temperature"] = -100
  326.         }
  327.  
  328.         $humidity = $Sensor.data | where{$_.name -eq "humidity"}
  329.         if($humidity.value)
  330.         {
  331.             $props["Humidity"] = [Double] $humidity.value  
  332.         }
  333.         else
  334.         {
  335.             $props["Humidity"] =
  336.             -100
  337.         }
  338.  
  339.         New-Object -TypeName PSCustomObject -Property $props
  340.        
  341.     }
  342.     End
  343.     {
  344.     }
  345. }
  346.  
  347.  
  348.  
  349.  
  350. <#
  351. .Synopsis
  352.    Returns all sensors from Telldus Live, including temperature and humidity
  353. .DESCRIPTION
  354.    Returns all sensors from Telldus Live, including temperature and humidity
  355. .EXAMPLE
  356.    Get-TelldusLiveSensorDataAll
  357. #>
  358. function Get-TelldusLiveSensorDataAll
  359. {
  360.     [CmdletBinding()]
  361.     Param
  362.     ()
  363.  
  364.     Begin
  365.     {
  366.     }
  367.     Process
  368.     {
  369.         Foreach($Sensor in Get-TelldusLiveSensor)
  370.         {
  371.             $SensorData = $Sensor | Get-TelldusLiveSensorData
  372.             $Sensor | Add-Member -MemberType NoteProperty -Name Temperature -Value $SensorData.Temperature
  373.             $Sensor | Add-Member -MemberType NoteProperty -Name Humidity -Value $SensorData.Humidity
  374.             $Sensor
  375.         }
  376.     }
  377.     End
  378.     {
  379.     }
  380. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top