|
| 1 | +import abc |
| 2 | +from typing import Any, Final, Mapping, Optional |
| 3 | + |
| 4 | +from viam.resource.types import API, RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT |
| 5 | +from viam.components.component_base import ComponentBase |
| 6 | + |
| 7 | + |
| 8 | +class Switch(ComponentBase): |
| 9 | + """ |
| 10 | + Switch represents a device with two or more finite states (or positions) than can be set and retrieved. |
| 11 | +
|
| 12 | + This acts as an abstract base class for any drivers representing specific |
| 13 | + switch implementations. This cannot be used on its own. If the ``__init__()`` function is |
| 14 | + overridden, it must call the ``super().__init__()`` function. |
| 15 | +
|
| 16 | + :: |
| 17 | +
|
| 18 | + from viam.components.switch import Switch |
| 19 | +
|
| 20 | + For more information, see `Switch component <https://docs.viam.com/dev/reference/apis/components/switch/>`_. |
| 21 | + """ |
| 22 | + |
| 23 | + API: Final = API( # pyright: ignore [reportIncompatibleVariableOverride] |
| 24 | + RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, "switch" |
| 25 | + ) |
| 26 | + |
| 27 | + @abc.abstractmethod |
| 28 | + async def get_position(self, *, extra: Optional[Mapping[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> int: |
| 29 | + """ |
| 30 | + Get the current position of the switch |
| 31 | +
|
| 32 | + :: |
| 33 | +
|
| 34 | + my_switch = Switch.from_robot(robot=machine, name="my_switch") |
| 35 | +
|
| 36 | + # Update the switch from its current position to the desired position of 1. |
| 37 | + await my_switch.set_position(1) |
| 38 | +
|
| 39 | + # Get the current set position of the switch. |
| 40 | + pos1 = await my_switch.get_position() |
| 41 | +
|
| 42 | + # Update the switch from its current position to the desired position. |
| 43 | + await my_switch.set_position(0) |
| 44 | +
|
| 45 | + # Get the current set position of the switch. |
| 46 | + pos2 = await my_switch.get_position() |
| 47 | +
|
| 48 | + Returns: |
| 49 | + int: The current position of the switch within the range of available positions. |
| 50 | +
|
| 51 | + For more information, see `Switch component <https://docs.viam.com/dev/reference/apis/components/Switch/#getposition>`_. |
| 52 | + """ |
| 53 | + ... |
| 54 | + |
| 55 | + @abc.abstractmethod |
| 56 | + async def set_position( |
| 57 | + self, position: int, *, extra: Optional[Mapping[str, Any]] = None, timeout: Optional[float] = None, **kwargs |
| 58 | + ) -> None: |
| 59 | + """ |
| 60 | + Sets the current position of the switch. |
| 61 | +
|
| 62 | + :: |
| 63 | +
|
| 64 | + my_switch = Switch.from_robot(robot=machine, name="my_switch") |
| 65 | +
|
| 66 | + # Update the switch from its current position to the desired position of 1. |
| 67 | + await my_switch.set_position(1) |
| 68 | +
|
| 69 | + # Update the switch from its current position to the desired position of 0. |
| 70 | + await my_switch.set_position(0) |
| 71 | +
|
| 72 | + Args: |
| 73 | + position (int): The position of the switch within the range of available positions. |
| 74 | +
|
| 75 | + For more information, see `Switch component <https://docs.viam.com/dev/reference/apis/components/switch/#setposition>`_. |
| 76 | + """ |
| 77 | + ... |
| 78 | + |
| 79 | + @abc.abstractmethod |
| 80 | + async def get_number_of_positions(self, *, extra: Optional[Mapping[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> int: |
| 81 | + """ |
| 82 | + Get the number of available positions on the switch. |
| 83 | +
|
| 84 | + :: |
| 85 | +
|
| 86 | + my_switch = Switch.from_robot(robot=machine, name="my_switch") |
| 87 | +
|
| 88 | + print(await my_switch.get_number_of_positions()) |
| 89 | +
|
| 90 | + Returns: |
| 91 | + int: The number of available positions. |
| 92 | +
|
| 93 | + For more information, see `Switch component <https://docs.viam.com/dev/reference/apis/components/switch/#getnumberofpositions>`_. |
| 94 | + """ |
| 95 | + ... |
0 commit comments