diff --git a/custom_components/fpl/FplMainRegionApiClient.py b/custom_components/fpl/FplMainRegionApiClient.py index 94d6de9..a6d2250 100644 --- a/custom_components/fpl/FplMainRegionApiClient.py +++ b/custom_components/fpl/FplMainRegionApiClient.py @@ -107,7 +107,7 @@ class FplMainRegionApiClient: try: async with async_timeout.timeout(TIMEOUT): await self.session.get(URL_LOGOUT) - except Exception: + except: pass async def update(self, account) -> dict: @@ -216,7 +216,7 @@ class FplMainRegionApiClient: data["daily_avg"] = dailyAvg data["avg_high_temp"] = avgHighTemp - except Exception: + except: pass return data @@ -225,6 +225,7 @@ class FplMainRegionApiClient: """Get budget billing data""" _LOGGER.info("Getting budget billing data") data = {} + try: async with async_timeout.timeout(TIMEOUT): response = await self.session.get( @@ -257,10 +258,6 @@ class FplMainRegionApiClient: data["budget_billing_projected_bill"] = float(projectedBudgetBill) - except Exception as e: - _LOGGER.error("Error getting BBL: %s", e) - - try: async with async_timeout.timeout(TIMEOUT): response = await self.session.get( URL_BUDGET_BILLING_GRAPH.format(account=account) @@ -269,9 +266,8 @@ class FplMainRegionApiClient: r = (await response.json())["data"] data["bill_to_date"] = float(r["eleAmt"]) data["defered_amount"] = float(r["defAmt"]) - - except Exception as e: - _LOGGER.error("Error getting BBL: %s", e) + except: + pass return data @@ -351,8 +347,8 @@ class FplMainRegionApiClient: r["CurrentUsage"]["dailyAverageKWH"] ) data["billToDateKWH"] = float(r["CurrentUsage"]["billToDateKWH"]) - data["recMtrReading"] = int(r["CurrentUsage"]["recMtrReading"]) - data["delMtrReading"] = int(r["CurrentUsage"]["delMtrReading"]) + data["recMtrReading"] = int(r["CurrentUsage"]["recMtrReading"] or 0) + data["delMtrReading"] = int(r["CurrentUsage"]["delMtrReading"] or 0) data["billStartDate"] = r["CurrentUsage"]["billStartDate"] except: pass @@ -365,6 +361,7 @@ class FplMainRegionApiClient: JSON = {"startDate": str(lastBilledDate.strftime("%m%d%Y"))} data = {} + try: async with async_timeout.timeout(TIMEOUT): response = await self.session.post( @@ -381,8 +378,7 @@ class FplMainRegionApiClient: else: rr = full data[e["category"].replace(" ", "_")] = rr - - except Exception: + except: pass return {"energy_percent_by_applicance": data} diff --git a/custom_components/fpl/FplNorthwestRegionApiClient.py b/custom_components/fpl/FplNorthwestRegionApiClient.py index 6f449ca..9a3a4ec 100644 --- a/custom_components/fpl/FplNorthwestRegionApiClient.py +++ b/custom_components/fpl/FplNorthwestRegionApiClient.py @@ -1,5 +1,5 @@ """FPL Northwest data collection api client""" -from datetime import datetime +from datetime import datetime, timedelta import logging import async_timeout import boto3 @@ -143,25 +143,31 @@ class FplNorthwestRegionApiClient: programInfo = accountSumary["programInfo"] result["budget_bill"] = False - result["bill_to_date"] = billAndMetterInfo["asOfDateAmount"] - result["projected_bill"] = billAndMetterInfo["projBillAmount"] - result["projectedKWH"] = billAndMetterInfo["projBillKWH"] + result["projected_bill"] = float(billAndMetterInfo["projBillAmount"] or 0) + result["projectedKWH"] = int(billAndMetterInfo["projBillKWH"] or 0) - result["bill_to_date"] = billAndMetterInfo["asOfDateUsage"] - result["billToDateKWH"] = billAndMetterInfo["asOfDateUsage"] + result["bill_to_date"] = float(billAndMetterInfo["asOfDateAmount"] or 0) + result["billToDateKWH"] = int(billAndMetterInfo["asOfDateUsage"] or 0) - result["daily_avg"] = billAndMetterInfo["dailyAvgAmount"] - result["dailyAverageKWH"] = billAndMetterInfo["dailyAvgKwh"] + result["daily_avg"] = float(billAndMetterInfo["dailyAvgAmount"] or 0) + result["dailyAverageKWH"] = int(billAndMetterInfo["dailyAvgKwh"] or 0) - result["billStartDate"] = programInfo["currentBillDate"] - result["next_bill_date"] = programInfo["nextBillDate"] + start = datetime.fromisoformat(programInfo["currentBillDate"]) + # + timedelta(days=1) - start = datetime.fromisoformat(result["billStartDate"]) - end = datetime.fromisoformat(result["next_bill_date"]) + end = datetime.fromisoformat(programInfo["nextBillDate"]) today = datetime.fromisoformat(data["today"]) - result["service_days"] = (end - start).days - result["as_of_days"] = (today - start).days + # result["billStartDate"] = programInfo["currentBillDate"] + result["current_bill_date"] = start.strftime("%Y-%m-%d") + result["next_bill_date"] = programInfo["nextBillDate"] + + service_days = (end - start).days + as_of_days = (today - start).days + + result["service_days"] = service_days + result["as_of_days"] = as_of_days + result["remaining_days"] = service_days - as_of_days return result