1- """Config flow for Hello World integration."""
2- from __future__ import annotations
3-
4- import logging
1+ from logging import getLogger
52from typing import Any
63
74import voluptuous as vol
8-
95from homeassistant import config_entries , exceptions
106from homeassistant .core import HomeAssistant
117
128from .const import DOMAIN
139
14- logger = logging . getLogger ("lelight" )
10+ logger = getLogger (DOMAIN )
1511
16- # This is the schema that used to display the UI to the user. This simple
17- # schema has a single required host field, but it could include a number of fields
18- # such as username, password etc. See other components in the HA core code for
19- # further examples.
20- # Note the input displayed to the user will be translated. See the
21- # translations/<lang>.json file and strings.json. See here for further information:
22- # https://developers.home-assistant.io/docs/config_entries_config_flow_handler/#translations
23- # At the time of writing I found the translations created by the scaffold didn't
24- # quite work as documented and always gave me the "Lokalise key references" string
25- # (in square brackets), rather than the actual translated value. I did not attempt to
26- # figure this out or look further into it.
2712DATA_SCHEMA = vol .Schema ({"host" : str })
2813
2914
3015async def validate_input (hass : HomeAssistant , data : dict ) -> dict [str , Any ]:
31- """Validate the user input allows us to connect.
32- Data has the keys from DATA_SCHEMA with values provided by the user.
33- """
34- # Validate the data can be used to set up a connection.
35-
36- # This is a simple example to show an error in the UI for a short hostname
37- # The exceptions are defined at the end of this file, and are used in the
38- # `async_step_user` method below.
39- if len (data ["host" ]) < 3 :
40- raise InvalidHost
16+ if len (data ["host" ].strip ()) != 8 :
17+ raise InvalidHost ("Host name must be 8 characters long" )
4118
4219 return {"title" : data ["host" ].strip ()}
4320
@@ -46,37 +23,22 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
4623 """Handle a config flow for Hello World."""
4724
4825 VERSION = 1
49- # Pick one of the available connection classes in homeassistant/config_entries.py
50- # This tells HA if it should be asking for updates, or it'll be notified of updates
51- # automatically. This example uses PUSH, as the dummy hub will notify HA of
52- # changes.
5326 CONNECTION_CLASS = config_entries .CONN_CLASS_LOCAL_PUSH
5427
5528 async def async_step_user (self , user_input = None ):
5629 """Handle the initial step."""
57- # This goes through the steps to take the user through the setup process.
58- # Using this it is possible to update the UI and prompt for additional
59- # information. This example provides a single form (built from `DATA_SCHEMA`),
60- # and when that has some validated input, it calls `async_create_entry` to
61- # actually create the HA config entry. Note the "title" value is returned by
62- # `validate_input` above.
6330 errors = {}
6431 if user_input is not None :
6532 try :
6633 info = await validate_input (self .hass , user_input )
6734
6835 return self .async_create_entry (title = info ["title" ], data = user_input )
6936 except InvalidHost :
70- # The error string is set here, and should be translated.
71- # This example does not currently cover translations, see the
72- # comments on `DATA_SCHEMA` for further details.
73- # Set the error on the `host` field, not the entire form.
7437 errors ["host" ] = "cannot_connect"
7538 except Exception : # pylint: disable=broad-except
7639 logger .exception ("Unexpected exception" )
7740 errors ["base" ] = "unknown"
7841
79- # If there is no user input or there were errors, show the form again, including any errors that were found with the input.
8042 return self .async_show_form (
8143 step_id = "user" , data_schema = DATA_SCHEMA , errors = errors
8244 )
0 commit comments