1 """Config flow for the integration."""
8 from madvr.madvr
import HeartBeatError, Madvr
9 import voluptuous
as vol
19 from .const
import DEFAULT_NAME, DEFAULT_PORT, DOMAIN
20 from .errors
import CannotConnect
22 _LOGGER = logging.getLogger(__name__)
24 STEP_USER_DATA_SCHEMA = vol.Schema(
26 vol.Required(CONF_HOST): str,
27 vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
35 """Handle a config flow for the integration."""
40 self, user_input: dict[str, Any] |
None =
None
41 ) -> ConfigFlowResult:
42 """Handle the initial step."""
46 self, user_input: dict[str, Any] |
None =
None
47 ) -> ConfigFlowResult:
48 """Handle a reconfiguration flow initialized by the user."""
52 self, user_input: dict[str, Any] |
None =
None, step_id: str =
"user"
53 ) -> ConfigFlowResult:
54 """Handle the configuration step for both initial setup and reconfiguration."""
55 errors: dict[str, str] = {}
57 if user_input
is not None:
58 _LOGGER.debug(
"User input: %s", user_input)
59 host = user_input[CONF_HOST]
60 port = user_input[CONF_PORT]
65 _LOGGER.error(
"CannotConnect error caught")
66 errors[
"base"] =
"cannot_connect"
69 errors[
"base"] =
"no_mac"
71 _LOGGER.debug(
"MAC address found: %s", mac)
77 _LOGGER.debug(
"Reconfiguration done")
80 data={**user_input, CONF_HOST: host, CONF_PORT: port},
85 _LOGGER.debug(
"Configuration successful")
90 _LOGGER.debug(
"Showing form with errors: %s", errors)
94 STEP_USER_DATA_SCHEMA, user_input
101 """Test if we can connect to the device and grab the mac."""
102 madvr_client = Madvr(host=host, port=port, loop=hass.loop)
103 _LOGGER.debug(
"Testing connection to madVR at %s:%s", host, port)
106 await asyncio.wait_for(madvr_client.open_connection(), timeout=15)
108 except (TimeoutError, aiohttp.ClientError, OSError, HeartBeatError)
as err:
109 _LOGGER.error(
"Error connecting to madVR: %s", err)
110 raise CannotConnect
from err
113 if not madvr_client.connected:
117 await madvr_client.async_add_tasks()
121 while not madvr_client.mac_address
and retry_time > 0:
122 await asyncio.sleep(RETRY_INTERVAL)
125 mac_address = madvr_client.mac_address
127 _LOGGER.debug(
"Connected to madVR with MAC: %s", mac_address)
130 _LOGGER.debug(
"Connection test successful")
135 """Close the test connection."""
136 _LOGGER.debug(
"Closing test connection")
138 await madvr_client.async_cancel_tasks()
139 await madvr_client.close_connection()
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
ConfigFlowResult _handle_config_step(self, dict[str, Any]|None user_input=None, str step_id="user")
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
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_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
ConfigEntry _get_reconfigure_entry(self)
None _abort_if_unique_id_mismatch(self, *str reason="unique_id_mismatch", Mapping[str, str]|None description_placeholders=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)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_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)
None close_test_connection(Madvr madvr_client)
str test_connection(HomeAssistant hass, str host, int port)