Bläddra i källkod

Fixed parsing of floats and ints in currentValue

Stan Janssen 5 år sedan
förälder
incheckning
9ee647b3fb

+ 1 - 7
pyopenadr/templates/parts/eiEventSignal.xml

@@ -19,17 +19,11 @@
     <ei:signalName>{{ signal.signal_name }}</ei:signalName>
     <ei:signalType>{{ signal.signal_type }}</ei:signalType>
     <ei:signalID>{{ signal.signal_id }}</ei:signalID>
-    {% if signal.current_value %}
+    {% if signal.current_value is not none %}
     <ei:currentValue>
         <ei:payloadFloat>
             <ei:value>{{ signal.current_value }}</ei:value>
         </ei:payloadFloat>
     </ei:currentValue>
-    {% else %}
-    <ei:currentValue>
-        <ei:payloadFloat>
-            <ei:value>12.34</ei:value>
-        </ei:payloadFloat>
-    </ei:currentValue>
     {% endif %}
 </ei:eiEventSignal>

+ 6 - 7
pyopenadr/utils.py

@@ -216,12 +216,11 @@ def normalize_dict(ordered_dict):
         # Payload values are wrapped in an object according to their type. We don't need that information.
         elif key in ("signal_payload", "current_value"):
             value = d[key]
-            while True:
-                if isinstance(value, dict):
-                    value = list(value.values())[0]
-                else:
-                    break
-            d[key] = value
+            if isinstance(d[key], dict):
+                if 'payload_float' in d[key]:
+                    d[key] = float(d[key]['payload_float']['value'])
+                elif 'payload_int' in d[key]:
+                    d[key] = int(d[key]['payload_int']['value'])
 
         # Promote the 'text' item
         elif isinstance(d[key], dict) and "text" in d[key] and len(d[key]) == 1:
@@ -253,7 +252,7 @@ def create_message(message_type, **message_payload):
     return indent_xml(template.render(**message_payload))
 
 def new_request_id(*args, **kwargs):
-    return ''.join(random.choice(string.hexdigits) for _ in range(10)).lower()
+    return random.choice(string.ascii_lowercase) + ''.join(random.choice(string.hexdigits) for _ in range(9)).lower()
 
 def generate_id(*args, **kwargs):
     return new_request_id()

+ 1 - 1
setup.py

@@ -4,7 +4,7 @@ with open("README.md", "r") as fh:
     long_description = fh.read()
 
 setup(name="pyopenadr",
-      version="0.2.1",
+      version="0.2.2",
       description="Python library for dealing with OpenADR",
       long_description=long_description,
       long_description_content_type="text/markdown",

+ 1 - 1
test/test_message_conversion.py

@@ -36,7 +36,7 @@ def create_dummy_event(ven_id):
                     #"signal_type": "level",
                     "signal_type": "x-loadControlCapacity",
                     "signal_id": generate_id(),
-                    "current_value": 12.34}]
+                    "current_value": 0.0}]
     event_targets = [{"ven_id": 'VEN001'}, {"ven_id": 'VEN002'}]
     event = {'active_period': active_period,
              'event_descriptor': event_descriptor,

+ 1 - 1
test/test_schema.py

@@ -45,7 +45,7 @@ def create_dummy_event(ven_id):
                     #"signal_type": "level",
                     "signal_type": "x-loadControlCapacity",
                     "signal_id": generate_id(),
-                    "current_value": 0.0}]
+                    "current_value": 9.99}]
 
     event_targets = [{"ven_id": 'VEN001'}, {"ven_id": 'VEN002'}]
     event = {'active_period': active_period,