Some tries to fix the unknown error

This commit is contained in:
dotKrad
2020-06-09 22:34:53 +00:00
parent 387cbb0309
commit a40ca5b37f

View File

@@ -1,91 +1,93 @@
from homeassistant import const from homeassistant import const
from datetime import datetime, timedelta from datetime import datetime, timedelta
from .fplapi import FplApi from .fplapi import FplApi
import aiohttp import aiohttp
import asyncio import asyncio
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant import util from homeassistant import util
from homeassistant.const import CONF_NAME, EVENT_CORE_CONFIG_UPDATE from homeassistant.const import CONF_NAME, EVENT_CORE_CONFIG_UPDATE, STATE_UNKNOWN
from .const import DOMAIN, ICON, LOGIN_RESULT_OK from .const import DOMAIN, ICON, LOGIN_RESULT_OK
MIN_TIME_BETWEEN_SCANS = timedelta(minutes=30) MIN_TIME_BETWEEN_SCANS = timedelta(minutes=30)
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=60) MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=60)
def setup(hass, config): def setup(hass, config):
return True return True
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
username = config_entry.data.get(const.CONF_USERNAME) username = config_entry.data.get(const.CONF_USERNAME)
password = config_entry.data.get(const.CONF_PASSWORD) password = config_entry.data.get(const.CONF_PASSWORD)
session = aiohttp.ClientSession() session = aiohttp.ClientSession()
try: try:
api = FplApi(username, password, hass.loop, session) api = FplApi(username, password, hass.loop, session)
result = await api.login() result = await api.login()
if result == LOGIN_RESULT_OK: if result == LOGIN_RESULT_OK:
accounts = await api.async_get_open_accounts() accounts = await api.async_get_open_accounts()
for account in accounts: for account in accounts:
async_add_entities([FplSensor(hass, config_entry.data, account)]) async_add_entities([FplSensor(hass, config_entry.data, account)])
pass pass
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
pass pass
await session.close() await session.close()
class FplSensor(Entity): class FplSensor(Entity):
def __init__(self, hass, config, account): def __init__(self, hass, config, account):
self._config = config self._config = config
self.username = config.get(const.CONF_USERNAME) self.username = config.get(const.CONF_USERNAME)
self.password = config.get(const.CONF_PASSWORD) self.password = config.get(const.CONF_PASSWORD)
self._state = 0 self._state = const.STATE_UNKNOWN
self.loop = hass.loop self.loop = hass.loop
self._account = account self._account = account
self._data = None self._data = None
async def async_added_to_hass(self): async def async_added_to_hass(self):
await self.async_update() await self.async_update()
#@property # @property
#def unique_id(self): # def unique_id(self):
# """Return the ID of this device.""" # """Return the ID of this device."""
# return "{}{}".format(self._account, hash(self._account)) # return "{}{}".format(self._account, hash(self._account))
@property @property
def name(self): def name(self):
return f"{DOMAIN.upper()} {self._account}" return f"{DOMAIN.upper()} {self._account}"
@property @property
def state(self): def state(self):
return self._data["bill_to_date"] return self._state #
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):
return " " return " "
@property @property
def icon(self): def icon(self):
return ICON return ICON
@property @property
def state_attributes(self): def state_attributes(self):
return self._data return self._data
@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):
session = aiohttp.ClientSession() self._state = const.STATE_UNKNOWN
try: self._data = None
api = FplApi(self.username, self.password, self.loop, session) session = aiohttp.ClientSession()
await api.login() try:
self._data = await api.async_get_data(self._account) api = FplApi(self.username, self.password, self.loop, session)
await api.login()
except Exception: # pylint: disable=broad-except self._data = await api.async_get_data(self._account)
pass self._state = self._data["bill_to_date"]
except Exception: # pylint: disable=broad-except
await session.close() pass
await session.close()