import abc
from typing import TYPE_CHECKING, ClassVar, List, Mapping, Optional, SupportsBytes, SupportsFloat, Union, cast
from typing_extensions import Self
from viam.resource.base import ResourceBase
if TYPE_CHECKING:
from viam.resource.types import Subtype
from viam.robot.client import RobotClient
ValueTypes = Union[bool, SupportsBytes, SupportsFloat, List, Mapping, str, None]
[docs]class ComponentBase(abc.ABC, ResourceBase):
"""
Base component.
All components must inherit from this class.
"""
SUBTYPE: ClassVar["Subtype"]
def __init__(self, name: str):
self.name = name
[docs] @classmethod
def from_robot(cls, robot: "RobotClient", name: str) -> Self:
"""Get the component named ``name`` from the provided robot.
Args:
robot (RobotClient): The robot
name (str): The name of the component
Returns:
Self: The component, if it exists on the robot
"""
component = robot.get_component(cls.get_resource_name(name))
return cast(cls, component)
[docs] async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
raise NotImplementedError()