def __init__(self, hass, serial_path): """Initialize the EnOcean dongle.""" self._communicator = SerialCommunicator(port=serial_path, callback=self.callback) self.serial_path = serial_path self.identifier = basename(normpath(serial_path)) self.hass = hass self.dispatcher_disconnect_handle = None
class EnoceanConnector: def __init__(self, port): self._port = port self._enocean = None self._cached_base_id = None def open(self): self._enocean = SerialCommunicator(self._port) self._enocean.start() _logger.debug("open") def close(self): if self._enocean is not None: # and self._enocean.is_alive(): self._enocean.stop() self._enocean = None def is_alive(self): if not self._enocean: return False return self._enocean.is_alive() def assure_connection(self): # force option? if self._enocean is None: self.open() else: if not self._enocean.is_alive(): _logger.warning("enocean is not alive - try to reopen! (may crash, restart via systemd)") self.close() self.open() def get_messages(self) -> [EnoceanMessage]: messages = [] # type[EnoceanMessage] loop = 0 while self._enocean.is_alive() and loop < 50: loop += 1 try: packet = self._enocean.receive.get(block=False) except queue.Empty: break # loop untile the queue is empty... if hasattr(packet, "sender_int"): message = EnoceanMessage(payload=packet, enocean_id=packet.sender_int) messages.append(message) return messages @property def base_id(self): if self._cached_base_id is None and self._enocean is not None: self._cached_base_id = self._enocean.base_id return self._cached_base_id def send(self, packet): if self._enocean is not None: self._enocean.send(packet) pass
def validate_path(path: str): """Return True if the provided path points to a valid serial port, False otherwise.""" try: # Creating the serial communicator will raise an exception # if it cannot connect SerialCommunicator(port=path) return True except serial.SerialException as exception: _LOGGER.warning("Dongle path %s is invalid: %s", path, str(exception)) return False
class EnOceanDongle: """Representation of an EnOcean dongle. The dongle is responsible for receiving the ENOcean frames, creating devices if needed, and dispatching messages to platforms. """ def __init__(self, hass, serial_path): """Initialize the EnOcean dongle.""" self._communicator = SerialCommunicator(port=serial_path, callback=self.callback) self.serial_path = serial_path self.identifier = basename(normpath(serial_path)) self.hass = hass self.dispatcher_disconnect_handle = None async def async_setup(self): """Finish the setup of the bridge and supported platforms.""" self._communicator.start() self.dispatcher_disconnect_handle = async_dispatcher_connect( self.hass, SIGNAL_SEND_MESSAGE, self._send_message_callback) def unload(self): """Disconnect callbacks established at init time.""" if self.dispatcher_disconnect_handle: self.dispatcher_disconnect_handle() self.dispatcher_disconnect_handle = None def _send_message_callback(self, command): """Send a command through the EnOcean dongle.""" self._communicator.send(command) def callback(self, packet): """Handle EnOcean device's callback. This is the callback function called by python-enocan whenever there is an incoming packet. """ if isinstance(packet, RadioPacket): _LOGGER.debug("Received radio packet: %s", packet) self.hass.helpers.dispatcher.dispatcher_send( SIGNAL_RECEIVE_MESSAGE, packet)
destination=destination, sender=communicator.base_id, command=1, IO=0x1E, OV=output_value)) def turn_on(destination): send_command(destination, 100) def turn_off(destination): send_command(destination, 0) communicator = SerialCommunicator() communicator.start() print('The Base ID of your module is %s.' % enocean.utils.to_hex_string(communicator.base_id)) # Example of turning switches on and off turn_on([0x01, 0x94, 0xB9, 0x46]) # Needs a bit of sleep in between, working too fast :S time.sleep(0.1) turn_on([0x01, 0x94, 0xE3, 0xB9]) time.sleep(1) turn_off([0x01, 0x94, 0xB9, 0x46]) time.sleep(0.1) turn_off([0x01, 0x94, 0xE3, 0xB9])
def send_command(destination, output_value): global communicator communicator.send( RadioPacket.create(rorg=RORG.VLD, rorg_func=0x01, rorg_type=0x01, destination=destination, sender=communicator.base_id, command=1, IO=0x1E, OV=output_value) ) def turn_on(destination): send_command(destination, 100) def turn_off(destination): send_command(destination, 0) communicator = SerialCommunicator() communicator.start() print('The Base ID of your module is %s.' % enocean.utils.to_hex_string(communicator.base_id)) # Example of turning switches on and off turn_on([0x01, 0x94, 0xB9, 0x46]) # Needs a bit of sleep in between, working too fast :S time.sleep(0.1) turn_on([0x01, 0x94, 0xE3, 0xB9]) time.sleep(1) turn_off([0x01, 0x94, 0xB9, 0x46]) time.sleep(0.1) turn_off([0x01, 0x94, 0xE3, 0xB9])
def open(self): self._enocean = SerialCommunicator(self._port) self._enocean.start() _logger.debug("open")