Repository navigation
Expand file tree
/
Copy pathHAWatchServiceRequest.m
More file actions
131 lines (116 loc) · 5.04 KB
/
Copy pathHAWatchServiceRequest.m
File metadata and controls
131 lines (116 loc) · 5.04 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
#import "HAWatchServiceRequest.h"
#import "HAHomeManager.h"
#import "HAURLCompatibility.h"
@interface HAWatchServiceRequest () <NSURLConnectionDataDelegate>
@property(nonatomic, retain) NSDictionary *home;
@property(nonatomic, retain) NSDictionary *message;
@property(nonatomic, copy) HAWatchReplyHandler replyHandler;
@property(nonatomic, retain) NSURLConnection *connection;
@property(nonatomic, retain) NSMutableData *responseData;
@property(nonatomic, assign) NSInteger statusCode;
@end
@implementation HAWatchServiceRequest
@synthesize home = _home;
@synthesize message = _message;
@synthesize replyHandler = _replyHandler;
@synthesize connection = _connection;
@synthesize responseData = _responseData;
@synthesize statusCode = _statusCode;
- (id)initWithHome:(NSDictionary *)home
message:(NSDictionary *)message
replyHandler:(HAWatchReplyHandler)replyHandler {
self = [super init];
if (self) {
self.home = home;
self.message = message;
self.replyHandler = replyHandler;
}
return self;
}
- (void)start {
[self retain];
NSString *domain = [self.message objectForKey:@"domain"];
NSString *service = [self.message objectForKey:@"service"];
NSString *entityID = [self.message objectForKey:@"entity_id"];
if (![self isAllowedService:service domain:domain] || [entityID length] == 0) {
[self finishWithSuccess:NO message:@"Unsupported watch action."];
return;
}
NSString *baseURL = [self.home objectForKey:HAHomeBaseURLKey];
NSString *accessToken = [HAHomeManager accessTokenForBaseURLString:baseURL];
NSString *path = [NSString stringWithFormat:@"/api/services/%@/%@", domain, service];
NSMutableURLRequest *request = HAMutableURLRequestWithURL(
HAURLWithString([baseURL stringByAppendingString:path]));
if (request == nil || [accessToken length] == 0) {
[self finishWithSuccess:NO message:@"The selected home is not signed in."];
return;
}
NSMutableDictionary *body = [NSMutableDictionary dictionaryWithObject:entityID forKey:@"entity_id"];
NSDictionary *serviceData = [self.message objectForKey:@"service_data"];
if ([serviceData isKindOfClass:[NSDictionary class]]) {
[body addEntriesFromDictionary:serviceData];
}
request.HTTPMethod = @"POST";
request.HTTPBody = [NSJSONSerialization dataWithJSONObject:body options:0 error:nil];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"Bearer %@", accessToken]
forHTTPHeaderField:@"Authorization"];
self.responseData = [NSMutableData data];
self.connection = HAStartURLConnection(request, self);
if (self.connection == nil) {
[self finishWithSuccess:NO message:@"Could not contact Home Assistant."];
}
}
- (BOOL)isAllowedService:(NSString *)service domain:(NSString *)domain {
NSDictionary *allowedServices = [NSDictionary dictionaryWithObjectsAndKeys:
[NSArray arrayWithObjects:@"toggle", @"turn_on", @"turn_off", nil], @"homeassistant",
[NSArray arrayWithObjects:@"turn_on", @"turn_off", nil], @"light",
[NSArray arrayWithObjects:@"turn_on", @"turn_off", nil], @"switch",
[NSArray arrayWithObjects:@"lock", @"unlock", nil], @"lock",
[NSArray arrayWithObjects:@"open_cover", @"stop_cover", @"close_cover", nil], @"cover",
[NSArray arrayWithObject:@"turn_on"], @"scene",
[NSArray arrayWithObject:@"turn_on"], @"script",
[NSArray arrayWithObject:@"turn_on"], @"automation",
[NSArray arrayWithObject:@"press"], @"button",
nil];
return [[allowedServices objectForKey:domain] containsObject:service];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.responseData setLength:0];
self.statusCode = [response respondsToSelector:@selector(statusCode)]
? [(id)response statusCode]
: 0;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.responseData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
BOOL success = self.statusCode >= 200 && self.statusCode < 300;
[self finishWithSuccess:success
message:success ? @"Action completed." : @"Home Assistant rejected the action."];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[self finishWithSuccess:NO message:[error localizedDescription]];
}
- (void)finishWithSuccess:(BOOL)success message:(NSString *)message {
self.connection = nil;
self.responseData = nil;
if (self.replyHandler != nil) {
self.replyHandler([NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:success], @"ok",
message ?: @"", @"message",
nil]);
}
self.replyHandler = nil;
[self release];
}
- (void)dealloc {
[_connection cancel];
[_connection release];
[_responseData release];
[_replyHandler release];
[_message release];
[_home release];
[super dealloc];
}
@end