mirror of
https://github.com/home-assistant/core.git
synced 2026-07-07 06:46:17 +01:00
92 lines
3.0 KiB
Python
92 lines
3.0 KiB
Python
"""Config flow for myStrom integration."""
|
|
|
|
import logging
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
import pymystrom
|
|
from pymystrom.exceptions import MyStromConnectionError
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
|
from homeassistant.const import CONF_HOST, CONF_NAME
|
|
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
|
|
|
|
from .const import DOMAIN
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
DEFAULT_NAME = "myStrom Device"
|
|
|
|
STEP_USER_DATA_SCHEMA = vol.Schema(
|
|
{
|
|
# Name field is no longer allowed in config flow schemas
|
|
# pylint: disable-next=home-assistant-config-flow-name-field
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): str,
|
|
vol.Required(CONF_HOST): str,
|
|
}
|
|
)
|
|
|
|
|
|
class MyStromConfigFlow(ConfigFlow, domain=DOMAIN):
|
|
"""Handle a config flow for myStrom."""
|
|
|
|
VERSION = 1
|
|
|
|
_host: str | None = None
|
|
|
|
async def async_step_user(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> ConfigFlowResult:
|
|
"""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)
|
|
|
|
async def async_step_dhcp(
|
|
self, discovery_info: DhcpServiceInfo
|
|
) -> ConfigFlowResult:
|
|
"""Handle DHCP discovery."""
|
|
mac_address = discovery_info.macaddress.upper()
|
|
await self.async_set_unique_id(mac_address)
|
|
self._abort_if_unique_id_configured(updates={CONF_HOST: discovery_info.ip})
|
|
|
|
try:
|
|
await pymystrom.get_device_info(discovery_info.ip)
|
|
except MyStromConnectionError:
|
|
return self.async_abort(reason="cannot_connect")
|
|
|
|
self._host = discovery_info.ip
|
|
self.context["title_placeholders"] = {"host": discovery_info.ip}
|
|
return await self.async_step_discovery_confirm()
|
|
|
|
async def async_step_discovery_confirm(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> ConfigFlowResult:
|
|
"""Handle discovery confirmation."""
|
|
if user_input is not None:
|
|
return self.async_create_entry(
|
|
title=DEFAULT_NAME,
|
|
data={CONF_HOST: self._host},
|
|
)
|
|
|
|
self._set_confirm_only()
|
|
if TYPE_CHECKING:
|
|
assert self._host is not None
|
|
return self.async_show_form(
|
|
step_id="discovery_confirm",
|
|
description_placeholders={CONF_HOST: self._host},
|
|
)
|