Skip to content

Commit

Permalink
Merge pull request #88 from StartAutomating/LightScriptImprovement
Browse files Browse the repository at this point in the history
Light script improvement
  • Loading branch information
StartAutomating authored Nov 12, 2022
2 parents a0875e2 + 1428247 commit 5c2212f
Show file tree
Hide file tree
Showing 32 changed files with 2,751 additions and 70 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## 0.2.9:

* Added Support for LaMetric Time Clocks!
* Connect-LaMetricTime (#67)
* Disconnect-LaMetricTime (#68)
* Get-LaMetricTime (#69, #71, #72, #74, #73, #75, #77)
* Set-LaMetricTime (#79, #80, #81, #82, #83, #84, #85, #86, #87 )
* Pixoo Improvement:
* Get-Pixoo -Font (#65) -Liked (#63) -Upload (#61)
* Set-Pixoo -FileID (#62)

---

## 0.2.8:

* Watch-HueSensor! (Fixes #58)
Expand Down
1 change: 1 addition & 0 deletions Formatting/LaMetric.Icon.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-FormatView -TypeName LaMetric.Icon -Property Name, BigThumbnail -AutoSize -GroupByProperty Category
1 change: 1 addition & 0 deletions Formatting/LaMetric.Time.Application.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-FormatView -TypeName LaMetric.Time.Application -Property title, vendor, version
1 change: 1 addition & 0 deletions Formatting/LaMetric.Time.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-FormatView -TypeName LaMetric.Time -Property Name, Model, IPAddress
88 changes: 88 additions & 0 deletions Functions/LaMetric/Connect-LaMetricTime.ps.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
function Connect-LaMetricTime
{
<#
.SYNOPSIS
Connects to a LaMetric clock
.DESCRIPTION
Connects to a LaMetric clock.
LaMetric Time devices require a local ApiKey.
Unfortunately, there is no way to get this key automatically.
To Connect-LaMetricTime, you'll need to visit [developer.lametric.com](https://developer.lametric.com) and sign in.
You will find API Keys for your devices at [developer.lametric.com/user/devices](https://developer.lametric.com/user/devices).
.EXAMPLE
Connect-LaMetric -IPAddress $laMetricIP -ApiKey $myApiKey -PassThru
.LINK
Get-LaMetricTime
.LINK
Disconnect-LaMetricTime
#>
[OutputType([Nullable], [PSObject])]
param(
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface.
[Parameter(Mandatory,Position=0,ValueFromPipelineByPropertyName)]
[Alias('LaMetricTimeIPAddress')]
[IPAddress]
$IPAddress,

# The IP Address for the Twinkly device. This can be discovered thru the phone user interface.
[Parameter(Mandatory,Position=1,ValueFromPipelineByPropertyName)]
[Alias('LaMetricTimeApiKey')]
[string]
$ApiKey,

# If set, will output the connection information.
[switch]
$PassThru
)

begin {
if ($home) {
$lightScriptRoot = Join-Path $home -ChildPath LightScript
}
}

process {
$laMetricB64Key = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("dev:$ApiKey"))
$ipAndPort = "${ipAddress}:8080"

#region Connect to the Device
$laMetricDevice =
http://$ipAndPort/api/v2/device -Headers @{
Authorization = "Basic $laMetricB64Key"
}

# If we could not connect, return
if (-not $laMetricDevice) {
return
}
#endregion Connect to the Device

if ($laMetricDevice) {
#region Save Device Information
[decorate(PSTypeName='LaMetric.Time',Clear)]$laMetricDevice

if ($home -and $laMetricDevice) {
if (-not (Test-Path $lightScriptRoot)) {
$createLightScriptDir = New-Item -ItemType Directory -Path $lightScriptRoot
if (-not $createLightScriptDir) { return }
}


$laMetricDataFile = Join-Path $lightScriptRoot ".$($laMetricDevice.serial_number).LaMetricTime.clixml"

$laMetricDevice |
Add-Member NoteProperty IpAddress $IPAddress -Force -PassThru |
Add-Member NoteProperty ApiKey $laMetricB64Key -Force -PassThru |
Export-Clixml -Path $laMetricDataFile
}
#endregion Save Device Information
if ($PassThru) {
$laMetricDevice
}
}
}
}
77 changes: 77 additions & 0 deletions Functions/LaMetric/Connect-LaMetricTime.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
function Connect-LaMetricTime {
<#
.SYNOPSIS
Connects to a LaMetric clock
.DESCRIPTION
Connects to a LaMetric clock.
LaMetric Time devices require a local ApiKey.
Unfortunately, there is no way to get this key automatically.
To Connect-LaMetricTime, you'll need to visit [developer.lametric.com](https://developer.lametric.com) and sign in.
You will find API Keys for your devices at [developer.lametric.com/user/devices](https://developer.lametric.com/user/devices).
.EXAMPLE
Connect-LaMetric -IPAddress $laMetricIP -ApiKey $myApiKey -PassThru
.LINK
Get-LaMetricTime
.LINK
Disconnect-LaMetricTime
#>
[OutputType([Nullable], [PSObject])]
param(
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface.
[Parameter(Mandatory,Position=0,ValueFromPipelineByPropertyName)]
[Alias('LaMetricTimeIPAddress')]
[IPAddress]
$IPAddress,
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface.
[Parameter(Mandatory,Position=1,ValueFromPipelineByPropertyName)]
[Alias('LaMetricTimeApiKey')]
[string]
$ApiKey,
# If set, will output the connection information.
[switch]
$PassThru
)
begin {
if ($home) {
$lightScriptRoot = Join-Path $home -ChildPath LightScript
}
}
process {
$laMetricB64Key = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("dev:$ApiKey"))
$ipAndPort = "${ipAddress}:8080"
#region Connect to the Device
$laMetricDevice =
Invoke-RestMethod ('http://',$ipAndPort,'/api/v2/device' -join '') -Headers @{
Authorization = "Basic $laMetricB64Key"
}
# If we could not connect, return
if (-not $laMetricDevice) {
return
}
#endregion Connect to the Device

if ($laMetricDevice) {
#region Save Device Information
$laMetricDevice.pstypenames.clear()
$laMetricDevice.pstypenames.add('LaMetric.Time')
if ($home -and $laMetricDevice) {
if (-not (Test-Path $lightScriptRoot)) {
$createLightScriptDir = New-Item -ItemType Directory -Path $lightScriptRoot
if (-not $createLightScriptDir) { return }
}
$laMetricDataFile = Join-Path $lightScriptRoot ".$($laMetricDevice.serial_number).LaMetricTime.clixml"
$laMetricDevice |
Add-Member NoteProperty IpAddress $IPAddress -Force -PassThru |
Add-Member NoteProperty ApiKey $laMetricB64Key -Force -PassThru |
Export-Clixml -Path $laMetricDataFile
}
#endregion Save Device Information
if ($PassThru) {
$laMetricDevice
}
}
}
}

40 changes: 40 additions & 0 deletions Functions/LaMetric/Disconnect-LaMetricTime.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function Disconnect-LaMetricTime
{
<#
.Synopsis
Disconnects a LaMetricTime
.Description
Disconnects a LaMetricTime, removing stored device info
.Example
Disconnect-LaMetricTime 1.2.3.4
.Link
Connect-LaMetricTime
#>
[OutputType([Nullable], [PSObject])]
[CmdletBinding(SupportsShouldProcess,ConfirmImpact='High')]
param(
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface.
[Parameter(Mandatory,Position=0,ValueFromPipelineByPropertyName)]
[Alias('LaMetricTimeIPAddress')]
[IPAddress]
$IPAddress
)

begin {
if ($home) {
$lightScriptRoot = Join-Path $home -ChildPath LightScript
}
}

process {
@(Get-ChildItem -Filter *.LaMetricTime.clixml -Path $lightScriptRoot) |
Foreach-Object {
$file = $_
$fileInfo = Import-Clixml -LiteralPath $file.FullName
if ($fileInfo.IPAddress -eq $IPAddress -and $PSCmdlet.ShouldProcess("Remove $($fileInfo.name)")) {
Remove-Item -LiteralPath $file.FullName -Force
}
}
}
}

153 changes: 153 additions & 0 deletions Functions/LaMetric/Get-LaMetricTime.ps.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
function Get-LaMetricTime
{
<#
.SYNOPSIS
Gets LaMetricTime
.DESCRIPTION
Gets LaMetricTime devices.
.EXAMPLE
Get-LaMetricTime
.EXAMPLE
Get-LaMetricTime -Audio # Gets audio settings
.EXAMPLE
Get-LaMetricTime -Bluetooth # Gets bluetooth settings
.EXAMPLE
Get-LaMetricTime -Notification # Gets notifications (there may be none)
.EXAMPLE
Get-LaMetricTime -Audio # Gets audio settings
.LINK
Connect-LaMetricTime
.LINK
Set-LaMetricTime
#>
[CmdletBinding(PositionalBinding=$false,DefaultParameterSetName='ListDevices')]
[OutputType([PSObject])]
param(
# One or more IP Addresses of LaMetricTime devices.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('LaMetricTimeIPAddress')]
[IPAddress[]]
$IPAddress,

# If set, will get apps from an LaMetric device.
[Parameter(Mandatory,ParameterSetName='api/v2/device/apps')]
[Alias('App','Apps','Applications')]
[switch]
$Application,

# If set, will get audio settings of an LaMetric Time device
[Parameter(Mandatory,ParameterSetName='api/v2/device/audio')]
[switch]
$Audio,

# If set, will get bluetooth settings of an LaMetric Time device
[Parameter(Mandatory,ParameterSetName='api/v2/device/bluetooth')]
[switch]
$Bluetooth,

# If set, will get display settings of an LaMetric Time device
[Parameter(Mandatory,ParameterSetName='api/v2/device/display')]
[switch]
$Display,

# If set, will get LaMetric Time notifications
[Parameter(Mandatory,ParameterSetName='api/v2/device/notifications')]
[Alias('Notifications')]
[switch]
$Notification,

# If set, will get details about a particular package of an LaMetric Time device.
[Parameter(Mandatory,ParameterSetName='api/v2/device/apps/$Package',ValueFromPipelineByPropertyName)]
[string]
$Package,

# If set, will get wifi settings of an LaMetric Time device
[Parameter(Mandatory,ParameterSetName='api/v2/device/wifi')]
[switch]
$WiFi
)

begin {
if (-not $script:LaMetricTimeCache) {
$script:LaMetricTimeCache = @{}
}
if ($home) {
$lightScriptRoot = Join-Path $home -ChildPath LightScript
}
$friendlyParameterSetNames = @{
"api/v2/device/apps" = "Application"
'api/v2/device/apps/$packages' = "Application.Details"
}
$expandPropertiesIn = @("api/v2/device/apps")
}
process {
#region Default to All Devices
if (-not $IPAddress) { # If no -IPAddress was passed
if ($home) {
# Read all .LaMetricTime.clixml files beneath your LightScript directory.
Get-ChildItem -Path $lightScriptRoot -ErrorAction SilentlyContinue -Filter *.LaMetricTime.clixml -Force |
Import-Clixml |
ForEach-Object {
if (-not $_) { return }
$laMetricTimeDevice = $_
$script:LaMetricTimeCache["$($laMetricTimeDevice.IPAddress)"] = $laMetricTimeDevice
}

$IPAddress = $script:LaMetricTimeCache.Keys # The keys of the device cache become the -IPAddress.
}
if (-not $IPAddress) { # If we still have no -IPAddress
Write-Warning "No -IPAddress provided and no cached devices found" # warn
return # and return.
}
}
#endregion Default to All Devices

if ($PSCmdlet.ParameterSetName -like 'api*') {
foreach ($ip in $IPAddress) {
$ipAndPort = "${ip}:8080"
$endpoint =
$ExecutionContext.SessionState.InvokeCommand.ExpandString($PSCmdlet.ParameterSetName) -replace '^api'
$typename =
if ($friendlyParameterSetNames[$PSCmdlet.ParameterSetName]) {
$friendlyParameterSetNames[$PSCmdlet.ParameterSetName]
} else {
$lastSegment = @($endpoint -split '/')[-1]
($lastSegment.Substring(0,1).ToUpper() + $lastSegment.Substring(1)) -replace 's$'
}
#region Connect to the Device

http://$ipAndPort/api/$endpoint -Headers @{
Authorization = "Basic $laMetricB64Key"
} |
& { process {
$out = $_
if ($expandPropertiesIn -contains $PSCmdlet.ParameterSetName) {
foreach ($prop in $out.psobject.properties) {
$prop.value.pstypenames.clear()
$prop.value.pstypenames.add("LaMetric.Time.$typeName")
$prop.value
}

}
elseif ($out -is [Collections.IList]) {
foreach ($o in $out) {
$o.pstypenames.clear()
$o.pstypenames.add("LaMetric.Time.$typename")
$o
}
}
else {
$out.pstypenames.clear()
$out.pstypenames.add("LaMetric.Time.$typename")
$out
}

} }
}
}
elseif ($PSCmdlet.ParameterSetName -eq 'ListDevices') {
$script:LaMetricTimeCache.Values
}
}
}

Loading

0 comments on commit 5c2212f

Please sign in to comment.