PowerShell based cmdlets / Commands

Moderator: Telldus

Post Reply
maui
Posts: 1
Joined: Thu Jan 18, 2018 11:08 am

PowerShell based cmdlets / Commands

Post by maui » Thu Jan 18, 2018 11:08 am

Hi all, :wave:

First time poster here! Using a Tellstick Net device for automating more and more of my home! It's great that you try and keep things as open as possible so new ways of integrating your product can be developed! Thanks.

Whilst it appears there's a fair amount of automation for the Tellstick / Duo, the Tellstick net seems much newer, and so far less developed. In my setup, I've a Media Center / Logitech Harmony remote, which I would like to do some automation against and ideally sequences (such as theatre mode, dim all lights etc.) - Ideally this will eventually be included into eventghost or another commander which would launch this as a script.

I'm a PowerShell programmer amongst many things at work, so thought would bridge a gap by creating commands / cmdlets to automate some basic power on / off / dim commands. I've managed to get my way through oAuth to autenticate against the API site, and now on my way to creating some cmdlets. I've still got some ways to allow the programming to do public API and authentication - but wanted to get some thoughts / feedback on what people may like to see.

So far I've got the following cmdlets:

Get-TDDevices - No options, just collects all devices associated to your account. returns a table (object) containing the ID, Name, State, and StateValue

Set-TDPower
- Options: operation (currently only turnon, and turnoff)
id (id of device to perform command against)
- This cmdlet allows you to specify a single ID to perform a turn on / off command against.
- The cmdlet returns the XML response back so you can understand operation success

Set-TDDimmer
- Options: level (currently between 0-255 - would % be of interest to people?)
id (id of device to perform command against)
- This cmdlet allows you to specify a single ID, and the level (between 0-255) to dim the device.
- The cmdlet returns the XML response back

I've still some more integration to go, but you can do stuff like:

All devices power off: Get-TDDevices | ForEach-Object { Set-TDPower -id $_.id -operation turnoff }
Power on Devices with specifc name: Get-TDDevices | ? { $_.name -like "*Room*" } | ForEach-Object { Set-TDPower -id $_.id -operation turnon }


So my question to everyone to help understand if this would help people in building further automation, if there is any thoughts, or inclusions you would like to see?

My second question, is if anyone has any thoughts on how I go about getting Authorization / Tokens in a text based console - or what the process is to authenticate users, as it seems most of the articles point to URL redirection?


Thanks in advance! Hope this helps others too! :)

Klingan
Posts: 4
Joined: Thu Jan 18, 2018 11:08 am

Re: PowerShell based cmdlets / Commands

Post by Klingan » Thu Jan 18, 2018 11:08 am

Hey!

I love the idé of being able to use powershell to control my devices!
Please post updates on how your project is going =)
Would really like to test this!

c8h10n4o2
Posts: 83
Joined: Thu Jan 18, 2018 11:08 am
Contact:

Re: PowerShell based cmdlets / Commands

Post by c8h10n4o2 » Thu Jan 18, 2018 11:08 am

This would indeed be awesome.

I'm glad to help if you want to (I'm doing quite a lot of powershell at work and home), I started looking at this but did'nt have the time to learn how to solve the oauth authentication... If you wan't to share that part of your code I'd obviously be very grateful :wave:

Let's not let this idea die! :D

c8h10n4o2
Posts: 83
Joined: Thu Jan 18, 2018 11:08 am
Contact:

Re: PowerShell based cmdlets / Commands

Post by c8h10n4o2 » Thu Jan 18, 2018 11:08 am

Just wanted to get back and say that I finally got some time to look at this...
I created a powershell script that does what I want now, it needs to be turned into proper cmdlets/modules, create output that is usable for piping and so on. But the basic functionality works now!

The syntax right now is:
.\ControllTellstick.ps1 -Username someone@somwhere.com -Password P@ssword -DeviceID 123456 -Action turnOff

and it returns the status from the xml response ("success" for example...)
PowerShell v3 is needed since it uses the "Invoke-WebRequest"-cmdlet.

I will try to finalize this asap! :)

At least I will have way more use for this product when it's controllable from powershell. (or any cmdline tool in windows...)

c8h10n4o2
Posts: 83
Joined: Thu Jan 18, 2018 11:08 am
Contact:

Re: PowerShell based cmdlets / Commands

Post by c8h10n4o2 » Thu Jan 18, 2018 11:08 am

Done! :)

I got some testing to do (I've done plenty already...), and probably some naming corrections to make the scripts consistant. But at least the functionality is there...

Dump of the Telldus powershell module in action: (my account info is added to the $username and $password variables, sorry for most devices being named in swedish...)
Image

It is of course possible to specify a device id directly, the above example just shows how the pipelining works, and that everything is returned as objects.

Any thoughts?

c8h10n4o2
Posts: 83
Joined: Thu Jan 18, 2018 11:08 am
Contact:

Re: PowerShell based cmdlets / Commands

Post by c8h10n4o2 » Thu Jan 18, 2018 11:08 am

So, the code needs some cleaning up, and it would be great to implement OAuth instead of using the website, but here it is:

http://pastebin.com/pTvs44ZF

Save as "Telldus.psm1" (for example...), open powershell and write "Import-Module .\Telldus.psm1" (you need to specify the path if you are not in the same folder as the file...) and you can try it yourself.
You need to install PowerShell v3 to make it work! Available for free from Microsoft's website if you have a compatible windows version.

This is a work in progress thou, and if/when Telldus wants to change their website the script might break...

But with that in mind, feel free to try it, and please improve it! :) (and get back to me if you do, please! :)

