Point 3 Initialisation au démarrage
<#
.Synopsis
Initilize HASS at starting phase
.Notes
Name: Hass_Initialisation.ps1
Author: Thierry Lenoir
Created: 14/10/2019
Last Edit:
.Description
Based on the Autmation « alias: ‹ IPX initialisation › » a script is launched via the shell_command
Initialisation:
1 - IPX switch statuses
#>
#----------------------------------------------------------------------------------------------------
Initialisation
#----------------------------------------------------------------------------------------------------
#param($msg, $timeCreated, $accountName, $ipAddress)
loading Push Bullet lib & configs
. $Env:ScriptsLib\PushBullet\SendMsgPushBullet.ps1;
. $Env:ScriptsEtc\PushBullet_idens.ps1;
Loading & setting properties
$currentScriptName = $MyInvocation.MyCommand.Name.substring(-3);
[xml]$xmlProperties = Get-Content « $Env:ScriptsEtc\Scripts_properties.xml »;
#[xml]$xmlHackerCache = Get-Content $xmlProperties.$MyInvocation.MyCommand.Name.cacheFilename;
#----------------------------------------------------------------------------------------------------
Functions
#----------------------------------------------------------------------------------------------------
Function HassChangeSwitchStatus
{
Param([string]$relayNum, [string]$relayName, [string]$relayState, [string]$IPXNum )
Write-Output « $($MyInvocation.MyCommand.Name) => $(Get-Date -format « dd-MM-yyyy HH:mm:ss ») → Params: $relayNum - $relayName - $relayState - $IPXNum »
#remove "led" key word
$relayNum = $relayNum.substring(3)
# add "ipx800_1_relay_" string at the relay number
$relayNum = "ipx800_" + $IPXNum + "_relay_" + $([int]$relayNum + 1)
# translate status code to hass codename 0=off 1=on
if($relayState -eq 1) {$relayState = "on"} else {$relayState = "off"}
# build url to call hass
$hassURL = $xmlProperties.scriptsConfig.hass.ha_url + "/api/states/switch.$relayNum"
# buld body to send in the JSON request
$body = @{
state = "$relayState"
attributes = @{ friendly_name = "$relayName"
assumed_state = 'true' }
} | ConvertTo-Json
# setting headers
$headers = @{
Authorization="Bearer $($xmlProperties.scriptsConfig.hass.ha_api_long_term_key)"
ContentType = "application/json"
}
# calling hass with Encoding to accept special char in the body
$resp = Invoke-WebRequest -Uri $hassURL -Method 'Post' -Headers $headers -body ([System.Text.Encoding]::UTF8.GetBytes($body))
Write-Output "$($MyInvocation.MyCommand.Name) <= $(Get-Date -format "dd-MM-yyyy HH:mm:ss")"
}
#----------------------------------------------------------------------------------------------------
MAIN
#----------------------------------------------------------------------------------------------------
try{
Write-Output « $($MyInvocation.MyCommand.Name) <=: $(Get-Date -format « dd-MM-yyyy HH:mm:ss ») »;
foreach ($ipx in $xmlProperties.scriptsConfig.SelectNodes("//ipx")){
# retrieving IPX's creds
$ipxUsrPass = $ipx.user + ":" + $ipx.pass
# converting IPX's creds in BASE64
$ipxCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($ipxUsrPass))
# setting headers based on request seen in the Chrome console data
$headers = @{
"Authorization" = "Basic " + $ipxCreds
"Accept" = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"
"Accept-Encoding" = "gzip, deflate"
"Accept-Language" = "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7"
}
# retrieving relay statuses
[xml]$relayStatuses = Invoke-WebRequest -Method Get -Uri "$($ipx.url)/globalstatus.xml" -Headers $headers
# retrieving relay names with Encoding to accept special char in the response
[xml]$relayNames = [system.Text.Encoding]::UTF8.GetString((Invoke-WebRequest -Method Get -Uri "$($ipx.url)/ioname.xml" -Headers $headers).RawContentStream.ToArray())
# initializing arrays
$data = @()
# Initiate relay status for each ones till max relay number param from script xml file
for ($count = 0; $count -lt $ipx.maxRelay; $count++){
HassChangeSwitchStatus $relayStatuses.response.SelectNodes("/response/led$($count)").LocalName $relayNames.SelectNodes("/response/output$($count+1)").InnerText $relayStatuses.response.SelectNodes("/response/led$($count)").InnerText $ipx.number
}
# temporary situation while the IPX 2 will migrate to version 3.47.0 or higher
break;
}
}
catch{
Write-Output « $($MyInvocation.MyCommand.Name) => Error: $_ - $(Get-Date -format « dd-MM-yyyy HH:mm:ss ») »;
}
finally{
#logging elapstime …
Write-Output « $($MyInvocation.MyCommand.Name) =>: $(Get-Date -format « dd-MM-yyyy HH:mm:ss ») »;
}
Désolé pour le retard