2022-01-11 00:23:31 +00:00
|
|
|
"""Config flow for Homewizard."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-12-16 15:53:54 +00:00
|
|
|
from collections.abc import Mapping
|
2022-01-11 00:23:31 +00:00
|
|
|
import logging
|
2022-05-31 08:33:34 +00:00
|
|
|
from typing import Any, cast
|
2022-01-11 00:23:31 +00:00
|
|
|
|
2022-05-25 07:05:11 +00:00
|
|
|
from homewizard_energy import HomeWizardEnergy
|
2022-06-20 08:30:57 +00:00
|
|
|
from homewizard_energy.errors import DisabledError, RequestError, UnsupportedError
|
2022-01-11 00:23:31 +00:00
|
|
|
from voluptuous import Required, Schema
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
2022-02-04 22:45:06 +00:00
|
|
|
from homeassistant.components import persistent_notification, zeroconf
|
2022-01-11 00:23:31 +00:00
|
|
|
from homeassistant.const import CONF_IP_ADDRESS
|
|
|
|
from homeassistant.data_entry_flow import AbortFlow, FlowResult
|
|
|
|
|
2022-02-08 22:51:28 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_API_ENABLED,
|
|
|
|
CONF_PATH,
|
|
|
|
CONF_PRODUCT_NAME,
|
|
|
|
CONF_PRODUCT_TYPE,
|
|
|
|
CONF_SERIAL,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
2022-01-11 00:23:31 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a config flow for P1 meter."""
|
|
|
|
|
|
|
|
VERSION = 1
|
2022-01-14 18:28:24 +00:00
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
"""Initialize the HomeWizard config flow."""
|
|
|
|
self.config: dict[str, str | int] = {}
|
2022-12-16 15:53:54 +00:00
|
|
|
self.entry: config_entries.ConfigEntry | None = None
|
2022-01-11 00:23:31 +00:00
|
|
|
|
2022-02-08 22:51:28 +00:00
|
|
|
async def async_step_import(self, import_config: dict[str, Any]) -> FlowResult:
|
2022-02-04 17:12:35 +00:00
|
|
|
"""Handle a flow initiated by older `homewizard_energy` component."""
|
|
|
|
_LOGGER.debug("config_flow async_step_import")
|
|
|
|
|
2022-02-04 22:45:06 +00:00
|
|
|
persistent_notification.async_create(
|
|
|
|
self.hass,
|
|
|
|
title="HomeWizard Energy",
|
|
|
|
message=(
|
2022-12-22 10:38:59 +00:00
|
|
|
"The custom integration of HomeWizard Energy has been migrated to core."
|
|
|
|
" You can safely remove the custom integration from the"
|
|
|
|
" custom_integrations folder."
|
2022-02-04 17:12:35 +00:00
|
|
|
),
|
2022-02-04 22:45:06 +00:00
|
|
|
notification_id=f"homewizard_energy_to_{DOMAIN}",
|
2022-02-04 17:12:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return await self.async_step_user({CONF_IP_ADDRESS: import_config["host"]})
|
|
|
|
|
2022-01-11 00:23:31 +00:00
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
|
|
|
"""Handle a flow initiated by the user."""
|
|
|
|
|
|
|
|
_LOGGER.debug("config_flow async_step_user")
|
|
|
|
|
2022-12-16 15:53:54 +00:00
|
|
|
data_schema = Schema(
|
|
|
|
{
|
|
|
|
Required(CONF_IP_ADDRESS): str,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-01-11 00:23:31 +00:00
|
|
|
if user_input is None:
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user",
|
2022-12-16 15:53:54 +00:00
|
|
|
data_schema=data_schema,
|
2022-01-11 00:23:31 +00:00
|
|
|
errors=None,
|
|
|
|
)
|
|
|
|
|
2022-12-16 15:53:54 +00:00
|
|
|
error = await self._async_try_connect(user_input[CONF_IP_ADDRESS])
|
|
|
|
if error is not None:
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user",
|
|
|
|
data_schema=data_schema,
|
|
|
|
errors={"base": error},
|
|
|
|
)
|
|
|
|
|
|
|
|
# Fetch device information
|
|
|
|
api = HomeWizardEnergy(user_input[CONF_IP_ADDRESS])
|
|
|
|
device_info = await api.device()
|
|
|
|
await api.close()
|
2022-01-11 00:23:31 +00:00
|
|
|
|
|
|
|
# Sets unique ID and aborts if it is already exists
|
|
|
|
await self._async_set_and_check_unique_id(
|
|
|
|
{
|
|
|
|
CONF_IP_ADDRESS: user_input[CONF_IP_ADDRESS],
|
2022-12-16 15:53:54 +00:00
|
|
|
CONF_PRODUCT_TYPE: device_info.product_type,
|
|
|
|
CONF_SERIAL: device_info.serial,
|
2022-01-11 00:23:31 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-02-04 17:12:35 +00:00
|
|
|
data: dict[str, str] = {CONF_IP_ADDRESS: user_input[CONF_IP_ADDRESS]}
|
|
|
|
|
|
|
|
if self.source == config_entries.SOURCE_IMPORT:
|
|
|
|
old_config_entry_id = self.context["old_config_entry_id"]
|
|
|
|
assert self.hass.config_entries.async_get_entry(old_config_entry_id)
|
|
|
|
data["old_config_entry_id"] = old_config_entry_id
|
|
|
|
|
2022-01-11 00:23:31 +00:00
|
|
|
# Add entry
|
|
|
|
return self.async_create_entry(
|
2022-12-16 15:53:54 +00:00
|
|
|
title=f"{device_info.product_name} ({device_info.serial})",
|
2022-02-04 17:12:35 +00:00
|
|
|
data=data,
|
2022-01-11 00:23:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def async_step_zeroconf(
|
|
|
|
self, discovery_info: zeroconf.ZeroconfServiceInfo
|
|
|
|
) -> FlowResult:
|
|
|
|
"""Handle zeroconf discovery."""
|
|
|
|
|
|
|
|
_LOGGER.debug("config_flow async_step_zeroconf")
|
|
|
|
|
|
|
|
# Validate doscovery entry
|
|
|
|
if (
|
2022-02-08 22:51:28 +00:00
|
|
|
CONF_API_ENABLED not in discovery_info.properties
|
|
|
|
or CONF_PATH not in discovery_info.properties
|
|
|
|
or CONF_PRODUCT_NAME not in discovery_info.properties
|
|
|
|
or CONF_PRODUCT_TYPE not in discovery_info.properties
|
|
|
|
or CONF_SERIAL not in discovery_info.properties
|
2022-01-11 00:23:31 +00:00
|
|
|
):
|
|
|
|
return self.async_abort(reason="invalid_discovery_parameters")
|
|
|
|
|
2022-02-08 22:51:28 +00:00
|
|
|
if (discovery_info.properties[CONF_PATH]) != "/api/v1":
|
2022-01-11 00:23:31 +00:00
|
|
|
return self.async_abort(reason="unsupported_api_version")
|
|
|
|
|
|
|
|
# Sets unique ID and aborts if it is already exists
|
|
|
|
await self._async_set_and_check_unique_id(
|
|
|
|
{
|
|
|
|
CONF_IP_ADDRESS: discovery_info.host,
|
2022-02-08 22:51:28 +00:00
|
|
|
CONF_PRODUCT_TYPE: discovery_info.properties[CONF_PRODUCT_TYPE],
|
|
|
|
CONF_SERIAL: discovery_info.properties[CONF_SERIAL],
|
2022-01-11 00:23:31 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
# Pass parameters
|
|
|
|
self.config = {
|
2022-02-08 22:51:28 +00:00
|
|
|
CONF_API_ENABLED: discovery_info.properties[CONF_API_ENABLED],
|
2022-01-11 00:23:31 +00:00
|
|
|
CONF_IP_ADDRESS: discovery_info.host,
|
2022-02-08 22:51:28 +00:00
|
|
|
CONF_PRODUCT_TYPE: discovery_info.properties[CONF_PRODUCT_TYPE],
|
|
|
|
CONF_PRODUCT_NAME: discovery_info.properties[CONF_PRODUCT_NAME],
|
|
|
|
CONF_SERIAL: discovery_info.properties[CONF_SERIAL],
|
2022-01-11 00:23:31 +00:00
|
|
|
}
|
|
|
|
return await self.async_step_discovery_confirm()
|
|
|
|
|
|
|
|
async def async_step_discovery_confirm(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
|
|
|
"""Confirm discovery."""
|
|
|
|
if user_input is not None:
|
2022-02-08 22:51:28 +00:00
|
|
|
|
|
|
|
# Check connection
|
2022-12-16 15:53:54 +00:00
|
|
|
error = await self._async_try_connect(str(self.config[CONF_IP_ADDRESS]))
|
|
|
|
if error is not None:
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="discovery_confirm",
|
|
|
|
errors={"base": error},
|
|
|
|
)
|
2022-02-08 22:51:28 +00:00
|
|
|
|
2022-01-11 00:23:31 +00:00
|
|
|
return self.async_create_entry(
|
|
|
|
title=f"{self.config[CONF_PRODUCT_NAME]} ({self.config[CONF_SERIAL]})",
|
|
|
|
data={
|
|
|
|
CONF_IP_ADDRESS: self.config[CONF_IP_ADDRESS],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
self._set_confirm_only()
|
|
|
|
|
2022-01-27 13:30:31 +00:00
|
|
|
self.context["title_placeholders"] = {
|
|
|
|
"name": f"{self.config[CONF_PRODUCT_NAME]} ({self.config[CONF_SERIAL]})"
|
|
|
|
}
|
|
|
|
|
2022-01-11 00:23:31 +00:00
|
|
|
return self.async_show_form(
|
|
|
|
step_id="discovery_confirm",
|
|
|
|
description_placeholders={
|
2022-05-31 08:33:34 +00:00
|
|
|
CONF_PRODUCT_TYPE: cast(str, self.config[CONF_PRODUCT_TYPE]),
|
|
|
|
CONF_SERIAL: cast(str, self.config[CONF_SERIAL]),
|
|
|
|
CONF_IP_ADDRESS: cast(str, self.config[CONF_IP_ADDRESS]),
|
2022-01-11 00:23:31 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-12-16 15:53:54 +00:00
|
|
|
async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
|
|
|
|
"""Handle re-auth if API was disabled."""
|
|
|
|
|
|
|
|
self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
|
|
|
|
return await self.async_step_reauth_confirm()
|
|
|
|
|
|
|
|
async def async_step_reauth_confirm(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
|
|
|
"""Confirm reauth dialog."""
|
|
|
|
|
|
|
|
if user_input is not None:
|
|
|
|
assert self.entry is not None
|
|
|
|
|
|
|
|
error = await self._async_try_connect(self.entry.data[CONF_IP_ADDRESS])
|
|
|
|
if error is not None:
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="reauth_confirm",
|
|
|
|
errors={"base": error},
|
|
|
|
)
|
|
|
|
|
|
|
|
await self.hass.config_entries.async_reload(self.entry.entry_id)
|
|
|
|
return self.async_abort(reason="reauth_successful")
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="reauth_confirm",
|
|
|
|
)
|
|
|
|
|
2022-01-11 00:23:31 +00:00
|
|
|
@staticmethod
|
2022-12-16 15:53:54 +00:00
|
|
|
async def _async_try_connect(ip_address: str) -> str | None:
|
2022-01-11 00:23:31 +00:00
|
|
|
"""Try to connect."""
|
|
|
|
|
2022-12-16 15:53:54 +00:00
|
|
|
_LOGGER.debug("config_flow _async_try_connect")
|
2022-01-11 00:23:31 +00:00
|
|
|
|
|
|
|
# Make connection with device
|
|
|
|
# This is to test the connection and to get info for unique_id
|
2022-05-25 07:05:11 +00:00
|
|
|
energy_api = HomeWizardEnergy(ip_address)
|
2022-01-11 00:23:31 +00:00
|
|
|
|
|
|
|
try:
|
2022-12-16 15:53:54 +00:00
|
|
|
await energy_api.device()
|
2022-01-11 00:23:31 +00:00
|
|
|
|
2022-12-16 15:53:54 +00:00
|
|
|
except DisabledError:
|
2022-01-11 00:23:31 +00:00
|
|
|
_LOGGER.error("API disabled, API must be enabled in the app")
|
2022-12-16 15:53:54 +00:00
|
|
|
return "api_not_enabled"
|
2022-01-11 00:23:31 +00:00
|
|
|
|
2022-05-25 07:05:11 +00:00
|
|
|
except UnsupportedError as ex:
|
|
|
|
_LOGGER.error("API version unsuppored")
|
|
|
|
raise AbortFlow("unsupported_api_version") from ex
|
|
|
|
|
2022-06-20 08:30:57 +00:00
|
|
|
except RequestError as ex:
|
2022-12-16 15:53:54 +00:00
|
|
|
_LOGGER.exception(ex)
|
|
|
|
return "network_error"
|
2022-06-20 08:30:57 +00:00
|
|
|
|
2022-01-11 12:41:57 +00:00
|
|
|
except Exception as ex:
|
2022-12-16 15:53:54 +00:00
|
|
|
_LOGGER.exception(ex)
|
2022-01-11 00:23:31 +00:00
|
|
|
raise AbortFlow("unknown_error") from ex
|
|
|
|
|
|
|
|
finally:
|
|
|
|
await energy_api.close()
|
|
|
|
|
2022-12-16 15:53:54 +00:00
|
|
|
return None
|
2022-01-11 00:23:31 +00:00
|
|
|
|
|
|
|
async def _async_set_and_check_unique_id(self, entry_info: dict[str, Any]) -> None:
|
|
|
|
"""Validate if entry exists."""
|
|
|
|
|
|
|
|
_LOGGER.debug("config_flow _async_set_and_check_unique_id")
|
|
|
|
|
|
|
|
await self.async_set_unique_id(
|
|
|
|
f"{entry_info[CONF_PRODUCT_TYPE]}_{entry_info[CONF_SERIAL]}"
|
|
|
|
)
|
|
|
|
self._abort_if_unique_id_configured(
|
|
|
|
updates={CONF_IP_ADDRESS: entry_info[CONF_IP_ADDRESS]}
|
|
|
|
)
|