Skip to content

Commit

Permalink
add some debug info for issue98
Browse files Browse the repository at this point in the history
  • Loading branch information
ARC-MX committed Dec 27, 2024
1 parent 17ad62f commit 9b75755
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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镜像部署,速度快

Expand Down Expand Up @@ -285,8 +285,6 @@ template:
<img src="assets/restart.jpg" alt="restart.jpg" style="zoom: 50%;" />
6. 更新容器及其代码(需要更新才需要)
```bash
Expand All @@ -298,7 +296,6 @@ docker-compose up -d # 重新运行

## 4)ha内数据展示


<img src="assets/edit1.jpg" alt="edit1.jpg" style="zoom: 50%;" />

结合[mini-graph-card](https://github.com/kalkih/mini-graph-card)[mushroom](https://github.com/piitaya/lovelace-mushroom)实现美化效果:
Expand Down Expand Up @@ -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)
# 其他
Expand Down
1 change: 0 additions & 1 deletion example.env
Original file line number Diff line number Diff line change
Expand Up @@ -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地址
Expand Down
55 changes: 35 additions & 20 deletions scripts/data_fetcher.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ast import Try
import logging
import os
import re
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
29 changes: 15 additions & 14 deletions scripts/sensor_updator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
)

0 comments on commit 9b75755

Please sign in to comment.