reworked fplApi

This commit is contained in:
Yordan Suarez
2019-12-31 12:28:42 -05:00
parent d11df67c18
commit 0e4ce1ba3f
5 changed files with 137 additions and 207 deletions

View File

@@ -17,25 +17,20 @@ def setup(hass, config):
return True
def setup_platform(hass, config, add_devices, discovery_info=None):
setup(hass, config)
add_devices([FplSensor(hass, config)])
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)
api = FplApi(username, password, hass.loop, session)
result = await api.login()
if result == LOGIN_RESULT_OK:
await api.async_get_headers()
accounts = await api.async_get_open_accounts()
for account in accounts:
async_add_entities(
[FplSensor(hass, config_entry.data, account)])
pass
except Exception: # pylint: disable=broad-except
@@ -45,37 +40,31 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class FplSensor(Entity):
def __init__(self, hass, config):
def __init__(self, hass, config, account):
self._config = config
self.username = config.get(const.CONF_USERNAME)
self.password = config.get(const.CONF_PASSWORD)
self._state = 0
self.loop = hass.loop
self.api = None
async def _core_config_updated(self, _event):
"""Handle core config updated."""
print("Core config updated")
# self._init_data()
# if self._unsub_fetch_data:
# self._unsub_fetch_data()
# self._unsub_fetch_data = None
# await self._fetch_data()
self._account = account
self._data = None
async def async_added_to_hass(self):
await self.async_update()
@property
def name(self):
name = self._config.get(CONF_NAME)
if name is not None:
return f"{DOMAIN.upper()} {name}"
def unique_id(self):
"""Return the ID of this device."""
return "{}{}".format(self._account, hash(self._account))
return DOMAIN
@property
def name(self):
return f"{DOMAIN.upper()} {self._account}"
@property
def state(self):
return self._state
return self._data["bill_to_date"]
@property
def icon(self):
@@ -83,24 +72,28 @@ class FplSensor(Entity):
@property
def state_attributes(self):
return self._data
"""
return {
# "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,
"projected_bill": self.api.projected_bill,
"mtd_kwh": self._data["mtd_kwh"],
"bill_to_date": self._data["bill_to_date"],
"projected_bill": self._data["projected_bill"],
# "details": self._data["details"],
"start_date": self._data["start_date"],
"end_date": self._data["end_date"],
"service_days": self._data["service_days"],
"current_days": self._data["current_days"],
"remaining_days": self._data["remaining_days"],
}
"""
@util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_UPDATES)
async def async_update(self):
session = aiohttp.ClientSession()
try:
api = FplApi(self.username, self.password, True, self.loop, session)
api = FplApi(self.username, self.password, 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._data = await api.async_get_data(self._account)
except Exception: # pylint: disable=broad-except
pass