2c8fbe5495c11ab493fcf13bce92c02642152bbe
[plcapi.git] / PLC / Methods / AddPCUType.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.PCUTypes import PCUType, PCUTypes
5 from PLC.Auth import Auth
6
7 can_update = lambda (field, value): field in \
8              ['model', 'name']
9
10 class AddPCUType(Method):
11     """
12     Adds a new pcu type.
13
14     Returns the new pcu_type_id (> 0) if successful, faults otherwise.
15     """
16
17     roles = ['admin']
18
19     pcu_type_fields = dict(filter(can_update, PCUType.fields.items()))
20
21     accepts = [
22         Auth(),
23         pcu_type_fields
24         ]
25
26     returns = Parameter(int, 'New pcu_type_id (> 0) if successful')
27
28
29     def call(self, auth, pcu_type_fields):
30         pcu_type_fields = dict(filter(can_update, pcu_type_fields.items()))
31         pcu_type = PCUType(self.api, pcu_type_fields)
32         pcu_type.sync()
33         self.event_object = {'PCUType': [pcu_type['pcu_type_id']]}
34
35         return pcu_type['pcu_type_id']