bunch of changes

This commit is contained in:
Yordan Suarez
2019-12-30 12:56:11 -05:00
parent 48e945c6a5
commit d11df67c18
8 changed files with 322 additions and 76 deletions

View File

@@ -7,7 +7,7 @@ from homeassistant.helpers.entity import Entity
from homeassistant import util
from homeassistant.const import CONF_NAME, EVENT_CORE_CONFIG_UPDATE
from .const import DOMAIN, ICON
from .const import DOMAIN, ICON, LOGIN_RESULT_OK
MIN_TIME_BETWEEN_SCANS = timedelta(minutes=1440)
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1440)
@@ -24,6 +24,24 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
async def async_setup_entry(hass, config_entry, async_add_entities):
async_add_entities([FplSensor(hass, config_entry.data)])
print("setup entry")
username = config_entry.data.get(const.CONF_USERNAME)
password = config_entry.data.get(const.CONF_PASSWORD)
session = aiohttp.ClientSession()
try:
api = FplApi(username, password, True, hass.loop, session)
result = await api.login()
if result == LOGIN_RESULT_OK:
await api.async_get_headers()
pass
except Exception: # pylint: disable=broad-except
pass
await session.close()
class FplSensor(Entity):
@@ -69,18 +87,22 @@ class FplSensor(Entity):
# "yesterday_kwh": self.api.yesterday_kwh,
# "yesterday_dollars": self.api.yesterday_dollars.replace("$", ""),
"mtd_kwh": self.api.mtd_kwh,
"mtd_dollars": self.api.mtd_dollars.replace("$", ""),
"mtd_dollars": self.api.mtd_dollars,
"projected_bill": self.api.projected_bill,
}
@util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_UPDATES)
async def async_update(self):
session = aiohttp.ClientSession()
api = FplApi(self.username, self.password, True, self.loop, session)
await api.login()
# await api.async_get_yesterday_usage()
await api.async_get_mtd_usage()
await session.close()
try:
api = FplApi(self.username, self.password, True, self.loop, session)
await api.login()
# await api.async_get_yesterday_usage()
await api.async_get_mtd_usage()
self._state = api.projected_bill
self.api = api
self._state = api.projected_bill
self.api = api
except Exception: # pylint: disable=broad-except
pass
await session.close()