config_flow.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. """Adds config flow for ZYXEL."""
  2. import logging
  3. from typing import Any
  4. from homeassistant.config_entries import (
  5. ConfigEntry,
  6. ConfigFlow,
  7. FlowResult,
  8. OptionsFlow,
  9. )
  10. from homeassistant.const import (
  11. CONF_SCAN_INTERVAL,
  12. CONF_USERNAME,
  13. CONF_PASSWORD,
  14. CONF_IP_ADDRESS
  15. )
  16. from homeassistant.core import callback
  17. from homeassistant.helpers.aiohttp_client import async_create_clientsession
  18. import homeassistant.helpers.config_validation as cv
  19. import voluptuous as vol
  20. from .api import (
  21. RouterApiClient,
  22. RouterApiClientError,
  23. RouterApiClientLoginError,
  24. RouterApiClientCommunicationError,
  25. RouterApiClientResponseError,
  26. )
  27. from .const import (DEFAULT_SCAN_INTERVAL,
  28. DOMAIN,
  29. DEFAULT_IP,
  30. DEFAULT_USER)
  31. _LOGGER: logging.Logger = logging.getLogger(__package__)
  32. class RouterFlowHandler(ConfigFlow, domain=DOMAIN):
  33. """Config flow for Odido Router."""
  34. VERSION = 2
  35. async def async_step_user(
  36. self, user_input: dict[str, Any] | None = None
  37. ) -> FlowResult:
  38. """Handle a flow initialized by the user."""
  39. _errors = {}
  40. if user_input is not None:
  41. try:
  42. await self._validate_user_input(
  43. user_input[CONF_IP_ADDRESS],
  44. user_input[CONF_USERNAME],
  45. user_input[CONF_PASSWORD],
  46. )
  47. except RouterApiClientCommunicationError as exception:
  48. _LOGGER.error(exception)
  49. _errors["base"] = "general"
  50. except RouterApiClientLoginError as exception:
  51. _LOGGER.error(exception)
  52. _errors["base"] = "api_key"
  53. except RouterApiClientResponseError as exception:
  54. _LOGGER.error(exception)
  55. _errors["base"] = "daily_limit"
  56. else:
  57. return self.async_create_entry(
  58. title=user_input[CONF_IP_ADDRESS], data=user_input
  59. )
  60. return self.async_show_form(
  61. step_id="user",
  62. data_schema=vol.Schema(
  63. {
  64. vol.Required(
  65. CONF_IP_ADDRESS, default=DEFAULT_IP
  66. ): str,
  67. vol.Required(
  68. CONF_USERNAME, default=DEFAULT_USER
  69. ): str,
  70. vol.Required(CONF_PASSWORD): str
  71. }
  72. ),
  73. errors=_errors,
  74. )
  75. async def _validate_user_input(self, endpoint: str, user: str, password: str):
  76. """Validate user input."""
  77. session = async_create_clientsession(self.hass)
  78. client = RouterApiClient(endpoint=endpoint,
  79. user=user,
  80. password=password,
  81. session=session)
  82. await client.async_login()
  83. @staticmethod
  84. @callback
  85. def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow:
  86. return RouterOptionsFlowHandler()
  87. class RouterOptionsFlowHandler(OptionsFlow):
  88. """Router config flow options handler."""
  89. async def async_step_init(
  90. self, user_input: dict[str, Any] | None = None
  91. ) -> FlowResult:
  92. """Manage the options."""
  93. if user_input is not None:
  94. return self.async_create_entry(
  95. title=self.config_entry.data.get(CONF_IP_ADDRESS), data=user_input
  96. )
  97. return self.async_show_form(
  98. step_id="init",
  99. data_schema=vol.Schema(
  100. {
  101. vol.Required(
  102. CONF_SCAN_INTERVAL,
  103. default=self.config_entry.options.get(
  104. CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
  105. ),
  106. ): vol.All(vol.Coerce(int), vol.Range(min=30, max=3600))
  107. }
  108. ),
  109. )