Skip to content

Commit

Permalink
[Python] export cluster status when sendCommand failed (#27335)
Browse files Browse the repository at this point in the history
  • Loading branch information
tianfeng-yang authored and pull[bot] committed Feb 13, 2024
1 parent 245a7d9 commit 7ce20d2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/controller/python/chip/clusters/Command.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def FindCommandClusterObject(isClientSideCommand: bool, path: CommandPath):
if inspect.isclass(command):
for name, field in inspect.getmembers(command):
if ('__dataclass_fields__' in name):
if (field['cluster_id'].default == path.ClusterId) and (field['command_id'].default == path.CommandId) and (field['is_client'].default == isClientSideCommand):
if (field['cluster_id'].default == path.ClusterId) and (field['command_id'].default ==
path.CommandId) and (field['is_client'].default == isClientSideCommand):
return eval('chip.clusters.Objects.' + clusterName + '.Commands.' + commandName)
return None

Expand Down Expand Up @@ -105,11 +106,11 @@ def _handleError(self, imError: Status, chipError: PyChipError, exception: Excep
else:
try:
self._future.set_exception(
chip.interaction_model.InteractionModelError(chip.interaction_model.Status(imError.IMStatus)))
chip.interaction_model.InteractionModelError(chip.interaction_model.Status(imError.IMStatus), imError.ClusterStatus))
except Exception as e2:
logger.exception("Failed to map interaction model status received: %s. Remapping to Failure." % imError)
self._future.set_exception(chip.interaction_model.InteractionModelError(
chip.interaction_model.Status.Failure))
chip.interaction_model.Status.Failure, imError.ClusterStatus))

def handleError(self, status: Status, chipError: PyChipError):
self._event_loop.call_soon_threadsafe(
Expand All @@ -126,7 +127,8 @@ def handleError(self, status: Status, chipError: PyChipError):


@_OnCommandSenderResponseCallbackFunct
def _OnCommandSenderResponseCallback(closure, endpoint: int, cluster: int, command: int, imStatus: int, clusterStatus: int, payload, size):
def _OnCommandSenderResponseCallback(closure, endpoint: int, cluster: int, command: int,
imStatus: int, clusterStatus: int, payload, size):
data = ctypes.string_at(payload, size)
closure.handleResponse(CommandPath(endpoint, cluster, command), Status(
imStatus, clusterStatus), data[:])
Expand All @@ -142,7 +144,8 @@ def _OnCommandSenderDoneCallback(closure):
ctypes.pythonapi.Py_DecRef(ctypes.py_object(closure))


def SendCommand(future: Future, eventLoop, responseType: Type, device, commandPath: CommandPath, payload: ClusterCommand, timedRequestTimeoutMs: Union[None, int] = None, interactionTimeoutMs: Union[None, int] = None, busyWaitMs: Union[None, int] = None) -> PyChipError:
def SendCommand(future: Future, eventLoop, responseType: Type, device, commandPath: CommandPath, payload: ClusterCommand,
timedRequestTimeoutMs: Union[None, int] = None, interactionTimeoutMs: Union[None, int] = None, busyWaitMs: Union[None, int] = None) -> PyChipError:
''' Send a cluster-object encapsulated command to a device and does the following:
- On receipt of a successful data response, returns the cluster-object equivalent through the provided future.
- None (on a successful response containing no data)
Expand Down
20 changes: 18 additions & 2 deletions src/controller/python/chip/interaction_model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
__all__ = ["Status", "InteractionModelError"]


# defined src/controller/python/chip/interaction_model/Delegate.h
kUndefinedClusterStatus: int = 0xFF


class Status(enum.IntEnum):
Success = 0x0
Failure = 0x01
Expand Down Expand Up @@ -79,12 +83,24 @@ class Status(enum.IntEnum):


class InteractionModelError(ChipStackException):
def __init__(self, status: Status):
def __init__(self, status: Status, clusterStatus: int = kUndefinedClusterStatus):
self._status = status
self._clusterStatus = clusterStatus

def __str__(self):
return f"InteractionModelError: {self._status.name} (0x{self._status.value:x})"
if self.hasClusterStatus:
return f"InteractionModelError: {self._status.name} (0x{self._status.value:x}, clusterStatus: {self._clusterStatus})"
else:
return f"InteractionModelError: {self._status.name} (0x{self._status.value:x})"

@property
def hasClusterStatus(self) -> bool:
return self._clusterStatus != kUndefinedClusterStatus

@property
def status(self) -> Status:
return self._status

@property
def clusterStatus(self) -> int:
return self._clusterStatus

0 comments on commit 7ce20d2

Please sign in to comment.