Added some logging

This commit is contained in:
Yordan Suarez
2020-06-25 00:57:53 +00:00
parent a40ca5b37f
commit 3179a43ba7

View File

@@ -1,4 +1,4 @@
from homeassistant import const import logging
from datetime import datetime, timedelta from datetime import datetime, timedelta
from .fplapi import FplApi from .fplapi import FplApi
import aiohttp import aiohttp
@@ -6,9 +6,18 @@ 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, STATE_UNKNOWN from homeassistant.const import (
CONF_NAME,
EVENT_CORE_CONFIG_UPDATE,
STATE_UNKNOWN,
CONF_USERNAME,
CONF_PASSWORD,
STATE_UNKNOWN,
)
from .const import DOMAIN, ICON, LOGIN_RESULT_OK from .const import DOMAIN, ICON, LOGIN_RESULT_OK
_LOGGER = logging.getLogger(__name__)
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)
@@ -18,19 +27,23 @@ def setup(hass, config):
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(CONF_USERNAME)
password = config_entry.data.get(const.CONF_PASSWORD) password = config_entry.data.get(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()
fpl_accounts = []
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)]) _LOGGER.info(f"Adding fpl account: {account}")
pass fpl_accounts.append(FplSensor(hass, config_entry.data, account))
async_add_entities(fpl_accounts)
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
pass pass
@@ -41,9 +54,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
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(CONF_USERNAME)
self.password = config.get(const.CONF_PASSWORD) self.password = config.get(CONF_PASSWORD)
self._state = const.STATE_UNKNOWN _LOGGER.info(f"Using: {self.username}")
self._state = STATE_UNKNOWN
self.loop = hass.loop self.loop = hass.loop
self._account = account self._account = account
@@ -52,10 +66,10 @@ class FplSensor(Entity):
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(DOMAIN, self._account)
@property @property
def name(self): def name(self):
@@ -79,7 +93,7 @@ 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 = const.STATE_UNKNOWN self._state = STATE_UNKNOWN
self._data = None self._data = None
session = aiohttp.ClientSession() session = aiohttp.ClientSession()
try: try: