@@ -23,19 +23,46 @@ class Inventory(CumulocityResource):
2323 def __init__ (self , c8y ):
2424 super ().__init__ (c8y , 'inventory/managedObjects' )
2525
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 :
2735 """ Retrieve a specific managed object from the database.
2836
2937 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.
3150
3251 Returns:
3352 A ManagedObject instance
3453
3554 Raises:
3655 KeyError: if the ID is not defined within the database
3756 """
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+ )
3966 managed_object .c8y = self .c8y # inject c8y connection into instance
4067 return managed_object
4168
@@ -98,6 +125,66 @@ def get_all(
98125 as_values = as_values ,
99126 ** kwargs ))
100127
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+
101188 def get_count (
102189 self ,
103190 expression : str = None ,
@@ -455,19 +542,46 @@ def accept(self, id: str): # noqa (id)
455542 """
456543 self .c8y .put ('/devicecontrol/newDeviceRequests/' + str (id ), {'status' : 'ACCEPTED' })
457544
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 :
459554 """ Retrieve a specific device object.
460555
461556 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.
463569
464570 Returns:
465571 A Device instance
466572
467573 Raises:
468574 KeyError: if the ID is not defined within the database
469575 """
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+ )
471585 device .c8y = self .c8y
472586 return device
473587
0 commit comments