From 34444997e1a7d2bd03b0f370b950575e8a749d11 Mon Sep 17 00:00:00 2001 From: Jeff Culverhouse Date: Tue, 4 Mar 2025 22:04:54 -0500 Subject: [PATCH] minor fixes to logging; qos; config sample --- amcrest_api.py | 2 +- app.py | 29 ++++++++++++----------------- config.yaml.sample | 7 ++++--- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/amcrest_api.py b/amcrest_api.py index 1931b4b..b627541 100644 --- a/amcrest_api.py +++ b/amcrest_api.py @@ -138,7 +138,7 @@ class AmcrestAPI(object): try: config = self.devices[device_id]['config'] - self.logger.info(f'Event on {config["host"]} - {code}: {payload}') + self.logger.debug(f'Event on {config["host"]} - {code}: {payload}') if ((code == "ProfileAlarmTransmit" and config["is_ad110"]) or (code == "VideoMotion" and not config["is_ad110"])): diff --git a/app.py b/app.py index 2db9c8e..951575d 100644 --- a/app.py +++ b/app.py @@ -22,15 +22,7 @@ def read_version(): return read_file('../VERSION') # Let's go! -logging.basicConfig( - format = '%(asctime)s.%(msecs)03d [%(levelname)s] %(name)s: %(message)s', - datefmt='%Y-%m-%d %H:%M:%S', - level=logging.INFO -) -logger = logging.getLogger(__name__) - version = read_version() -logger.info(f'Starting: amcrest2mqtt v{version}') # cmd-line args argparser = argparse.ArgumentParser() @@ -51,11 +43,9 @@ try: configfile = configpath + 'config.yaml' with open(configfile) as file: config = yaml.safe_load(file) - logger.info(f'Reading config file {configpath}') config['config_from'] = 'file' config['config_path'] = configpath except: - logger.info(f'config.yaml not found, checking ENV') config = { 'mqtt': { 'host': os.getenv('MQTT_HOST') or 'localhost', @@ -80,10 +70,20 @@ except: 'device_update_interval': int(os.getenv("DEVICE_UPDATE_INTERVAL") or 600), }, 'debug': True if os.getenv('DEBUG') else False, + 'hide_ts': True if os.getenv('HIDE_TS') else False, 'config_from': 'env', 'timezone': os.getenv('TZ'), } +logging.basicConfig( + format = '%(asctime)s.%(msecs)03d [%(levelname)s] %(name)s: %(message)s' if config['hide_ts'] == False else '[%(levelname)s] %(name)s: %(message)s', + datefmt='%Y-%m-%d %H:%M:%S', + level=logging.INFO +) +logger = logging.getLogger(__name__) +logger.info(f'Starting: amcrest2mqtt v{version}') +logger.info(f'Config loaded from {config["config_from"]}') + config['version'] = version config['configpath'] = os.path.dirname(configpath) @@ -116,10 +116,5 @@ else: if config['debug']: logger.setLevel(logging.DEBUG) -try: - with AmcrestMqtt(config) as mqtt: - asyncio.run(mqtt.main_loop()) -except KeyboardInterrupt: - pass -except Exception as err: - logging.exception("Exception caught", exc_info=True) \ No newline at end of file +with AmcrestMqtt(config) as mqtt: + asyncio.run(mqtt.main_loop()) \ No newline at end of file diff --git a/config.yaml.sample b/config.yaml.sample index 988c8ad..8b211cd 100644 --- a/config.yaml.sample +++ b/config.yaml.sample @@ -5,7 +5,8 @@ mqtt: password: password qos: 0 prefix: amcrest2mqtt - homeassistant: homeassistant + home_assistant: True + discovery_prefix: homeassistant tls_enabled: False tls_ca_cert: filename tls_cert: filename @@ -23,6 +24,6 @@ amcrest: password: password storage_poll_interval: 60 -home_assistant: True -home_assistant_prefix: homeassistant +timezone: America/New_York +hide_ts: False debug: False \ No newline at end of file