Button API

The button API allows you to get button presses from your button components.

The button component supports the following methods:

Method NameDescriptionviam-micro-server Support
PushPush the button.

DoCommandExecute model-specific commands that are not otherwise defined by the component API.

CloseSafely shut down the resource and prevent further use.

API

Push

Push the button. Supported by viam-micro-server.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • None.

Example:

my_button = Button.from_robot(robot=machine, name="my_button")

# Push the button
await my_button.push()

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

Parameters:

  • extra (None) (optional)
  • callOptions (CallOptions) (optional)

Returns:

  • (Promise)

Example:

const button = new VIAM.ButtonClient(machine, 'my_button');

// Push the button
await button.push();

For more information, see the TypeScript SDK Docs.

DoCommand

Execute model-specific commands that are not otherwise defined by the component API. Most models do not implement DoCommand. Any available model-specific commands should be covered in the model’s documentation. If you are implementing your own button and want to add features that have no corresponding built-in API method, you can implement them with DoCommand. Supported by viam-micro-server.

Parameters:

  • command (Mapping[str, ValueTypes]) (required): The command to execute.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (Mapping[str, viam.utils.ValueTypes]): Result of the executed command.

Raises:

  • (NotImplementedError): Raised if the Resource does not support arbitrary commands.

Example:

my_button = Button.from_robot(robot=machine, name="my_button")
command = {"cmd": "test", "data1": 500}
result = await my_button.do_command(command)

For more information, see the Python SDK Docs.

Parameters:

Returns:

Example:

myButton, err := button.FromRobot(machine, "my_button")

command := map[string]interface{}{"cmd": "test", "data1": 500}
result, err := myButton.DoCommand(context.Background(), command)

For more information, see the Go SDK Docs.

Parameters:

  • command (Struct) (required): The command to execute.
  • callOptions (CallOptions) (optional)

Returns:

Example:

import { Struct } from '@viamrobotics/sdk';

const result = await resource.doCommand(
  Struct.fromJson({
    myCommand: { key: 'value' },
  })
);

For more information, see the TypeScript SDK Docs.

Close

Safely shut down the resource and prevent further use.

Parameters:

  • None.

Returns:

  • None.

Example:

my_button = Button.from_robot(robot=machine, name="my_button")
await my_button.close()

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.

Returns:

  • (error): An error, if one occurred.

Example:

myButton, err := button.FromRobot(machine, "my_button")

err = myButton.Close(context.Background())

For more information, see the Go SDK Docs.