def main(): """ Example application that opens a device that has been exposed to the network with ser2sock and SSL encryption and authentication. """ try: # Retrieve an AD2 device that has been exposed with ser2sock on localhost:10000. ssl_device = SocketDevice(interface=('localhost', 10000)) # Enable SSL and set the certificates to be used. # # The key/cert attributes can either be a filesystem path or an X509/PKey # object from pyopenssl. ssl_device.ssl = True ssl_device.ssl_ca = SSL_CA # CA certificate ssl_device.ssl_key = SSL_KEY # Client private key ssl_device.ssl_certificate = SSL_CERT # Client certificate device = AlarmDecoder(ssl_device) # Set up an event handler and open the device device.on_message += handle_message with device.open(): while True: time.sleep(1) except Exception, ex: print 'Exception:', ex
def open(self): """ Opens the AlarmDecoder device. """ with self.app.app_context(): self._device_type = Setting.get_by_name('device_type').value self._device_location = Setting.get_by_name( 'device_location').value if self._device_type: interface = ('localhost', 10000) use_ssl = False devicetype = SocketDevice # Set up device interfaces based on our location. if self._device_location == 'local': devicetype = SerialDevice interface = Setting.get_by_name('device_path').value self._device_baudrate = Setting.get_by_name( 'device_baudrate').value elif self._device_location == 'network': interface = (Setting.get_by_name('device_address').value, Setting.get_by_name('device_port').value) use_ssl = Setting.get_by_name('use_ssl', False).value # Create and open the device. try: device = devicetype(interface=interface) if use_ssl: ca_cert = Certificate.query.filter_by( name='AlarmDecoder CA').one() internal_cert = Certificate.query.filter_by( name='AlarmDecoder Internal').one() device.ssl = True device.ssl_ca = ca_cert.certificate_obj device.ssl_certificate = internal_cert.certificate_obj device.ssl_key = internal_cert.key_obj self.device = AlarmDecoder(device) self.bind_events() self.device.open(baudrate=self._device_baudrate) except NoDeviceError, err: self.app.logger.warning('Open failed: %s', err[0], exc_info=True) raise except SSL.Error, err: source, fn, message = err[0][0] self.app.logger.warning('SSL connection failed: %s - %s', fn, message, exc_info=True) raise
def create_device(device_args): """ Creates an AlarmDecoder from the specified USB device arguments. :param device_args: Tuple containing information on the USB device to open. :type device_args: Tuple (vid, pid, serialnumber, interface_count, description) """ device = AlarmDecoder(USBDevice.find(device_args)) device.on_message += handle_message device.open() return device
def main(): """ Example application that prints messages from the panel to the terminal. """ try: # Retrieve the first USB device device = AlarmDecoder(SerialDevice(interface=SERIAL_DEVICE)) # Set up an event handler and open the device device.on_lrr_message += handle_lrr_message with device.open(baudrate=BAUDRATE): while True: time.sleep(1) except Exception, ex: print 'Exception:', ex
def main(): """ Example application that prints messages from the panel to the terminal. """ try: # Retrieve the first USB device device = AlarmDecoder(USBDevice.find()) # Set up an event handler and open the device device.on_message += handle_message with device.open(): while True: time.sleep(1) except Exception as ex: print('Exception:', ex)
def main(): """ Example application that sends an email when an alarm event is detected. """ try: # Retrieve the first USB device device = AlarmDecoder(SerialDevice(interface=SERIAL_DEVICE)) # Set up an event handler and open the device device.on_alarm += handle_alarm with device.open(baudrate=BAUDRATE): while True: time.sleep(1) except Exception as ex: print('Exception:', ex)
def main(): """ Example application that opens a device that has been exposed to the network with ser2sock or similar serial-to-IP software. """ try: # Retrieve an AD2 device that has been exposed with ser2sock on localhost:10000. device = AlarmDecoder(SocketDevice(interface=(HOSTNAME, PORT))) # Set up an event handler and open the device device.on_message += handle_message with device.open(): while True: time.sleep(1) except Exception as ex: print('Exception:', ex)
def main(): """ Example application that opens a serial device and prints messages to the terminal. """ try: # Retrieve the specified serial device. device = AlarmDecoder(SerialDevice(interface=SERIAL_DEVICE)) # Set up an event handler and open the device device.on_message += handle_message # Override the default SerialDevice baudrate since we're using a USB device # over serial in this example. with device.open(baudrate=BAUDRATE): while True: time.sleep(1) except Exception, ex: print 'Exception:', ex
def main(): """ Example application that periodically faults a virtual zone and then restores it. This is an advanced feature that allows you to emulate a virtual zone. When the AlarmDecoder is configured to emulate a zone expander we can fault and restore those zones programmatically at will. These events can also be seen by others, such as home automation platforms which allows you to connect other devices or services and monitor them as you would any physical zone. For example, you could connect a ZigBee device and receiver and fault or restore it's zone(s) based on the data received. In order for this to happen you need to perform a couple configuration steps: 1. Enable zone expander emulation on your AlarmDecoder device by hitting '!' in a terminal and going through the prompts. 2. Enable the zone expander in your panel programming. """ try: # Retrieve the first USB device device = AlarmDecoder(SerialDevice(interface=SERIAL_DEVICE)) # Set up an event handlers and open the device device.on_zone_fault += handle_zone_fault device.on_zone_restore += handle_zone_restore with device.open(baudrate=BAUDRATE): last_update = time.time() while True: if time.time() - last_update > WAIT_TIME: last_update = time.time() device.fault_zone(TARGET_ZONE) time.sleep(1) except Exception, ex: print 'Exception:', ex
def main(): """ Example application that watches for an event from a specific RF device. This feature allows you to watch for events from RF devices if you have an RF receiver. This is useful in the case of internal sensors, which don't emit a FAULT if the sensor is tripped and the panel is armed STAY. It also will monitor sensors that aren't configured. NOTE: You must have an RF receiver installed and enabled in your panel for RFX messages to be seen. """ try: # Retrieve the first USB device device = AlarmDecoder(SerialDevice(interface=SERIAL_DEVICE)) # Set up an event handler and open the device device.on_rfx_message += handle_rfx with device.open(baudrate=BAUDRATE): while True: time.sleep(1) except Exception as ex: print('Exception:', ex)
def async_setup(hass, config): """Set up for the AlarmDecoder devices.""" from alarmdecoder import AlarmDecoder from alarmdecoder.devices import (SocketDevice, SerialDevice, USBDevice) conf = config.get(DOMAIN) device = conf.get(CONF_DEVICE) display = conf.get(CONF_PANEL_DISPLAY) zones = conf.get(CONF_ZONES) device_type = device.get(CONF_DEVICE_TYPE) host = DEFAULT_DEVICE_HOST port = DEFAULT_DEVICE_PORT path = DEFAULT_DEVICE_PATH baud = DEFAULT_DEVICE_BAUD sync_connect = asyncio.Future(loop=hass.loop) def handle_open(device): """Handle the successful connection.""" _LOGGER.info("Established a connection with the alarmdecoder") hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_alarmdecoder) sync_connect.set_result(True) @callback def stop_alarmdecoder(event): """Handle the shutdown of AlarmDecoder.""" _LOGGER.debug("Shutting down alarmdecoder") controller.close() @callback def handle_message(sender, message): """Handle message from AlarmDecoder.""" async_dispatcher_send(hass, SIGNAL_PANEL_MESSAGE, message) def zone_fault_callback(sender, zone): """Handle zone fault from AlarmDecoder.""" async_dispatcher_send(hass, SIGNAL_ZONE_FAULT, zone) def zone_restore_callback(sender, zone): """Handle zone restore from AlarmDecoder.""" async_dispatcher_send(hass, SIGNAL_ZONE_RESTORE, zone) controller = False if device_type == 'socket': host = device.get(CONF_DEVICE_HOST) port = device.get(CONF_DEVICE_PORT) controller = AlarmDecoder(SocketDevice(interface=(host, port))) elif device_type == 'serial': path = device.get(CONF_DEVICE_PATH) baud = device.get(CONF_DEVICE_BAUD) controller = AlarmDecoder(SerialDevice(interface=path)) elif device_type == 'usb': AlarmDecoder(USBDevice.find()) return False controller.on_open += handle_open controller.on_message += handle_message controller.on_zone_fault += zone_fault_callback controller.on_zone_restore += zone_restore_callback hass.data[DATA_AD] = controller controller.open(baud) result = yield from sync_connect if not result: return False hass.async_add_job( async_load_platform(hass, 'alarm_control_panel', DOMAIN, conf, config)) if zones: hass.async_add_job( async_load_platform(hass, 'binary_sensor', DOMAIN, {CONF_ZONES: zones}, config)) if display: hass.async_add_job( async_load_platform(hass, 'sensor', DOMAIN, conf, config)) return True
def setup(hass, config): """Set up for the AlarmDecoder devices.""" from alarmdecoder import AlarmDecoder from alarmdecoder.devices import (SocketDevice, SerialDevice, USBDevice) conf = config.get(DOMAIN) restart = False device = conf.get(CONF_DEVICE) display = conf.get(CONF_PANEL_DISPLAY) zones = conf.get(CONF_ZONES) device_type = device.get(CONF_DEVICE_TYPE) host = DEFAULT_DEVICE_HOST port = DEFAULT_DEVICE_PORT path = DEFAULT_DEVICE_PATH baud = DEFAULT_DEVICE_BAUD def stop_alarmdecoder(event): """Handle the shutdown of AlarmDecoder.""" _LOGGER.debug("Shutting down alarmdecoder") nonlocal restart restart = False controller.close() def open_connection(now=None): """Open a connection to AlarmDecoder.""" from alarmdecoder.util import NoDeviceError nonlocal restart try: controller.open(baud) except NoDeviceError: _LOGGER.debug("Failed to connect. Retrying in 5 seconds") hass.helpers.event.track_point_in_time( open_connection, dt_util.utcnow() + timedelta(seconds=5)) return _LOGGER.debug("Established a connection with the alarmdecoder") restart = True def handle_closed_connection(event): """Restart after unexpected loss of connection.""" nonlocal restart if not restart: return restart = False _LOGGER.warning("AlarmDecoder unexpectedly lost connection.") hass.add_job(open_connection) def handle_message(sender, message): """Handle message from AlarmDecoder.""" hass.helpers.dispatcher.dispatcher_send(SIGNAL_PANEL_MESSAGE, message) def handle_rfx_message(sender, message): """Handle RFX message from AlarmDecoder.""" hass.helpers.dispatcher.dispatcher_send(SIGNAL_RFX_MESSAGE, message) def zone_fault_callback(sender, zone): """Handle zone fault from AlarmDecoder.""" hass.helpers.dispatcher.dispatcher_send(SIGNAL_ZONE_FAULT, zone) def zone_restore_callback(sender, zone): """Handle zone restore from AlarmDecoder.""" hass.helpers.dispatcher.dispatcher_send(SIGNAL_ZONE_RESTORE, zone) def handle_rel_message(sender, message): """Handle relay message from AlarmDecoder.""" hass.helpers.dispatcher.dispatcher_send(SIGNAL_REL_MESSAGE, message) controller = False if device_type == 'socket': host = device.get(CONF_DEVICE_HOST) port = device.get(CONF_DEVICE_PORT) controller = AlarmDecoder(SocketDevice(interface=(host, port))) elif device_type == 'serial': path = device.get(CONF_DEVICE_PATH) baud = device.get(CONF_DEVICE_BAUD) controller = AlarmDecoder(SerialDevice(interface=path)) elif device_type == 'usb': AlarmDecoder(USBDevice.find()) return False controller.on_message += handle_message controller.on_rfx_message += handle_rfx_message controller.on_zone_fault += zone_fault_callback controller.on_zone_restore += zone_restore_callback controller.on_close += handle_closed_connection controller.on_relay_changed += handle_rel_message hass.data[DATA_AD] = controller open_connection() hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_alarmdecoder) load_platform(hass, 'alarm_control_panel', DOMAIN, conf, config) if zones: load_platform(hass, 'binary_sensor', DOMAIN, {CONF_ZONES: zones}, config) if display: load_platform(hass, 'sensor', DOMAIN, conf, config) return True