forked from pathartl/TeamsPresence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeAssistantService.cs
41 lines (34 loc) · 1.16 KB
/
HomeAssistantService.cs
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
using RestSharp;
using RestSharp.Serializers.NewtonsoftJson;
namespace TeamsPresence
{
public class HomeAssistantService
{
private string Token { get; set; }
private string Url { get; set; }
private RestClient Client { get; set; }
public HomeAssistantService(string url, string token)
{
Token = token;
Url = url;
Client = new RestClient(url);
Client.AddDefaultHeader("Authorization", $"Bearer {Token}");
Client.UseNewtonsoftJson();
}
public void UpdateEntity(string entity, string state, string stateFriendlyName, string icon)
{
var update = new HomeAssistantEntityStateUpdate()
{
State = state,
Attributes = new HomeAssistantEntityStateUpdateAttributes()
{
FriendlyName = stateFriendlyName,
Icon = icon
}
};
var request = new RestRequest($"api/states/{entity}", Method.POST, DataFormat.Json);
request.AddJsonBody(update);
Client.Execute(request);
}
}
}