PoshCode Logo PowerShell Code Repository

Home Automation Module by DollarUnderscore 5 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/5013"></script>download | new post

This is an updated version of the home automation module. The main difference is that some output is removed (no news is good news…) if “-Verbose” is not used and that it has a separate cmdlet for connecting to Telldus Live! (Connect-TelldusLive) which uses a PSCredential.

This removes the need of specififying credentials on all commands which helps a lot with both performance and usability.

For more information see my blog at:
http://dollarunderscore.azurewebsites.net

  1. #========================================================================
  2. # Created By: Anders Wahlqvist
  3. # Website: DollarUnderscore (http://dollarunderscore.azurewebsites.net)
  4. #========================================================================
  5.  
  6.  
  7. function Connect-TelldusLive
  8. {
  9.     [cmdletbinding()]
  10.     param(
  11.           [Parameter(Mandatory=$True)]
  12.           [System.Management.Automation.PSCredential] $Credential)
  13.  
  14.  
  15.     $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#"
  16.     $turnOffURI="http://api.telldus.com/explore/device/turnOff"
  17.  
  18.     $TelldusWEB = Invoke-WebRequest $turnOffURI -SessionVariable Global:Telldus
  19.  
  20.     $form = $TelldusWEB.Forms[0]
  21.     $form.Fields["email"] = $Credential.UserName
  22.     $form.Fields["password"] = $Credential.GetNetworkCredential().Password
  23.  
  24.     $TelldusWEB = Invoke-WebRequest -Uri $LoginPostURI -WebSession $Global:Telldus -Method POST -Body $form.Fields
  25.  
  26.     $form = $null
  27.  
  28.     [gc]::Collect()
  29. }
  30.  
  31. function Get-TDDevice
  32. {
  33.     <#
  34.     .SYNOPSIS
  35.     Retrieves all devices associated with a Telldus Live! account.
  36.  
  37.     .DESCRIPTION
  38.     This command will list all devices associated with an Telldus Live!-account and their current status and other information.
  39.  
  40.     .EXAMPLE
  41.     Get-TDDevice
  42.  
  43.     .EXAMPLE
  44.     Get-TDDevice | Format-Table
  45.  
  46.     #>
  47.  
  48.     if ($Telldus -eq $null) {
  49.         Write-Error "You must first connect using the Connect-TelldusLive cmdlet"
  50.         return
  51.     }
  52.  
  53.     $PostActionURI="http://api.telldus.com/explore/doCall"
  54.     $Action='list'
  55.     $SupportedMethods=19
  56.  
  57.     $request = @{'group'='devices';'method'= $Action;'param[supportedMethods]'= $SupportedMethods;'responseAsXml'='xml'}
  58.  
  59.     [xml] $ActionResults=Invoke-WebRequest -Uri $PostActionURI -WebSession $Global:Telldus -Method POST -Body $request
  60.  
  61.     $Results=$ActionResults.devices.ChildNodes
  62.  
  63.     foreach ($Result in $Results)
  64.     {
  65.         $PropertiesToOutput = @{
  66.                              'Name' = $Result.name;
  67.                              'State' = switch ($Result.state)
  68.                                        {
  69.                                              1 { "On" }
  70.                                              2 { "Off" }
  71.                                             16 { "Dimmed" }
  72.                                             default { "Unknown" }
  73.                                        }
  74.                              'DeviceID' = $Result.id;
  75.                              
  76.  
  77.                              'Statevalue' = $Result.statevalue
  78.                              'Methods' = switch ($Result.methods)
  79.                                          {
  80.                                              3 { "On/Off" }
  81.                                             19 { "On/Off/Dim" }
  82.                                             default { "Unknown" }
  83.                                          }
  84.                              'Type' = $Result.type;
  85.                              'Client' = $Result.client;
  86.                              'ClientName' = $Result.clientName;
  87.                              'Online' = switch ($Result.online)
  88.                                         {
  89.                                             0 { $false }
  90.                                             1 { $true }
  91.                                         }
  92.                              }
  93.  
  94.         $returnObject = New-Object -TypeName PSObject -Property $PropertiesToOutput
  95.  
  96.         Write-Output $returnObject | Select-Object Name, DeviceID, State, Statevalue, Methods, Type, ClientName, Client, Online
  97.     }
  98. }
  99.  
  100. function Set-TDDevice
  101. {
  102.  
  103.     <#
  104.     .SYNOPSIS
  105.     Turns a device on or off.
  106.  
  107.     .DESCRIPTION
  108.     This command can set the state of a device to on or off through the Telldus Live! service.
  109.  
  110.     .EXAMPLE
  111.     Set-TDDevice -DeviceID 123456 -Action turnOff
  112.  
  113.     .EXAMPLE
  114.     Set-TDDevice -DeviceID 123456 -Action turnOn
  115.  
  116.     .PARAMETER DeviceID
  117.     The DeviceID of the device to turn off or on. (Pipelining possible)
  118.  
  119.     .PARAMETER Action
  120.     What to do with that device. Possible values are "turnOff" or "turnOn".
  121.  
  122.     #>
  123.  
  124.     [CmdletBinding()]
  125.     param(
  126.  
  127.       [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
  128.       [Alias('id')]
  129.       [string] $DeviceID,
  130.       [Parameter(Mandatory=$True)]
  131.       [ValidateSet("turnOff","turnOn")]
  132.       [string] $Action)
  133.  
  134.  
  135.     BEGIN {
  136.         if ($Telldus -eq $null) {
  137.             Write-Error "You must first connect using the Connect-TelldusLive cmdlet"
  138.             return
  139.         }
  140.  
  141.         $PostActionURI = "http://api.telldus.com/explore/doCall"
  142.     }
  143.  
  144.     PROCESS {
  145.  
  146.         $request = @{'group'='device';'method'= $Action;'param[id]'= $DeviceID;'responseAsXml'='xml'}
  147.  
  148.         [xml] $ActionResults=Invoke-WebRequest -Uri $PostActionURI -WebSession $Global:Telldus -Method POST -Body $request
  149.  
  150.         $Results=$ActionResults.device.status -replace "\s"
  151.  
  152.         Write-Verbose "Doing action $Action on device $DeviceID. Result: $Results."
  153.     }
  154. }
  155.  
  156. function Get-TDSensor
  157. {
  158.     <#
  159.     .SYNOPSIS
  160.     Retrieves all sensors associated with a Telldus Live! account.
  161.  
  162.     .DESCRIPTION
  163.     This command will list all sensors associated with an Telldus Live!-account and their current status and other information.
  164.  
  165.     .EXAMPLE
  166.     Get-TDSensor
  167.  
  168.     .EXAMPLE
  169.     Get-TDSensor | Format-Table
  170.  
  171.     #>
  172.  
  173.     if ($Telldus -eq $null) {
  174.         Write-Error "You must first connect using the Connect-TelldusLive cmdlet"
  175.         return
  176.     }
  177.  
  178.     $sensorListURI="http://api.telldus.com/explore/sensors/list"
  179.     $PostActionURI="http://api.telldus.com/explore/doCall"
  180.  
  181.  
  182.     $SensorList=Invoke-WebRequest -Uri $sensorListURI -WebSession $Global:Telldus
  183.     $SensorListForm=$SensorList.Forms
  184.  
  185.     $ActionResults=$null
  186.  
  187.     [xml] $ActionResults=Invoke-WebRequest -Uri $PostActionURI -WebSession $Global:Telldus -Method POST -Body $SensorListForm.Fields
  188.     [datetime] $TelldusDate="1970-01-01 00:00:00"
  189.  
  190.     $TheResults=$ActionResults.sensors.ChildNodes
  191.  
  192.     foreach ($Result in $TheResults) {
  193.         $SensorInfo=$Result
  194.  
  195.         $DeviceID=$SensorInfo.id.trim()
  196.         $SensorName=$SensorInfo.name.trim()
  197.         $SensorLastUpdated=$SensorInfo.lastupdated.trim()
  198.         $SensorLastUpdatedDate=$TelldusDate.AddSeconds($SensorLastUpdated)
  199.         $clientid=$SensorInfo.client.trim()
  200.         $clientName=$SensorInfo.clientname.trim()
  201.         $sensoronline=$SensorInfo.online.trim()
  202.  
  203.         $returnObject = New-Object System.Object
  204.         $returnObject | Add-Member -Type NoteProperty -Name DeviceID -Value $DeviceID
  205.         $returnObject | Add-Member -Type NoteProperty -Name Name -Value $SensorName
  206.         $returnObject | Add-Member -Type NoteProperty -Name LocationID -Value $clientid
  207.         $returnObject | Add-Member -Type NoteProperty -Name LocationName -Value $clientName
  208.         $returnObject | Add-Member -Type NoteProperty -Name LastUpdate -Value $SensorLastUpdatedDate
  209.         $returnObject | Add-Member -Type NoteProperty -Name Online -Value $sensoronline
  210.  
  211.         Write-Output $returnObject
  212.     }
  213. }
  214.  
  215. function Get-TDSensorData
  216. {
  217.     <#
  218.     .SYNOPSIS
  219.     Retrieves the sensordata of specified sensor.
  220.  
  221.     .DESCRIPTION
  222.     This command will retrieve the sensordata associated with the specified ID.
  223.  
  224.     .EXAMPLE
  225.     Get-TDSensorData -DeviceID 123456
  226.  
  227.     .PARAMETER DeviceID
  228.     The DeviceID of the sensor which data you want to retrieve.
  229.  
  230.     #>
  231.  
  232.     [CmdletBinding()]
  233.     param(
  234.  
  235.       [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [Alias('id')] [string] $DeviceID)
  236.  
  237.     BEGIN {
  238.         if ($Telldus -eq $null) {
  239.             Write-Error "You must first connect using the Connect-TelldusLive cmdlet"
  240.             return
  241.         }
  242.  
  243.         $sensorDataURI="http://api.telldus.com/explore/sensor/info"
  244.         $PostActionURI="http://api.telldus.com/explore/doCall"
  245.     }
  246.  
  247.     PROCESS {
  248.         $request = @{'group'='sensor';'method'= 'info';'param[id]'= $DeviceID;'responseAsXml'='xml'}
  249.  
  250.         [xml] $ActionResults=Invoke-WebRequest -Uri $PostActionURI -WebSession $Global:Telldus -Method POST -Body $request
  251.         [datetime] $TelldusDate="1970-01-01 00:00:00"
  252.  
  253.         $SensorInfo=$ActionResults.sensor
  254.         $SensorData=$ActionResults.sensor.data
  255.  
  256.         $SensorName=$SensorInfo.name.trim()
  257.         $SensorLastUpdated=$SensorInfo.lastupdated.trim()
  258.         $SensorLastUpdatedDate=$TelldusDate.AddSeconds($SensorLastUpdated)
  259.         $clientName=$SensorInfo.clientname.trim()
  260.         $SensorTemp=($SensorData | ? name -eq "temp").value | select -First 1
  261.         $SensorHumidity=($SensorData | ? name -eq "humidity").value | select -First 1
  262.  
  263.         $returnObject = New-Object System.Object
  264.         $returnObject | Add-Member -Type NoteProperty -Name DeviceID -Value $DeviceID
  265.         $returnObject | Add-Member -Type NoteProperty -Name Name -Value $SensorName
  266.         $returnObject | Add-Member -Type NoteProperty -Name LocationName -Value $clientName
  267.         $returnObject | Add-Member -Type NoteProperty -Name Temperature -Value $SensorTemp
  268.         $returnObject | Add-Member -Type NoteProperty -Name Humidity -Value $SensorHumidity
  269.         $returnObject | Add-Member -Type NoteProperty -Name LastUpdate -Value $SensorLastUpdatedDate
  270.  
  271.         Write-Output $returnObject
  272.     }
  273. }
  274.  
  275. function Set-TDDimmer
  276. {
  277.     <#
  278.     .SYNOPSIS
  279.     Dims a device to a certain level.
  280.  
  281.     .DESCRIPTION
  282.     This command can set the dimming level of a device to through the Telldus Live! service.
  283.  
  284.     .EXAMPLE
  285.     Set-TDDimmer -DeviceID 123456 -Level 89
  286.  
  287.     .EXAMPLE
  288.     Set-TDDimmer -Level 180
  289.  
  290.     .PARAMETER DeviceID
  291.     The DeviceID of the device to dim. (Pipelining possible)
  292.  
  293.     .PARAMETER Level
  294.     What level to dim to. Possible values are 0 - 255.
  295.  
  296.     #>
  297.  
  298.     [CmdletBinding()]
  299.     param(
  300.  
  301.       [Parameter(Mandatory=$True,
  302.                  ValueFromPipeline=$true,
  303.                  ValueFromPipelineByPropertyName=$true,
  304.                  HelpMessage="Enter the DeviceID.")] [Alias('id')] [string] $DeviceID,
  305.  
  306.       [Parameter(Mandatory=$True,
  307.                  HelpMessage="Enter the level to dim to between 0 and 255.")]
  308.       [ValidateRange(0,255)]
  309.       [int] $Level)
  310.  
  311.  
  312.     BEGIN {
  313.  
  314.         if ($Telldus -eq $null) {
  315.             Write-Error "You must first connect using the Connect-TelldusLive cmdlet"
  316.             return
  317.         }
  318.  
  319.         $PostActionURI="http://api.telldus.com/explore/doCall"
  320.         $Action='dim'
  321.     }
  322.  
  323.     PROCESS {
  324.  
  325.         $request = @{'group'='device';'method'= $Action;'param[id]'= $DeviceID;'param[level]'= $Level;'responseAsXml'='xml'}
  326.  
  327.         [xml] $ActionResults=Invoke-WebRequest -Uri $PostActionURI -WebSession $Global:Telldus -Method POST -Body $request
  328.  
  329.         $Results=$ActionResults.device.status -replace "\s"
  330.  
  331.         Write-Verbose "Dimming device $DeviceID to level $Level. Result: $Results."
  332.     }
  333. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me