@@ -68,17 +68,8 @@ async def async_step_user(self, user_input=None):
6868 """Handle the initial step."""
6969 errors = {}
7070 if user_input is not None :
71- try :
72- info = await validate_input (self .hass , user_input )
73- except CannotConnect :
74- errors ["base" ] = "cannot_connect"
75- except InvalidAuth :
76- errors ["base" ] = "invalid_auth"
77- except Exception : # pylint: disable=broad-except
78- _LOGGER .exception ("Unexpected exception" )
79- errors ["base" ] = "unknown"
80-
81- if "base" not in errors :
71+ info , errors = await self ._async_validate_or_error (user_input )
72+ if not errors :
8273 await self .async_set_unique_id (info ["mac_addr" ])
8374 self ._abort_if_unique_id_configured ()
8475 return self .async_create_entry (title = info ["title" ], data = user_input )
@@ -119,8 +110,31 @@ async def async_step_zeroconf(self, discovery_info):
119110
120111 async def async_step_import (self , user_input ):
121112 """Handle import."""
113+ if user_input :
114+ info , errors = await self ._async_validate_or_error (user_input )
115+ if not errors :
116+ await self .async_set_unique_id (
117+ info ["mac_addr" ], raise_on_progress = False
118+ )
119+ self ._abort_if_unique_id_configured ()
120+ return self .async_create_entry (title = info ["title" ], data = user_input )
122121 return await self .async_step_user (user_input )
123122
123+ async def _async_validate_or_error (self , user_input ):
124+ """Validate doorbird or error."""
125+ errors = {}
126+ info = {}
127+ try :
128+ info = await validate_input (self .hass , user_input )
129+ except CannotConnect :
130+ errors ["base" ] = "cannot_connect"
131+ except InvalidAuth :
132+ errors ["base" ] = "invalid_auth"
133+ except Exception : # pylint: disable=broad-except
134+ _LOGGER .exception ("Unexpected exception" )
135+ errors ["base" ] = "unknown"
136+ return info , errors
137+
124138 @staticmethod
125139 @callback
126140 def async_get_options_flow (config_entry ):
0 commit comments