1 Commits
Author SHA1 Message Date
HassBox a1e5993fd1 deploy 2024-05-09 00:08:18 +08:00
4 changed files with 161 additions and 167 deletions
+2 -2
View File
@@ -1,4 +1,4 @@
DOMAIN = "state_grid"
DOMAIN = 'state_grid'
PACKAGE_NAME = "custom_components.state_grid"
VERSION = "0.0.6"
VERSION = "0.0.5"
VERSION_STORAGE = 1
+23 -23
View File
@@ -269,7 +269,7 @@ class StateGridDataClient:
if _B in A and A[_B]!=0:return A
B.need_login=_s;await B.save_data();return{_B:0,_A:B.powerUserList}
async def refresh_data(B,setup=_s):
n='daily_t_ele_num';m='daily_n_ele_num';l='daily_v_ele_num';k='daily_p_ele_num';j='last_month_ele_cost';i='year_ele_cost';h='monthEleNum';g='thisTPq';f='thisNPq';e='thisVPq';d='thisPPq';U='daily_ele_num';T='last_month_ele_num';S='year_ele_num';R='ladder_level_num';Q='ladder_level';L='%Y%m%d';K='day';I='balance';H='dayElePq'
n='daily_t_ele_num';m='daily_n_ele_num';l='daily_v_ele_num';k='daily_p_ele_num';j='daily_ele_num';i='last_month_ele_cost';h='last_month_ele_num';g='year_ele_cost';f='year_ele_num';e='monthEleNum';d='thisTPq';c='thisNPq';b='thisVPq';a='thisPPq';R='ladder_level_num';Q='ladder_level';L='balance';K='%Y%m%d';J='day';H='dayElePq'
if B.need_login is True:LOGGER.error('国家电网需要重新登录!');return
o=setup or int(time.time()*1000)-B.timestamp>B.refresh_interval*3600*1000
if o is _s:return
@@ -282,50 +282,50 @@ class StateGridDataClient:
else:B.need_login=True;LOGGER.error('刷新 Token 失败');return
D=await B.__get_door_number()
if _B in D and D[_B]!=0:B.need_login=True;LOGGER.error('重新请求失败');return
M=datetime.datetime.now();J=M-datetime.timedelta(days=1);p=f"{J.year}-{J.month:02d}-{J.day:02d}";N=J-datetime.timedelta(days=40);q=f"{N.year}-{N.month:02d}-{N.day:02d}"
M=datetime.datetime.now();I=M-datetime.timedelta(days=1);p=f"{I.year}-{I.month:02d}-{I.day:02d}";N=I-datetime.timedelta(days=40);q=f"{N.year}-{N.month:02d}-{N.day:02d}"
for A in B.powerUserList:
r=A[_m];B.doorAccountDict[r]=A;await B.__get_door_balance(A);await B.__get_door_daily_bill(A,M.year,q,p);O=A[_V][0]
try:float(O[H])
except:A[_V].pop(0)
O=A[_V][0];E=datetime.datetime.strptime(O[K],L);V=0;W=0;X=0;Y=0;Z=0
O=A[_V][0];E=datetime.datetime.strptime(O[J],K);S=0;T=0;U=0;V=0;W=0
for C in A[_V]:
G=datetime.datetime.strptime(C[K],L)
G=datetime.datetime.strptime(C[J],K)
if G.month!=E.month:break
V+=catchFloat(C,H);W+=catchFloat(C,d);X+=catchFloat(C,e);Y+=catchFloat(C,f);Z+=catchFloat(C,g)
P=E-datetime.timedelta(days=E.day);a=f"{P.year}-{P.month:02d}"
if _AI not in A or A[_AI]!=a:await B.__get_door_bill(A,P.year);await B.__get_door_ladder(A,a)
S+=catchFloat(C,H);T+=catchFloat(C,a);U+=catchFloat(C,b);V+=catchFloat(C,c);W+=catchFloat(C,d)
P=E-datetime.timedelta(days=E.day);X=f"{P.year}-{P.month:02d}"
if _AI not in A or A[_AI]!=X:await B.__get_door_bill(A,P.year);await B.__get_door_ladder(A,X)
s=datetime.datetime.strptime(A[_AI],'%Y%m')
if s.month==12:
F=0
for C in A[_V]:
G=datetime.datetime.strptime(C[K],L)
G=datetime.datetime.strptime(C[J],K)
if G.month!=12:break
F+=catchFloat(C,H)
else:
F=0
for C in A[_A4]:F+=catchFloat(C,h)
for C in A[_A4]:F+=catchFloat(C,e)
t=len(A[_A4])
for C in A[_V]:
G=datetime.datetime.strptime(C[K],L)
G=datetime.datetime.strptime(C[J],K)
if G.month<=t:break
F+=catchFloat(C,H)
if F<=2760:A[Q]='第一阶梯';A[R]=1
elif F<=4800:A[Q]='第二阶梯';A[R]=2
else:A[Q]='第三阶梯';A[R]=3
if _A3 in A:
u=catchFloat(A[_A3],'estiAmt');b=catchFloat(A[_A3],'prepayBal');v=catchFloat(A[_A3],'sumMoney')
if b==0:A[I]=v
else:A[I]=b-u
c=catchFloat(A[_A3],'historyOwe')
if c>0:A[I]=-c
elif I not in A:A[I]=0
if _AH in A:A[S]=catchFloat(A[_AH],'totalEleNum');A[i]=catchFloat(A[_AH],'totalEleCost')
elif S not in A:A[S]=0;A[i]=0
if _A4 in A:A[T]=catchFloat(A[_A4][-1],h);A[j]=catchFloat(A[_A4][-1],'monthEleCost')
elif T not in A:A[T]=0;A[j]=0
if _V in A:A[U]=catchFloat(A[_V][0],H);A[k]=catchFloat(A[_V][0],d);A[l]=catchFloat(A[_V][0],e);A[m]=catchFloat(A[_V][0],f);A[n]=catchFloat(A[_V][0],g)
elif U not in A:A[U]=0;A[k]=0;A[l]=0;A[m]=0;A[n]=0
A['month_ele_num']=V;A['month_p_ele_num']=W;A['month_v_ele_num']=X;A['month_n_ele_num']=Y;A['month_t_ele_num']=Z;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')
u=catchFloat(A[_A3],'estiAmt');Y=catchFloat(A[_A3],'prepayBal');v=catchFloat(A[_A3],'sumMoney')
if Y==0:A[L]=v
else:A[L]=Y-u
Z=catchFloat(A[_A3],'historyOwe')
if Z>0:A[L]=-Z
else:A[L]=0
if _AH in A:A[f]=catchFloat(A[_AH],'totalEleNum');A[g]=catchFloat(A[_AH],'totalEleCost')
else:A[f]=0;A[g]=0
if _A4 in A:A[h]=catchFloat(A[_A4][-1],e);A[i]=catchFloat(A[_A4][-1],'monthEleCost')
else:A[h]=0;A[i]=0
if _V in A:A[j]=catchFloat(A[_V][0],H);A[k]=catchFloat(A[_V][0],a);A[l]=catchFloat(A[_V][0],b);A[m]=catchFloat(A[_V][0],c);A[n]=catchFloat(A[_V][0],d)
else:A[j]=0;A[k]=0;A[l]=0;A[m]=0;A[n]=0
A['month_ele_num']=S;A['month_p_ele_num']=T;A['month_v_ele_num']=U;A['month_n_ele_num']=V;A['month_t_ele_num']=W;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 B.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://github.com/hass-box/state-grid",
"iot_class": "cloud_polling",
"version": "0.0.6"
"version": "0.0.5"
}
+135 -141
View File
@@ -2,11 +2,13 @@ from homeassistant.components.sensor import (
DOMAIN as SENSOR_DOMAIN,
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfEnergy
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
@@ -20,128 +22,128 @@ CURRENCY_YUAN = "元"
ENTITY_ID_SENSOR_FORMAT = SENSOR_DOMAIN + ".state_grid_"
SENSOR_TYPES = [
{
"key":"balance",
"name": "账户余额",
"native_unit_of_measurement": CURRENCY_YUAN,
"device_class": SensorDeviceClass.MONETARY,
"state_class": SensorStateClass.TOTAL
},
{
"key": "year_ele_num",
"name": "年度累计用电",
"native_unit_of_measurement": UnitOfEnergy.KILO_WATT_HOUR,
"device_class": SensorDeviceClass.ENERGY,
"state_class": SensorStateClass.TOTAL
},
{
"key": "year_ele_cost",
"name": "年度累计电费",
"native_unit_of_measurement": CURRENCY_YUAN,
"device_class": SensorDeviceClass.MONETARY,
"state_class": SensorStateClass.TOTAL
},
{
"key": "last_month_ele_num",
"name": "上个月用电",
"native_unit_of_measurement": UnitOfEnergy.KILO_WATT_HOUR,
"device_class": SensorDeviceClass.ENERGY,
"state_class": SensorStateClass.TOTAL
},
{
"key": "last_month_ele_cost",
"name": "上个月电费",
"native_unit_of_measurement": CURRENCY_YUAN,
"device_class": SensorDeviceClass.MONETARY,
"state_class": SensorStateClass.TOTAL
},
{
"key": "month_ele_num",
"name": "当月累计用电",
"native_unit_of_measurement": UnitOfEnergy.KILO_WATT_HOUR,
"device_class": SensorDeviceClass.ENERGY,
"state_class": SensorStateClass.TOTAL
},
{
"key": "month_p_ele_num",
"name": "当月累计峰用电",
"native_unit_of_measurement": UnitOfEnergy.KILO_WATT_HOUR,
"device_class": SensorDeviceClass.ENERGY,
"state_class": SensorStateClass.TOTAL
},
{
"key": "month_v_ele_num",
"name": "当月累计谷用电",
"native_unit_of_measurement": UnitOfEnergy.KILO_WATT_HOUR,
"device_class": SensorDeviceClass.ENERGY,
"state_class": SensorStateClass.TOTAL
},
{
"key": "month_n_ele_num",
"name": "当月累计平用电",
"native_unit_of_measurement": UnitOfEnergy.KILO_WATT_HOUR,
"device_class": SensorDeviceClass.ENERGY,
"state_class": SensorStateClass.TOTAL
},
{
"key": "month_t_ele_num",
"name": "当月累计尖用电",
"native_unit_of_measurement": UnitOfEnergy.KILO_WATT_HOUR,
"device_class": SensorDeviceClass.ENERGY,
"state_class": SensorStateClass.TOTAL
},
{
"key": "daily_ele_num",
"name": "日总用电",
"native_unit_of_measurement": UnitOfEnergy.KILO_WATT_HOUR,
"device_class": SensorDeviceClass.ENERGY,
"state_class": SensorStateClass.TOTAL
},
{
"key": "daily_p_ele_num",
"name": "日峰用电",
"native_unit_of_measurement": UnitOfEnergy.KILO_WATT_HOUR,
"device_class": SensorDeviceClass.ENERGY,
"state_class": SensorStateClass.TOTAL
},
{
"key": "daily_v_ele_num",
"name": "日谷用电",
"native_unit_of_measurement": UnitOfEnergy.KILO_WATT_HOUR,
"device_class": SensorDeviceClass.ENERGY,
"state_class": SensorStateClass.TOTAL
},
{
"key": "daily_n_ele_num",
"name": "日平用电",
"native_unit_of_measurement": UnitOfEnergy.KILO_WATT_HOUR,
"device_class": SensorDeviceClass.ENERGY,
"state_class": SensorStateClass.TOTAL
},
{
"key": "daily_t_ele_num",
"name": "日尖用电",
"native_unit_of_measurement": UnitOfEnergy.KILO_WATT_HOUR,
"device_class": SensorDeviceClass.ENERGY,
"state_class": SensorStateClass.TOTAL
},
{
"key": "daily_lasted_date",
"name": "最新日用电日期"
},
{
"key": "refresh_time",
"name": "最近刷新时间"
}
]
SENSOR_TYPES = (
SensorEntityDescription(
key="balance",
translation_key="balance",
native_unit_of_measurement=CURRENCY_YUAN,
device_class=SensorDeviceClass.MONETARY,
state_class=SensorStateClass.TOTAL
),
SensorEntityDescription(
key="year_ele_num",
translation_key="year_ele_num",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL
),
SensorEntityDescription(
key="year_ele_cost",
translation_key="year_ele_cost",
native_unit_of_measurement=CURRENCY_YUAN,
device_class=SensorDeviceClass.MONETARY,
state_class=SensorStateClass.TOTAL
),
SensorEntityDescription(
key="last_month_ele_num",
translation_key="last_month_ele_num",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL
),
SensorEntityDescription(
key="last_month_ele_cost",
translation_key="last_month_ele_cost",
native_unit_of_measurement=CURRENCY_YUAN,
device_class=SensorDeviceClass.MONETARY,
state_class=SensorStateClass.TOTAL
),
SensorEntityDescription(
key="month_ele_num",
translation_key="month_ele_num",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL
),
SensorEntityDescription(
key="month_p_ele_num",
translation_key="month_p_ele_num",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL
),
SensorEntityDescription(
key="month_v_ele_num",
translation_key="month_v_ele_num",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL
),
SensorEntityDescription(
key="month_n_ele_num",
translation_key="month_n_ele_num",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL
),
SensorEntityDescription(
key="month_t_ele_num",
translation_key="month_t_ele_num",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL
),
SensorEntityDescription(
key="daily_ele_num",
translation_key="daily_ele_num",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL
),
SensorEntityDescription(
key="daily_p_ele_num",
translation_key="daily_p_ele_num",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL
),
SensorEntityDescription(
key="daily_v_ele_num",
translation_key="daily_v_ele_num",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL
),
SensorEntityDescription(
key="daily_n_ele_num",
translation_key="daily_n_ele_num",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL
),
SensorEntityDescription(
key="daily_t_ele_num",
translation_key="daily_t_ele_num",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL
),
SensorEntityDescription(
key="daily_lasted_date",
translation_key="daily_lasted_date"
),
SensorEntityDescription(
key="refresh_time",
translation_key="refresh_time"
)
)
SENSOR_TYPES_FOR_LADDER = [
{
"key": "ladder_level",
"name": "当前阶梯"
},
]
SENSOR_TYPES_FOR_LADDER = (
SensorEntityDescription(
key="ladder_level",
translation_key="ladder_level"
),
)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
@@ -152,47 +154,39 @@ async def async_setup_entry(
door_account_list = await data_client.get_door_account_list()
for door_account in door_account_list:
async_add_entities(
[StateGridSensor(door_account, sensor_type, entry.entry_id, coordinator) for sensor_type in SENSOR_TYPES]
[StateGridSensor(door_account, description, entry.entry_id, coordinator) for description in SENSOR_TYPES]
)
# if door_account["ladder_flag"] == 1:
# async_add_entities(
# StateGridSensor(door_account, sensor_type, entry.entry_id)
# for sensor_type in SENSOR_TYPES_FOR_LADDER
# StateGridSensor(door_account, description, entry.entry_id)
# for description in SENSOR_TYPES_FOR_LADDER
# )
class StateGridSensor(CoordinatorEntity[StateGridCoordinator], SensorEntity):
_attr_has_entity_name = True
entity_description: SensorEntityDescription
def __init__(
self, door_account, sensor_type, entry_id: str, coordinator: StateGridCoordinator,
self, door_account, entity_description: SensorEntityDescription, entry_id: str, coordinator: StateGridCoordinator,
) -> None:
super().__init__(coordinator)
self.entity_description = entity_description
self.door_account = door_account
self.sensor_type = sensor_type
self.entity_id = SENSOR_DOMAIN + ".state_grid" + "_" + door_account["consNo_dst"] + "_" + sensor_type["key"]
self._attr_name = sensor_type["name"]
self._attr_unique_id = entry_id + "-" + door_account["consNo_dst"] + "-" + sensor_type["key"]
if "device_class" in sensor_type:
self._attr_device_class = sensor_type["device_class"]
if "state_class" in sensor_type:
self._attr_state_class = sensor_type["state_class"]
if "native_unit_of_measurement" in sensor_type:
self._attr_native_unit_of_measurement = sensor_type["native_unit_of_measurement"]
self.entity_id = SENSOR_DOMAIN + ".state_grid" + "_" + door_account["consNo_dst"] + "_" + entity_description.key
self._attr_translation_key = entity_description.key
self._attr_unique_id = entry_id + "-" + door_account["consNo_dst"] + "-" + entity_description.key
self._attr_device_info = {
"name": door_account["elecAddr_dst"],
"identifiers": {(DOMAIN, door_account["consNo_dst"])},
"sw_version": VERSION,
"manufacturer": "HassBox",
"model": "户号:" + door_account["consName_dst"] + " - " + door_account["consNo_dst"]
"model": "户号:" + door_account["consName_dst"] + " - " + door_account["consNo_dst"],
"entry_type": DeviceEntryType.SERVICE,
}
@property
def native_value(self):
data = self.coordinator.data[self.door_account["consNo_dst"]]
return data[self.sensor_type["key"]]
return data[self.entity_description.key]