Switch API | Component APIs
Switch API
The switch API allows you to give commands to your switch components for reading the state of a physical switch that has multiple discrete positions. A simple switch has two positions, and a knob could have any number of positions.
The switch component supports the following methods:
| Method Name | Description | viam-micro-server Support |
|---|---|---|
SetPosition |
Set the position of the switch. | |
GetPosition |
Return the current position of the switch. | |
GetNumberOfPositions |
Return the number of valid positions for this switch. | |
DoCommand |
Execute model-specific commands that are not otherwise defined by the component API. | |
GetResourceName |
Get the ResourceName for this servo. |
|
Close |
Safely shut down the resource and prevent further use. |
API
SetPosition
Set the position of the switch. Position must be within the valid range for the switch type. Supported by viam-micro-server.
Parameters:
position( int) (required): The position of the switch within the range of available positions.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:
For more information, see the Python SDK Docs.
GetPosition
Return the current position of the switch. 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:
- ( int): : The current position of the switch within the range of available positions.
Example:
For more information, see the Python SDK Docs.
GetNumberOfPositions
Return the number of valid positions for this switch. Supported by viam-micro-server.
Parameters:
Returns:
Example:
For more information, see the Python 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 switch 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:
For more information, see the Python SDK Docs.
GetResourceName
Get the ResourceName for this servo.
Parameters:
name( str) (required): The name of the Resource.
Returns:
- ( viam.proto.common.ResourceName): : The ResourceName of this Resource.
Example:
For more information, see the Python SDK Docs.
Close
Safely shut down the resource and prevent further use.
Parameters:
- None.
Returns:
- None.
Example: