2021-11-22 14:06:42 +00:00
|
|
|
"""Config flow for ViCare integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from PyViCare.PyViCareUtils import PyViCareInvalidCredentialsError
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
2021-12-01 18:39:03 +00:00
|
|
|
from homeassistant.components import dhcp
|
2022-01-05 21:49:42 +00:00
|
|
|
from homeassistant.const import CONF_CLIENT_ID, CONF_PASSWORD, CONF_USERNAME
|
2021-12-01 18:39:03 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2021-11-22 14:06:42 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.device_registry import format_mac
|
|
|
|
|
|
|
|
from . import vicare_login
|
|
|
|
from .const import (
|
|
|
|
CONF_CIRCUIT,
|
|
|
|
CONF_HEATING_TYPE,
|
|
|
|
DEFAULT_HEATING_TYPE,
|
|
|
|
DOMAIN,
|
2022-01-05 21:49:42 +00:00
|
|
|
VICARE_NAME,
|
2021-11-22 14:06:42 +00:00
|
|
|
HeatingType,
|
|
|
|
)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a config flow for ViCare."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input: dict[str, Any] | None = None):
|
|
|
|
"""Invoke when a user initiates a flow via the user interface."""
|
|
|
|
if self._async_current_entries():
|
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
|
|
|
|
|
|
|
data_schema = {
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Required(CONF_CLIENT_ID): cv.string,
|
|
|
|
vol.Required(CONF_HEATING_TYPE, default=DEFAULT_HEATING_TYPE.value): vol.In(
|
|
|
|
[e.value for e in HeatingType]
|
|
|
|
),
|
|
|
|
}
|
|
|
|
errors: dict[str, str] = {}
|
|
|
|
|
|
|
|
if user_input is not None:
|
|
|
|
try:
|
|
|
|
await self.hass.async_add_executor_job(
|
|
|
|
vicare_login, self.hass, user_input
|
|
|
|
)
|
2022-01-05 21:49:42 +00:00
|
|
|
except PyViCareInvalidCredentialsError:
|
2021-11-22 14:06:42 +00:00
|
|
|
errors["base"] = "invalid_auth"
|
2022-01-05 21:49:42 +00:00
|
|
|
else:
|
|
|
|
return self.async_create_entry(title=VICARE_NAME, data=user_input)
|
2021-11-22 14:06:42 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user",
|
|
|
|
data_schema=vol.Schema(data_schema),
|
|
|
|
errors=errors,
|
|
|
|
)
|
|
|
|
|
2021-12-01 18:39:03 +00:00
|
|
|
async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
|
2021-11-22 14:06:42 +00:00
|
|
|
"""Invoke when a Viessmann MAC address is discovered on the network."""
|
2021-12-01 18:39:03 +00:00
|
|
|
formatted_mac = format_mac(discovery_info.macaddress)
|
2021-11-22 14:06:42 +00:00
|
|
|
_LOGGER.info("Found device with mac %s", formatted_mac)
|
|
|
|
|
|
|
|
await self.async_set_unique_id(formatted_mac)
|
|
|
|
self._abort_if_unique_id_configured()
|
|
|
|
|
|
|
|
if self._async_current_entries():
|
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
|
|
|
|
|
|
|
return await self.async_step_user()
|
|
|
|
|
|
|
|
async def async_step_import(self, import_info):
|
|
|
|
"""Handle a flow initiated by a YAML config import."""
|
|
|
|
if self._async_current_entries():
|
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
|
|
|
|
|
|
|
# Remove now unsupported config parameters
|
2022-01-05 21:49:42 +00:00
|
|
|
import_info.pop(CONF_CIRCUIT, None)
|
2021-11-22 14:06:42 +00:00
|
|
|
|
2022-01-05 21:49:42 +00:00
|
|
|
# CONF_HEATING_TYPE is now required but was optional in yaml config. Add if missing.
|
2021-11-22 14:06:42 +00:00
|
|
|
if import_info.get(CONF_HEATING_TYPE) is None:
|
|
|
|
import_info[CONF_HEATING_TYPE] = DEFAULT_HEATING_TYPE.value
|
|
|
|
|
2022-01-05 21:49:42 +00:00
|
|
|
return await self.async_step_user(import_info)
|