c8h10n4o2
Posts: 83
Joined: Thu Jan 18, 2018 11:08 am
Contact:

Re: PowerShell based cmdlets / Commands

Post by c8h10n4o2 » Thu Jan 18, 2018 11:08 am

New version available.

Notes:
  • Set-TDDimmer is now available.
    Help is added to all commands
    State, methods etc. is now working
    Other improvements such as tab completion on possible values, some validation of input etc...
Available at the same link:
http://pastebin.com/pTvs44ZF

This is how it looks now:
Image

Disclaimer:
This is under development! That means certain object properties could change! I just did som minor changes (in Get-TDDevice) on property names etc which could cause problems in current scripts. ("id" is now "DeviceID".)

If you run into any problems, feel free to ask me here!

c8h10n4o2
Posts: 83
Joined: Thu Jan 18, 2018 11:08 am
Contact:

Re: PowerShell based cmdlets / Commands

Post by c8h10n4o2 » Thu Jan 18, 2018 11:08 am

Updated version available!

Information about the update: (mainly a separate connect-cmdlet, and some changes in output, and improved performance)
http://dollarunderscore.azurewebsites.net/?p=1661

Directlink to the code:
http://poshcode.org/5013

Since it now has a separate "Connect-TelldusLive"-cmdlet, the username/password parameters are removed from all the other cmdlets. This gains a lot of performance (less authentication overhead).

Hope someone might have use for this :)

m.janssen
Posts: 3
Joined: Thu Jan 18, 2018 11:08 am

Re: PowerShell based cmdlets / Commands

Post by m.janssen » Thu Jan 18, 2018 11:08 am

Hi c8h10n4o2,

Thanks for your work!

I have a question regarding Connect-TelldusLive: How do I make the credentials stick?

Each time I open a powershell cli I need to connect first but I want to automate stuff through Event-Ghost, run on/off commands through a .bat file.
Any suggestions on how to handle that?

:help:

I'm running Windows 8.1.

c8h10n4o2
Posts: 83
Joined: Thu Jan 18, 2018 11:08 am
Contact:

Re: PowerShell based cmdlets / Commands

Post by c8h10n4o2 » Thu Jan 18, 2018 11:08 am

m.janssen wrote:Hi c8h10n4o2,

Thanks for your work!

I have a question regarding Connect-TelldusLive: How do I make the credentials stick?

Each time I open a powershell cli I need to connect first but I want to automate stuff through Event-Ghost, run on/off commands through a .bat file.
Any suggestions on how to handle that?

:help:

I'm running Windows 8.1.
Glad you like it! :-)

The Connect-TelldusLive cmdlet uses a PSCredential object, which is basically storing the credential encrypted using your user session.

To save the password in a file (to be able to use it with automation) you can to this:

Code: Select all

$MyTelldusCred = Get-Credential

$MyTelldusCred.Password | ConvertFrom-SecureString | Out-File 'C:\SomePath\TelldusPassword.txt'
Once you have saved this to a file you can load it with this code:

Code: Select all

# You need to import the module (save the code I've published as "Telldus.psm1")
Import-Module 'C:\SomePath\Modules\Telldus.psm1'

# Load the username (the e-mail used for TelldusLive
$Username = "myemail@telldus.com"

# Get the password from the file
$Password = Get-Content 'C:\SomePath\TelldusPassword.txt' | ConvertTo-SecureString
 
# Build the credential
$TelldusCredential = New-Object System.Management.Automation.PsCredential($Username,$Password)

# Connect to Telldus Live!
Connect-TelldusLive -Credential $TelldusCredential

You will only be able to load the credential using the same user account that saved it. If that's not the case you could to something like this instead (less secure since the password is saved in clear text):

The changes are in the "$Password ="-line.

Code: Select all

# You need to import the module (save the code I've published as "Telldus.psm1")
Import-Module 'C:\SomePath\Modules\Telldus.psm1'

# Load the username (the e-mail used for TelldusLive
$Username = "myemail@telldus.com"

# Convert the cleartext password to a secure string
$Password = ConvertTo-SecureString "MyPassword" -AsPlainText -Force
 
# Build the credential
$TelldusCredential = New-Object System.Management.Automation.PsCredential($Username,$Password)

# Connect to Telldus Live!
Connect-TelldusLive -Credential $TelldusCredential
I hope you'll get it working as you want, get back to me otherwise :-)

c8h10n4o2
Posts: 83
Joined: Thu Jan 18, 2018 11:08 am
Contact:

