A Dart package for interacting with the Home Connect API, which provides a unified interface for controlling a variety of smart home appliances. Getting started Prerequisites
Dart 2.17 or higher
Add the following dependency to your pubspec.yaml
file:
yaml
dependencies:
homeconnect: ^0.0.2
Then, run dart pub get
to install the package.
First, import the package:
import 'package:homeconnect/homeconnect.dart';
To create an instance of the HomeConnectApi class, you need to provide the base URL of the Home Connect API, as well as the client credentials for authentication. Checkout out the example file to get a better idea.
// set up the api
HomeConnectApi api = HomeConnectApi(
Uri.parse('https://simulator.home-connect.com/'),
credentials: HomeConnectClientCredentials(
clientId: 'Your client id',
clientSecret: 'Your client secret',
redirectUri: 'https://example.com',
),
authenticator: SandboxAuthorizer(),
);
api.storage.setCredentials(HomeConnectAuthCredentials(
accessToken: accessToken,
refreshToken: refreshToken,
expirationDate: DateTime.now().add(Duration(days: 1)),
));
final api = HomeConnectApi(Uri.parse('https://api.home-connect.com'), credentials: HomeConnectClientCredentials(clientId: '...', clientSecret: '...'));
final devices = await api.getDevices();
You can pick a device using any method, here we select the first oven from the device list.
final selectedDevice = devices.firstWhere((element) => element.info.type == DeviceType.oven);
Once you have the selected device, to fetch its data we need to run selectedDevice.init()
await selectedDevice.init();
After this we will have access to the devices programs and status.
// print all available programs
for (var program in selectedDevice.programs) {
print(program.key);
}
// print device status
for (var stat in selectedDevice.status) {
print(stat.key);
}
We need to select a program before getting its options and contraints.
await selectedDevice.selectProgram(programKey: 'Cooking.Oven.Program.HeatingMode.TopBottomHeating');
Selecting a program will allows to use startProgram
, but first we need to set some valid values for the options.
In the example we will print the program options and their coinstraints to get the valid values.
for (var option in selectedDevice.selectedProgram.options) {
print(option.key);
print(option.constraints!.toJson());
}
The output will be something like this:
Cooking.Oven.Option.SetpointTemperature
{min: 30, max: 250, stepsize: 5}
BSH.Common.Option.Duration
{min: 1, max: 86340, stepsize: 1}
Now we can a start a program with valid options.
First, lets generate the option payloads
We use toCommandPayload
to parse the data for the request.
final tempOption = ProgramOptions.toCommandPayload(key: 'Cooking.Oven.Option.SetpointTemperature', value: 200);
final durationOption = ProgramOptions.toCommandPayload(key: 'BSH.Common.Option.Duration', value: 500);
To start the program just call the method like this:
await selectedDevice.startProgram(options: [tempOption, durationOption]);
HomeDevice class also allows you to turn off and on your device.
selectedDevice.turnOff();
selectedDevice.turnOn();