djtimca f7cf82be6d
Add Omnilogic Switch platform (#42116)
* Bump omnilogic dependency to 0.4.4 to fix Lights key error.

* Bumped dependency to 0.4.5.

* Fixed pump type issue for unique pool structure.

* Create full platform bundle for final testing and PR to Home Assistant dev.

* Removed logger instances not required.

* Fixed lint issues.

* Fixed pylint issues.

* Fix pylint issues. Fix issue with pH sensor offset.

* Stripped light, water_heater platform for PR submit.

* Correct pH and ORP sensor report to unknown with offset if pump is off.

* Moving guard condition check to helper function.

* Update to asyncio.sleep to wait for switch status delay in Hayward API status.

* Removed sleep, added state delay to handle slow Hayward API state update response.

* Fix flake8 issue.

* Fix flake8 issue.

* Fix isort issue.

* Addressed PR Comments.

* Addressed PR comments. Corrected Unit of Measure for sensor where pump speed is not variable.

* Fix pylint issue.

* Address pylint issue.

* Update homeassistant/components/omnilogic/switch.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>
2021-04-29 16:46:04 +02:00

104 lines
3.3 KiB
Python

"""Config flow for Omnilogic integration."""
import logging
from omnilogic import LoginException, OmniLogic, OmniLogicException
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import callback
from homeassistant.helpers import aiohttp_client
from .const import CONF_SCAN_INTERVAL, DEFAULT_PH_OFFSET, DEFAULT_SCAN_INTERVAL, DOMAIN
_LOGGER = logging.getLogger(__name__)
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Omnilogic."""
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
@staticmethod
@callback
def async_get_options_flow(config_entry):
"""Get the options flow for this handler."""
return OptionsFlowHandler(config_entry)
async def async_step_user(self, user_input=None):
"""Handle the initial step."""
errors = {}
config_entry = self.hass.config_entries.async_entries(DOMAIN)
if config_entry:
return self.async_abort(reason="single_instance_allowed")
errors = {}
if user_input is not None:
username = user_input[CONF_USERNAME]
password = user_input[CONF_PASSWORD]
session = aiohttp_client.async_get_clientsession(self.hass)
omni = OmniLogic(username, password, session)
try:
await omni.connect()
except LoginException:
errors["base"] = "invalid_auth"
except OmniLogicException:
errors["base"] = "cannot_connect"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
await self.async_set_unique_id(user_input["username"])
self._abort_if_unique_id_configured()
return self.async_create_entry(title="Omnilogic", data=user_input)
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
}
),
errors=errors,
)
class OptionsFlowHandler(config_entries.OptionsFlow):
"""Handle Omnilogic client options."""
def __init__(self, config_entry):
"""Initialize options flow."""
self.config_entry = config_entry
async def async_step_init(self, user_input=None):
"""Manage options."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)
return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
{
vol.Optional(
CONF_SCAN_INTERVAL,
default=self.config_entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
),
): int,
vol.Optional(
"ph_offset",
default=self.config_entry.options.get(
"ph_offset", DEFAULT_PH_OFFSET
),
): vol.All(vol.Coerce(float)),
}
),
)