except Exception:
    logger.error("Could not parse config.ini, please check that file.")
    sys.exit(1)

event_handler = EventHandler()

logger.debug("~ connecting to mqtt")
username = None
password = None
if config.get_mqtt_broker_use_auth():
    logger.debug("~ using username and password to connect to mqtt")
    username = config.get_mqtt_broker_username()
    password = config.get_mqtt_broker_password()

mqtt = MqttConnection(config.get_mqtt_broker_address(),
                      config.get_mqtt_broker_port(), username, password,
                      config.get_mqtt_client_cafile(), event_handler)
if not mqtt.start_connection():
    exit(1)

logger.debug("~ starting chromecast discovery")
discovery = ChromecastDiscovery(event_handler)
discovery.start_discovery()

logger.debug("~ initialization finished")

is_running = True
while is_running:
    try:
        sleep(1)
    except KeyboardInterrupt:
logger.debug("~ reading config")
config_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'config.ini')
config = Config(config_path)

event_handler = EventHandler()

logger.debug("~ connecting to mqtt")
username = None
password = None
if config.get_mqtt_broker_use_auth():
    logger.debug("~ using username and password to connect to mqtt")
    username = config.get_mqtt_broker_username()
    password = config.get_mqtt_broker_password()

mqtt = MqttConnection(config.get_mqtt_broker_address(), config.get_mqtt_broker_port(), username, password,
                      event_handler)
if not mqtt.start_connection():
    exit(1)

logger.debug("~ starting chromecast discovery")
discovery = ChromecastDiscovery(event_handler)
discovery.start_discovery()

logger.debug("~ initialization finished")

is_running = True
while is_running:
    try:
        sleep(1)
    except KeyboardInterrupt: