-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHAAPI.cs
More file actions
175 lines (161 loc) · 6.76 KB
/
HAAPI.cs
File metadata and controls
175 lines (161 loc) · 6.76 KB
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
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using System.Web.Script.Serialization;
using System.Windows.Forms;
namespace HA_Volume
{
/// <summary>
/// Helper class to simplify sending and retrieving of HA API RESTful functions
/// </summary>
public class HAAPI
{
/// <summary>
/// Sends a HTTP POST request to send commands to the media_player.
/// </summary>
/// <param name="servicename">HA service name e.g volume_up or toggle</param>
/// <param name="json">Some services require additional json to action the service</param>
public static void POST(string servicename, [Optional] string json)
{
try
{
if (String.IsNullOrEmpty(json)) json = new JavaScriptSerializer().Serialize(new { entity_id = Properties.Settings.Default.HAEntity });
HttpClientHandler handler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + Properties.Settings.Default.HAToken);
var response = httpClient.PostAsync(Properties.Settings.Default.HAURL + "/api/services/" + servicename, new StringContent(json, Encoding.UTF8, "application/json")).Result;
}
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
MessageBox.Show("Unable to communicate with Home Assistant correctly, please confirm Home Assistant is running and that your settings are correct and that the entity you have selected is showing in Home Assistant.", "HA Volume - Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
/// <summary>
/// Sends a HTTP GET request to HA to retrieve attributes.
/// </summary>
/// <param name="entity">Optional: filter it to a specific entity.</param>
public static dynamic GET([Optional] string entity)
{
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
try
{
HttpClientHandler handler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
using (var httpClient = new HttpClient(handler))
{
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + Properties.Settings.Default.HAToken);
if(String.IsNullOrEmpty(entity))
{
var response = httpClient.GetStringAsync(new Uri(Properties.Settings.Default.HAURL + "/api/states")).Result;
return jsonSerializer.Deserialize<dynamic>((response));
}
else
{
var response = httpClient.GetStringAsync(new Uri(Properties.Settings.Default.HAURL + "/api/states/" + entity)).Result;
return jsonSerializer.Deserialize<dynamic>((response));
}
}
}
catch (WebException)
{
return 99;
}
catch (HttpRequestException)
{
return 99;
}
}
/// <summary>
/// Toggles Power of the media_player.
/// </summary>
public static void Power() => POST("media_player/toggle");
/// <summary>
/// Increases volume of the media_player.
/// </summary>
public static void Volume_Up() => POST("media_player/volume_up");
/// <summary>
/// Decreases volume of the media_player.
/// </summary>
public static void Volume_Down() => POST("media_player/volume_down");
/// <summary>
/// Sets volume of the media_player.
/// </summary>
public static void Volume_Set(decimal volume)
{
string json = new JavaScriptSerializer().Serialize(new
{
entity_id = Properties.Settings.Default.HAEntity,
volume_level = volume
});
POST("media_player/volume_set", json);
}
/// <summary>
/// Detects mute state and toggles mute of the media_player.
/// </summary>
/// <param name="state">Pass the variable which contains True/False of the media_player mute state</param>
public static void Volume_Mute(bool state)
{
string json = new JavaScriptSerializer().Serialize(new
{
entity_id = Properties.Settings.Default.HAEntity,
is_volume_muted = !state
});
POST("media_player/volume_mute", json);
}
/// <summary>
/// Changes source of media_player, if no source set will use "OnInput"
/// </summary>
/// <param name="state">Optional: string containing the source you want to switch to.</param>
public static void Set_Input([Optional] string source)
{
string json;
if (String.IsNullOrEmpty(source))
{
json = new JavaScriptSerializer().Serialize(new
{
entity_id = Properties.Settings.Default.HAEntity,
source = Properties.Settings.Default.OnInput
});
}
else
{
json = new JavaScriptSerializer().Serialize(new
{
entity_id = Properties.Settings.Default.HAEntity,
source = source
});
}
POST("media_player/select_source", json);
}
/// <summary>
/// Validates HA URL to make sure its a valid URL.
/// </summary>
/// <param name="url">text to validate as URL.</param>
public static bool Validate_URL(string url)
{
Uri uriResult;
return Uri.TryCreate(url, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
}
/// <summary>
/// Turns ON additional seitch
/// </summary>
public static void Toggle_Switch(string action, string additionalswitch)
{
string json = new JavaScriptSerializer().Serialize(new {entity_id = additionalswitch});
POST("switch/" + action, json);
}
}
}