Refactor: separate models.Device method for device JSON obj

This commit is contained in:
DarkCat09 2024-05-31 19:12:42 +04:00
parent e9ac3d291d
commit 29d49dfeb2
Signed by: DarkCat09
GPG key ID: 0A26CD5B3345D6E3

View file

@ -93,13 +93,23 @@ class Device:
'''Generate a JSON format request body''' '''Generate a JSON format request body'''
obj = { obj = {
'devices': [{ 'devices': [
'mac': self.mac, self._json_device_obj(),
'sensors': [], ]
}] }
return json.dumps(obj)
def _json_device_obj(self) -> dict[str, Any]:
'''Create a device JSON object for json_req_data().
Moved to a separate function to allow performing tests
with multiple devices in a request at once.'''
dev = {
'mac': self.mac,
'sensors': [],
} }
dev = obj['devices'][0]
if self.name is not None: if self.name is not None:
dev['name'] = self.name dev['name'] = self.name
if self.owner is not None: if self.owner is not None:
@ -121,4 +131,4 @@ class Device:
sobj['name'] = s.name sobj['name'] = s.name
sl.append(sobj) sl.append(sobj)
return json.dumps(obj) return dev