From b9d87cb14da6c41dc5bfa8b118bd55a26c412a5a Mon Sep 17 00:00:00 2001 From: Yordan Suarez Date: Sat, 4 Jul 2020 03:10:31 -0400 Subject: [PATCH] Some stability changes --- custom_components/fpl/fplapi.py | 21 +++++++++------- custom_components/fpl/sensor.py | 43 ++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/custom_components/fpl/fplapi.py b/custom_components/fpl/fplapi.py index c70ee4e..44b9810 100644 --- a/custom_components/fpl/fplapi.py +++ b/custom_components/fpl/fplapi.py @@ -119,7 +119,6 @@ class FplApi(object): for program in programsData: if "enrollmentStatus" in program.keys(): key = program["name"] - _LOGGER.info(f"{key} : {program['enrollmentStatus']}") programs[key] = program["enrollmentStatus"] == ENROLLED if programs["BBL"]: @@ -163,6 +162,7 @@ class FplApi(object): return [] async def getBBL_async(self, account): + _LOGGER.info(f"Getting budget billing data") URL = "https://www.fpl.com/api/resources/account/{account}/budgetBillingGraph" async with async_timeout.timeout(TIMEOUT, loop=self._loop): @@ -178,6 +178,7 @@ class FplApi(object): return [] async def getDataFromEnergyService(self, account, premise, lastBilledDate): + _LOGGER.info(f"Getting data from energy service") URL = "https://www.fpl.com/dashboard-api/resources/account/{account}/energyService/{account}" date = str(lastBilledDate.strftime("%m%d%Y")) @@ -205,20 +206,22 @@ class FplApi(object): dailyUsage = [] for daily in r["DailyUsage"]["data"]: - dailyUsage.append( - { - "usage": daily["kwhUsed"], - "cost": daily["billingCharge"], - "date": daily["date"], - "max_temperature": daily["averageHighTemperature"], - } - ) + if "kwhUsed" in daily.keys(): + dailyUsage.append( + { + "usage": daily["kwhUsed"], + "cost": daily["billingCharge"], + "date": daily["date"], + "max_temperature": daily["averageHighTemperature"], + } + ) return {"daily_usage": dailyUsage} return [] async def getDataFromApplianceUsage(self, account, lastBilledDate): + _LOGGER.info(f"Getting data from applicance usage") URL = "https://www.fpl.com/dashboard-api/resources/account/{account}/applianceUsage/{account}" JSON = {"startDate": str(lastBilledDate.strftime("%m%d%Y"))} diff --git a/custom_components/fpl/sensor.py b/custom_components/fpl/sensor.py index 3abd07f..4267cdf 100644 --- a/custom_components/fpl/sensor.py +++ b/custom_components/fpl/sensor.py @@ -46,9 +46,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities): fpl_accounts.append(FplSensor(hass, config_entry.data, account)) async_add_entities(fpl_accounts) - except Exception as e: # pylint: disable=broad-except - _LOGGER.error(f"Adding fpl accounts: {e}") + _LOGGER.error(f"Adding fpl accounts: {str(e)}") async_call_later( hass, 15, async_setup_entry(hass, config_entry, async_add_entities) ) @@ -82,11 +81,23 @@ class FplSensor(Entity): @property def state(self): - return self._state # + state = self._state + data = self._data - @property - def unit_of_measurement(self): - return " " + 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"] + else: + if "projected_bill" in data.keys(): + state = data["projected_bill"] + + return state + + # @property + # def unit_of_measurement(self): + # return "$" @property def icon(self): @@ -98,18 +109,16 @@ class FplSensor(Entity): @util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_UPDATES) async def async_update(self): - self._state = STATE_UNKNOWN - self._data = None - session = aiohttp.ClientSession() try: + session = aiohttp.ClientSession() api = FplApi(self.username, self.password, self.loop, session) await api.login() - self._data = await api.async_get_data(self._account) - if self._data["budget_bill"]: - self._state = self._data["projected_budget_bill"] - else: - self._state = self._data["projected_bill"] - except Exception: # pylint: disable=broad-except - pass + data = await api.async_get_data(self._account) + if data != {}: + self._data = data - await session.close() + except Exception as e: # pylint: disable=broad-except + _LOGGER.warning(f"Error ocurred during update: { str(e)}") + + finally: + await session.close()