def setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the connection to the XBee Zigbee device.""" usb_device = config[DOMAIN].get(CONF_DEVICE, DEFAULT_DEVICE) baud = int(config[DOMAIN].get(CONF_BAUD, DEFAULT_BAUD)) try: ser = Serial(usb_device, baud) except SerialException as exc: _LOGGER.exception("Unable to open serial port for XBee: %s", exc) return False zigbee_device = ZigBee(ser) def close_serial_port(*args): """Close the serial port we're using to communicate with the XBee.""" zigbee_device.zb.serial.close() def _frame_received(frame): """Run when a XBee Zigbee frame is received. Pickles the frame, then encodes it into base64 since it contains non JSON serializable binary. """ dispatcher_send(hass, SIGNAL_XBEE_FRAME_RECEIVED, frame) hass.data[DOMAIN] = zigbee_device hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, close_serial_port) zigbee_device.add_frame_rx_handler(_frame_received) return True
def setup(hass, config): """Setup the connection to the ZigBee device.""" global DEVICE global GPIO_DIGITAL_OUTPUT_LOW global GPIO_DIGITAL_OUTPUT_HIGH global ADC_PERCENTAGE global DIGITAL_PINS global ANALOG_PINS global CONVERT_ADC global ZIGBEE_EXCEPTION global ZIGBEE_TX_FAILURE import xbee_helper.const as xb_const from xbee_helper import ZigBee from xbee_helper.device import convert_adc from xbee_helper.exceptions import ZigBeeException, ZigBeeTxFailure from serial import Serial, SerialException GPIO_DIGITAL_OUTPUT_LOW = xb_const.GPIO_DIGITAL_OUTPUT_LOW GPIO_DIGITAL_OUTPUT_HIGH = xb_const.GPIO_DIGITAL_OUTPUT_HIGH ADC_PERCENTAGE = xb_const.ADC_PERCENTAGE DIGITAL_PINS = xb_const.DIGITAL_PINS ANALOG_PINS = xb_const.ANALOG_PINS CONVERT_ADC = convert_adc ZIGBEE_EXCEPTION = ZigBeeException ZIGBEE_TX_FAILURE = ZigBeeTxFailure usb_device = config[DOMAIN].get(CONF_DEVICE, DEFAULT_DEVICE) baud = int(config[DOMAIN].get(CONF_BAUD, DEFAULT_BAUD)) try: ser = Serial(usb_device, baud) except SerialException as exc: _LOGGER.exception("Unable to open serial port for ZigBee: %s", exc) return False DEVICE = ZigBee(ser) hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, close_serial_port) def _frame_received(frame): """Called when a ZigBee frame is received. Pickles the frame, then encodes it into base64 since it contains non JSON serializable binary. """ hass.bus.fire( EVENT_ZIGBEE_FRAME_RECEIVED, {ATTR_FRAME: b64encode(pickle.dumps(frame)).decode("ascii")}) DEVICE.add_frame_rx_handler(_frame_received) return True
def setup(hass, config): """Setup the connection to the ZigBee device.""" global DEVICE global GPIO_DIGITAL_OUTPUT_LOW global GPIO_DIGITAL_OUTPUT_HIGH global ADC_PERCENTAGE global DIGITAL_PINS global ANALOG_PINS global CONVERT_ADC global ZIGBEE_EXCEPTION global ZIGBEE_TX_FAILURE import xbee_helper.const as xb_const from xbee_helper import ZigBee from xbee_helper.device import convert_adc from xbee_helper.exceptions import ZigBeeException, ZigBeeTxFailure from serial import Serial, SerialException GPIO_DIGITAL_OUTPUT_LOW = xb_const.GPIO_DIGITAL_OUTPUT_LOW GPIO_DIGITAL_OUTPUT_HIGH = xb_const.GPIO_DIGITAL_OUTPUT_HIGH ADC_PERCENTAGE = xb_const.ADC_PERCENTAGE DIGITAL_PINS = xb_const.DIGITAL_PINS ANALOG_PINS = xb_const.ANALOG_PINS CONVERT_ADC = convert_adc ZIGBEE_EXCEPTION = ZigBeeException ZIGBEE_TX_FAILURE = ZigBeeTxFailure usb_device = config[DOMAIN].get(CONF_DEVICE, DEFAULT_DEVICE) baud = int(config[DOMAIN].get(CONF_BAUD, DEFAULT_BAUD)) try: ser = Serial(usb_device, baud) except SerialException as exc: _LOGGER.exception("Unable to open serial port for ZigBee: %s", exc) return False DEVICE = ZigBee(ser) hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, close_serial_port) def _frame_received(frame): """Called when a ZigBee frame is received. Pickles the frame, then encodes it into base64 since it contains non JSON serializable binary. """ hass.bus.fire( EVENT_ZIGBEE_FRAME_RECEIVED, {ATTR_FRAME: b64encode(pickle.dumps(frame)).decode("ascii")}) DEVICE.add_frame_rx_handler(_frame_received) return True
def setup(hass, config): """ Set up the connection to the ZigBee device and instantiate the helper class for it. """ global DEVICE global GPIO_DIGITAL_OUTPUT_LOW global GPIO_DIGITAL_OUTPUT_HIGH global ADC_PERCENTAGE global ZIGBEE_EXCEPTION global ZIGBEE_TX_FAILURE import xbee_helper.const as xb_const from xbee_helper import ZigBee from xbee_helper.exceptions import ZigBeeException, ZigBeeTxFailure from serial import Serial, SerialException GPIO_DIGITAL_OUTPUT_LOW = xb_const.GPIO_DIGITAL_OUTPUT_LOW GPIO_DIGITAL_OUTPUT_HIGH = xb_const.GPIO_DIGITAL_OUTPUT_HIGH ADC_PERCENTAGE = xb_const.ADC_PERCENTAGE ZIGBEE_EXCEPTION = ZigBeeException ZIGBEE_TX_FAILURE = ZigBeeTxFailure usb_device = config[DOMAIN].get(CONF_DEVICE, DEFAULT_DEVICE) baud = int(config[DOMAIN].get(CONF_BAUD, DEFAULT_BAUD)) try: ser = Serial(usb_device, baud) except SerialException as exc: _LOGGER.exception("Unable to open serial port for ZigBee: %s", exc) return False DEVICE = ZigBee(ser) hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, close_serial_port) return True
def setup(opp, config): """Set up the connection to the Zigbee device.""" global DEVICE global GPIO_DIGITAL_OUTPUT_LOW global GPIO_DIGITAL_OUTPUT_HIGH global ADC_PERCENTAGE global DIGITAL_PINS global ANALOG_PINS global CONVERT_ADC global ZIGBEE_EXCEPTION global ZIGBEE_TX_FAILURE GPIO_DIGITAL_OUTPUT_LOW = xb_const.GPIO_DIGITAL_OUTPUT_LOW GPIO_DIGITAL_OUTPUT_HIGH = xb_const.GPIO_DIGITAL_OUTPUT_HIGH ADC_PERCENTAGE = xb_const.ADC_PERCENTAGE DIGITAL_PINS = xb_const.DIGITAL_PINS ANALOG_PINS = xb_const.ANALOG_PINS CONVERT_ADC = convert_adc ZIGBEE_EXCEPTION = ZigBeeException ZIGBEE_TX_FAILURE = ZigBeeTxFailure usb_device = config[DOMAIN].get(CONF_DEVICE, DEFAULT_DEVICE) baud = int(config[DOMAIN].get(CONF_BAUD, DEFAULT_BAUD)) try: ser = Serial(usb_device, baud) except SerialException as exc: _LOGGER.exception("Unable to open serial port for Zigbee: %s", exc) return False DEVICE = ZigBee(ser) opp.bus.listen_once(EVENT_OPENPEERPOWER_STOP, close_serial_port) def _frame_received(frame): """Run when a Zigbee frame is received. Pickles the frame, then encodes it into base64 since it contains non JSON serializable binary. """ dispatcher_send(opp, SIGNAL_ZIGBEE_FRAME_RECEIVED, frame) DEVICE.add_frame_rx_handler(_frame_received) return True