Browse Source

[models] cast is a method again + fix bugs

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

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

@@ -47,10 +47,10 @@ class Heatpump:
47 47
         return ''.join(self.mac_address.split(':')).lower()
48 48
 
49 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 55
     def get_var_values_since(self):
56 56
         r = {}

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

@@ -56,7 +56,7 @@ class Variable(RowClass):
56 56
     @staticmethod
57 57
     def getall():
58 58
         return {
59
-            str(row): Variable.getall_of_type(row)
59
+            row.slabel: Variable.getall_of_type(row)
60 60
             for _, row in VariableType.getall().items()
61 61
         }
62 62
 
@@ -89,8 +89,9 @@ class Variable(RowClass):
89 89
         floatcast = lambda x: round(float(x) / 10, 2)
90 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 96
         params = {
96 97
             'slabel': var_type.slabel,
@@ -100,7 +101,7 @@ class Variable(RowClass):
100 101
         }
101 102
 
102 103
         return {
103
-            row['address']: var_type.cast(row['value'])
104
+            row['address']: var_type.cast()(row['value'])
104 105
 
105 106
             for row in DB.sql(
106 107
             """SELECT var.address as address, val.value as value

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

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

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

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

Loading…
Cancel
Save