Browse Source

[models] cast is a method again + fix bugs

Maxime Alves LIRMM@home 4 years ago
parent
commit
407d8b4351

+ 4
- 4
pyheatpump/models/heatpump.py View File

47
         return ''.join(self.mac_address.split(':')).lower()
47
         return ''.join(self.mac_address.split(':')).lower()
48
 
48
 
49
     def get_var_values(self):
49
     def get_var_values(self):
50
-        r = {}
51
-        for label, var_type in self.var_types.items():
52
-            r[label] = var_type.get_variables_values()
53
-        return r
50
+        return {
51
+            var_type.label: var_type.get_variables_values()
52
+            for var_type in self.var_types.values()
53
+        }
54
 
54
 
55
     def get_var_values_since(self):
55
     def get_var_values_since(self):
56
         r = {}
56
         r = {}

+ 5
- 4
pyheatpump/models/variable.py View File

56
     @staticmethod
56
     @staticmethod
57
     def getall():
57
     def getall():
58
         return {
58
         return {
59
-            str(row): Variable.getall_of_type(row)
59
+            row.slabel: Variable.getall_of_type(row)
60
             for _, row in VariableType.getall().items()
60
             for _, row in VariableType.getall().items()
61
         }
61
         }
62
 
62
 
89
         floatcast = lambda x: round(float(x) / 10, 2)
89
         floatcast = lambda x: round(float(x) / 10, 2)
90
         cast_fct = floatcast if type.slabel == 'A' else lambda x: x
90
         cast_fct = floatcast if type.slabel == 'A' else lambda x: x
91
         """
91
         """
92
-        if not var_type.cast:
93
-            raise Exception('var_type has no cast property')
92
+        if not var_type.cast():
93
+            # Should not happen
94
+            return {}
94
 
95
 
95
         params = {
96
         params = {
96
             'slabel': var_type.slabel,
97
             'slabel': var_type.slabel,
100
         }
101
         }
101
 
102
 
102
         return {
103
         return {
103
-            row['address']: var_type.cast(row['value'])
104
+            row['address']: var_type.cast()(row['value'])
104
 
105
 
105
             for row in DB.sql(
106
             for row in DB.sql(
106
             """SELECT var.address as address, val.value as value
107
             """SELECT var.address as address, val.value as value

+ 2
- 2
pyheatpump/models/variable_type.py View File

19
         return self.slabel or self.label[0]
19
         return self.slabel or self.label[0]
20
 
20
 
21
 
21
 
22
-    @property
23
     def cast(self):
22
     def cast(self):
23
+        # Function to convert numbers > 2**15 to negative numbers (issue #30)
24
         complement = lambda x: x - (1 << 16 if x & (1 << 15) else 0)
24
         complement = lambda x: x - (1 << 16 if x & (1 << 15) else 0)
25
 
25
 
26
-        # Function to convert numbers > 2**15 to negative numbers (issue #30)
27
         if self.type == 'bool':
26
         if self.type == 'bool':
28
             # returns a boolean
27
             # returns a boolean
29
             return lambda x: bool(x)
28
             return lambda x: bool(x)
65
 
64
 
66
     def get_variables_values(self):
65
     def get_variables_values(self):
67
         from .variable import Variable
66
         from .variable import Variable
67
+        print(self)
68
         return Variable.getall_values_of_type(self)
68
         return Variable.getall_values_of_type(self)
69
 
69
 
70
     def get_variables_values_since(self, since: int):
70
     def get_variables_values_since(self, since: int):

+ 1
- 1
pyheatpump/models/variable_value.py View File

51
 
51
 
52
 
52
 
53
     def get_value(self):
53
     def get_value(self):
54
-        return self.type.cast(self.value)
54
+        return self.type.cast()(self.value)
55
 
55
 
56
     def equals(self, var_value):
56
     def equals(self, var_value):
57
         return self.get_value() == var_value.get_value()
57
         return self.get_value() == var_value.get_value()

Loading…
Cancel
Save