Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure firmata component."""
2 
3 import logging
4 from typing import Any
5 
6 from pymata_express.pymata_express_serial import serial
7 
8 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
9 from homeassistant.const import CONF_NAME
10 
11 from .board import get_board
12 from .const import CONF_SERIAL_PORT, DOMAIN
13 
14 _LOGGER = logging.getLogger(__name__)
15 
16 
17 class FirmataFlowHandler(ConfigFlow, domain=DOMAIN):
18  """Handle a firmata config flow."""
19 
20  VERSION = 1
21 
22  async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
23  """Import a firmata board as a config entry.
24 
25  This flow is triggered by `async_setup` for configured boards.
26 
27  This will execute for any board that does not have a
28  config entry yet (based on entry_id). It validates a connection
29  and then adds the entry.
30  """
31  name = f"serial-{import_data[CONF_SERIAL_PORT]}"
32  import_data[CONF_NAME] = name
33 
34  # Connect to the board to verify connection and then shutdown
35  # If either fail then we cannot continue
36  _LOGGER.debug("Connecting to Firmata board %s to test connection", name)
37  try:
38  api = await get_board(import_data)
39  await api.shutdown()
40  except RuntimeError as err:
41  _LOGGER.error("Error connecting to PyMata board %s: %s", name, err)
42  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
43  except serial.SerialTimeoutException as err:
44  _LOGGER.error(
45  "Timeout writing to serial port for PyMata board %s: %s", name, err
46  )
47  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
48  except serial.SerialException as err:
49  _LOGGER.error(
50  "Error connecting to serial port for PyMata board %s: %s", name, err
51  )
52  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
53  _LOGGER.debug("Connection test to Firmata board %s successful", name)
54 
55  return self.async_create_entryasync_create_entryasync_create_entry(title=import_data[CONF_NAME], data=import_data)
ConfigFlowResult async_step_import(self, dict[str, Any] import_data)
Definition: config_flow.py:22
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)
_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)
PymataExpress get_board(Mapping data)
Definition: board.py:138