From 8c8daa548d54ae94e7d9f4fce722463298908451 Mon Sep 17 00:00:00 2001 From: roman Date: Mon, 8 Feb 2016 23:16:17 +0100 Subject: [PATCH] allow setting EEP enums by numeric value (see issue #13) --- enocean/protocol/eep.py | 22 +++++++++++++++------- tests/test_packet_creation.py | 17 ++++++++++++++++- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/enocean/protocol/eep.py b/enocean/protocol/eep.py index 4fc0230..a9f9445 100644 --- a/enocean/protocol/eep.py +++ b/enocean/protocol/eep.py @@ -88,6 +88,7 @@ def _set_raw(self, target, raw_value, bitarray): return bitarray def _set_value(self, target, value, bitarray): + ''' set given numeric value to target field in bitarray ''' # derive raw value rng = target.find('range') rng_min = float(rng.find('min').text) @@ -100,17 +101,24 @@ def _set_value(self, target, value, bitarray): return self._set_raw(target, int(raw_value), bitarray) def _set_enum(self, target, value, bitarray): - if isinstance(value, int): - raise ValueError('No integers here, use the description string provided in EEP.') - + ''' set given enum value (by string or integer value) to target field in bitarray ''' # derive raw value - value_item = target.find('item', {'description': value}) - if value_item is None: - raise ValueError('Enum description for value "%s" not found in EEP.' % (value)) - raw_value = int(value_item['value']) + if isinstance(value, int): + # check whether this value exists + if target.find('item', {'value': value}): + # set integer values directly + raw_value = value + else: + raise ValueError('Enum value "%s" not found in EEP.' % (value)) + else: + value_item = target.find('item', {'description': value}) + if value_item is None: + raise ValueError('Enum description for value "%s" not found in EEP.' % (value)) + raw_value = int(value_item['value']) return self._set_raw(target, raw_value, bitarray) def _set_boolean(self, target, data, bitarray): + ''' set given value to target bit in bitarray ''' bitarray[int(target['offset'])] = data return bitarray diff --git a/tests/test_packet_creation.py b/tests/test_packet_creation.py index 0f19f77..ce963fa 100644 --- a/tests/test_packet_creation.py +++ b/tests/test_packet_creation.py @@ -1,5 +1,6 @@ # -*- encoding: utf-8 -*- from __future__ import print_function, unicode_literals, division +from nose.tools import raises from enocean.protocol.packet import Packet, RadioPacket from enocean.protocol.constants import PACKET, RORG @@ -196,9 +197,10 @@ def test_switch(): 0x61 ]) + # test also enum setting by integer value with EB0 p = RadioPacket.create(rorg=RORG.RPS, func=0x02, type=0x02, sender=[0x00, 0x29, 0x89, 0x79], SA='No 2nd action', - EBO='pressed', + EBO=1, R1='Button BI', T21=True, NU=True, @@ -225,3 +227,16 @@ def test_switch(): packet_serialized = p.build() assert len(packet_serialized) == len(SWITCH) assert list(packet_serialized) == list(SWITCH) + + +@raises(ValueError) +def test_illegal_eep_enum1(): + p = RadioPacket.create(rorg=RORG.RPS, func=0x02, type=0x02, sender=[0x00, 0x29, 0x89, 0x79], + EBO='inexisting', + ) + +@raises(ValueError) +def test_illegal_eep_enum2(): + p = RadioPacket.create(rorg=RORG.RPS, func=0x02, type=0x02, sender=[0x00, 0x29, 0x89, 0x79], + EBO=2, + )