-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
82 lines (68 loc) · 2.76 KB
/
setup.py
File metadata and controls
82 lines (68 loc) · 2.76 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
""" This script will create the necessary databases for data collection and
trigger an initial backfill of the database given the available data.
Unfortunately I don't know of any free/good historical API for weather, so you
can only really wait for enough weather data to train.
"""
# Author: Daniel Williams
__version__ = '0.0.1'
import sqlite3
import logging
import sys
import teslapy
import time
from powerwallrl.data.weather import WeatherData
from powerwallrl.data.tesla import TeslaPowerwallData
from powerwallrl.settings import PowerwallRLConfig
import webview
def custom_auth(url):
result = ['']
window = webview.create_window('Login', url)
def on_loaded():
result[0] = window.get_current_url()
if 'void/callback' in result[0].split('?')[0]:
window.destroy()
window.loaded += on_loaded
webview.start()
return result[0]
def main():
# Log INFO level message to stdout for the user to see progress.
root = logging.getLogger()
root.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)
config = PowerwallRLConfig()
root.info("Setting up and backfilling data to database: %s",
config.database_location)
# Weather Data.
db = sqlite3.connect(config.database_location)
weather = WeatherData(config.openweathermap_api_key, config.latitude,
config.longitude, db, config.local_timezone)
root.info("Setting up weather data database tables.")
weather.setup()
root.info("Collecting weather data.")
weather.save_weather_data()
tesla = teslapy.Tesla(email=config.tesla_username, cache_file=config.tesla_cache_file)
if not tesla.authorized:
# Setup Tesla API authentication
with teslapy.Tesla(email=config.tesla_username, cache_file=config.tesla_cache_file) as tesla:
print('Use browser to login. Page Not Found will be shown at success.')
print('Open this URL: ' + tesla.authorization_url())
tesla.fetch_token(authorization_response=input(
'Enter URL after authentication to cache authorization token '
'locally: '))
# Power Data.
powerwall = TeslaPowerwallData(config.tesla_username, db,
config.local_timezone,
config.tesla_cache_file)
root.info("Setting up powerwall data database tables.")
powerwall.setup()
root.info("Backfilling powerwall data. (Depending on how long installation "
"date was ago, this might take quite sometime.)")
powerwall.backfill_data()
root.info("All setup! Now setup regular data collection.")
if __name__ == "__main__":
main()