diff options
Diffstat (limited to 'scripts/esphomeapi.py')
-rw-r--r-- | scripts/esphomeapi.py | 54 |
1 files changed, 20 insertions, 34 deletions
diff --git a/scripts/esphomeapi.py b/scripts/esphomeapi.py index b3c684b..8c79282 100644 --- a/scripts/esphomeapi.py +++ b/scripts/esphomeapi.py @@ -8,7 +8,7 @@ from aioesphomeapi import APIClient, ReconnectLogic, SensorState, APIConnectionE import asyncio import argparse import logging -import colorlog +#import colorlog import zeroconf import common @@ -58,6 +58,7 @@ async def main(args): if type(state) == SensorState and state.missing_state == False: log.debug('function callback(' + str(state) + ')') sensor = sensors[state.key] + value = state.state if 'accuracy_decimals' in sensor: decimals = sensor['accuracy_decimals'] @@ -65,23 +66,8 @@ async def main(args): unit = sensor['unit_of_measurement'] if 'unit_of_measurement' in sensor else '' device_class = sensor['device_class'] if 'device_class' in sensor else '' - sql = """INSERT INTO states - (sensor_id, - value, - device_class, - unit, - time) - SELECT sensors.id, %s, %s, %s, %s - FROM sensors - WHERE sensors.name = %s;""" - values = (value, - device_class, - unit, - datetime.now(), - device['friendly_name']) - log.info(sensor['name'] + ' ' + sensor['device_class'] + ' - ' + str(value) + str(unit)) - common.dbi(sql, values, verbose=True) + common.statein(sensor['name'], value, device_class, unit, verbose=True) async def on_connect() -> None: log.debug('function on_connect()') @@ -126,23 +112,23 @@ async def main(args): if __name__ == "__main__": # Logging - handler = colorlog.StreamHandler() - handler.setFormatter(colorlog.ColoredFormatter( - "%(log_color)s%(levelname)s - %(message)s {%(filename)s:%(lineno)d}", - log_colors={ - 'DEBUG': 'light_black', - 'INFO': 'cyan', - 'WARNING': 'yellow', - 'ERROR': 'red', - 'CRITICAL': 'red,bg_white' - })) - log = colorlog.getLogger(__name__) - log.setLevel(logging.WARNING) - log.addHandler(handler) - - #log = logging.getLogger(__name__) - #log.setLevel(logging.INFO) - #logging.basicConfig(format="%(name)s: %(levelname)s %(message)s") + #handler = colorlog.StreamHandler() + #handler.setFormatter(colorlog.ColoredFormatter( + # "%(log_color)s%(levelname)s - %(message)s {%(filename)s:%(lineno)d}", + # log_colors={ + # 'DEBUG': 'light_black', + # 'INFO': 'cyan', + # 'WARNING': 'yellow', + # 'ERROR': 'red', + # 'CRITICAL': 'red,bg_white' + # })) + #log = colorlog.getLogger(__name__) + #log.setLevel(logging.WARNING) + #log.addHandler(handler) + + log = logging.getLogger(__name__) + log.setLevel(logging.INFO) + logging.basicConfig(format="%(levelname)s - %(message)s {%(filename)s:%(lineno)d}") parser = argparse.ArgumentParser(description="Connect to an esphome-device and access the native api") parser.add_argument( "-a", "--address", help="Address of esp-device to connect to") |