Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/viam/components/power_sensor/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
GetStatusResponse,
)
from viam.proto.component.powersensor import (
GetBatteryPercentRequest,
GetBatteryPercentResponse,
GetCurrentRequest,
GetCurrentResponse,
GetPowerRequest,
GetPowerResponse,
GetPropertiesRequest,
GetPropertiesResponse,
GetVoltageRequest,
GetVoltageResponse,
PowerSensorServiceStub,
Expand Down Expand Up @@ -80,6 +84,30 @@ async def get_readings(
response: GetReadingsResponse = await self.client.GetReadings(request, timeout=timeout, metadata=md)
return sensor_readings_value_to_native(response.readings)

async def get_battery_percent(
self,
*,
extra: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = None,
**kwargs,
) -> float:
md = kwargs.get("metadata", self.Metadata()).proto
request = GetBatteryPercentRequest(name=self.name, extra=dict_to_struct(extra))
response: GetBatteryPercentResponse = await self.client.GetBatteryPercent(request, timeout=timeout, metadata=md)
return response.percent

async def get_properties(
self,
*,
extra: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = None,
**kwargs,
) -> PowerSensor.Properties:
md = kwargs.get("metadata", self.Metadata()).proto
request = GetPropertiesRequest(name=self.name, extra=dict_to_struct(extra))
response: GetPropertiesResponse = await self.client.GetProperties(request, timeout=timeout, metadata=md)
return response

async def do_command(
self,
command: Mapping[str, ValueTypes],
Expand Down
44 changes: 43 additions & 1 deletion src/viam/components/power_sensor/power_sensor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import abc
from typing import Any, Dict, Final, Mapping, Optional, Tuple
from typing import Any, Dict, Final, Mapping, Optional, Tuple, TypeAlias

from viam.components.component_base import ComponentBase
from viam.proto.component.powersensor import GetPropertiesResponse
from viam.resource.types import API, RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT
from viam.utils import SensorReading

Expand All @@ -23,6 +24,8 @@ class PowerSensor(ComponentBase):
RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, "power_sensor"
)

Properties: "TypeAlias" = GetPropertiesResponse

@abc.abstractmethod
async def get_voltage(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> Tuple[float, bool]:
"""Return the voltage reading of a specified device and whether it is AC or DC.
Expand Down Expand Up @@ -102,3 +105,42 @@ async def get_readings(
For more information, see `Power Sensor component <https://docs.viam.com/reference/apis/components/power-sensor/#getreadings>`_.
"""
...

@abc.abstractmethod
async def get_battery_percent(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> float:
"""Return the remaining battery charge, from 0 to 100.

::

my_power_sensor = PowerSensor.from_robot(robot=machine, name='my_power_sensor')

# Get the remaining battery charge from the power sensor
percent = await my_power_sensor.get_battery_percent()
print("The battery charge remaining is", percent, "%")

Returns:
float: The remaining battery charge, from 0 to 100.

For more information, see `Power Sensor component
<https://docs.viam.com/reference/apis/components/power-sensor/#getbatterypercent>`_.
"""
...

@abc.abstractmethod
async def get_properties(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> Properties:
"""Return a dictionary of the optional methods this power sensor supports.

::

my_power_sensor = PowerSensor.from_robot(robot=machine, name='my_power_sensor')

# Get whether the power sensor supports GetBatteryPercent.
properties = await my_power_sensor.get_properties()

Returns:
PowerSensor.Properties: Map of optional properties to supported status.

For more information, see `Power Sensor component
<https://docs.viam.com/reference/apis/components/power-sensor/#getproperties>`_.
"""
...
23 changes: 23 additions & 0 deletions src/viam/components/power_sensor/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
GetStatusResponse,
)
from viam.proto.component.powersensor import (
GetBatteryPercentRequest,
GetBatteryPercentResponse,
GetCurrentRequest,
GetCurrentResponse,
GetPowerRequest,
GetPowerResponse,
GetPropertiesRequest,
GetPropertiesResponse,
GetVoltageRequest,
GetVoltageResponse,
PowerSensorServiceBase,
Expand Down Expand Up @@ -69,6 +73,25 @@ async def GetPower(self, stream: Stream[GetPowerRequest, GetPowerResponse]) -> N
response = GetPowerResponse(watts=power)
await stream.send_message(response)

async def GetBatteryPercent(self, stream: Stream[GetBatteryPercentRequest, GetBatteryPercentResponse]) -> None:
request = await stream.recv_message()
assert request is not None
name = request.name
sensor = self.get_resource(name)
timeout = stream.deadline.time_remaining() if stream.deadline else None
percent = await sensor.get_battery_percent(extra=struct_to_dict(request.extra), timeout=timeout, metadata=stream.metadata)
response = GetBatteryPercentResponse(percent=percent)
await stream.send_message(response)

async def GetProperties(self, stream: Stream[GetPropertiesRequest, GetPropertiesResponse]) -> None:
request = await stream.recv_message()
assert request is not None
name = request.name
sensor = self.get_resource(name)
timeout = stream.deadline.time_remaining() if stream.deadline else None
properties = await sensor.get_properties(extra=struct_to_dict(request.extra), timeout=timeout, metadata=stream.metadata)
await stream.send_message(properties)

async def DoCommand(self, stream: Stream[DoCommandRequest, DoCommandResponse]) -> None:
request = await stream.recv_message()
assert request is not None
Expand Down
376 changes: 188 additions & 188 deletions src/viam/gen/app/v1/app_pb2.py

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions src/viam/gen/app/v1/app_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5800,6 +5800,7 @@ class Model(_message.Message):
MARKDOWN_DOCUMENTATION_FIELD_NUMBER: _builtins.int
DESCRIPTION_FIELD_NUMBER: _builtins.int
SUPPORTED_HARDWARE_FIELD_NUMBER: _builtins.int
APIS_FIELD_NUMBER: _builtins.int
api: _builtins.str
'The colon-delimited-triplet of the api implemented by the model'
model: _builtins.str
Expand All @@ -5813,13 +5814,19 @@ class Model(_message.Message):
def supported_hardware(self) -> _containers.RepeatedScalarFieldContainer[_builtins.str]:
"""A list of supported hardware names"""

def __init__(self, *, api: _builtins.str=..., model: _builtins.str=..., markdown_documentation: _builtins.str | None=..., description: _builtins.str | None=..., supported_hardware: _abc.Iterable[_builtins.str] | None=...) -> None:
@_builtins.property
def apis(self) -> _containers.RepeatedScalarFieldContainer[_builtins.str]:
"""The full set of co-equal APIs this model serves. The `api` field is always one of these and is
retained for tooling that predates `apis`; new code should read `apis`.
"""

def __init__(self, *, api: _builtins.str=..., model: _builtins.str=..., markdown_documentation: _builtins.str | None=..., description: _builtins.str | None=..., supported_hardware: _abc.Iterable[_builtins.str] | None=..., apis: _abc.Iterable[_builtins.str] | None=...) -> None:
...
_HasFieldArgType: _TypeAlias = _typing.Literal['_description', b'_description', '_markdown_documentation', b'_markdown_documentation', 'description', b'description', 'markdown_documentation', b'markdown_documentation']

def HasField(self, field_name: _HasFieldArgType) -> _builtins.bool:
...
_ClearFieldArgType: _TypeAlias = _typing.Literal['_description', b'_description', '_markdown_documentation', b'_markdown_documentation', 'api', b'api', 'description', b'description', 'markdown_documentation', b'markdown_documentation', 'model', b'model', 'supported_hardware', b'supported_hardware']
_ClearFieldArgType: _TypeAlias = _typing.Literal['_description', b'_description', '_markdown_documentation', b'_markdown_documentation', 'api', b'api', 'apis', b'apis', 'description', b'description', 'markdown_documentation', b'markdown_documentation', 'model', b'model', 'supported_hardware', b'supported_hardware']

def ClearField(self, field_name: _ClearFieldArgType) -> None:
...
Expand Down
12 changes: 6 additions & 6 deletions src/viam/gen/app/v1/billing_pb2.py

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/viam/gen/app/v1/billing_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class _UsageCostTypeEnumTypeWrapper(_enum_type_wrapper._EnumTypeWrapper[_UsageCo
USAGE_COST_TYPE_PIPELINE_SINK_COMPUTE: _UsageCostType.ValueType
USAGE_COST_TYPE_PREMIUM_MODULE: _UsageCostType.ValueType
USAGE_COST_TYPE_SIMULATION_COMPUTE: _UsageCostType.ValueType
USAGE_COST_TYPE_AI_USAGE: _UsageCostType.ValueType

class UsageCostType(_UsageCostType, metaclass=_UsageCostTypeEnumTypeWrapper):
...
Expand Down Expand Up @@ -121,6 +122,7 @@ USAGE_COST_TYPE_PIPELINE_SINK_CLOUD_STORAGE: UsageCostType.ValueType
USAGE_COST_TYPE_PIPELINE_SINK_COMPUTE: UsageCostType.ValueType
USAGE_COST_TYPE_PREMIUM_MODULE: UsageCostType.ValueType
USAGE_COST_TYPE_SIMULATION_COMPUTE: UsageCostType.ValueType
USAGE_COST_TYPE_AI_USAGE: UsageCostType.ValueType
Global___UsageCostType: _TypeAlias = UsageCostType

class _SourceType:
Expand Down
Loading