updateDeviceApi function

Future<void> updateDeviceApi({
  1. required BuildContext context,
  2. required int deviceId,
  3. String? displayName,
  4. DeviceAddress? deviceAddress,
  5. ConnectionTypeDTO? connectionType,
})

Update a device's details with the given display name and device address

Implementation

Future<void> updateDeviceApi({
  required BuildContext context,
  required int deviceId,
  String? displayName,
  DeviceAddress? deviceAddress,
  ConnectionTypeDTO? connectionType,
}) async {
  if (kDebugMode) {
    print('Updating device $deviceId with the following:\n'
        '- Display name: $displayName\n'
        '- Address: ${deviceAddress?.displayAddress}');
  }

  final parentContext = context
      .getElementForInheritedWidgetOfExactType<UncontrolledProviderScope>();

  // Use the parent context to allow for refreshing a device even if the
  // original context is disposed
  if (parentContext != null) {
    context = parentContext;
  }

  final api = await context.read(energyManagementV3ApiProvider.future);

  final response = await api.kemApiV3DevicesIdPut(
    id: deviceId,
    body: DeviceUpdateV3Body(
      displayName: displayName,
      address: deviceAddress != null ? deviceAddressToDto(deviceAddress) : null,
      connectionType: connectionType,
    ),
  );

  assertSuccessfulResponse(response);

  if (!context.mounted) return;

  final refreshDetails = context.read(fullRefreshGeneratorDetailsProvider);
  await refreshDetails(context, deviceId);
}