aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/esphomeapi.py
diff options
context:
space:
mode:
authorDennis Eriksen <d@ennis.no>2023-04-03 12:23:40 +0200
committerDennis Eriksen <d@ennis.no>2023-04-03 12:23:40 +0200
commit50f62d0b6a17677104f0e4d39c5ff7223c5239b2 (patch)
tree40fba651452a6a5fd4cc46603abaccaca9c53426 /scripts/esphomeapi.py
parentadding service for esphomeapi, and allowing run.sh to take arguments (diff)
downloadenergyscripts-50f62d0b6a17677104f0e4d39c5ff7223c5239b2.tar.gz
adding function to insert state without defining sql
Diffstat (limited to 'scripts/esphomeapi.py')
-rw-r--r--scripts/esphomeapi.py54
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")