Skip to content

Commit

Permalink
fix: fix grdf login page connection
Browse files Browse the repository at this point in the history
  • Loading branch information
bsegault committed Aug 30, 2024
1 parent 30665a2 commit f8e4bd7
Showing 1 changed file with 45 additions and 18 deletions.
63 changes: 45 additions & 18 deletions apps/meters_to_ha/meters_to_ha.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@
LOGGER = logging.getLogger()

HA_API_SENSOR_FORMAT = "/api/states/%s"
PARAM_NO_CAPTCHA = "nocaptcha"
PARAM_2CAPTCHA_TOKEN = "2captcha_token"
PARAM_CAPMONSTER_TOKEN = "capmonster_token"
PARAM_CAPTCHAAI_TOKEN = "captchaai_token"
CAPTCHA_TOKENS = (
PARAM_NO_CAPTCHA,
PARAM_CAPMONSTER_TOKEN,
PARAM_2CAPTCHA_TOKEN,
PARAM_CAPTCHAAI_TOKEN,
Expand Down Expand Up @@ -476,6 +478,7 @@ def __init__(
PARAM_TIMEOUT: "30",
PARAM_DOWNLOAD_FOLDER: self.install_dir,
PARAM_LOGS_FOLDER: self.install_dir,
PARAM_NO_CAPTCHA: PARAM_OPTIONAL_VALUE,
PARAM_2CAPTCHA_TOKEN: PARAM_OPTIONAL_VALUE,
PARAM_CAPMONSTER_TOKEN: PARAM_OPTIONAL_VALUE,
PARAM_CAPTCHAAI_TOKEN: PARAM_OPTIONAL_VALUE,
Expand Down Expand Up @@ -1084,11 +1087,18 @@ def resolve_captcha2(self) -> str | None:

method = None
for m in CAPTCHA_TOKENS:
key = self.configuration[m]
if key is not None and key != "":
value = self.configuration[m]
if value is not None and value != "":
method = m
break

if method == PARAM_NO_CAPTCHA and value:
self.mylog(
"Captcha resolution is disabled by config. The following steps may not work.",

Check failure on line 1097 in apps/meters_to_ha/meters_to_ha.py

View workflow job for this annotation

GitHub Actions / pre-commit

E501 line too long (94 > 79 characters)
st="WW",
)
return method

if method is None:
self.mylog(
"Can not resolve using captcha service - "
Expand Down Expand Up @@ -1749,24 +1759,14 @@ def get_gazpar_file(self):
self.__browser.get(self.__class__.site_grdf_url)
self.mylog(st="OK")

# Wait for Password #####
self.mylog("Waiting for Password", end="")

ep = EC.presence_of_element_located((By.ID, "pass"))
el_password = self.__wait.until(
ep,
message="failed, page timeout (timeout="
+ str(self.configuration[PARAM_TIMEOUT])
+ ")",
)
self.mylog(st="OK")

# Wait for Email #####
self.mylog("Waiting for Email", end="")
ep = EC.presence_of_element_located((By.ID, "mail"))
ep = EC.presence_of_element_located(
(By.XPATH, r"//input[@name='identifier']")
)
el_email = self.__wait.until(
ep,
message="failed, page timeout (timeout="
message="failed to locate email field, page timeout (timeout="
+ str(self.configuration[PARAM_TIMEOUT])
+ ")",
)
Expand All @@ -1778,6 +1778,33 @@ def get_gazpar_file(self):
el_email.send_keys(self.configuration[PARAM_GRDF_LOGIN])
self.mylog(st="OK")

# Try to click Next button to enter password
try:
self.click_in_view(
By.XPATH,
r"//input[@value='Suivant']",
# wait_message="",
click_message="Click on connexion",
delay=random.uniform(1, 2),
)
# Even if click succeeded, not always connected
except Exception:
pass

# Wait for Password #####
self.mylog("Waiting for Password", end="")

ep = EC.presence_of_element_located(
(By.XPATH, r"//input[@name='credentials.passcode']")
)
el_password = self.__wait.until(
ep,
message="failed, page timeout (timeout="
+ str(self.configuration[PARAM_TIMEOUT])
+ ")",
)
self.mylog(st="OK")

# Type Password #####
self.mylog("Type Password", end="")
el_password.send_keys(self.configuration[PARAM_GRDF_PASSWORD])
Expand All @@ -1790,12 +1817,12 @@ def get_gazpar_file(self):
# Give the user some time to resolve the captcha
# FEAT: Wait until it disappears, use 2captcha if configured

CONNEXION_XPATH = r"//input[@value='Connexion']"
CONNEXION_XPATH = r"//input[@value='Se Connecter']"

self.mylog("Proceed with captcha resolution. ", end="~~")
if self.resolve_captcha2() is not None:
# Some time for captcha to remove.
self.mylog("Automatic resolution succeeded. ", end="~~")
self.mylog("Captcha resolution done. ", end="~~")
time.sleep(2)
else:
# Manual
Expand Down

0 comments on commit f8e4bd7

Please sign in to comment.