home_assistant 1.0.0 home_assistant: ^1.0.0 copied to clipboard
A Dart library for interacting with Home Assistant APIs.
example/home_assistant_example.dart
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));
}