From 134aca4d2e58bd9c4c1b82f7d8394f533b242d2f Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Fri, 19 Mar 2021 10:22:15 -0700 Subject: [PATCH 1/4] Add clientIdentifier attribute to SystemDevice --- plexapi/server.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plexapi/server.py b/plexapi/server.py index d90c76dac..e57b739b2 100644 --- a/plexapi/server.py +++ b/plexapi/server.py @@ -842,6 +842,7 @@ class SystemDevice(PlexObject): Attributes: TAG (str): 'Device' + clientIdentifier (str): The unique identifier for the device. createdAt (datatime): Datetime the device was created. id (int): The ID of the device (not the same as :class:`~plexapi.myplex.MyPlexDevice` ID). key (str): API URL (/devices/) @@ -852,6 +853,7 @@ class SystemDevice(PlexObject): def _loadData(self, data): self._data = data + self.clientIdentifier = data.attrib.get('clientIdentifier') self.createdAt = utils.toDatetime(data.attrib.get('createdAt')) self.id = cast(int, data.attrib.get('id')) self.key = '/devices/%s' % self.id From 1a70501d25144c68aef6cea5abffb0a351a5c809 Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Fri, 19 Mar 2021 10:26:20 -0700 Subject: [PATCH 2/4] Add test for SystemDevice clientIdentifier --- tests/test_server.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_server.py b/tests/test_server.py index eb85177b4..c66626c51 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -394,6 +394,7 @@ def test_server_system_devices(plex): devices = plex.systemDevices() assert len(devices) device = devices[-1] + assert len(device.clientIdentifier) or device.clientIdentifier is None assert utils.is_datetime(device.createdAt) assert utils.is_int(device.id) assert len(device.key) From 74767e15076a768559612b59d0654196bc332d6c Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Sat, 20 Mar 2021 21:25:58 -0700 Subject: [PATCH 3/4] Add methods to return a specific SystemAccount or SystemDevice by ID --- plexapi/server.py | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/plexapi/server.py b/plexapi/server.py index e57b739b2..894dbe0c7 100644 --- a/plexapi/server.py +++ b/plexapi/server.py @@ -217,19 +217,41 @@ def createToken(self, type='delegation', scope='all'): return q.attrib.get('token') def systemAccounts(self): - """ Returns a list of :class:`~plexapi.server.SystemAccounts` objects this server contains. """ + """ Returns a list of :class:`~plexapi.server.SystemAccount` objects this server contains. """ if self._systemAccounts is None: key = '/accounts' self._systemAccounts = self.fetchItems(key, SystemAccount) return self._systemAccounts + def systemAccount(self, accountID): + """ Returns the :class:`~plexapi.server.SystemAccount` object for the specified account ID. + + Parameters: + accountID (int): The :class:`~plexapi.server.SystemAccount` ID. + """ + try: + return next(account for account in self.systemAccounts() if account.id == accountID) + except StopIteration: + raise NotFound('Unknown account with accountID=%s' % accountID) from None + def systemDevices(self): - """ Returns a list of :class:`~plexapi.server.SystemDevices` objects this server contains. """ + """ Returns a list of :class:`~plexapi.server.SystemDevice` objects this server contains. """ if self._systemDevices is None: key = '/devices' self._systemDevices = self.fetchItems(key, SystemDevice) return self._systemDevices + def systemDevice(self, deviceID): + """ Returns the :class:`~plexapi.server.SystemDevice` object for the specified device ID. + + Parameters: + deviceID (int): The :class:`~plexapi.server.SystemDevice` ID. + """ + try: + return next(device for device in self.systemDevices() if device.id == deviceID) + except StopIteration: + raise NotFound('Unknown device with deviceID=%s' % deviceID) from None + def myPlexAccount(self): """ Returns a :class:`~plexapi.myplex.MyPlexAccount` object using the same token to access this server. If you are not the owner of this PlexServer @@ -896,19 +918,11 @@ def __repr__(self): def account(self): """ Returns the :class:`~plexapi.server.SystemAccount` associated with the bandwidth data. """ - accounts = self._server.systemAccounts() - try: - return next(account for account in accounts if account.id == self.accountID) - except StopIteration: - raise NotFound('Unknown account for this bandwidth data: accountID=%s' % self.accountID) + return self._server.systemAccount(self.accountID) def device(self): """ Returns the :class:`~plexapi.server.SystemDevice` associated with the bandwidth data. """ - devices = self._server.systemDevices() - try: - return next(device for device in devices if device.id == self.deviceID) - except StopIteration: - raise NotFound('Unknown device for this bandwidth data: deviceID=%s' % self.deviceID) + return self._server.systemDevice(self.deviceID) class StatisticsResources(PlexObject): From 5f4d3b8c343833eb88031d0c54133c83aa15261e Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Sat, 20 Mar 2021 21:26:25 -0700 Subject: [PATCH 4/4] Update tests for SystemAccounts and SystemDevices --- tests/test_server.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_server.py b/tests/test_server.py index c66626c51..3089d583f 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -388,18 +388,20 @@ def test_server_system_accounts(plex): assert account.thumb == "" assert account.accountID == account.id assert account.accountKey == account.key + assert plex.systemAccount(account.id) == account def test_server_system_devices(plex): devices = plex.systemDevices() assert len(devices) device = devices[-1] - assert len(device.clientIdentifier) or device.clientIdentifier is None + assert device.clientIdentifier or device.clientIdentifier is None assert utils.is_datetime(device.createdAt) assert utils.is_int(device.id) assert len(device.key) assert len(device.name) or device.name == "" assert len(device.platform) or device.platform == "" + assert plex.systemDevice(device.id) == device @pytest.mark.authenticated