def setup(hass, config):
    """Set up the Egardia platform."""

    conf = config[DOMAIN]
    username = conf.get(CONF_USERNAME)
    password = conf.get(CONF_PASSWORD)
    host = conf.get(CONF_HOST)
    port = conf.get(CONF_PORT)
    version = conf.get(CONF_VERSION)
    rs_enabled = conf.get(CONF_REPORT_SERVER_ENABLED)
    rs_port = conf.get(CONF_REPORT_SERVER_PORT)
    try:
        device = hass.data[EGARDIA_DEVICE] = egardiadevice.EgardiaDevice(
            host, port, username, password, "", version
        )
    except requests.exceptions.RequestException:
        _LOGGER.error(
            "An error occurred accessing your Egardia device. "
            "Please check configuration"
        )
        return False
    except egardiadevice.UnauthorizedError:
        _LOGGER.error("Unable to authorize. Wrong password or username")
        return False
    # Set up the egardia server if enabled
    if rs_enabled:
        _LOGGER.debug("Setting up EgardiaServer")
        try:
            if EGARDIA_SERVER not in hass.data:
                server = egardiaserver.EgardiaServer("", rs_port)
                bound = server.bind()
                if not bound:
                    raise OSError(
                        "Binding error occurred while starting EgardiaServer."
                    )
                hass.data[EGARDIA_SERVER] = server
                server.start()

            def handle_stop_event(event):
                """Handle Home Assistant stop event."""
                server.stop()

            # listen to Home Assistant stop event
            hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, handle_stop_event)

        except OSError:
            _LOGGER.error("Binding error occurred while starting EgardiaServer")
            return False

    discovery.load_platform(
        hass, "alarm_control_panel", DOMAIN, discovered=conf, hass_config=config
    )

    # Get the sensors from the device and add those
    sensors = device.getsensors()
    discovery.load_platform(
        hass, "binary_sensor", DOMAIN, {ATTR_DISCOVER_DEVICES: sensors}, config
    )

    return True
Exemple #2
0
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Set up the Egardia platform."""
    from pythonegardia import egardiadevice
    from pythonegardia import egardiaserver

    name = config.get(CONF_NAME)
    username = config.get(CONF_USERNAME)
    password = config.get(CONF_PASSWORD)
    host = config.get(CONF_HOST)
    port = config.get(CONF_PORT)
    rs_enabled = config.get(CONF_REPORT_SERVER_ENABLED)
    rs_port = config.get(CONF_REPORT_SERVER_PORT)
    rs_codes = config.get(CONF_REPORT_SERVER_CODES)
    version = config.get(CONF_VERSION)

    try:
        egardiasystem = egardiadevice.EgardiaDevice(host, port, username,
                                                    password, '', version)
    except requests.exceptions.RequestException:
        raise exc.PlatformNotReady()
    except egardiadevice.UnauthorizedError:
        _LOGGER.error("Unable to authorize. Wrong password or username")
        return

    eg_dev = EgardiaAlarm(name, egardiasystem, rs_enabled, rs_codes)

    if rs_enabled:
        # Set up the egardia server
        _LOGGER.info("Setting up EgardiaServer")
        try:
            if D_EGARDIASRV not in hass.data:
                server = egardiaserver.EgardiaServer('', rs_port)
                bound = server.bind()
                if not bound:
                    raise IOError("Binding error occurred while " +
                                  "starting EgardiaServer")
                hass.data[D_EGARDIASRV] = server
                server.start()
        except IOError:
            return
        hass.data[D_EGARDIASRV].register_callback(eg_dev.handle_status_event)

    def handle_stop_event(event):
        """Callback function for HA stop event."""
        hass.data[D_EGARDIASRV].stop()

    # listen to home assistant stop event
    hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, handle_stop_event)

    # add egardia alarm device
    add_devices([eg_dev], True)