1 Commits
Author SHA1 Message Date
yangchengdong 77865ecf1d deploy 2024-07-19 19:21:17 +08:00
4 changed files with 62 additions and 52 deletions
BIN
View File
Binary file not shown.
+1 -1
View File
@@ -1,4 +1,4 @@
DOMAIN = "state_grid"
PACKAGE_NAME = "custom_components.state_grid"
VERSION = "0.1.6"
VERSION = "0.1.8"
VERSION_STORAGE = 1
+60 -50
View File
@@ -164,8 +164,7 @@ class StateGridDataClient:
elif _Q in A and C in A[_Q]:B=A[_Q][C]
elif D in A:B=A[D]
else:B=json_dumps(A)
if E.is_debug:LOGGER.error(api+': '+B);LOGGER.error(json_dumps(A))
return B
LOGGER.error(api+': '+B);LOGGER.error(json_dumps(A));return B
async def __fetch_safe(C,api,data):
B=await C.__fetch(api,data)
if _H not in B:return B
@@ -271,16 +270,25 @@ class StateGridDataClient:
if _H in B and B[_H]==1 and _A in B and _u in B[_A]:
D=B[_A][_u]
if len(D)!=0:D=D[0];E['ladder']=D
async def __get_door_mouth_bill(E,door_account,monthBill):
O='pointList';H=monthBill;G='quantity';F='priceName';B=door_account;I=datetime.datetime.strptime(H[_AN],'%Y%m');P=f"{I.year}-{I.month:02d}";Q={_A:{_I:configuration[_V][_I],_D:configuration[_V][_D],_T:configuration[_V][_T],_e:configuration[_V][_e],_q:B[_g],_d:B[_U],_Y:B[_Y],_Av:P,_AV:B[_U],_AL:B[_AW],_AI:E.userInfo[_R],_N:'',_L:'',_A4:E.userInfo[_AB],_A3:E.userInfo[_R]},_C:configuration[_V][_C],_E:configuration[_V][_E],_S:B[_U]};C=await E.__fetch(get_door_ladder_api,Q);S=E.handle_request_result_message(_Aw,C)
if _H in C and C[_H]==1 and _A in C and _u in C[_A]:
J=C[_A][_u][0];R=catchFloat(J[O][0]['readList'][0],'activeCount');K=0;L=0;M=0;N=0
for A in J[O][0]['amtGroupList'][0]['amtList']:
if A[F]=='':K=catchFloat(A,G)
elif A[F]=='':L=catchFloat(A,G)
elif A[F]=='':M=catchFloat(A,G)
elif A[F]=='':N=catchFloat(A,G)
D={};D[_AX]=normal_round(R,2);D[_AY]=normal_round(K,2);D[_AZ]=normal_round(L,2);D[_Aa]=normal_round(M,2);D[_Ab]=normal_round(N,2);H[_h]=D
async def __get_door_mouth_bill(I,door_account,monthBill):
S=monthBill;O='quantity';N='priceName';K='amtGroupList';H='readList';E=door_account;C='amtList';B='pointList';T=datetime.datetime.strptime(S[_AN],'%Y%m');V=f"{T.year}-{T.month:02d}";W={_A:{_I:configuration[_V][_I],_D:configuration[_V][_D],_T:configuration[_V][_T],_e:configuration[_V][_e],_q:E[_g],_d:E[_U],_Y:E[_Y],_Av:V,_AV:E[_U],_AL:E[_AW],_AI:I.userInfo[_R],_N:'',_L:'',_A4:I.userInfo[_AB],_A3:I.userInfo[_R]},_C:configuration[_V][_C],_E:configuration[_V][_E],_S:E[_U]};F=await I.__fetch(get_door_ladder_api,W);X=I.handle_request_result_message(_Aw,F)
if _H in F and F[_H]==1 and _A in F and _u in F[_A]:
A=F[_A][_u][0];U=0;P=0;Q=0;L=0;R=0;M=[]
if H in A and len(A[H])>0:M=A[H]
elif B in A and len(A[B])>0 and H in A[B][0]and len(A[B][0][H])>0:M=A[B][0][H]
if len(M)>0:U=catchFloat(M[0],'activeCount')
J=[]
if C in A and len(A[C])>0:J=A[C]
elif B in A and len(A[B])>0 and C in A[B][0]and len(A[B][0][C])>0:J=A[B][0][C]
elif B in A and len(A[B])>0 and K in A[B][0]and len(A[B][0][K])>0 and C in A[B][0][K][0]and len(A[B][0][K][0][C])>0:J=A[B][0][K][0][C]
if len(J)>0:
for D in J:
if D[N]=='':P=catchFloat(D,O)
elif D[N]=='':Q=catchFloat(D,O)
elif D[N]=='':L=catchFloat(D,O)
elif D[N]=='':R=catchFloat(D,O)
if L==0:L=P+Q+R
G={};G[_AX]=normal_round(U,2);G[_AY]=normal_round(P,2);G[_AZ]=normal_round(Q,2);G[_Aa]=normal_round(L,2);G[_Ab]=normal_round(R,2);S[_h]=G
async def __get_door_daily_bill(B,door_account,year,start_date,end_date):
D='sevenEleList';A=door_account;E={'params1':{_C:configuration[_C],_E:configuration[_E],_S:configuration[_S],_K:{_W:configuration[_K][_W],_Z:configuration[_K][_Z],_a:configuration[_K][_a],_X:configuration[_K][_X]},_A1:{_R:B.userInfo[_R]},_p:B.token},'params3':{_A:{_A3:B.userInfo[_R],_q:A[_g],_AL:_c,'endTime':end_date,_Y:A[_Y],_Au:year,_AJ:A.get(_U,A.get(_AK,_F)),_N:'',_L:'','startTime':start_date,_A4:B.userInfo[_AB],_D:configuration[_m][_D],_I:configuration[_m][_I],_e:configuration[_m][_e],_d:configuration[_m][_d],_T:configuration[_m][_T]},_C:configuration[_m][_C],_E:configuration[_m][_E],_S:A.get(_U,A.get(_AK,_F))},'params4':'010103'};C=await B.__fetch_safe(get_door_daily_bill_api,E);F=B.handle_request_result_message('get_door_daily_bill_api',C)
if _H in C and C[_H]==1 and _A in C and D in C[_A]:A[_A6]=C[_A][D]
@@ -321,65 +329,67 @@ class StateGridDataClient:
elif A.retry_times>=3:A.need_login=_o
else:A.retry_times=A.retry_times+1
async def refresh_data(B,setup=_i,force_refresh=_i):
t='last_month_ele_cost';s='year_ele_cost';r='thisTPq';q='thisNPq';p='thisVPq';o='thisPPq';n='%Y%m%d';m='day';l='refresh_time';a=setup;Z='last_month_ele_num';Y='dayElePq';T='year_ele_num';G='balance'
if a is _o:
t='last_month_ele_cost';s='year_ele_cost';r='thisTPq';q='thisNPq';p='thisVPq';o='thisPPq';n='%Y%m%d';m='day';l='refresh_time';e=setup;d='last_month_ele_num';c='dayElePq';U='year_ele_num';K='balance'
if e is _o:
if B.dataVersion!=VERSION:B.powerUserList=_F
b=await B.__get_door_number()
if _B in b and b[_B]!=0:B.need_login=_o
f=await B.__get_door_number()
if _B in f and f[_B]!=0:B.need_login=_o
if B.need_login is _o:
if B.powerUserList is not _F:
for A in B.get_door_account_list():A[l]='Token刷新失败,请重新登录'
LOGGER.error('国家电网 - Token刷新失败,请重新登录!');return
u=a or force_refresh or int(time.time()*1000)-B.timestamp>B.refresh_interval*3600*1000
u=e or force_refresh or int(time.time()*1000)-B.timestamp>B.refresh_interval*3600*1000
if u is _i:return
U=datetime.datetime.now();F=U-datetime.timedelta(days=1);v=f"{F.year}-{F.month:02d}-{F.day:02d}";V=F-datetime.timedelta(days=40);w=f"{V.year}-{V.month:02d}-{V.day:02d}"
V=datetime.datetime.now();G=V-datetime.timedelta(days=1);v=f"{G.year}-{G.month:02d}-{G.day:02d}";W=G-datetime.timedelta(days=40);w=f"{W.year}-{W.month:02d}-{W.day:02d}"
for A in B.powerUserList:
x=A[_g];B.doorAccountDict[x]=A;await B.__get_door_balance(A)
if B.retry_times!=0:return
if _A5 in A:
y=catchFloat(A[_A5],'estiAmt');W=catchFloat(A[_A5],'prepayBal');c=catchFloat(A[_A5],'sumMoney')
if W==0 or W==c:A[G]=c
else:A[G]=W-y
d=catchFloat(A[_A5],'historyOwe')
if d>0:A[G]=-d
y=catchFloat(A[_A5],'estiAmt');X=catchFloat(A[_A5],'prepayBal');g=catchFloat(A[_A5],'sumMoney')
if X==0 or X==g:A[K]=g
else:A[K]=X-y
h=catchFloat(A[_A5],'historyOwe')
if h>0:A[K]=-h
else:LOGGER.error('国家电网账户余额获取失败!')
if G not in A:A[G]=0
await B.__get_door_daily_bill(A,U.year,w,v)
if K not in A:A[K]=0
await B.__get_door_daily_bill(A,V.year,w,v)
if _A6 not in A:LOGGER.error('国家电网无法获取日用电数据!');continue
X=0;H=_i
for e in range(10):
D=A[_A6][e]
try:float(D[Y]);H=_o;break
except:X=X+1
f=0;g=0;h=0;i=0;j=0
if H:
for e in range(X):A[_A6].pop(0)
D=A[_A6][0];E=datetime.datetime.strptime(D[m],n);A['daily_lasted_date']=f"{E.year}-{E.month:02d}-{E.day:02d}";f=catchFloat(D,Y);g=catchFloat(D,o);h=catchFloat(D,p);i=catchFloat(D,q);j=catchFloat(D,r)
A['daily_ele_num']=normal_round(f,2);A['daily_p_ele_num']=normal_round(g,2);A['daily_v_ele_num']=normal_round(h,2);A['daily_n_ele_num']=normal_round(i,2);A['daily_t_ele_num']=normal_round(j,2);I=0;J=0;K=0;L=0;M=0
if H:
Y=0;L=_i
for i in range(10):
D=A[_A6][i]
try:float(D[c]);L=_o;break
except:Y=Y+1
j=0;Z=0;a=0;M=0;b=0
if L:
for i in range(Y):A[_A6].pop(0)
D=A[_A6][0];E=datetime.datetime.strptime(D[m],n);A['daily_lasted_date']=f"{E.year}-{E.month:02d}-{E.day:02d}";j=catchFloat(D,c);Z=catchFloat(D,o);a=catchFloat(D,p);M=catchFloat(D,q);b=catchFloat(D,r)
if M==0:M=Z+a+b
A['daily_ele_num']=normal_round(j,2);A['daily_p_ele_num']=normal_round(Z,2);A['daily_v_ele_num']=normal_round(a,2);A['daily_n_ele_num']=normal_round(M,2);A['daily_t_ele_num']=normal_round(b,2);N=0;H=0;I=0;F=0;J=0
if L:
for C in A[_A6]:
z=datetime.datetime.strptime(C[m],n)
if z.month!=E.month:break
I+=catchFloat(C,Y);J+=catchFloat(C,o);K+=catchFloat(C,p);L+=catchFloat(C,q);M+=catchFloat(C,r)
A[_AX]=normal_round(I,2);A[_AY]=normal_round(J,2);A[_AZ]=normal_round(K,2);A[_Aa]=normal_round(L,2);A[_Ab]=normal_round(M,2)
if H:
N=E-datetime.timedelta(days=E.day);A0=f"{N.year}{N.month:02d}"
if _AC not in A or A[_AC]!=N.year or _v not in A or A[_v][_AN]!=A0:await B.__get_door_bill(A,N)
N+=catchFloat(C,c);H+=catchFloat(C,o);I+=catchFloat(C,p);F+=catchFloat(C,q);J+=catchFloat(C,r)
if F==0:F=H+I+J
A[_AX]=normal_round(N,2);A[_AY]=normal_round(H,2);A[_AZ]=normal_round(I,2);A[_Aa]=normal_round(F,2);A[_Ab]=normal_round(J,2)
if L:
O=E-datetime.timedelta(days=E.day);A0=f"{O.year}{O.month:02d}"
if _AC not in A or A[_AC]!=O.year or _v not in A or A[_v][_AN]!=A0:await B.__get_door_bill(A,O)
if _l in A:
for C in A[_l]:
if _h not in C:await B.__get_door_mouth_bill(A,C)
if _AM in A:A[T]=catchFloat(A[_AM],'totalEleNum');A[s]=catchFloat(A[_AM],'totalEleCost')
if T not in A:A[T]=0;A[s]=0
if _v in A:A[Z]=catchFloat(A[_v],'monthEleNum');A[t]=catchFloat(A[_v],'monthEleCost');k=datetime.datetime.strptime(A[_v][_AN],'%Y%m')
if Z not in A:A[Z]=0;A[t]=0;k=F.month
O=0;P=0;Q=0;R=0;S=0
if k.month==12:O=I;P=J;Q=K;R=L;S=M
if _AM in A:A[U]=catchFloat(A[_AM],'totalEleNum');A[s]=catchFloat(A[_AM],'totalEleCost')
if U not in A:A[U]=0;A[s]=0
if _v in A:A[d]=catchFloat(A[_v],'monthEleNum');A[t]=catchFloat(A[_v],'monthEleCost');k=datetime.datetime.strptime(A[_v][_AN],'%Y%m')
if d not in A:A[d]=0;A[t]=0;k=G.month
P=0;Q=0;R=0;S=0;T=0
if k.month==12:P=N;Q=H;R=I;S=F;T=J
else:
if _l in A:
for C in A[_l]:
if _h in C:O+=C[_h][_AX];P+=C[_h][_AY];Q+=C[_h][_AZ];R+=C[_h][_Aa];S+=C[_h][_Ab]
O+=I;P+=J;Q+=K;R+=L;S+=M
A[T]=normal_round(O,2);A['year_p_ele_num']=normal_round(P,2);A['year_v_ele_num']=normal_round(Q,2);A['year_n_ele_num']=normal_round(R,2);A['year_t_ele_num']=normal_round(S,2);A[l]=datetime.datetime.strftime(U,'%Y-%m-%d %H:%M:%S')
if _h in C:P+=C[_h][_AX];Q+=C[_h][_AY];R+=C[_h][_AZ];S+=C[_h][_Aa];T+=C[_h][_Ab]
P+=N;Q+=H;R+=I;S+=F;T+=J
A[U]=normal_round(P,2);A['year_p_ele_num']=normal_round(Q,2);A['year_v_ele_num']=normal_round(R,2);A['year_n_ele_num']=normal_round(S,2);A['year_t_ele_num']=normal_round(T,2);A[l]=datetime.datetime.strftime(V,'%Y-%m-%d %H:%M:%S')
await B.save_data()
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.6"
"version": "0.1.8"
}