-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinverter
executable file
·92 lines (75 loc) · 3.17 KB
/
inverter
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
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3.11
import getpass
import glob
import os
import sys
def setup() -> None:
script_dir = os.path.dirname(os.path.realpath(__file__))
venv_path = os.path.join(script_dir, ".venv")
source_path = os.path.join(script_dir, "source")
if not os.path.exists(venv_path):
print("Error: Virtual environment does not exist.")
sys.exit(1)
possible_site_packages = glob.glob(os.path.join(venv_path, "lib", "python*", "site-packages"))
if not possible_site_packages:
print("Error: Could not find site-packages in the virtual environment.")
sys.exit(1)
if not os.path.exists(source_path):
print("Error: Source directory does not exist.")
sys.exit(1)
sys.path.append(source_path)
sys.path.append(possible_site_packages[0])
def print_help_and_exit() -> None:
print("This script allows you to interact with the inverter.")
print("")
print("You have to supply one of the following arguments:")
print(" --status Get the current state of charge and operation mode of the inverter")
print(" --mode {GENERAL,ECO_CHARGE} Set the operation mode of the inverter")
print(" --tibber Fetches, displays the tibber rates and writes them to the database")
print("")
print("It uses all the variables defined in the .env file")
sys.exit(1)
if len(sys.argv) < 2 or sys.argv[1] not in ["--status", "--mode", "--tibber"]:
print_help_and_exit()
if sys.argv[1] == "--mode" and (len(sys.argv) != 3 or sys.argv[2] not in ["GENERAL", "ECO_CHARGE"]):
print('Unsupported mode, valid modes are "GENERAL" and "ECO_CHARGE"\n')
print_help_and_exit()
setup()
from goodwe.et import OperationMode
from inverter import Inverter
from logger import LoggerMixin
from tibber_api_handler import TibberAPIHandler
logger = LoggerMixin("Bashscript")
logger.write_newlines_to_log_file()
logger.log.info(f"Inverter is manually controlled by the user {getpass.getuser()}: {sys.argv}")
if sys.argv[1] == "--status":
try:
inverter = Inverter()
inverter.log.name += " User"
soc = inverter.get_state_of_charge(True)
mode = inverter.get_operation_mode(True).name
print(f"SoC: {soc}\nmode: {mode}")
except Exception as e:
logger.log.exception(e)
sys.exit(1)
if sys.argv[1] == "--mode":
try:
new_mode = OperationMode[sys.argv[2]]
inverter = Inverter()
inverter.log.name += " User"
last_mode = inverter.get_operation_mode(True).name
inverter.set_operation_mode(new_mode)
print(f"last mode: {last_mode}\nnew mode: {new_mode.name}")
except Exception as e:
logger.log.exception(e)
sys.exit(1)
if sys.argv[1] == "--tibber":
try:
tibber_api_handler = TibberAPIHandler()
api_result = tibber_api_handler._fetch_upcoming_prices_from_api()
all_energy_rates = tibber_api_handler._extract_energy_rates_from_api_response(api_result)
tibber_api_handler._remove_energy_rates_from_the_past(all_energy_rates)
tibber_api_handler.write_energy_rates_to_database(all_energy_rates)
except Exception as e:
logger.log.exception(e)
exit(1)