-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
35 lines (28 loc) · 929 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import datetime
import json
import base64
from google.cloud import bigquery
DATASET = ''
TABLE = ''
bigquery_client = bigquery.Client()
dataset = bigquery_client.dataset(DATASET)
table_ref = dataset.table(TABLE)
table = bigquery_client.get_table(table_ref)
def hass_to_bigquery(data, context):
if 'data' in data:
state = json.loads(
base64.b64decode(
data['data']
)
)
else:
raise ValueError('No data provided')
dt = {
'entity_id': state['entity_id'],
'state': state['state'],
'attributes': json.dumps(state['attributes']),
'last_changed': datetime.datetime.fromisoformat(state['last_changed'].replace('"', '')),
'last_updated': datetime.datetime.fromisoformat(state['last_updated'].replace('"', '')),
'context': json.dumps(state['context'])
}
res = bigquery_client.insert_rows(table, [dt])