Skip to content

Commit

Permalink
Update config_flow.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Xerolux authored Sep 15, 2024
1 parent 621c590 commit 82eec5b
Showing 1 changed file with 43 additions and 13 deletions.
56 changes: 43 additions & 13 deletions custom_components/violet_pool_controller/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,61 @@
import logging

import aiohttp
import async_timeout
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.selector import TextSelector

from .const import DOMAIN, CONF_API_URL, CONF_POLLING_INTERVAL

@callback
def configured_instances(hass):
"""Return a set of configured instances."""
return set(entry.data[CONF_API_URL] for entry in hass.config_entries.async_entries(DOMAIN))
_LOGGER = logging.getLogger(__name__)


class VioletDeviceConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Violet Device."""

VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL

async def async_step_user(self, user_input=None):
async def async_step_user(self, user_input=None) -> FlowResult:
"""Handle the initial step."""
errors = {}

if user_input is not None:
# Save the user input
return self.async_create_entry(title="Violet Device", data=user_input)
api_url = user_input[CONF_API_URL]

# Check for duplicate entries
await self.async_set_unique_id(api_url)
self._abort_if_unique_id_configured()

# Validate the API URL
session = aiohttp_client.async_get_clientsession(self.hass)
try:
async with async_timeout.timeout(10):
async with session.get(api_url, ssl=False) as response:
response.raise_for_status()
await response.json()
except aiohttp.ClientError as err:
_LOGGER.error("Error connecting to API: %s", err)
errors["base"] = "cannot_connect"
except Exception as err:
_LOGGER.error("Unexpected exception: %s", err)
errors["base"] = "unknown"
else:
# Input is valid, create the config entry
return self.async_create_entry(title="Violet Device", data=user_input)

# Show the configuration form with errors (if any)
data_schema = vol.Schema({
vol.Required(CONF_API_URL, default="http://192.168.178.55/getReadings?ALL"): str,
vol.Optional(CONF_POLLING_INTERVAL, default=10): int,
})

# Show the configuration form
return self.async_show_form(
step_id="user",
data_schema=vol.Schema({
vol.Required(CONF_API_URL, default="http://192.168.178.55/getReadings?ALL"): str,
vol.Optional(CONF_POLLING_INTERVAL, default=10): int,
})
data_schema=data_schema,
errors=errors,
)

0 comments on commit 82eec5b

Please sign in to comment.