diff --git a/src/meshctrl/device.py b/src/meshctrl/device.py index 3efefe8..ee9a651 100644 --- a/src/meshctrl/device.py +++ b/src/meshctrl/device.py @@ -295,6 +295,23 @@ class Device(object): ''' return await self._session.reset_devices(self.nodeid, timeout=timeout) + async def remove(self, timeout=None): + ''' + Remove device from MeshCentral + + Args: + nodeids (str|list[str]): nodeid(s) of the device(s) that have to be removed + timeout (int): duration in seconds to wait for a response before throwing an error + + Returns: + bool: True on success, raise otherwise + + Raises: + :py:class:`~meshctrl.exceptions.SocketError`: Info about socket closure + asyncio.TimeoutError: Command timed out + ''' + return self._session.remove_devices(self.nodeid, timeout) + async def sleep(self, timeout=None): ''' Sleep device diff --git a/src/meshctrl/session.py b/src/meshctrl/session.py index f047776..6aebdf1 100644 --- a/src/meshctrl/session.py +++ b/src/meshctrl/session.py @@ -571,7 +571,7 @@ class Session(object): while True: data = await event_queue.get() if filter and not util.compare_dict(filter, data): - continue + continue yield data finally: self._eventer.off("server_event", _) @@ -1062,6 +1062,30 @@ class Session(object): raise exceptions.ServerError(data["result"]) return True + async def remove_devices(self, nodeids, timeout=None): + ''' + Remove device(s) from MeshCentral + + Args: + nodeids (str|list[str]): nodeid(s) of the device(s) that have to be removed + timeout (int): duration in seconds to wait for a response before throwing an error + + Returns: + bool: True on success, raise otherwise + + Raises: + :py:class:`~meshctrl.exceptions.ServerError`: Error text from server if there is a failure + :py:class:`~meshctrl.exceptions.SocketError`: Info about socket closure + asyncio.TimeoutError: Command timed out + ''' + if isinstance(nodeids, str): + nodeids = [nodeids] + + data = await self._send_command({ "action": 'removedevices', "nodeids": nodeids}, "remove_devices", timeout=timeout) + + if data.get("result", "ok").lower() != "ok": + raise exceptions.ServerError(data["result"]) + return True async def add_device_group(self, name, description="", amtonly=False, features=0, consent=0, timeout=None): ''' diff --git a/tests/test_session.py b/tests/test_session.py index dd1c973..13bd48b 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -336,6 +336,10 @@ async def test_mesh_device(env): r = await admin_session.remove_users_from_device_group((await privileged_session.user_info())["_id"], mesh.meshid, timeout=10) print("\ninfo remove_users_from_device_group: {}\n".format(r)) assert (r[(await privileged_session.user_info())["_id"]]["success"]), "Failed to remove user from device group" + + # Dunno how to test if this actually works, so just check for errors, I guess. + admin_session.remove_devices(agent.nodeid, timeout=10) + assert (await admin_session.remove_users_from_device(agent.nodeid, (await unprivileged_session.user_info())["_id"], timeout=10)), "Failed to remove user from device"