Repository navigation
Expand file tree
/
Copy pathHAEntityDetailViewController.m
More file actions
208 lines (186 loc) · 8.87 KB
/
Copy pathHAEntityDetailViewController.m
File metadata and controls
208 lines (186 loc) · 8.87 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#import "HAEntityDetailViewController.h"
#import "HACameraViewController.h"
#import "HAHomeManager.h"
#import "HAURLCompatibility.h"
@interface HAEntityDetailViewController () <NSURLConnectionDataDelegate>
@property(nonatomic, retain) NSDictionary *entity;
@property(nonatomic, copy) NSString *baseURLString;
@property(nonatomic, copy) NSString *accessToken;
@property(nonatomic, retain) UILabel *stateLabel;
@property(nonatomic, retain) UISlider *brightnessSlider;
@property(nonatomic, retain) NSMutableData *responseData;
@property(nonatomic, retain) NSURLConnection *connection;
@end
@implementation HAEntityDetailViewController
@synthesize entity = _entity;
@synthesize baseURLString = _baseURLString;
@synthesize accessToken = _accessToken;
@synthesize stateLabel = _stateLabel;
@synthesize brightnessSlider = _brightnessSlider;
@synthesize responseData = _responseData;
@synthesize connection = _connection;
- (id)initWithEntity:(NSDictionary *)entity baseURLString:(NSString *)baseURLString
accessToken:(NSString *)accessToken {
self = [super init];
if (self) {
self.entity = entity;
self.baseURLString = baseURLString;
self.accessToken = accessToken;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
NSDictionary *attributes = [self.entity objectForKey:@"attributes"];
self.title = [attributes objectForKey:@"friendly_name"] ?: [self.entity objectForKey:@"entity_id"];
self.stateLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
self.stateLabel.textAlignment = NSTextAlignmentCenter;
self.stateLabel.font = [UIFont boldSystemFontOfSize:22.0];
self.stateLabel.text = [NSString stringWithFormat:@"State: %@", [self.entity objectForKey:@"state"] ?: @"unknown"];
[self.view addSubview:self.stateLabel];
[self buildControlsForDomain:[self entityDomain]];
}
- (void)buildControlsForDomain:(NSString *)domain {
if ([domain isEqualToString:@"camera"]) {
[self addButtonWithTitle:@"View Camera" action:@selector(viewCamera:) tag:0];
return;
}
if ([domain isEqualToString:@"cover"]) {
[self addButtonWithTitle:@"Open" action:@selector(coverAction:) tag:1];
[self addButtonWithTitle:@"Stop" action:@selector(coverAction:) tag:2];
[self addButtonWithTitle:@"Close" action:@selector(coverAction:) tag:3];
return;
}
if ([domain isEqualToString:@"lock"]) {
[self addButtonWithTitle:@"Lock" action:@selector(lockAction:) tag:1];
[self addButtonWithTitle:@"Unlock" action:@selector(lockAction:) tag:2];
return;
}
if ([domain isEqualToString:@"light"]) {
[self addButtonWithTitle:@"Turn On / Off" action:@selector(toggle:) tag:0];
self.brightnessSlider = [[[UISlider alloc] initWithFrame:CGRectZero] autorelease];
self.brightnessSlider.minimumValue = 1.0;
self.brightnessSlider.maximumValue = 100.0;
id brightness = [[self.entity objectForKey:@"attributes"] objectForKey:@"brightness"];
self.brightnessSlider.value = [brightness respondsToSelector:@selector(floatValue)]
? [brightness floatValue] * 100.0 / 255.0
: 50.0;
[self.brightnessSlider addTarget:self action:@selector(brightnessChanged:)
forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
[self.view addSubview:self.brightnessSlider];
[self addButtonWithTitle:@"Red" action:@selector(colorSelected:) tag:1];
[self addButtonWithTitle:@"Green" action:@selector(colorSelected:) tag:2];
[self addButtonWithTitle:@"Blue" action:@selector(colorSelected:) tag:3];
[self addButtonWithTitle:@"White" action:@selector(colorSelected:) tag:4];
return;
}
if ([domain isEqualToString:@"scene"] || [domain isEqualToString:@"script"] ||
[domain isEqualToString:@"button"] || [domain isEqualToString:@"automation"]) {
[self addButtonWithTitle:@"Run" action:@selector(runAction:) tag:0];
return;
}
[self addButtonWithTitle:@"Turn On / Off" action:@selector(toggle:) tag:0];
}
- (UIButton *)addButtonWithTitle:(NSString *)title action:(SEL)action tag:(NSInteger)tag {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag = tag;
[button setTitle:title forState:UIControlStateNormal];
[button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
return button;
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGFloat width = MIN(self.view.bounds.size.width - 48.0, 560.0);
CGFloat left = floor((self.view.bounds.size.width - width) / 2.0);
self.stateLabel.frame = CGRectMake(left, 80, width, 40);
CGFloat top = 140.0;
for (UIView *view in self.view.subviews) {
if (view == self.stateLabel) continue;
view.frame = CGRectMake(left, top, width, [view isKindOfClass:[UISlider class]] ? 44.0 : 48.0);
top += 58.0;
}
}
- (NSString *)entityDomain {
return [[[self.entity objectForKey:@"entity_id"] componentsSeparatedByString:@"."] objectAtIndex:0];
}
- (void)toggle:(id)sender {
[self callService:@"toggle" domain:@"homeassistant" additionalData:nil];
}
- (void)coverAction:(UIButton *)sender {
NSArray *services = [NSArray arrayWithObjects:@"", @"open_cover", @"stop_cover", @"close_cover", nil];
[self callService:[services objectAtIndex:sender.tag] domain:@"cover" additionalData:nil];
}
- (void)lockAction:(UIButton *)sender {
[self callService:sender.tag == 1 ? @"lock" : @"unlock" domain:@"lock" additionalData:nil];
}
- (void)runAction:(id)sender {
NSString *domain = [self entityDomain];
[self callService:[domain isEqualToString:@"button"] ? @"press" : @"turn_on" domain:domain additionalData:nil];
}
- (void)brightnessChanged:(UISlider *)slider {
[self callService:@"turn_on" domain:@"light" additionalData:
[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:(int)slider.value] forKey:@"brightness_pct"]];
}
- (void)colorSelected:(UIButton *)sender {
NSArray *colors = [NSArray arrayWithObjects:
[NSArray arrayWithObjects:@255, @0, @0, nil],
[NSArray arrayWithObjects:@0, @255, @0, nil],
[NSArray arrayWithObjects:@0, @0, @255, nil],
[NSArray arrayWithObjects:@255, @255, @255, nil], nil];
[self callService:@"turn_on" domain:@"light" additionalData:
[NSDictionary dictionaryWithObject:[colors objectAtIndex:sender.tag - 1] forKey:@"rgb_color"]];
}
- (void)viewCamera:(id)sender {
HACameraViewController *controller = [[[HACameraViewController alloc]
initWithEntity:self.entity baseURLString:self.baseURLString accessToken:self.accessToken] autorelease];
[self.navigationController pushViewController:controller animated:YES];
}
- (void)callService:(NSString *)service domain:(NSString *)domain additionalData:(NSDictionary *)additionalData {
NSString *savedAccessToken = [HAHomeManager accessTokenForBaseURLString:self.baseURLString];
if ([savedAccessToken length] > 0) {
self.accessToken = savedAccessToken;
}
NSString *path = [NSString stringWithFormat:@"/api/services/%@/%@", domain, service];
NSMutableURLRequest *request = HAMutableURLRequestWithURL(
HAURLWithString([self.baseURLString stringByAppendingString:path]));
request.HTTPMethod = @"POST";
NSMutableDictionary *body = [NSMutableDictionary dictionaryWithObject:[self.entity objectForKey:@"entity_id"]
forKey:@"entity_id"];
if (additionalData != nil) {
[body addEntriesFromDictionary:additionalData];
}
request.HTTPBody = [NSJSONSerialization dataWithJSONObject:body options:0 error:nil];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"Bearer %@", self.accessToken]
forHTTPHeaderField:@"Authorization"];
self.responseData = [NSMutableData data];
self.connection = HAStartURLConnection(request, self);
self.stateLabel.text = @"Sending command…";
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.responseData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
self.connection = nil;
self.responseData = nil;
self.stateLabel.text = @"Command sent";
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
self.connection = nil;
self.responseData = nil;
self.stateLabel.text = [NSString stringWithFormat:@"Failed: %@", [error localizedDescription]];
}
- (void)dealloc {
[_connection cancel];
[_connection release];
[_responseData release];
[_brightnessSlider release];
[_stateLabel release];
[_entity release];
[_baseURLString release];
[_accessToken release];
[super dealloc];
}
@end