1 Commits
Author SHA1 Message Date
HassBox f571748155 deploy 2024-06-03 13:52:22 +08:00
3 changed files with 26 additions and 26 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
DOMAIN = "state_grid"
PACKAGE_NAME = "custom_components.state_grid"
VERSION = "0.1.1"
VERSION = "0.1.0"
VERSION_STORAGE = 1
+24 -24
View File
@@ -137,7 +137,7 @@ def catchFloat(data,key):
except:return 0
else:return 0
class StateGridDataClient:
hass=_G;session=_G;keyCode=_G;publicKey=_G;need_login=_j;phone=_G;codeKey=_G;serialNo=_G;qrCodeSerial=_G;userInfo=_G;accountInfo=_G;powerUserList=_G;doorAccountDict={};cookie=[];timestamp=int(time.time()*1000);accessToken=_G;refreshToken=_G;token=_G;expirationDate=_G;refresh_interval=6;is_debug=_j
hass=_G;session=_G;keyCode=_G;publicKey=_G;need_login=_j;phone=_G;codeKey=_G;serialNo=_G;qrCodeSerial=_G;userInfo=_G;accountInfo=_G;powerUserList=_G;doorAccountDict={};cookie=[];timestamp=int(time.time()*1000);accessToken=_G;refreshToken=_G;token=_G;expirationDate=_G;refresh_interval=12;is_debug=_j
def __init__(A,hass,config=_G):
B=config;A.hass=hass;C=aiohttp.TCPConnector(ssl=_j);D=aiohttp.CookieJar(quote_cookie=_y);A.session=aiohttp.ClientSession(cookie_jar=D,connector=C)
if B is not _G:
@@ -290,23 +290,23 @@ class StateGridDataClient:
if _B in B and B[_B]==0:A.need_login=_j;await A.save_data()
else:A.need_login=_y;LOGGER.error('国家电网刷新 Token 失败')
async def refresh_data(C,setup=_j):
k='last_month_ele_cost';j='monthEleNum';i='year_ele_cost';h='thisTPq';g='thisNPq';f='thisVPq';e='thisPPq';d='refresh_time';U=setup;T='ladder_level_num';S='ladder_level';R='last_month_ele_num';Q='year_ele_num';L='%Y%m%d';K='day';I='dayElePq';H='balance'
j='last_month_ele_cost';i='monthEleNum';h='year_ele_cost';g='thisTPq';f='thisNPq';e='thisVPq';d='thisPPq';U=setup;T='ladder_level_num';S='ladder_level';R='last_month_ele_num';Q='year_ele_num';L='%Y%m%d';K='day';I='dayElePq';H='balance'
if U is _y:await C.__get_door_number()
if C.need_login is _y:A[d]='刷新 Token 失败,需重新登录!';LOGGER.error('国家电网需要重新登录!');return
l=U or int(time.time()*1000)-C.timestamp>C.refresh_interval*3600*1000
if l is _j:return
M=datetime.datetime.now();J=M-datetime.timedelta(days=1);m=f"{J.year}-{J.month:02d}-{J.day:02d}";N=J-datetime.timedelta(days=40);n=f"{N.year}-{N.month:02d}-{N.day:02d}"
if C.need_login is _y:LOGGER.error('国家电网需要重新登录!');return
k=U or int(time.time()*1000)-C.timestamp>C.refresh_interval*3600*1000
if k is _j:return
M=datetime.datetime.now();J=M-datetime.timedelta(days=1);l=f"{J.year}-{J.month:02d}-{J.day:02d}";N=J-datetime.timedelta(days=40);m=f"{N.year}-{N.month:02d}-{N.day:02d}"
for A in C.powerUserList:
o=A[_r];C.doorAccountDict[o]=A;await C.__get_door_balance(A)
n=A[_r];C.doorAccountDict[n]=A;await C.__get_door_balance(A)
if _A8 in A:
V=catchFloat(A[_A8],'prepayBal');p=catchFloat(A[_A8],'sumMoney')
if V==0:A[H]=p
V=catchFloat(A[_A8],'prepayBal');o=catchFloat(A[_A8],'sumMoney')
if V==0:A[H]=o
else:A[H]=V
W=catchFloat(A[_A8],'historyOwe')
if W>0:A[H]=-W
else:LOGGER.error('国家电网账户余额获取失败!')
if H not in A:A[H]=0
await C.__get_door_daily_bill(A,M.year,n,m)
await C.__get_door_daily_bill(A,M.year,m,l)
if _l not in A:LOGGER.error('国家电网无法获取日用电数据!');return
O=0
for X in range(10):
@@ -314,20 +314,20 @@ class StateGridDataClient:
try:float(D[I]);break
except:O=O+1
for X in range(O):A[_l].pop(0)
D=A[_l][0];E=datetime.datetime.strptime(D[K],L);A['daily_ele_num']=catchFloat(D,I);A['daily_p_ele_num']=catchFloat(D,e);A['daily_v_ele_num']=catchFloat(D,f);A['daily_n_ele_num']=catchFloat(D,g);A['daily_t_ele_num']=catchFloat(D,h);Y=0;Z=0;a=0;b=0;c=0
D=A[_l][0];E=datetime.datetime.strptime(D[K],L);A['daily_ele_num']=catchFloat(D,I);A['daily_p_ele_num']=catchFloat(D,d);A['daily_v_ele_num']=catchFloat(D,e);A['daily_n_ele_num']=catchFloat(D,f);A['daily_t_ele_num']=catchFloat(D,g);Y=0;Z=0;a=0;b=0;c=0
for B in A[_l]:
G=datetime.datetime.strptime(B[K],L)
if G.month!=E.month:break
Y+=catchFloat(B,I);Z+=catchFloat(B,e);a+=catchFloat(B,f);b+=catchFloat(B,g);c+=catchFloat(B,h)
A['month_ele_num']=normal_round(Y,2);A['month_p_ele_num']=normal_round(Z,2);A['month_v_ele_num']=normal_round(a,2);A['month_n_ele_num']=normal_round(b,2);A['month_t_ele_num']=normal_round(c,2);P=E-datetime.timedelta(days=E.day);q=f"{P.year}-{P.month:02d}"
if _s not in A or A[_s][_AR]!=q:await C.__get_door_bill(A,P.year)
Y+=catchFloat(B,I);Z+=catchFloat(B,d);a+=catchFloat(B,e);b+=catchFloat(B,f);c+=catchFloat(B,g)
A['month_ele_num']=normal_round(Y,2);A['month_p_ele_num']=normal_round(Z,2);A['month_v_ele_num']=normal_round(a,2);A['month_n_ele_num']=normal_round(b,2);A['month_t_ele_num']=normal_round(c,2);P=E-datetime.timedelta(days=E.day);p=f"{P.year}-{P.month:02d}"
if _s not in A or A[_s][_AR]!=p:await C.__get_door_bill(A,P.year)
if _s not in A:LOGGER.error('国家电网无法获取年度用电数据!');return
if _AH in A:A[Q]=catchFloat(A[_AH],'totalEleNum');A[i]=catchFloat(A[_AH],'totalEleCost')
if Q not in A:A[Q]=0;A[i]=0
if _AI in A:A[R]=catchFloat(A[_s],j);A[k]=catchFloat(A[_s],'monthEleCost')
if R not in A:A[R]=0;A[k]=0
r=datetime.datetime.strptime(A[_s][_AR],'%Y%m')
if r.month==12:
if _AH in A:A[Q]=catchFloat(A[_AH],'totalEleNum');A[h]=catchFloat(A[_AH],'totalEleCost')
if Q not in A:A[Q]=0;A[h]=0
if _AI in A:A[R]=catchFloat(A[_s],i);A[j]=catchFloat(A[_s],'monthEleCost')
if R not in A:A[R]=0;A[j]=0
q=datetime.datetime.strptime(A[_s][_AR],'%Y%m')
if q.month==12:
F=0
for B in A[_l]:
G=datetime.datetime.strptime(B[K],L)
@@ -335,16 +335,16 @@ class StateGridDataClient:
F+=catchFloat(B,I)
else:
F=0
for B in A[_AI]:F+=catchFloat(B,j)
s=len(A[_AI])
for B in A[_AI]:F+=catchFloat(B,i)
r=len(A[_AI])
for B in A[_l]:
G=datetime.datetime.strptime(B[K],L)
if G.month<=s:break
if G.month<=r:break
F+=catchFloat(B,I)
if F<=2760:A[S]='第一阶梯';A[T]=1
elif F<=4800:A[S]='第二阶梯';A[T]=2
else:A[S]='第三阶梯';A[T]=3
A['daily_lasted_date']=f"{E.year}-{E.month:02d}-{E.day:02d}";A[d]=datetime.datetime.strftime(M,'%Y-%m-%d %H:%M:%S')
A['daily_lasted_date']=f"{E.year}-{E.month:02d}-{E.day:02d}";A['refresh_time']=datetime.datetime.strftime(M,'%Y-%m-%d %H:%M:%S')
await C.save_data()
async def get_door_account_list(A):return list(A.doorAccountDict.values())
def get_door_account(A):return A.doorAccountDict
+1 -1
View File
@@ -5,5 +5,5 @@
"config_flow": true,
"documentation": "https://hassbox.cn/",
"iot_class": "cloud_polling",
"version": "0.1.1"
"version": "0.1.0"
}