@@ -23,19 +23,46 @@ class Inventory(CumulocityResource):
23
23
def __init__ (self , c8y ):
24
24
super ().__init__ (c8y , 'inventory/managedObjects' )
25
25
26
- def get (self , id ) -> ManagedObject : # noqa (id)
26
+ def get (
27
+ self ,
28
+ id : str , # noqa
29
+ with_children : bool = None ,
30
+ with_children_count : bool = None ,
31
+ skip_children_names : bool = None ,
32
+ with_parents : bool = None ,
33
+ with_latest_values : bool = None ,
34
+ ** kwargs ) -> ManagedObject :
27
35
""" Retrieve a specific managed object from the database.
28
36
29
37
Args:
30
- ID of the managed object
38
+ id (str): Cumulocity ID of the managed object
39
+ with_children (bool): Whether children with ID and name should be
40
+ included with each returned object
41
+ with_children_count (bool): When set to true, the returned result
42
+ will contain the total number of children in the respective
43
+ child additions, assets and devices sub fragments.
44
+ skip_children_names (bool): If true, returned references of child
45
+ devices won't contain their names.
46
+ with_parents (bool): Whether to include a device's parents.
47
+ with_latest_values (bool): If true the platform includes the
48
+ fragment `c8y_LatestMeasurements, which contains the latest
49
+ measurement values reported by the device to the platform.
31
50
32
51
Returns:
33
52
A ManagedObject instance
34
53
35
54
Raises:
36
55
KeyError: if the ID is not defined within the database
37
56
"""
38
- managed_object = ManagedObject .from_json (self ._get_object (id ))
57
+ managed_object = ManagedObject .from_json (self ._get_object (
58
+ id ,
59
+ with_children = with_children ,
60
+ with_children_count = with_children_count ,
61
+ skip_children_names = skip_children_names ,
62
+ with_parents = with_parents ,
63
+ with_latest_values = with_latest_values ,
64
+ ** kwargs )
65
+ )
39
66
managed_object .c8y = self .c8y # inject c8y connection into instance
40
67
return managed_object
41
68
@@ -98,6 +125,66 @@ def get_all(
98
125
as_values = as_values ,
99
126
** kwargs ))
100
127
128
+ def get_by (
129
+ self ,
130
+ expression : str = None ,
131
+ query : str = None ,
132
+ ids : list [str | int ] = None ,
133
+ order_by : str = None ,
134
+ type : str = None ,
135
+ parent : str = None ,
136
+ fragment : str = None ,
137
+ fragments : list [str ] = None ,
138
+ name : str = None ,
139
+ owner : str = None ,
140
+ text : str = None ,
141
+ only_roots : str = None ,
142
+ with_children : bool = None ,
143
+ with_children_count : bool = None ,
144
+ skip_children_names : bool = None ,
145
+ with_groups : bool = None ,
146
+ with_parents : bool = None ,
147
+ with_latest_values : bool = None ,
148
+ as_values : str | tuple | list [str | tuple ] = None ,
149
+ ** kwargs ) -> ManagedObject :
150
+ """ Query the database for a specific managed object.
151
+
152
+ This function is a special version of the `select` function assuming a single
153
+ result being returned by the query.
154
+
155
+ Returns:
156
+ A ManagedObject instance
157
+
158
+ Raises:
159
+ ValueError: if the query did not return any or more than one result.
160
+ """
161
+ result = list (self .select (
162
+ expression = expression ,
163
+ query = query ,
164
+ ids = ids ,
165
+ order_by = order_by ,
166
+ type = type ,
167
+ parent = parent ,
168
+ fragment = fragment ,
169
+ fragments = fragments ,
170
+ name = name ,
171
+ owner = owner ,
172
+ text = text ,
173
+ only_roots = only_roots ,
174
+ with_children = with_children ,
175
+ with_children_count = with_children_count ,
176
+ skip_children_names = skip_children_names ,
177
+ with_groups = with_groups ,
178
+ with_parents = with_parents ,
179
+ with_latest_values = with_latest_values ,
180
+ page_size = 2 ,
181
+ as_values = as_values ,
182
+ ** kwargs ))
183
+ if len (result ) == 1 :
184
+ return result [0 ]
185
+ raise ValueError ("No matching object found." if not result
186
+ else "Ambiguous query; multiple matching objects found." )
187
+
101
188
def get_count (
102
189
self ,
103
190
expression : str = None ,
@@ -455,19 +542,46 @@ def accept(self, id: str): # noqa (id)
455
542
"""
456
543
self .c8y .put ('/devicecontrol/newDeviceRequests/' + str (id ), {'status' : 'ACCEPTED' })
457
544
458
- def get (self , id : str ) -> Device : # noqa (id)
545
+ def get (
546
+ self ,
547
+ id : str , # noqa
548
+ with_children : bool = None ,
549
+ with_children_count : bool = None ,
550
+ skip_children_names : bool = None ,
551
+ with_parents : bool = None ,
552
+ with_latest_values : bool = None ,
553
+ ** kwargs ) -> Device :
459
554
""" Retrieve a specific device object.
460
555
461
556
Args:
462
- id (str): ID of the device object
557
+ id (str): Cumulocity ID of the device object
558
+ with_children (bool): Whether children with ID and name should be
559
+ included with each returned object
560
+ with_children_count (bool): When set to true, the returned result
561
+ will contain the total number of children in the respective
562
+ child additions, assets and devices sub fragments.
563
+ skip_children_names (bool): If true, returned references of child
564
+ devices won't contain their names.
565
+ with_parents (bool): Whether to include a device's parents.
566
+ with_latest_values (bool): If true the platform includes the
567
+ fragment `c8y_LatestMeasurements, which contains the latest
568
+ measurement values reported by the device to the platform.
463
569
464
570
Returns:
465
571
A Device instance
466
572
467
573
Raises:
468
574
KeyError: if the ID is not defined within the database
469
575
"""
470
- device = Device .from_json (self ._get_object (id ))
576
+ device = Device .from_json (self ._get_object (
577
+ id ,
578
+ with_children = with_children ,
579
+ with_children_count = with_children_count ,
580
+ skip_children_names = skip_children_names ,
581
+ with_parents = with_parents ,
582
+ with_latest_values = with_latest_values ,
583
+ ** kwargs )
584
+ )
471
585
device .c8y = self .c8y
472
586
return device
473
587
0 commit comments