Some stability changes

This commit is contained in:
Yordan Suarez
2020-07-04 03:10:31 -04:00
parent 6446df667b
commit b9d87cb14d
2 changed files with 38 additions and 26 deletions

View File

@@ -119,7 +119,6 @@ class FplApi(object):
for program in programsData: for program in programsData:
if "enrollmentStatus" in program.keys(): if "enrollmentStatus" in program.keys():
key = program["name"] key = program["name"]
_LOGGER.info(f"{key} : {program['enrollmentStatus']}")
programs[key] = program["enrollmentStatus"] == ENROLLED programs[key] = program["enrollmentStatus"] == ENROLLED
if programs["BBL"]: if programs["BBL"]:
@@ -163,6 +162,7 @@ class FplApi(object):
return [] return []
async def getBBL_async(self, account): async def getBBL_async(self, account):
_LOGGER.info(f"Getting budget billing data")
URL = "https://www.fpl.com/api/resources/account/{account}/budgetBillingGraph" URL = "https://www.fpl.com/api/resources/account/{account}/budgetBillingGraph"
async with async_timeout.timeout(TIMEOUT, loop=self._loop): async with async_timeout.timeout(TIMEOUT, loop=self._loop):
@@ -178,6 +178,7 @@ class FplApi(object):
return [] return []
async def getDataFromEnergyService(self, account, premise, lastBilledDate): 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}" URL = "https://www.fpl.com/dashboard-api/resources/account/{account}/energyService/{account}"
date = str(lastBilledDate.strftime("%m%d%Y")) date = str(lastBilledDate.strftime("%m%d%Y"))
@@ -205,6 +206,7 @@ class FplApi(object):
dailyUsage = [] dailyUsage = []
for daily in r["DailyUsage"]["data"]: for daily in r["DailyUsage"]["data"]:
if "kwhUsed" in daily.keys():
dailyUsage.append( dailyUsage.append(
{ {
"usage": daily["kwhUsed"], "usage": daily["kwhUsed"],
@@ -219,6 +221,7 @@ class FplApi(object):
return [] return []
async def getDataFromApplianceUsage(self, account, lastBilledDate): 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}" URL = "https://www.fpl.com/dashboard-api/resources/account/{account}/applianceUsage/{account}"
JSON = {"startDate": str(lastBilledDate.strftime("%m%d%Y"))} JSON = {"startDate": str(lastBilledDate.strftime("%m%d%Y"))}

View File

@@ -46,9 +46,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
fpl_accounts.append(FplSensor(hass, config_entry.data, account)) fpl_accounts.append(FplSensor(hass, config_entry.data, account))
async_add_entities(fpl_accounts) async_add_entities(fpl_accounts)
except Exception as e: # pylint: disable=broad-except 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( async_call_later(
hass, 15, async_setup_entry(hass, config_entry, async_add_entities) hass, 15, async_setup_entry(hass, config_entry, async_add_entities)
) )
@@ -82,11 +81,23 @@ class FplSensor(Entity):
@property @property
def state(self): def state(self):
return self._state # state = self._state
data = self._data
@property if type(data) is dict:
def unit_of_measurement(self): if "budget_bill" in data.keys():
return " " 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 @property
def icon(self): def icon(self):
@@ -98,18 +109,16 @@ class FplSensor(Entity):
@util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_UPDATES) @util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_UPDATES)
async def async_update(self): async def async_update(self):
self._state = STATE_UNKNOWN
self._data = None
session = aiohttp.ClientSession()
try: try:
session = aiohttp.ClientSession()
api = FplApi(self.username, self.password, self.loop, session) api = FplApi(self.username, self.password, self.loop, session)
await api.login() await api.login()
self._data = await api.async_get_data(self._account) data = await api.async_get_data(self._account)
if self._data["budget_bill"]: if data != {}:
self._state = self._data["projected_budget_bill"] self._data = data
else:
self._state = self._data["projected_bill"]
except Exception: # pylint: disable=broad-except
pass
except Exception as e: # pylint: disable=broad-except
_LOGGER.warning(f"Error ocurred during update: { str(e)}")
finally:
await session.close() await session.close()