-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
executable file
·72 lines (62 loc) · 3.12 KB
/
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
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
#!/usr/bin/env python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from requests import get
from argparse import ArgumentParser
from selenium.webdriver.support.select import Select
from selenium.common.exceptions import NoSuchElementException
parser = ArgumentParser(description="Syncs the value in a helper entity in home assistant to a fronius inverter's soft limit field.")
parser.add_argument('-t', '--home_assistant_token', type=str, required=True, help='Home Assistant Token')
parser.add_argument('-u', '--home_assistant_url', type=str, required=True, help='Home Assistant URL. Eg: http://192.168.2.100:8123')
parser.add_argument('-f', '--fronius_url', type=str, required=True, help='Fronius URL. Eg: http://192.168.2.100')
parser.add_argument('-p', '--fronius_password', type=str, required=True, help='Fronius service account password')
parser.add_argument('-e', '--home_assistant_export_limit_entity', type=str, default='input_number.export_limit', help='Home Assistant Export Limit Entity (default: input_number.export_limit)')
parser.add_argument('-n', '--not_headless', action="store_true", help="Do not run firefox in headless mode. Might be useful if running the script locally to test it or something...")
args = parser.parse_args()
options = webdriver.FirefoxOptions()
if not args.not_headless:
options.add_argument("-headless")
driver = webdriver.Firefox(options)
try:
driver.implicitly_wait(10)
driver.get(f"{args.fronius_url}/#/settings/evu")
# While we're waiting for firefox to load, lets get the expected limit
response = get(
f"{args.home_assistant_url}/api/states/{args.home_assistant_export_limit_entity}",
headers={
"Authorization": f"Bearer {args.home_assistant_token}"
}
).json()
desired_limit = int(float(response["state"]))
if desired_limit < -1:
print("Setting to 0, rather than", desired_limit)
desired_limit = 0
else:
print("Setting to", desired_limit)
# Ok, back to webdriver.
username = Select(driver.find_element(By.TAG_NAME, "select"))
username.select_by_visible_text("service")
password = driver.find_element(By.CSS_SELECTOR, "[type=password]")
password.send_keys(args.fronius_password)
password.send_keys(Keys.RETURN)
try:
limit = driver.find_element(By.CSS_SELECTOR, '[input-validator="softLimitValidator"]')
except NoSuchElementException:
driver.get(f"{args.fronius_url}/#/settings/evu")
limit = driver.find_element(By.CSS_SELECTOR, '[input-validator="softLimitValidator"]')
current_limit = limit.get_property("value")
print("Existing limit: ", current_limit)
if current_limit == str(desired_limit):
print("They match. Skip!")
exit(0)
limit.clear()
limit.send_keys(str(desired_limit))
ok_button = driver.find_elements(By.CSS_SELECTOR, "button.OK")
ok_button[2].click()
driver.find_elements(By.XPATH, '//button[normalize-space()="OK"]')
except Exception:
print(driver.find_element(By.TAG_NAME, "body").screenshot_as_base64)
raise
finally:
driver.close()