Skip to content

Commit

Permalink
allow setting EEP enums by numeric value (see issue kipe#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
romor committed Feb 8, 2016
1 parent cd754da commit 8c8daa5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
22 changes: 15 additions & 7 deletions enocean/protocol/eep.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
17 changes: 16 additions & 1 deletion tests/test_packet_creation.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
)

0 comments on commit 8c8daa5

Please sign in to comment.