-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrigger-homeassistant.ps1
189 lines (168 loc) · 6.03 KB
/
trigger-homeassistant.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
. $PSScriptRoot/utils.ps1
function Get-Token {
# load token from a encrypted location, secured with the user session so only the user who created it can read the data
Begin {
# todo: improve locattion to the user space
$CredDirectory = "C:\Users\Public\Credentials"
$CredFile = [System.IO.Path]::Combine($CredDirectory, "Home_Assistant_cred.xml")
if (-Not(Test-Path $CredDirectory)) {
New-Item -Path $CredDirectory -ItemType Directory
}
}
Process {
if (-Not(Test-Path $CredFile)) {
# Get the token and store it securely
$CredentialParams = @{
Message = "Enter Home Assistant Token:"
Username = "token"
Title = "Home assistant token"
}
$Credential = Get-Credential @CredentialParams
$Credential | Export-Clixml -Path $CredFile
}
$credential = [PSCredential] (Import-Clixml -Path $CredFile)
$token = ConvertFrom-SecureString -SecureString $credential.Password -AsPlainText
Write-Message "Found a token with length [$($token.Length)]"
return $token
}
}
$localHomeAssistant = "http://192.168.1.39:8123"
<#
$command options: toggle, turn_on, turn_off
#>
function switchToggle{
param (
[string]$entityId,
[string]$token,
[string]$homeAssistant = $env:homeAssistant,
[string]$command = "toggle"
)
if ($token -eq "") {
Write-Message "Loading token from disk"
try {
$token = Get-Token
} catch {
Write-Message "Could not load token from disk. Please set the token as environment variable or in the script."
Write-Message $_
throw
}
}
if ($homeAssistant -eq "") {
#Write-Error "Parameter 'homeAssistant' is missing. You can set this as environment value with the url to your home assistant instance."
# overwrite with hardcode value for now
$homeAssistant = $localHomeAssistant
}
$url = "$homeAssistant/api/services/switch/$command"
Write-Message "We are using this url for the command: [$url]"
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer $token"
}
Write-Message "Calling the url"
Invoke-RestMethod -Uri $url -Body "{""entity_id"": ""$entityId""}" -Headers $headers -Method POST
Write-Message "Call made successfully"
}
<#
$command options: toggle, turn_on, turn_off
#>
function switchScene{
param (
[string]$entityId,
[string]$token,
[string]$homeAssistant = $env:homeAssistant,
[string]$command = "toggle"
)
if ($token -eq "") {
Write-Message "Loading token from disk"
try {
$token = Get-Token
} catch {
Write-Message "Could not load token from disk. Please set the token as environment variable or in the script."
Write-Message $_
throw
}
}
if ($homeAssistant -eq "") {
#Write-Error "Parameter 'homeAssistant' is missing. You can set this as environment value with the url to your home assistant instance."
# overwrite with hardcode value for now
$homeAssistant = $localHomeAssistant
}
$url = "$homeAssistant/api/services/scene/$command"
Write-Message "We are using this url for the command: [$url]"
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer $token"
}
Write-Message "Calling the url"
Invoke-RestMethod -Uri $url -Body "{""entity_id"": ""$entityId""}" -Headers $headers -Method POST
Write-Message "Call made successfully"
}
<#
$command options: toggle, turn_on, turn_off
#>
function getEntityState{
param (
[string]$entityId,
[string]$token,
[string]$homeAssistant = $env:homeAssistant
)
if ($token -eq "") {
Write-Message "Loading token from disk"
try {
$token = Get-Token
} catch {
Write-Message "Could not load token from disk. Please set the token as environment variable or in the script."
Write-Message $_
throw
}
}
if ($homeAssistant -eq "") {
#Write-Error "Parameter 'homeAssistant' is missing. You can set this as environment value with the url to your home assistant instance."
# overwrite with hardcode value for now
$homeAssistant = $localHomeAssistant
}
$url = "$homeAssistant/api/states/$entityId"
Write-Message "We are using this url for the command: [$url]"
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer $token"
}
Write-Message "Calling the url"
Invoke-RestMethod -Uri $url -Headers $headers -Method GET
Write-Message "Call made successfully"
}
<#
$command options: toggle, turn_on, turn_off
#>
function runScript{
param (
[string]$entityId,
[string]$token,
[string]$homeAssistant = $env:homeAssistant,
[string]$command = "turn_on"
)
if ($token -eq "") {
Write-Message "Loading token from disk"
try {
$token = Get-Token
} catch {
Write-Message "Could not load token from disk. Please set the token as environment variable or in the script."
Write-Message $_
throw
}
}
if ($homeAssistant -eq "") {
#Write-Error "Parameter 'homeAssistant' is missing. You can set this as environment value with the url to your home assistant instance."
# overwrite with hardcode value for now
$homeAssistant = $localHomeAssistant
}
$url = "$homeAssistant/api/services/script/$command"
Write-Message "We are using this url for the command: [$url]"
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer $token"
}
Write-Message "Calling the url"
Invoke-RestMethod -Uri $url -Body "{""entity_id"": ""$entityId""}" -Headers $headers -Method POST
Write-Message "Call made successfully"
}