Used Interface for configuration

This commit is contained in:
dotKrad
2019-12-17 23:23:59 -05:00
parent adea133b3d
commit 0aedb1df2d
19 changed files with 105 additions and 755 deletions

View File

@@ -1,18 +1,20 @@
{
"config": {
"title": "Fpl",
"title": "Florida Power & Light",
"step": {
"user": {
"title": "Florida Power & Light",
"description": "If you need help with the configuration have a look here: https://github.com/custom-components/blueprint",
"description": "If you need help with the configuration have a look here: https://github.com/dotKrad/hass-fpl",
"data": {
"name": "Name",
"username": "Username",
"password": "Password"
}
}
},
"error": {
"auth": "Username/Password is wrong."
"auth": "Username/Password is wrong.",
"name_exists": "Configuration already exists."
},
"abort": {
"single_instance_allowed": "Only a single configuration of Fpl is allowed."

View File

@@ -1,21 +0,0 @@
{
"config": {
"title": "Blueprint",
"step": {
"user": {
"title": "Blueprint",
"description": "Hvis du trenger hjep til konfigurasjon ta en titt her: https://github.com/custom-components/blueprint",
"data": {
"username": "Brukernavn",
"password": "Passord"
}
}
},
"error": {
"auth": "Brukernavn/Passord er feil."
},
"abort": {
"single_instance_allowed": "Du kan konfigurere Blueprint kun en gang."
}
}
}

View File

@@ -1,5 +0,0 @@
{
"state": {
"Some sample static text.": "Eksempel tekst."
}
}

View File

@@ -1 +1,26 @@
""" FPL Component """
from homeassistant.core import Config, HomeAssistant
from .config_flow import FplFlowHandler
from .const import DOMAIN
async def async_setup(hass: HomeAssistant, config: Config) -> bool:
"""Set up configured Fpl."""
return True
async def async_setup_entry(hass, config_entry):
"""Set up Fpl as config entry."""
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, "sensor")
)
return True
async def async_unload_entry(hass, config_entry):
"""Unload a config entry."""
await hass.config_entries.async_forward_entry_unload(config_entry, "sensor")
return True

View File

@@ -4,11 +4,20 @@ import voluptuous as vol
from .fplapi import FplApi
from homeassistant import config_entries
import aiohttp
from .const import DOMAIN
from .const import DOMAIN, CONF_USERNAME, CONF_PASSWORD, CONF_NAME
from homeassistant.core import callback
@config_entries.HANDLERS.register(DOMAIN)
class FplFlowHandler(config_entries.ConfigFlow):
@callback
def configured_instances(hass):
"""Return a set of configured SimpliSafe instances."""
entites = []
for entry in hass.config_entries.async_entries(DOMAIN):
entites.append(f"{entry.data.get(CONF_USERNAME)}")
return set(entites)
class FplFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
@@ -22,19 +31,27 @@ class FplFlowHandler(config_entries.ConfigFlow):
): # pylint: disable=dangerous-default-value
"""Handle a flow initialized by the user."""
self._errors = {}
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
if self.hass.data.get(DOMAIN):
return self.async_abort(reason="single_instance_allowed")
# if self._async_current_entries():
# return self.async_abort(reason="single_instance_allowed")
# if self.hass.data.get(DOMAIN):
# return self.async_abort(reason="single_instance_allowed")
if user_input is not None:
valid = await self._test_credentials(
user_input["username"], user_input["password"]
)
if valid:
return self.async_create_entry(title="", data=user_input)
username = user_input[CONF_USERNAME]
password = user_input[CONF_PASSWORD]
if username not in configured_instances(self.hass):
valid = await self._test_credentials(username, password)
if valid:
return self.async_create_entry(
title=user_input[CONF_NAME], data=user_input
)
else:
self._errors["base"] = "auth"
else:
self._errors["base"] = "auth"
self._errors[CONF_NAME] = "name_exists"
return await self._show_config_form(user_input)
@@ -46,35 +63,28 @@ class FplFlowHandler(config_entries.ConfigFlow):
# Defaults
username = ""
password = ""
name = "Home"
if user_input is not None:
if "username" in user_input:
username = user_input["username"]
if "password" in user_input:
password = user_input["password"]
if CONF_USERNAME in user_input:
username = user_input[CONF_USERNAME]
if CONF_PASSWORD in user_input:
password = user_input[CONF_PASSWORD]
if CONF_NAME in user_input:
name = user_input[CONF_NAME]
data_schema = OrderedDict()
data_schema[vol.Required("username", default=username)] = str
data_schema[vol.Required("password", default=password)] = str
data_schema[vol.Required(CONF_NAME, default=name)] = str
data_schema[vol.Required(CONF_USERNAME, default=username)] = str
data_schema[vol.Required(CONF_PASSWORD, default=password)] = str
return self.async_show_form(
step_id="user", data_schema=vol.Schema(data_schema), errors=self._errors
)
async def async_step_import(self, user_input): # pylint: disable=unused-argument
"""Import a config entry.
Special type of import, we're not actually going to store any data.
Instead, we're going to rely on the values that are in config file.
"""
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
return self.async_create_entry(title="configuration.yaml", data={})
async def _test_credentials(self, username, password):
"""Return true if credentials is valid."""
try:
# client = Client(username, password)
# client.get_data()
session = aiohttp.ClientSession()
api = FplApi(username, password, True, None, session)
await api.login()

View File

@@ -3,7 +3,7 @@
DOMAIN = "fpl"
DOMAIN_DATA = f"{DOMAIN}_data"
VERSION = "0.0.1"
PLATFORMS = ["binary_sensor", "sensor", "switch"]
PLATFORMS = ["sensor"]
REQUIRED_FILES = [
".translations/en.json",
"binary_sensor.py",
@@ -14,10 +14,10 @@ REQUIRED_FILES = [
"switch.py",
]
ISSUE_URL = "https://github.com/dotKrad/hass-fpl/issues"
ATTRIBUTION = "Data from this is provided by blueprint."
ATTRIBUTION = "Data from this is provided by FPL."
# Icons
ICON = "mdi:format-quote-close"
ICON = "mdi:flash"
# Device classes
BINARY_SENSOR_DEVICE_CLASS = "connectivity"

View File

@@ -1,7 +1,7 @@
{
"domain": "fpl",
"name": "FPL",
"documentation": "https://github.com/custom-components/blueprint",
"documentation": "https://github.com/dotKrad/hass-fpl",
"dependencies": [],
"config_flow": true,
"codeowners": [

View File

@@ -6,6 +6,9 @@ import asyncio
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
MIN_TIME_BETWEEN_SCANS = timedelta(minutes=1440)
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1440)
@@ -19,24 +22,47 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
add_devices([FplSensor(hass, config)])
async def async_setup_entry(hass, config_entry, async_add_entities):
async_add_entities([FplSensor(hass, config_entry.data)])
class FplSensor(Entity):
def __init__(self, hass, config):
print("init")
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()
async def async_added_to_hass(self):
await self.async_update()
@property
def name(self):
"""Returns the name of the sensor."""
return "fpl"
name = self._config.get(CONF_NAME)
if name is not None:
return f"{DOMAIN.upper()} {name}"
return DOMAIN
@property
def state(self):
return self._state
@property
def icon(self):
return ICON
@property
def state_attributes(self):
return {