Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
Signed-off-by: CubicPill <CubicPill@users.noreply.github.com>
  • Loading branch information
CubicPill committed May 22, 2023
1 parent 4d71e7b commit 987dd8d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"pycryptodome==3.15.0"
],
"ssdp": [],
"version": "1.1.2-beta",
"version": "1.1.2-beta.2",
"zeroconf": []
}
58 changes: 35 additions & 23 deletions custom_components/china_southern_power_grid_stat/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from homeassistant.const import (
CONF_USERNAME,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
UnitOfEnergy,
)
from homeassistant.core import HomeAssistant, callback
Expand Down Expand Up @@ -549,22 +548,20 @@ async def _async_update_last_year_stats(self, account: CSGElectricityAccount):

@staticmethod
def merge_by_day_data(
by_day_from_cost: list | str | None,
kwh_from_cost: float | str | None,
by_day_from_cost: list | str,
kwh_from_cost: float | str,
by_day_from_usage: list | str,
kwh_from_usage: float | str,
) -> (list | str, float | str):
"""Merge by_day_from_usage and by_day_from_cost data"""
# merge by_day
# determine which is the latest
# by_day_from_cost could be in [STATE_UNAVAILABLE, None]
# STATE_UNAVAILABLE is request failure and None is no data
if (
by_day_from_cost in [STATE_UNAVAILABLE, None]
by_day_from_cost == STATE_UNAVAILABLE
and by_day_from_usage == STATE_UNAVAILABLE
):
by_day = STATE_UNAVAILABLE
elif by_day_from_cost in [STATE_UNAVAILABLE, None]:
elif by_day_from_cost == STATE_UNAVAILABLE:
by_day = by_day_from_usage
elif by_day_from_usage == STATE_UNAVAILABLE:
by_day = by_day_from_cost
Expand All @@ -581,15 +578,12 @@ def merge_by_day_data(
by_day[idx][WF_ATTR_CHARGE] = item[WF_ATTR_CHARGE]

# determine which one to use as kwh
if (
kwh_from_cost in [STATE_UNAVAILABLE, None]
and kwh_from_usage == STATE_UNAVAILABLE
):
if kwh_from_cost == STATE_UNAVAILABLE and kwh_from_usage == STATE_UNAVAILABLE:
kwh = STATE_UNAVAILABLE
elif kwh_from_cost in [STATE_UNAVAILABLE, None]:
elif kwh_from_cost == STATE_UNAVAILABLE:
kwh = kwh_from_usage
elif kwh_from_usage == STATE_UNAVAILABLE:
kwh = kwh_from_cost if kwh_from_cost is not None else STATE_UNAVAILABLE
kwh = kwh_from_cost
else:
# determine which kwh is the latest
# get the larger one
Expand Down Expand Up @@ -629,6 +623,11 @@ async def _async_update_this_month_stats_and_ladder(
ladder,
this_month_by_day_from_cost,
) = result_cost
# special processing
if this_month_cost is None:
this_month_cost = STATE_UNAVAILABLE
if this_month_kwh_from_cost is None:
this_month_kwh_from_cost = STATE_UNAVAILABLE
ladder_stage = (
ladder[WF_ATTR_LADDER]
if ladder[WF_ATTR_LADDER] is not None
Expand Down Expand Up @@ -674,10 +673,6 @@ async def _async_update_this_month_stats_and_ladder(
kwh_from_cost=this_month_kwh_from_cost,
)

this_month_cost = (
this_month_cost if this_month_cost is not None else STATE_UNAVAILABLE
)

if this_month_by_day == STATE_UNAVAILABLE:
# need last month's data to update `latest_day` entity
self._if_update_last_month = True
Expand Down Expand Up @@ -759,14 +754,23 @@ async def _async_update_last_month_stats(self, account: CSGElectricityAccount):
last_month_cost,
last_month_kwh_from_cost,
_, # ladder is discarded
this_month_by_day_from_cost,
last_month_by_day_from_cost,
) = result_cost

# for last month, it's safe to calculate total kwh from cost
if not last_month_cost:
last_month_cost = sum(
d[WF_ATTR_CHARGE] for d in last_month_by_day_from_cost
)
if not last_month_kwh_from_cost:
last_month_kwh_from_cost = sum(
d[WF_ATTR_KWH] for d in last_month_by_day_from_cost
)
else:
(
last_month_cost,
last_month_kwh_from_cost,
this_month_by_day_from_cost,
last_month_by_day_from_cost,
) = (
STATE_UNAVAILABLE,
STATE_UNAVAILABLE,
Expand All @@ -775,7 +779,7 @@ async def _async_update_last_month_stats(self, account: CSGElectricityAccount):
last_month_by_day, last_month_kwh = self.merge_by_day_data(
by_day_from_usage=last_month_by_day_from_usage,
kwh_from_usage=last_month_kwh_from_usage,
by_day_from_cost=this_month_by_day_from_cost,
by_day_from_cost=last_month_by_day_from_cost,
kwh_from_cost=last_month_kwh_from_cost,
)

Expand Down Expand Up @@ -809,7 +813,7 @@ def _update_latest_day(self, account: CSGElectricityAccount):
# we have this month's data, use the latest day
latest_day_kwh = this_month_by_day[-1][WF_ATTR_KWH]
latest_day_cost = (
this_month_by_day[-1].get(WF_ATTR_CHARGE) or STATE_UNKNOWN
this_month_by_day[-1].get(WF_ATTR_CHARGE) or STATE_UNAVAILABLE
)
latest_day_date = this_month_by_day[-1][WF_ATTR_DATE]
else:
Expand All @@ -824,7 +828,7 @@ def _update_latest_day(self, account: CSGElectricityAccount):
and len(last_month_by_day) >= 1
):
latest_day_kwh = last_month_by_day[-1][WF_ATTR_KWH]
latest_day_cost = STATE_UNKNOWN
latest_day_cost = STATE_UNAVAILABLE
latest_day_date = last_month_by_day[-1][WF_ATTR_DATE]
else:
_LOGGER.error(
Expand Down Expand Up @@ -924,7 +928,15 @@ async def _async_update_account_data(self, account: CSGElectricityAccount):
self._async_update_last_month_stats(account),
return_exceptions=True,
)
self._update_latest_day(account)
try:
self._update_latest_day(account)
except Exception as exc: # pylint: disable=broad-except
_LOGGER.error(
"Ele account %s, update latest day data failed: %s",
account.account_number,
exc,
)

_LOGGER.debug(
"Ele account %s, update took %s seconds",
account.account_number,
Expand Down

0 comments on commit 987dd8d

Please sign in to comment.