core/homeassistant/components/mystrom/config_flow.py

58 lines
1.8 KiB
Python
Raw Normal View History

Add config flow to mystrom (#74719) * rebase * fixed review comments * fix test * Update tests * increase test coverage * implement some review comments * Enhance device check for old FWs and add tests * Reworked exception handling * small code optimizations * fix test * Increase test coverage * Update __init__.py changed from hass.config_entries.async_setup_platforms(entry, PLATFORMS) to await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) * Update __init__.py remove spaces * Bump python-mystrom to 2.2.0 * Migrate to get_device_info from python-mystrom * Migrate to get_device_info from python-mystrom * Update __init__.py * update requirements_all.txt * update config_flow * fix tests * Update homeassistant/components/mystrom/__init__.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Implemented review changes * increase test coverage * Implemented user defined title * implemented user defined title * Additional review comments * fix test * fix linter * fix linter * Update homeassistant/components/mystrom/models.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * fix review comments * fix missing import * simplify * Update homeassistant/components/mystrom/__init__.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/mystrom/__init__.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/mystrom/config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/mystrom/config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/mystrom/config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_init.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_init.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_init.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_init.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/mystrom/light.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/mystrom/switch.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * fix review comments * fix review comments * Update tests/components/mystrom/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * fix review comment * Update tests/components/mystrom/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> --------- Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
2023-06-06 12:16:27 +00:00
"""Config flow for myStrom integration."""
from __future__ import annotations
import logging
from typing import Any
import pymystrom
from pymystrom.exceptions import MyStromConnectionError
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.data_entry_flow import FlowResult
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = "myStrom Device"
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Optional(CONF_NAME, default=DEFAULT_NAME): str,
vol.Required(CONF_HOST): str,
}
)
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for myStrom."""
VERSION = 1
async def async_step_import(self, import_config: dict[str, Any]) -> FlowResult:
"""Handle import from config."""
return await self.async_step_user(import_config)
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle the initial step."""
errors: dict[str, str] = {}
if user_input is not None:
try:
info = await pymystrom.get_device_info(user_input[CONF_HOST])
except MyStromConnectionError:
errors["base"] = "cannot_connect"
else:
await self.async_set_unique_id(info["mac"])
self._abort_if_unique_id_configured()
data = {CONF_HOST: user_input[CONF_HOST]}
title = user_input.get(CONF_NAME) or DEFAULT_NAME
return self.async_create_entry(title=title, data=data)
schema = self.add_suggested_values_to_schema(STEP_USER_DATA_SCHEMA, user_input)
return self.async_show_form(step_id="user", data_schema=schema, errors=errors)