postState method
Updates or creates a state. You can create any state that you want, it does not have to be backed by an entity in Home Assistant.
This endpoint sets the representation of a device within Home Assistant and will not communicate with the actual device. To communicate with the device, use postService.
The return code is 200 if the entity existed, 201 if the state of a new entity was set. A location header will be returned with the URL of the new resource. The response body will contain a JSON encoded State object.
Implementation
Future<(HaState?, HaFailure?)> postState({
required String entityId,
required String state,
Map<String, dynamic>? attributes,
}) async {
final endpoint = '/api/states/$entityId';
final data = jsonEncode({
'state': state,
'attributes': attributes,
});
final response =
await sl.get<HttpClient>().post(url + endpoint, _headers, data);
return response.success
? (HaState.fromJson(jsonDecode(response.dataStr)), null)
: (null, HaFailure(message: response.dataStr));
}