mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Move HomeWizard config options to class (#135778)
This commit is contained in:
parent
5ca68cb273
commit
55bde60f1a
@ -3,7 +3,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
from typing import Any, NamedTuple
|
from typing import Any
|
||||||
|
|
||||||
from homewizard_energy import HomeWizardEnergyV1
|
from homewizard_energy import HomeWizardEnergyV1
|
||||||
from homewizard_energy.errors import DisabledError, RequestError, UnsupportedError
|
from homewizard_energy.errors import DisabledError, RequestError, UnsupportedError
|
||||||
@ -29,21 +29,15 @@ from .const import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class DiscoveryData(NamedTuple):
|
|
||||||
"""User metadata."""
|
|
||||||
|
|
||||||
ip: str
|
|
||||||
product_name: str
|
|
||||||
product_type: str
|
|
||||||
serial: str
|
|
||||||
|
|
||||||
|
|
||||||
class HomeWizardConfigFlow(ConfigFlow, domain=DOMAIN):
|
class HomeWizardConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a config flow for P1 meter."""
|
"""Handle a config flow for P1 meter."""
|
||||||
|
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
|
|
||||||
discovery: DiscoveryData
|
ip_address: str | None = None
|
||||||
|
product_name: str | None = None
|
||||||
|
product_type: str | None = None
|
||||||
|
serial: str | None = None
|
||||||
|
|
||||||
async def async_step_user(
|
async def async_step_user(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
@ -95,16 +89,12 @@ class HomeWizardConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
if (discovery_info.properties[CONF_PATH]) != "/api/v1":
|
if (discovery_info.properties[CONF_PATH]) != "/api/v1":
|
||||||
return self.async_abort(reason="unsupported_api_version")
|
return self.async_abort(reason="unsupported_api_version")
|
||||||
|
|
||||||
self.discovery = DiscoveryData(
|
self.ip_address = discovery_info.host
|
||||||
ip=discovery_info.host,
|
self.product_type = discovery_info.properties[CONF_PRODUCT_TYPE]
|
||||||
product_type=discovery_info.properties[CONF_PRODUCT_TYPE],
|
self.product_name = discovery_info.properties[CONF_PRODUCT_NAME]
|
||||||
product_name=discovery_info.properties[CONF_PRODUCT_NAME],
|
self.serial = discovery_info.properties[CONF_SERIAL]
|
||||||
serial=discovery_info.properties[CONF_SERIAL],
|
|
||||||
)
|
|
||||||
|
|
||||||
await self.async_set_unique_id(
|
await self.async_set_unique_id(f"{self.product_type}_{self.serial}")
|
||||||
f"{self.discovery.product_type}_{self.discovery.serial}"
|
|
||||||
)
|
|
||||||
self._abort_if_unique_id_configured(
|
self._abort_if_unique_id_configured(
|
||||||
updates={CONF_IP_ADDRESS: discovery_info.host}
|
updates={CONF_IP_ADDRESS: discovery_info.host}
|
||||||
)
|
)
|
||||||
@ -141,34 +131,39 @@ class HomeWizardConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Confirm discovery."""
|
"""Confirm discovery."""
|
||||||
|
assert self.ip_address
|
||||||
|
assert self.product_name
|
||||||
|
assert self.product_type
|
||||||
|
assert self.serial
|
||||||
|
|
||||||
errors: dict[str, str] | None = None
|
errors: dict[str, str] | None = None
|
||||||
if user_input is not None or not onboarding.async_is_onboarded(self.hass):
|
if user_input is not None or not onboarding.async_is_onboarded(self.hass):
|
||||||
try:
|
try:
|
||||||
await self._async_try_connect(self.discovery.ip)
|
await self._async_try_connect(self.ip_address)
|
||||||
except RecoverableError as ex:
|
except RecoverableError as ex:
|
||||||
LOGGER.error(ex)
|
LOGGER.error(ex)
|
||||||
errors = {"base": ex.error_code}
|
errors = {"base": ex.error_code}
|
||||||
else:
|
else:
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=self.discovery.product_name,
|
title=self.product_name,
|
||||||
data={CONF_IP_ADDRESS: self.discovery.ip},
|
data={CONF_IP_ADDRESS: self.ip_address},
|
||||||
)
|
)
|
||||||
|
|
||||||
self._set_confirm_only()
|
self._set_confirm_only()
|
||||||
|
|
||||||
# We won't be adding mac/serial to the title for devices
|
# We won't be adding mac/serial to the title for devices
|
||||||
# that users generally don't have multiple of.
|
# that users generally don't have multiple of.
|
||||||
name = self.discovery.product_name
|
name = self.product_name
|
||||||
if self.discovery.product_type not in ["HWE-P1", "HWE-WTR"]:
|
if self.product_type not in ["HWE-P1", "HWE-WTR"]:
|
||||||
name = f"{name} ({self.discovery.serial})"
|
name = f"{name} ({self.serial})"
|
||||||
self.context["title_placeholders"] = {"name": name}
|
self.context["title_placeholders"] = {"name": name}
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="discovery_confirm",
|
step_id="discovery_confirm",
|
||||||
description_placeholders={
|
description_placeholders={
|
||||||
CONF_PRODUCT_TYPE: self.discovery.product_type,
|
CONF_PRODUCT_TYPE: self.product_type,
|
||||||
CONF_SERIAL: self.discovery.serial,
|
CONF_SERIAL: self.serial,
|
||||||
CONF_IP_ADDRESS: self.discovery.ip,
|
CONF_IP_ADDRESS: self.ip_address,
|
||||||
},
|
},
|
||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user