diff --git a/enocean/communicators/communicator.py b/enocean/communicators/communicator.py index 1629684..b2b0ee4 100644 --- a/enocean/communicators/communicator.py +++ b/enocean/communicators/communicator.py @@ -7,7 +7,7 @@ import queue except ImportError: import Queue as queue -from enocean.protocol.packet import Packet +from enocean.protocol.packet import Packet, UTETeachInPacket from enocean.protocol.constants import PACKET, PARSE_RESULT, RETURN_CODE @@ -60,13 +60,20 @@ def parse(self): ''' Parses messages and puts them to receive queue ''' # Loop while we get new messages while True: - status, self._buffer, packet = Packet.parse_msg(self._buffer, communicator=self) + status, self._buffer, packet = Packet.parse_msg(self._buffer) # If message is incomplete -> break the loop if status == PARSE_RESULT.INCOMPLETE: return status # If message is OK, add it to receive queue or send to the callback method if status == PARSE_RESULT.OK and packet: + + if isinstance(packet, UTETeachInPacket) and self.teach_in: + response_packet = packet.create_response_packet(self.base_id) + + self.logger.info('Sending response to UTE teach-in.') + self.send(response_packet) + if self.__callback is None: self.receive.put(packet) else: diff --git a/enocean/protocol/packet.py b/enocean/protocol/packet.py index 1cba721..248af0e 100644 --- a/enocean/protocol/packet.py +++ b/enocean/protocol/packet.py @@ -92,7 +92,7 @@ def _bit_status(self, value): self.status = enocean.utils.from_bitarray(value) @staticmethod - def parse_msg(buf, communicator=None): + def parse_msg(buf): ''' Parses message from buffer. returns: @@ -144,11 +144,7 @@ def parse_msg(buf, communicator=None): if packet_type == PACKET.RADIO: # Need to handle UTE Teach-in here, as it's a separate packet type... if data[0] == RORG.UTE: - packet = UTETeachIn(packet_type, data, opt_data, communicator=communicator) - # Send a response automatically, works only if - # - communicator is set - # - communicator.teach_in == True - packet.send_response() + packet = UTETeachInPacket(packet_type, data, opt_data) else: packet = RadioPacket(packet_type, data, opt_data) elif packet_type == PACKET.RESPONSE: @@ -346,7 +342,7 @@ def parse(self): return super(RadioPacket, self).parse() -class UTETeachIn(RadioPacket): +class UTETeachInPacket(RadioPacket): # Request types TEACH_IN = 0b00 DELETE = 0b01 @@ -367,10 +363,6 @@ class UTETeachIn(RadioPacket): contains_eep = True - def __init__(self, packet_type, data=None, optional=None, communicator=None): - self.__communicator = communicator - super(UTETeachIn, self).__init__(packet_type=packet_type, data=data, optional=optional) - @property def bidirectional(self): return not self.unidirectional @@ -384,7 +376,7 @@ def delete(self): return self.request_type == self.DELETE def parse(self): - super(UTETeachIn, self).parse() + super(UTETeachInPacket, self).parse() self.unidirectional = not self._bit_data[DB6.BIT_7] self.response_expected = not self._bit_data[DB6.BIT_6] self.request_type = enocean.utils.from_bitarray(self._bit_data[DB6.BIT_5:DB6.BIT_3]) @@ -397,7 +389,7 @@ def parse(self): self.learn = True return self.parsed - def _create_response_packet(self, sender_id, response=TEACHIN_ACCEPTED): + def create_response_packet(self, sender_id, response=TEACHIN_ACCEPTED): # Create data: # - Respond with same RORG (UTE Teach-in) # - Always use bidirectional communication, set response code, set command identifier. @@ -413,17 +405,6 @@ def _create_response_packet(self, sender_id, response=TEACHIN_ACCEPTED): return RadioPacket(PACKET.RADIO, data=data, optional=optional) - def send_response(self, response=TEACHIN_ACCEPTED): - if self.__communicator is None: - self.logger.error('Communicator not set, cannot send UTE teach-in response.') - return - if not self.__communicator.teach_in: - self.logger.info('Communicator not set to teach-in mode, not sending UTE teach-in response.') - return - self.logger.info('Sending response to UTE teach-in.') - self.__communicator.send( - self._create_response_packet(self.__communicator.base_id)) - class ResponsePacket(Packet): response = 0 diff --git a/enocean/protocol/tests/test_teachin.py b/enocean/protocol/tests/test_teachin.py index 002b4c0..d908a94 100644 --- a/enocean/protocol/tests/test_teachin.py +++ b/enocean/protocol/tests/test_teachin.py @@ -20,8 +20,7 @@ def test_ute_in(): 0xD4, 0xA0, 0xFF, 0x3E, 0x00, 0x01, 0x01, 0xD2, 0x01, 0x94, 0xE3, 0xB9, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x40, 0x00, 0xAB - ]), - communicator=communicator + ]) ) assert packet.sender_hex == '01:94:E3:B9' @@ -38,7 +37,7 @@ def test_ute_in(): assert packet.learn is True assert packet.contains_eep is True - response_packet = packet._create_response_packet(communicator.base_id) + response_packet = packet.create_response_packet(communicator.base_id) assert response_packet.sender_hex == 'DE:AD:BE:EF' assert response_packet.destination_hex == '01:94:E3:B9' assert response_packet._bit_data[DB6.BIT_5:DB6.BIT_3] == [False, True] diff --git a/examples/example_D2-05-00.py b/examples/example_D2-05-00.py index bb03566..20f655d 100644 --- a/examples/example_D2-05-00.py +++ b/examples/example_D2-05-00.py @@ -12,7 +12,7 @@ import traceback import enocean.utils from enocean.communicators import SerialCommunicator -from enocean.protocol.packet import RadioPacket, UTETeachIn +from enocean.protocol.packet import RadioPacket, UTETeachInPacket from enocean.protocol.constants import RORG try: @@ -47,7 +47,7 @@ def set_position(destination, percentage): try: # Loop to empty the queue... packet = communicator.receive.get(block=True, timeout=1) - if isinstance(packet, UTETeachIn): + if isinstance(packet, UTETeachInPacket): print('New device learned! The ID is %s.' % (packet.sender_hex)) devices_learned.append(packet.sender) except queue.Empty: diff --git a/examples/example_DO21-11B-E.py b/examples/example_DO21-11B-E.py index c828ca4..52464ec 100644 --- a/examples/example_DO21-11B-E.py +++ b/examples/example_DO21-11B-E.py @@ -12,7 +12,7 @@ import traceback import enocean.utils from enocean.communicators import SerialCommunicator -from enocean.protocol.packet import RadioPacket, UTETeachIn +from enocean.protocol.packet import RadioPacket, UTETeachInPacket from enocean.protocol.constants import RORG try: @@ -60,7 +60,7 @@ def turn_off(destination): try: # Loop to empty the queue... packet = communicator.receive.get(block=True, timeout=1) - if isinstance(packet, UTETeachIn): + if isinstance(packet, UTETeachInPacket): print('New device learned! The ID is %s.' % (packet.sender_hex)) devices_learned.append(packet.sender) except queue.Empty: