-
Notifications
You must be signed in to change notification settings - Fork 2
/
home_assistant_example.dart
executable file
·39 lines (29 loc) · 1.45 KB
/
home_assistant_example.dart
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
import 'package:home_assistant/home_assistant.dart';
import 'package:home_assistant/src/models/configuration.dart';
import 'package:home_assistant/src/models/entity.dart';
import 'package:home_assistant/src/models/service.dart';
void main() {
// Initialize Home Assistant
final HomeAssistant homeAssistant = HomeAssistant(baseUrl: 'http://example.com', bearerToken: 'your_token');
fetchData(homeAssistant);
}
fetchData(HomeAssistant homeAssistant) async {
print("The API is working: ${await homeAssistant.verifyApiIsWorking()}");
final Configuration config = await homeAssistant.fetchConfig();
print(config.toJson());
final List<Entity> entities = await homeAssistant.fetchStates();
print(entities.first.entityId);
final Entity entity = await homeAssistant.fetchState(entities.first.entityId);
print(entity.entityId);
final List<Service> services = await homeAssistant.fetchServices();
print(services.first.domain);
homeAssistant.executeService("switch.ceiling_lights_socket_1", "turn_on", additionalActions: {});
homeAssistant.executeService("light.bedside_lamp_mercury", "turn_on", additionalActions: {
"brightness": 255,
"color_temp": 400,
});
await Future.delayed(Duration(seconds: 1));
homeAssistant.executeService("switch.ceiling_lights_socket_1", "turn_off", additionalActions: {});
homeAssistant.executeService("light.bedside_lamp_mercury", "turn_off", additionalActions: {});
await Future.delayed(Duration(seconds: 1));
}