Re: PowerShell based cmdlets / Commands

Post by c8h10n4o2 » Thu Jan 18, 2018 11:08 am

Oh, one more thing...

after you've added the commands you want to run after the code above you save everything in a ".ps1"-file. To run this from a .bat-file you can add the following line:

PowerShell.exe -File "C:\SomePath\MyFile.ps1"

You might need to change your PowerShell executionpolicy on your machine, to do this open a PowerShell prompt as an admin and run the following command:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force

Alternative, change the line in the bat-file to this:
PowerShell.exe -File "C:\SomePath\MyFile.ps1" –ExecutionPolicy Bypass

Good luck :-)

extremesanta
Posts: 8
Joined: Thu Jan 18, 2018 11:08 am

Re: PowerShell based cmdlets / Commands

Post by extremesanta » Thu Jan 18, 2018 11:08 am

Well, ill need som help. Im not that good with PS.
when ill run the script:

Code: Select all

PS E:\Telldus> # You need to import the module (save the code I've published as "Telldus.psm1")
Import-Module 'E:\Telldus\Telldus.psm1'

# Load the username (the e-mail used for TelldusLive
$Username = "emailadress@blabla.bla"

# Convert the cleartext password to a secure string
$Password = ConvertTo-SecureString "MyPassword" -AsPlainText -Force
 
# Build the credential
$TelldusCredential = New-Object System.Management.Automation.PsCredential($Username,$Password)

# Connect to Telldus Live!
Connect-TelldusLive -Credential $TelldusCredential



PS E:\Telldus> Connect-TelldusLive -Credential $TelldusCredential
Thats all that happens. No errors and no nothing.
What should i do to get any results?

c8h10n4o2
Posts: 83
Joined: Thu Jan 18, 2018 11:08 am
Contact:

Re: PowerShell based cmdlets / Commands

Post by c8h10n4o2 » Thu Jan 18, 2018 11:08 am

No problem, I'd love to help if I can :)

It seems everything is working fine for you, that code is meant to connect to Telldus Live!, nothing else. It is also meant to be used within a script file (non interactively), but it works in a normal powershell prompt aswell. (but if you run it interactively, you can just run 'Connect-TelldusLive' after the module is loaded and you'll be prompted for a credential).

Anyway, what I think you're after... After the code you posted has ran, just run "Get-TDDevice" to list all the devices associated with your account. To change those device, you can use Set-TDDevice or Set-TDDimmer.

For example:
Get-TDDevice | Where-Object { $_.Name -like '*lamp*' } | Set-TDDevice -Action turnOff

Will turn off all devices that has a name containing "lamp". If used in a script, I recommend you just specify the ID of the device in Set-TDDevice directly since that is quicker.

There are also commands for getting information regarding sensors, you can for example try:
Get-TDSensor | Get-TDSensorData

Which will first fetch all your sensors and their values.

Please feel free to get back to me if you need more help (or check my blog linked in this thread for more examples on what you can do!), good luck! :)

mariussm
Posts: 2
Joined: Thu Jan 18, 2018 11:08 am

Re: PowerShell based cmdlets / Commands

Post by mariussm » Thu Jan 18, 2018 11:08 am

Hey guys,

I see that your code uses Invoke-WebRequest and cookies etc. for authentication, and uses the user's username and password. I made a module that actually uses OAuth and your API keys, and Invoke-RestMethod instead. This is much better and will continue to work even if Telldus changes the login-webpage etc. :)

http://pastebin.com/HVebcCux

To use the module, simply get your API keys from http://api.telldus.com/keys/index and paste into the module and you are good to go.

Here is a list of all cmdlets defined (easy to define more, but these are the ones i need), they all have some examples you can get using Get-Help.

Get-TelldusConsumerRequest (Used internally in the cmdlets only)
Get-TelldusLiveDevice
Get-TelldusLiveSensor
Get-TelldusLiveSensorData
Get-TelldusLiveSensorDataAll
Set-TelldusLiveDevice
Set-TelldusLiveDimmer
Set-TelldusLiveSensorData

c8h10n4o2
Posts: 83
Joined: Thu Jan 18, 2018 11:08 am
Contact:

Re: PowerShell based cmdlets / Commands

Post by c8h10n4o2 » Thu Jan 18, 2018 11:08 am

Nice work! I agree OAuth is better since it complies better with the API, but I never got that to work using native .Net classes, dependencies on 3rd party dll's makes things less portable imho.

But great work, I might take that for a testdrive later :)

(btw, I was a bit scared that this would stop working when the page changed aswell, but it has run without any modifications for more than 2,5 years now... :) )

Also, I think OAuth is not really suited for this kind of "application". It's perfect for when different services needs to talk to each other on a users behalf, but when it's a noncompiled client cli application/script you are forced to store "secrets" (api keys) in the module and it becomes a hassle for non techies to use when everyone needs a new pair of api keys.

But then again, my code is almost on a "web scraping" level which as you say can be very fragile, even though in this case it has worked for years without any changes, it could break anytime Telldus wants to change something...

Post Reply