diff --git a/custom_components/fpl/fplapi.py b/custom_components/fpl/fplapi.py index 040619f..7801954 100644 --- a/custom_components/fpl/fplapi.py +++ b/custom_components/fpl/fplapi.py @@ -124,7 +124,7 @@ class FplApi(object): if programs["BBL"]: # budget billing data["budget_bill"] = True - bblData = await self.getBBL_async(account) + bblData = await self.getBBL_async(account, data) data.update(bblData) data.update( @@ -161,21 +161,50 @@ class FplApi(object): return [] - async def getBBL_async(self, account): + async def getBBL_async(self, account, projectedBillData): _LOGGER.info(f"Getting budget billing data") + data = {} + + URL = "https://www.fpl.com/api/resources/account/{account}/budgetBillingGraph/premiseDetails" + async with async_timeout.timeout(TIMEOUT, loop=self._loop): + response = await self._session.get(URL.format(account=account)) + if response.status == 200: + r = (await response.json())["data"] + dataList = r["graphData"] + + startIndex = len(dataList) - 1 + + billingCharge = 0 + budgetBillDeferBalance = r["defAmt"] + + projectedBill = projectedBillData["projected_bill"] + asOfDays = projectedBillData["as_of_days"] + + for det in dataList: + billingCharge += det["actuallBillAmt"] + + calc1 = (projectedBill + billingCharge) / 12 + calc2 = (1 / 12) * (budgetBillDeferBalance) + + projectedBudgetBill = round(calc1 + calc2, 2) + bbDailyAvg = round(projectedBudgetBill / 30, 2) + bbAsOfDateAmt = round(projectedBudgetBill / 30 * asOfDays, 2) + + data["budget_billing_daily_avg"] = bbDailyAvg + data["budget_billing_bill_to_date"] = bbAsOfDateAmt + + data["budget_billing_projected_bill"] = float(projectedBudgetBill) + URL = "https://www.fpl.com/api/resources/account/{account}/budgetBillingGraph" async with async_timeout.timeout(TIMEOUT, loop=self._loop): response = await self._session.get(URL.format(account=account)) if response.status == 200: r = (await response.json())["data"] - data = {} - data["projected_budget_bill"] = float(r["bbAmt"]) data["bill_to_date"] = float(r["eleAmt"]) data["defered_amount"] = float(r["defAmt"]) - return data - return [] + return data async def getDataFromEnergyService(self, account, premise, lastBilledDate): _LOGGER.info(f"Getting data from energy service") diff --git a/custom_components/fpl/sensor.py b/custom_components/fpl/sensor.py index 4267cdf..63e35f4 100644 --- a/custom_components/fpl/sensor.py +++ b/custom_components/fpl/sensor.py @@ -87,8 +87,8 @@ class FplSensor(Entity): if type(data) is dict: if "budget_bill" in data.keys(): if data["budget_bill"]: - if "projected_budget_bill" in data.keys(): - state = data["projected_budget_bill"] + if "budget_billing_projected_bill" in data.keys(): + state = data["budget_billing_projected_bill"] else: if "projected_bill" in data.keys(): state = data["projected_bill"]