Make config flows general based on entities
This commit is contained in:
@@ -1,75 +1,81 @@
|
||||
"""Config flow for LocalTuya integration integration."""
|
||||
import logging
|
||||
from importlib import import_module
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries, core, exceptions
|
||||
from homeassistant.const import (
|
||||
CONF_SWITCHES,
|
||||
CONF_ENTITIES,
|
||||
CONF_ID,
|
||||
CONF_HOST,
|
||||
CONF_DEVICE_ID,
|
||||
CONF_NAME,
|
||||
CONF_FRIENDLY_NAME,
|
||||
CONF_PLATFORM,
|
||||
CONF_SWITCHES,
|
||||
)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
from . import pytuya
|
||||
from .const import ( # pylint: disable=unused-import
|
||||
CONF_DEVICE_TYPE,
|
||||
CONF_LOCAL_KEY,
|
||||
CONF_PROTOCOL_VERSION,
|
||||
CONF_CURRENT,
|
||||
CONF_CURRENT_CONSUMPTION,
|
||||
CONF_VOLTAGE,
|
||||
DOMAIN,
|
||||
DEVICE_TYPE_POWER_OUTLET,
|
||||
PLATFORMS,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ADD_ANOTHER_SWITCH = "add_another_switch"
|
||||
PLATFORM_TO_ADD = "platform_to_add"
|
||||
NO_ADDITIONAL_PLATFORMS = "no_additional_platforms"
|
||||
|
||||
USER_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_NAME): str,
|
||||
vol.Required(CONF_HOST): str,
|
||||
vol.Required(CONF_DEVICE_ID): str,
|
||||
vol.Required(CONF_LOCAL_KEY): str,
|
||||
vol.Required(CONF_NAME, default="bla"): str,
|
||||
vol.Required(CONF_HOST, default="10.0.10.110"): str,
|
||||
vol.Required(CONF_DEVICE_ID, default="307001182462ab4de00b"): str,
|
||||
vol.Required(CONF_LOCAL_KEY, default="28ac0651f2d187df"): str,
|
||||
vol.Required(CONF_PROTOCOL_VERSION, default="3.3"): vol.In(["3.1", "3.3"]),
|
||||
vol.Required(CONF_DEVICE_TYPE, default=DEVICE_TYPE_POWER_OUTLET): vol.In(
|
||||
[DEVICE_TYPE_POWER_OUTLET]
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
POWER_OUTLET_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_ID, default=1): int,
|
||||
vol.Required(CONF_NAME): str,
|
||||
vol.Required(CONF_FRIENDLY_NAME): str,
|
||||
vol.Required(CONF_CURRENT, default=18): int,
|
||||
vol.Required(CONF_CURRENT_CONSUMPTION, default=19): int,
|
||||
vol.Required(CONF_VOLTAGE, default=20): int,
|
||||
vol.Required(ADD_ANOTHER_SWITCH, default=False): bool,
|
||||
}
|
||||
PICK_ENTITY_SCHEMA = vol.Schema(
|
||||
{vol.Required(PLATFORM_TO_ADD, default=PLATFORMS[0]): vol.In(PLATFORMS)}
|
||||
)
|
||||
|
||||
|
||||
def platform_schema(dps, additional_fields):
|
||||
"""Generate input validation schema for a platform."""
|
||||
dps_list = vol.In([f"{id} (value: {value})" for id, value in dps.items()])
|
||||
return vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_ID): dps_list,
|
||||
vol.Required(CONF_FRIENDLY_NAME): str,
|
||||
}
|
||||
).extend({conf: dps_list for conf in additional_fields})
|
||||
|
||||
|
||||
def strip_dps_values(user_input, fields):
|
||||
"""Remove values and keep only index for DPS config items."""
|
||||
for field in [CONF_ID] + fields:
|
||||
user_input[field] = user_input[field].split(" ")[0]
|
||||
return user_input
|
||||
|
||||
|
||||
async def validate_input(hass: core.HomeAssistant, data):
|
||||
"""Validate the user input allows us to connect."""
|
||||
pytuyadevice = pytuya.OutletDevice(
|
||||
data[CONF_DEVICE_ID], data[CONF_HOST], data[CONF_LOCAL_KEY]
|
||||
)
|
||||
pytuyadevice.set_version(float(data[CONF_PROTOCOL_VERSION]))
|
||||
pytuyadevice.set_dpsUsed({"1": None})
|
||||
pytuyadevice.set_dpsUsed({})
|
||||
try:
|
||||
await hass.async_add_executor_job(pytuyadevice.status)
|
||||
except ConnectionRefusedError:
|
||||
data = await hass.async_add_executor_job(pytuyadevice.status)
|
||||
except (ConnectionRefusedError, ConnectionResetError):
|
||||
raise CannotConnect
|
||||
except ValueError:
|
||||
raise InvalidAuth
|
||||
return data
|
||||
return data["dps"]
|
||||
|
||||
|
||||
class LocaltuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
@@ -81,7 +87,10 @@ class LocaltuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
def __init__(self):
|
||||
"""Initialize a new LocaltuyaConfigFlow."""
|
||||
self.basic_info = None
|
||||
self.switches = []
|
||||
self.dps_data = None
|
||||
self.platform = None
|
||||
self.platform_dps_fields = None
|
||||
self.entities = []
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
"""Handle the initial step."""
|
||||
@@ -91,11 +100,9 @@ class LocaltuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
try:
|
||||
self.basic_info = await validate_input(self.hass, user_input)
|
||||
if self.basic_info[CONF_DEVICE_TYPE] == "Power Outlet":
|
||||
return await self.async_step_power_outlet()
|
||||
|
||||
return self.async_abort(reason="unsupported_device_type")
|
||||
self.basic_info = user_input
|
||||
self.dps_data = await validate_input(self.hass, user_input)
|
||||
return await self.async_step_pick_entity_type()
|
||||
except CannotConnect:
|
||||
errors["base"] = "cannot_connect"
|
||||
except InvalidAuth:
|
||||
@@ -108,29 +115,88 @@ class LocaltuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
step_id="user", data_schema=USER_SCHEMA, errors=errors
|
||||
)
|
||||
|
||||
async def async_step_power_outlet(self, user_input=None):
|
||||
"""Handle the initial step."""
|
||||
async def async_step_pick_entity_type(self, user_input=None):
|
||||
"""Handle asking if user wants to add another entity."""
|
||||
if user_input is not None:
|
||||
if user_input.get(NO_ADDITIONAL_PLATFORMS):
|
||||
config = {**self.basic_info, CONF_ENTITIES: self.entities}
|
||||
return self.async_create_entry(title=config[CONF_NAME], data=config)
|
||||
|
||||
self._set_platform(user_input[PLATFORM_TO_ADD])
|
||||
return await self.async_step_add_entity()
|
||||
|
||||
# Add a checkbox that allows bailing out from config flow iff at least one
|
||||
# entity has been added
|
||||
schema = PICK_ENTITY_SCHEMA
|
||||
if self.platform is not None:
|
||||
schema = schema.extend(
|
||||
{vol.Required(NO_ADDITIONAL_PLATFORMS, default=True): bool}
|
||||
)
|
||||
|
||||
return self.async_show_form(step_id="pick_entity_type", data_schema=schema)
|
||||
|
||||
async def async_step_add_entity(self, user_input=None):
|
||||
"""Handle adding a new entity."""
|
||||
errors = {}
|
||||
if user_input is not None:
|
||||
already_configured = any(
|
||||
switch[CONF_ID] == user_input[CONF_ID] for switch in self.switches
|
||||
switch[CONF_ID] == user_input[CONF_ID] for switch in self.entities
|
||||
)
|
||||
if not already_configured:
|
||||
add_another_switch = user_input.pop(ADD_ANOTHER_SWITCH)
|
||||
self.switches.append(user_input)
|
||||
if not add_another_switch:
|
||||
config = {**self.basic_info, CONF_SWITCHES: self.switches}
|
||||
return self.async_create_entry(title=config[CONF_NAME], data=config)
|
||||
else:
|
||||
errors["base"] = "switch_already_configured"
|
||||
user_input[CONF_PLATFORM] = self.platform
|
||||
self.entities.append(
|
||||
strip_dps_values(user_input, self.platform_dps_fields)
|
||||
)
|
||||
return await self.async_step_pick_entity_type()
|
||||
|
||||
errors["base"] = "entity_already_configured"
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="power_outlet",
|
||||
data_schema=POWER_OUTLET_SCHEMA,
|
||||
step_id="add_entity",
|
||||
data_schema=platform_schema(self.dps_data, self.platform_dps_fields),
|
||||
errors=errors,
|
||||
description_placeholders={"number": len(self.switches) + 1},
|
||||
description_placeholders={"platform": self.platform},
|
||||
)
|
||||
|
||||
async def async_step_import(self, user_input):
|
||||
"""Handle import from YAML."""
|
||||
|
||||
def _convert_entity(conf):
|
||||
converted = {
|
||||
CONF_ID: conf[CONF_ID],
|
||||
CONF_FRIENDLY_NAME: conf[CONF_FRIENDLY_NAME],
|
||||
CONF_PLATFORM: self.platform,
|
||||
}
|
||||
for field in self.platform_dps_fields:
|
||||
converted[str(field)] = conf[field]
|
||||
return converted
|
||||
|
||||
await self.async_set_unique_id(user_input[CONF_DEVICE_ID])
|
||||
self._set_platform(user_input[CONF_PLATFORM])
|
||||
|
||||
if len(user_input[CONF_SWITCHES]) > 0:
|
||||
for switch_conf in user_input[CONF_SWITCHES].values():
|
||||
self.entities.append(_convert_entity(switch_conf))
|
||||
else:
|
||||
self.entities.append(_convert_entity(user_input))
|
||||
|
||||
config = {
|
||||
CONF_NAME: f"{user_input[CONF_DEVICE_ID]} (import from configuration.yaml)",
|
||||
CONF_HOST: user_input[CONF_HOST],
|
||||
CONF_DEVICE_ID: user_input[CONF_DEVICE_ID],
|
||||
CONF_LOCAL_KEY: user_input[CONF_LOCAL_KEY],
|
||||
CONF_PROTOCOL_VERSION: user_input[CONF_PROTOCOL_VERSION],
|
||||
CONF_ENTITIES: self.entities,
|
||||
}
|
||||
self._abort_if_unique_id_configured(updates=config)
|
||||
return self.async_create_entry(title=config[CONF_NAME], data=config)
|
||||
|
||||
def _set_platform(self, platform):
|
||||
self.platform = platform
|
||||
self.platform_dps_fields = import_module(
|
||||
f"homeassistant.components.localtuya.{platform}"
|
||||
).DPS_FIELDS
|
||||
|
||||
|
||||
class CannotConnect(exceptions.HomeAssistantError):
|
||||
"""Error to indicate we cannot connect."""
|
||||
|
Reference in New Issue
Block a user