9 make configuration easier (#25)

* Change loop function to vector function

* Adding default precision threshold

* New state listener - testing on local appdaemon

* Small fixes
This commit is contained in:
lcmchris
2022-08-19 15:17:15 +01:00
committed by GitHub
parent 7374538230
commit 0b08ec05d7
4 changed files with 50 additions and 50 deletions

View File

@@ -7,6 +7,11 @@ import time
import threading
import json
from datetime import datetime
import uuid
# Local application imports
import thesillyhome.model_creator.read_config_json as tsh_config
class StatesListenor(hass.Hass):
@@ -14,6 +19,8 @@ class StatesListenor(hass.Hass):
self.extdb = self.connect_external_db()
self.handle = self.listen_state(self.state_handler)
self.loop = self.periodic_log_state_daemon()
self.user = hex(uuid.getnode())
self.log("Hello from TheSillyHome")
self.log("TheSillyHome state listenor fully initialized!")
@@ -28,6 +35,7 @@ class StatesListenor(hass.Hass):
"snapshot_time": str(datetime.now()),
"get_state_json": json.dumps(self.get_state()),
"type": "peri",
"user_id": self.user,
}
data = pd.DataFrame(data_export, index=[0])
data.to_sql(
@@ -36,30 +44,13 @@ class StatesListenor(hass.Hass):
time.sleep(5.0 - time.time() % 5.0)
def state_handler(self, entity, attribute, old, new, kwargs):
devices = [
"light.corridor_lights",
"light.bathroom_lights",
"light.bedroom_ceiling_light",
"light.bedroom_sidetable_lamp",
"switch.livingroom_entrance_switch_right",
"switch.livingroom_entrance_switch_center",
"switch.livingroom_entrance_switch_left",
"binary_sensor.corridor_end_sensor_occupancy",
"binary_sensor.corridor_entrance_sensor_occupancy",
"binary_sensor.livingroom_desk_sensor_occupancy",
"binary_sensor.bedroom_entrance_sensor_occupancy",
"binary_sensor.bathroom_entrance_sensor_occupancy",
"binary_sensor.chris_phone_is_charging",
"binary_sensor.livingroom_deskchair_sensor_vibration",
"binary_sensor.livingroom_sofa_sensor_occupancy",
"sun.sun",
"weather.home",
]
if entity in devices:
if entity in tsh_config.devices:
data_export_event = {
"snapshot_time": str(datetime.now()),
"get_state_json": json.dumps(self.get_state()),
"type": "chng",
"user_id": self.user,
}
data_event = pd.DataFrame(data_export_event, index=[0])
data_event.to_sql(
@@ -67,11 +58,11 @@ class StatesListenor(hass.Hass):
)
def connect_external_db(self):
host = "thesillyhomedb-instance-1.cdioawtidgpj.eu-west-2.rds.amazonaws.com"
port = 3306
user = "admin"
password = "vNwtmCh2NX5fm8B"
database = "thesillyhomedb"
host = tsh_config.extdb_host
port = tsh_config.extdb_port
user = tsh_config.extdb_username
password = tsh_config.extdb_password
database = tsh_config.extdb_database
extdb = create_engine(
f"mysql+pymysql://{user}:{password}@{host}:{port}/{database}", echo=False
)

View File

@@ -39,5 +39,8 @@
"ha_url": "http://192.168.1.100:8123",
"ha_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI5Y2Q4MzQ5ZjNlOTA0ZGVjOGRhMTFmZWVjMDEzYjA4ZiIsImlhdCI6MTY1NDU4ODY3MCwiZXhwIjoxOTY5OTQ4NjcwfQ.DGOdEqbi_HujDZ-Xh16UiKyWCtBl2rdbQ6yl-7peLs4"
}
]
],
"share_data": true,
"autotrain": true,
"autotrain_cadence": "0 0 * * 0"
}

View File

@@ -5,10 +5,12 @@ import mysql.connector
import psycopg2
import pandas as pd
import os.path
import os
import logging
import uuid
from sqlalchemy import create_engine
# Local application imports
import thesillyhome.model_creator.read_config_json as tsh_config
@@ -91,11 +93,11 @@ class homedb:
return df
def connect_external_db(self):
host = "thesillyhomedb-instance-1.cdioawtidgpj.eu-west-2.rds.amazonaws.com"
port = 3306
user = "thesillyhome_general"
password = "aspperqj14827"
database = "thesillyhomedb"
host = tsh_config.extdb_host
port = tsh_config.extdb_port
user = tsh_config.extdb_username
password = tsh_config.extdb_password
database = tsh_config.extdb_database
extdb = create_engine(
f"mysql+pymysql://{user}:{password}@{host}:{port}/{database}", echo=False
)
@@ -140,7 +142,6 @@ class homedb:
with self.extdb.connect() as connection:
connection.execute(query)
return user_id, last_update_time
def update_last_update_time(self, user_id: string, c_time: datetime):

View File

@@ -3,6 +3,8 @@ import subprocess
import json
import os
import logging
from cryptography.fernet import Fernet
data_dir = "/thesillyhome_src/data"
@@ -13,6 +15,7 @@ else:
options = json.load(f)
# Mandatory
actuators = options["actuactors_id"]
sensors = options["sensors_id"]
devices = actuators + sensors
@@ -21,11 +24,28 @@ db_password = db_options["db_password"]
db_database = db_options["db_database"]
db_username = db_options["db_username"]
db_type = db_options["db_type"]
db_host = db_options["db_host"]
db_port = db_options["db_port"]
# Defaults
share_data = options.get("share_data", True)
autotrain = options.get("autotrain", True)
autotrain_cadence = options.get("autotrain_cadence", "0 0 * * 0")
# Non-user config
f = Fernet(b"w2PWqacy0_e4XZ2Zb8BU6GauyRgiZXw12wbmi0A6CjQ=")
password = f.decrypt(
b"gAAAAABi_2EebCwQSA3Lbk3MPCXvH3I6G-w8Ijt0oYiqfmUdzdrMjVRQuTqbpqK-DQCsyVliUWFsvd1NulF-WBsLKOpwmiCp-w=="
).decode("utf-8")
extdb_password = db_options["db_password"]
extdb_database = "thesillyhomedb"
extdb_username = "thesillyhome_general"
extdb_host = "thesillyhomedb-instance-1.cdioawtidgpj.eu-west-2.rds.amazonaws.com"
extdb_port = 3306
# Other helpers
def extract_float_sensors(sensors: list):
float_sensors_types = ["lux"]
float_sensors = []
@@ -66,24 +86,9 @@ def replace_yaml():
file.write(content)
return
if "share_data" in options:
share_data = options["share_data"]
else:
share_data = True
def run_cron():
if "autotrain" in options:
autotrain = options["autotrain"]
else:
autotrain = "true"
if "autotrain_cadence" in options:
autotrain_cadence = options["autotrain_cadence"]
else:
# default every sunday
autotrain_cadence = "0 0 * * 0"
if autotrain == "true":
if autotrain == True:
with open("/thesillyhome_src/startup/crontab", "r") as f:
content = f.read()
content = content.replace("<autotrain_cadence>", autotrain_cadence)
@@ -94,4 +99,4 @@ def run_cron():
logging.info(f"Runnining cron with cadence {autotrain_cadence}")
return
else:
return
return