config_flow.py 3.9 KB

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