From 9b757554cd942e871bc1665770c19b06d77ffacd Mon Sep 17 00:00:00 2001 From: ARC-MX Date: Fri, 27 Dec 2024 14:45:57 +0800 Subject: [PATCH] add some debug info for issue98 --- .github/workflows/docker-image.yml | 2 +- README.md | 7 ++-- example.env | 1 - scripts/data_fetcher.py | 55 +++++++++++++++++++----------- scripts/sensor_updator.py | 29 ++++++++-------- 5 files changed, 53 insertions(+), 41 deletions(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 4b20dd6..960f9ff 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -5,7 +5,7 @@ on: inputs: username: description: 'version of this branch' - default: 'v1.5.4' + default: 'v1.6.0' required: true type: string paths: diff --git a/README.md b/README.md index 8896498..96597fe 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ ## 2)获取HA token - token获取方法参考[https://cloud.tencent.com/developer/article/2139538](https://cloud.tencent.com/developer/article/2139538) + token获取方法参考[https://blog.csdn.net/qq_25886111/article/details/106282492](https://blog.csdn.net/qq_25886111/article/details/106282492) ## 3)docker镜像部署,速度快 @@ -285,8 +285,6 @@ template: restart.jpg - - 6. 更新容器及其代码(需要更新才需要) ```bash @@ -298,7 +296,6 @@ docker-compose up -d # 重新运行 ## 4)ha内数据展示 - edit1.jpg 结合[mini-graph-card](https://github.com/kalkih/mini-graph-card) 和[mushroom](https://github.com/piitaya/lovelace-mushroom)实现美化效果: @@ -371,10 +368,10 @@ cards: type: vertical-stack ``` - ## 5)电量通知 更新电费余额不足提醒,在.env里设置提醒余额。目前我是用[pushplus](https://www.pushplus.plus/)的方案,注册pushplus然后,获取token,通知给谁就让谁注册并将token填到.env中 + token获取方法参考[https://cloud.tencent.com/developer/article/2139538](https://cloud.tencent.com/developer/article/2139538) # 其他 diff --git a/example.env b/example.env index e9eaca8..ce7bef0 100644 --- a/example.env +++ b/example.env @@ -12,7 +12,6 @@ IGNORE_USER_ID=xxxxxxx,xxxxxxx,xxxxxxx ENABLE_DATABASE_STORAGE=True # 数据库名,默认为homeassistant DB_NAME="homeassistant.db" -# COLLECTION_NAME默认为electricity_daily_usage_{国网用户id},不支持修改。 ## homeassistant配置 # 改为你的localhost为你的homeassistant地址 diff --git a/scripts/data_fetcher.py b/scripts/data_fetcher.py index 1c50d54..9b77400 100644 --- a/scripts/data_fetcher.py +++ b/scripts/data_fetcher.py @@ -1,3 +1,4 @@ +from ast import Try import logging import os import re @@ -302,16 +303,19 @@ def fetch(self): logging.info("login successed !") else: logging.info("login unsuccessed !") - logging.info(f"Login successfully on {LOGIN_URL}") - time.sleep(self.RETRY_WAIT_TIME_OFFSET_UNIT*2) - user_id_list = self._get_user_ids(driver) - logging.info(f"Here are a total of {len(user_id_list)} userids, which are {user_id_list} among which {self.IGNORE_USER_ID} will be ignored.") - time.sleep(self.RETRY_WAIT_TIME_OFFSET_UNIT) except Exception as e: logging.error( f"Webdriver quit abnormly, reason: {e}. {self.RETRY_TIMES_LIMIT} retry times left.") driver.quit() + logging.info(f"Login successfully on {LOGIN_URL}") + time.sleep(self.RETRY_WAIT_TIME_OFFSET_UNIT*2) + logging.info(f"Try to get the userid list") + user_id_list = self._get_user_ids(driver) + logging.info(f"Here are a total of {len(user_id_list)} userids, which are {user_id_list} among which {self.IGNORE_USER_ID} will be ignored.") + time.sleep(self.RETRY_WAIT_TIME_OFFSET_UNIT) + + for userid_index, user_id in enumerate(user_id_list): try: # switch to electricity charge balance page @@ -408,21 +412,32 @@ def _get_all_data(self, driver, user_id, userid_index): return balance, last_daily_date, last_daily_usage, yearly_charge, yearly_usage, month_charge, month_usage def _get_user_ids(self, driver): - # click roll down button for user id - self._click_button(driver, By.XPATH, "//div[@class='el-dropdown']/span") - time.sleep(self.RETRY_WAIT_TIME_OFFSET_UNIT) - # wait for roll down menu displayed - target = driver.find_element(By.CLASS_NAME, "el-dropdown-menu.el-popper").find_element(By.TAG_NAME, "li") - WebDriverWait(driver, self.DRIVER_IMPLICITY_WAIT_TIME).until(EC.visibility_of(target)) - WebDriverWait(driver, self.DRIVER_IMPLICITY_WAIT_TIME).until( - EC.text_to_be_present_in_element((By.XPATH, "//ul[@class='el-dropdown-menu el-popper']/li"), ":")) - - # get user id one by one - userid_elements = driver.find_element(By.CLASS_NAME, "el-dropdown-menu.el-popper").find_elements(By.TAG_NAME, "li") - userid_list = [] - for element in userid_elements: - userid_list.append(re.findall("[0-9]+", element.text)[-1]) - return userid_list + try: + # click roll down button for user id + self._click_button(driver, By.XPATH, "//div[@class='el-dropdown']/span") + logging.debug(f'''self._click_button(driver, By.XPATH, "//div[@class='el-dropdown']/span")''') + time.sleep(self.RETRY_WAIT_TIME_OFFSET_UNIT) + # wait for roll down menu displayed + target = driver.find_element(By.CLASS_NAME, "el-dropdown-menu.el-popper").find_element(By.TAG_NAME, "li") + logging.debug(f'''target = driver.find_element(By.CLASS_NAME, "el-dropdown-menu.el-popper").find_element(By.TAG_NAME, "li")''') + time.sleep(self.RETRY_WAIT_TIME_OFFSET_UNIT) + WebDriverWait(driver, self.DRIVER_IMPLICITY_WAIT_TIME).until(EC.visibility_of(target)) + time.sleep(self.RETRY_WAIT_TIME_OFFSET_UNIT) + logging.debug(f'''WebDriverWait(driver, self.DRIVER_IMPLICITY_WAIT_TIME).until(EC.visibility_of(target))''') + WebDriverWait(driver, self.DRIVER_IMPLICITY_WAIT_TIME).until( + EC.text_to_be_present_in_element((By.XPATH, "//ul[@class='el-dropdown-menu el-popper']/li"), ":")) + time.sleep(self.RETRY_WAIT_TIME_OFFSET_UNIT) + + # get user id one by one + userid_elements = driver.find_element(By.CLASS_NAME, "el-dropdown-menu.el-popper").find_elements(By.TAG_NAME, "li") + userid_list = [] + for element in userid_elements: + userid_list.append(re.findall("[0-9]+", element.text)[-1]) + return userid_list + except Exception as e: + logging.error( + f"Webdriver quit abnormly, reason: {e}. get user_id list failed.") + driver.quit() def _get_electric_balance(self, driver): try: diff --git a/scripts/sensor_updator.py b/scripts/sensor_updator.py index 6980be7..2d5e274 100644 --- a/scripts/sensor_updator.py +++ b/scripts/sensor_updator.py @@ -130,21 +130,22 @@ def send_url(self, sensorName, request_body): logging.error(f"Homeassistant REST API invoke failed, reason is {e}") def balance_notify(self, user_id, balance): - logging.info( - f"Check the electricity bill balance. When the balance is less than {self.BALANCE} CNY, the notification will be sent = {self.RECHARGE_NOTIFY}" - ) if self.RECHARGE_NOTIFY : - self.BALANCE = float(os.getenv("BALANCE", 10.0)) - self.PUSHPLUS_TOKEN = os.getenv("PUSHPLUS_TOKEN").split(",") + BALANCE = float(os.getenv("BALANCE", 10.0)) + PUSHPLUS_TOKEN = os.getenv("PUSHPLUS_TOKEN").split(",") + logging.info(f"Check the electricity bill balance. When the balance is less than {BALANCE} CNY, the notification will be sent = {self.RECHARGE_NOTIFY}") + if balance < BALANCE : + for token in PUSHPLUS_TOKEN: + title = "电费余额不足提醒" + content = (f"您用户号{user_id}的当前电费余额为:{balance}元,请及时充值。" ) + url = ("http://www.pushplus.plus/send?token="+ token+ "&title="+ title+ "&content="+ content) + requests.get(url) + logging.info( + f"The current balance of user id {user_id} is {balance} CNY less than {BALANCE} CNY, notice has been sent, please pay attention to check and recharge." + ) else : + logging.info( + f"Check the electricity bill balance, the notification will be sent = {self.RECHARGE_NOTIFY}") return - if balance < self.BALANCE : - for token in self.PUSHPLUS_TOKEN: - title = "电费余额不足提醒" - content = (f"您用户号{user_id}的当前电费余额为:{balance}元,请及时充值。" ) - url = ("http://www.pushplus.plus/send?token="+ token+ "&title="+ title+ "&content="+ content) - requests.get(url) - logging.info( - f"The current balance of user id {user_id} is {balance} CNY less than {self.BALANCE} CNY, notice has been sent, please pay attention to check and recharge." - ) +