Created new sensor with specific data and group them by devices(account)

This commit is contained in:
Yordan Suarez
2020-11-01 04:57:10 +00:00
parent 4e4cd3c633
commit 8c644d35f1
9 changed files with 279 additions and 87 deletions

View File

@@ -1,18 +1,62 @@
""" FPL Component """
import logging
from datetime import timedelta
from homeassistant.core import Config, HomeAssistant
from homeassistant.util import Throttle
from .fplapi import FplApi
from .const import DOMAIN_DATA, CONF_USERNAME, CONF_PASSWORD
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
_LOGGER = logging.getLogger(__name__)
from .config_flow import FplFlowHandler
from .const import DOMAIN
class FplData:
"""This class handle communication and stores the data."""
def __init__(self, hass, client):
"""Initialize the class."""
self.hass = hass
self.client = client
@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def update_data(self):
"""Update data."""
# This is where the main logic to update platform data goes.
try:
data = await self.client.get_data()
self.hass.data[DOMAIN_DATA]["data"] = data
except Exception as error: # pylint: disable=broad-except
_LOGGER.error("Could not update data - %s", error)
async def async_setup(hass: HomeAssistant, config: Config) -> bool:
"""Set up configured Fpl."""
return True
async def async_setup_entry(hass, config_entry):
# Get "global" configuration.
username = config_entry.data.get(CONF_USERNAME)
password = config_entry.data.get(CONF_PASSWORD)
# Create DATA dict
hass.data[DOMAIN_DATA] = {}
# Configure the client.
_LOGGER.info(f"Configuring the client")
client = FplApi(username, password, hass.loop)
fplData = FplData(hass, client)
await fplData.update_data()
hass.data[DOMAIN_DATA]["client"] = fplData
"""Set up Fpl as config entry."""
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, "sensor")