Browse Source

[heatpump] fix conversion adresses

Maxime Alves LIRMM@home 3 years ago
parent
commit
6c7e585b49

+ 13
- 15
pyheatpump/cli.py View File

@@ -1,16 +1,17 @@
1 1
 #!/usr/bin/env python3
2 2
 # builtins
3 3
 import click
4
-import uvicorn
4
+from datetime import datetime
5
+import importlib
6
+import json
5 7
 import os
6
-import sys
7 8
 import re
8
-import importlib
9
-from pprint import pprint
9
+import sys
10
+import time
10 11
 import urllib
12
+import uvicorn
13
+from pprint import pprint
11 14
 import requests
12
-import json
13
-from datetime import datetime
14 15
 
15 16
 from pyheatpump.logger import logger_init
16 17
 from pyheatpump.models import *
@@ -205,23 +206,20 @@ def supervise(since):
205 206
 
206 207
     logger.info(build_url(post_url))
207 208
 
208
-    for packet in post_packets:
209
-        try:
210
-            logger.debug(json.dumps(shift_response(packet)))
211
-        except Exception as e:
212
-            print(e)
213
-            sys.exit(1)
214
-
215
-
216 209
     for packet in post_packets:
217 210
         logger.debug('Will send %s', shift_response(packet))
218 211
         post_resp = requests.post(
219 212
             url=build_url(post_url),
220 213
             json=shift_response(packet),
221
-            verify=False
214
+            verify=False,
215
+            timeout=20
222 216
         )
217
+        time.sleep(5)
223 218
         if post_resp.status_code == 200:
224 219
             logger.info('POST to supervisor succeeded')
220
+            continue
221
+
222
+        logger.info('POST to supervisor failed')
225 223
 
226 224
     set_last_update(int(datetime.now().strftime('%s')))
227 225
 

+ 1
- 1
pyheatpump/lib.py View File

@@ -10,7 +10,7 @@ def shift_response(d):
10 10
         d_res = {
11 11
             str(k - offset): d_[k]
12 12
             for k in d_k
13
-            if (k in d_ and isinstance(d_[k], int))
13
+            if (k in d_ and (isinstance(d_[k], int) or isinstance(d_[k], float)))
14 14
         }
15 15
 
16 16
         return d_res

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

@@ -31,7 +31,8 @@ class Heatpump:
31 31
 
32 32
     def __dict__(self):
33 33
         r = {'macAddress': self.macformat}
34
-        if self.last_update is None:
34
+        if not self.last_update:
35
+            # Since beginning
35 36
             return { **r, **self.get_var_values() }
36 37
         return { **r, **self.get_var_values_since() }
37 38
 
@@ -52,8 +53,8 @@ class Heatpump:
52 53
         return r
53 54
 
54 55
     def control(self, data):
55
-        logger.warn('Received orders data :')
56
-        logger.warn(data)
56
+        logger.warning('Received orders data :')
57
+        logger.warning(data)
57 58
         if 'macAddress' not in data.keys():
58 59
             logger.info(f'No orders')
59 60
             return True

+ 3
- 0
pyheatpump/models/variable.py View File

@@ -6,6 +6,9 @@ from pyheatpump.modbus import rtu
6 6
 from .variable_type import VariableType
7 7
 from .variable_value import VariableValue
8 8
 
9
+from pyheatpump.logger import logger_init
10
+logger = logger_init()
11
+
9 12
 class Variable(RowClass):
10 13
     address: int = None
11 14
     unit: str = None

+ 37
- 4
tests/cli/test_supervise.py View File

@@ -33,8 +33,8 @@ def fetch(set_test_db, runner):
33 33
     except ValueError:
34 34
         pass
35 35
 
36
-@patch('pyheatpump.cli.requests')
37
-def test_supervise(requestsMock, fetch, runner):
36
+@pytest.fixture
37
+def default_config():
38 38
     config['supervisor']['scheme'] = 'http'
39 39
     config['supervisor']['host'] = '127.0.0.1'
40 40
     config['supervisor']['port'] = '8080'
@@ -42,7 +42,38 @@ def test_supervise(requestsMock, fetch, runner):
42 42
     config['supervisor']['get_path'] = '/get'
43 43
     config['heatpump']['mac_address'] = '00:11:22:33:44:55'
44 44
 
45
+@pytest.mark.skip
46
+@patch('pyheatpump.cli.requests')
47
+def test_supervise(requestsMock, fetch, runner, default_config):
48
+    data = {'macAddress':'001122334455'}
49
+    data_analog = { **data, **{'Analog':{} } }
50
+    data_integer = { **data, **{'Integer':{} } }
51
+    data_digital = { **data, **{'Digital':{} } }
52
+
53
+    try:
54
+        r = runner.invoke(cli, 'supervise')
55
+    except ValueError:
56
+        pass
57
+
58
+    requestsMock.post.assert_called()
59
+    requestsMock.post.assert_called_with(
60
+        url='http://127.0.0.1:8080/post',
61
+        json=data_integer,
62
+        verify=False
63
+    )
64
+
65
+    requestsMock.get.assert_called()
66
+    requestsMock.get.assert_called_with(
67
+        url=''.join('http://127.0.0.1:8080/get/001122334455'),
68
+        verify=False)
69
+
70
+
71
+@patch('pyheatpump.cli.requests')
72
+def test_supervise_values(requestsMock, fetch, runner, default_config, set_db_values):
45 73
     data = {'macAddress':'001122334455'}
74
+    data_analog = { **data, **{'Analog':{} } }
75
+    data_integer = { **data, **{'Integer':{} } }
76
+    data_digital = { **data, **{'Digital':{} } }
46 77
 
47 78
     try:
48 79
         r = runner.invoke(cli, 'supervise')
@@ -52,9 +83,11 @@ def test_supervise(requestsMock, fetch, runner):
52 83
     requestsMock.post.assert_called()
53 84
     requestsMock.post.assert_called_with(
54 85
         url='http://127.0.0.1:8080/post',
55
-        json=data
86
+        json=data_integer,
87
+        verify=False
56 88
     )
57 89
 
58 90
     requestsMock.get.assert_called()
59 91
     requestsMock.get.assert_called_with(
60
-        url=''.join('http://127.0.0.1:8080/get/001122334455'))
92
+        url=''.join('http://127.0.0.1:8080/get/001122334455'),
93
+        verify=False)

+ 14
- 3
tests/test_lib.py View File

@@ -6,15 +6,15 @@ from pyheatpump.lib import shift_response
6 6
 def test_shift_response():
7 7
     heatpump_dict = {}
8 8
     heatpump_dict['Analog'] = {
9
-        key: 0
9
+        key: (0 if key % 2 == 0 else 1)
10 10
         for key in range(1, 1251)
11 11
     }
12 12
     heatpump_dict['Integer'] = {
13
-        key: 0
13
+        key: (0 if key % 2 == 0 else 1)
14 14
         for key in range(5002, 6252 )
15 15
     }
16 16
     heatpump_dict['Digital'] = {
17
-        key: 0
17
+            key: (0 if key % 2 == 0 else 1 )
18 18
         for key in range(1, 1001)
19 19
     }
20 20
 
@@ -25,10 +25,19 @@ def test_shift_response():
25 25
 
26 26
     assert 'Analog' in shifted.keys()
27 27
     assert isinstance(shifted['Analog'], dict)
28
+    analog_keys = list(map(int, shifted['Analog'].keys()))
29
+    assert min(analog_keys) == 1
30
+    assert shifted['Analog']["1"] == 1
31
+    assert shifted['Analog']["2"] == 0
28 32
 
29 33
     assert 'Digital' in shifted.keys()
30 34
     assert isinstance(shifted['Digital'], dict)
31 35
 
36
+    digital_keys = list(map(int, shifted['Digital'].keys()))
37
+    assert min(digital_keys) == 1
38
+    assert shifted['Digital']["1"] == 1
39
+    assert shifted['Digital']["2"] == 0
40
+
32 41
     assert 'Integer' in shifted.keys()
33 42
     assert isinstance(shifted['Integer'], dict)
34 43
     for k in shifted['Integer'].keys():
@@ -37,3 +46,5 @@ def test_shift_response():
37 46
     int_keys = list(map(int, shifted['Integer'].keys()))
38 47
     assert min(int_keys) == 1
39 48
     assert max(int_keys) == 1250
49
+    assert shifted['Integer']["1"] == 0
50
+    assert shifted['Integer']["2"] == 1

Loading…
Cancel
Save