Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix sqlite migration #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ home-assistant-core
.python-version
.idea/
venv/
.vscode/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Setup:
1. `sudo apt install python3 python3.7-dev`
2. `git clone <this repository> migrate2influxdb`
3. `cd migrate2influxdb`
4. `git clone git@github.com:home-assistant/core.git home-assistant-core`
4. `git clone https://github.com/home-assistant/core.git home-assistant-core`
5. `python3 -m venv .venv`

Dependency installation:
Expand Down
15 changes: 9 additions & 6 deletions homeassistant2influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from tqdm import tqdm
import voluptuous as vol
import yaml
from datetime import datetime

sys.path.append("home-assistant-core")

Expand Down Expand Up @@ -211,7 +212,7 @@ def main():
event = Event(
_event_type,
data={"new_state": state},
time_fired=_time_fired
time_fired=datetime.strptime(_time_fired, "%Y-%m-%d %H:%M:%S.%f")
fabm3n marked this conversation as resolved.
Show resolved Hide resolved
)
except InvalidEntityFormatError:
pass
Expand Down Expand Up @@ -253,7 +254,9 @@ def main():
# Clean up by closing influx connection, and removing temporary table
influx.close()
if args.table == 'both':
remove_tmp_table()
cursor = connection.cursor()
remove_tmp_table(cursor)
cursor.close()

# print statistics - ideally you have one friendly name per entity_id
# you can use the output to see where the same sensor has had different
Expand Down Expand Up @@ -289,7 +292,7 @@ def formulate_sql_query(table: str, arg_tables: str):
if table == "states":
# Using two different SQL queries in a Union to support data made with older HA db schema:
# https://github.com/home-assistant/core/pull/71165
sql_query = """select SQL_NO_CACHE states.entity_id,
sql_query = """select states.entity_id,
states.state,
states.attributes,
events.event_type as event_type,
Expand All @@ -314,7 +317,7 @@ def formulate_sql_query(table: str, arg_tables: str):
else:
inset_query = ''
sql_query = f"""
SELECT SQL_NO_CACHE statistics_meta.statistic_id,
SELECT statistics_meta.statistic_id,
statistics.mean,
statistics.min,
statistics.max,
Expand Down Expand Up @@ -342,7 +345,7 @@ def formulate_tmp_table_sql():
TODO Not perfect solution, some entities have multiple attributes that change over time.
TODO Here we select the most recent
"""
return """CREATE TEMPORARY TABLE IF NOT EXISTS state_tmp
return """CREATE TEMPORARY TABLE IF NOT EXISTS state_tmp AS
SELECT max(states.attributes_id) as attributes_id, states.entity_id
FROM states
WHERE states.attributes_id IS NOT NULL
Expand All @@ -351,7 +354,7 @@ def formulate_tmp_table_sql():


def remove_tmp_table(cursor):
cursor.execute("""DROP TEMPORARY TABLE state_tmp;""")
cursor.execute("""DROP TABLE IF EXISTS state_tmp;""")


if __name__ == "__main__":
Expand Down
10 changes: 5 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
influxdb
influxdb_client
mysqlclient~=2.1.1
pyyaml~=6.0
mysqlclient
pyyaml
setuptools
tqdm~=4.64.0
tqdm
wheel

voluptuous~=0.13.1
homeassistant~=2022.6.7
voluptuous
homeassistant