1 """Config flow for the D-Link Power Plug integration."""
3 from __future__
import annotations
8 from pyW215.pyW215
import SmartPlug
9 import voluptuous
as vol
15 from .const
import CONF_USE_LEGACY_PROTOCOL, DEFAULT_NAME, DEFAULT_USERNAME, DOMAIN
17 _LOGGER = logging.getLogger(__name__)
21 """Handle a config flow for D-Link Power Plug."""
24 """Initialize a D-Link Power Plug flow."""
28 self, discovery_info: dhcp.DhcpServiceInfo
29 ) -> ConfigFlowResult:
30 """Handle dhcp discovery."""
33 for entry
in self.hass.config_entries.async_entries(DOMAIN):
34 if not entry.unique_id
and entry.data[CONF_HOST] == discovery_info.ip:
36 self.hass.config_entries.async_update_entry(
37 entry, unique_id=discovery_info.macaddress
45 self, user_input: dict[str, Any] |
None =
None
46 ) -> ConfigFlowResult:
47 """Allow the user to confirm adding the device."""
49 if user_input
is not None:
51 error := await self.hass.async_add_executor_job(
57 data=user_input | {CONF_HOST: self.
ip_addressip_address},
59 errors[
"base"] = error
61 user_input = user_input
or {}
63 step_id=
"confirm_discovery",
64 data_schema=vol.Schema(
68 default=user_input.get(CONF_USERNAME, DEFAULT_USERNAME),
70 vol.Required(CONF_PASSWORD): str,
71 vol.Required(CONF_USE_LEGACY_PROTOCOL): bool,
78 self, user_input: dict[str, Any] |
None =
None
79 ) -> ConfigFlowResult:
80 """Handle a flow initiated by the user."""
82 if user_input
is not None:
86 error := await self.hass.async_add_executor_job(
94 errors[
"base"] = error
96 user_input = user_input
or {}
99 data_schema=vol.Schema(
102 CONF_HOST, default=user_input.get(CONF_HOST, self.
ip_addressip_address)
106 default=user_input.get(CONF_USERNAME, DEFAULT_USERNAME),
108 vol.Required(CONF_PASSWORD): str,
109 vol.Required(CONF_USE_LEGACY_PROTOCOL): bool,
116 """Try connecting to D-Link Power Plug."""
118 smartplug = SmartPlug(
119 user_input.get(CONF_HOST, self.
ip_addressip_address),
120 user_input[CONF_PASSWORD],
121 user_input[CONF_USERNAME],
122 user_input[CONF_USE_LEGACY_PROTOCOL],
125 _LOGGER.exception(
"Unexpected exception")
127 if not smartplug.authenticated
and smartplug.use_legacy_protocol:
128 return "cannot_connect"
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_dhcp(self, dhcp.DhcpServiceInfo discovery_info)
ConfigFlowResult async_step_confirm_discovery(self, dict[str, Any]|None user_input=None)
str|None _try_connect(self, dict[str, Any] user_input)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)