config_flow.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """Config flow for Gardena integration."""
  2. import logging
  3. from collections import OrderedDict
  4. import homeassistant.helpers.config_validation as cv
  5. import voluptuous as vol
  6. from gardena.smart_system import SmartSystem
  7. from homeassistant import config_entries
  8. from homeassistant.const import (
  9. CONF_CLIENT_ID,
  10. CONF_CLIENT_SECRET,
  11. CONF_ID,
  12. )
  13. from homeassistant.core import callback
  14. from .const import (
  15. DOMAIN,
  16. CONF_MOWER_DURATION,
  17. CONF_SMART_IRRIGATION_DURATION,
  18. CONF_SMART_WATERING_DURATION,
  19. DEFAULT_MOWER_DURATION,
  20. DEFAULT_SMART_IRRIGATION_DURATION,
  21. DEFAULT_SMART_WATERING_DURATION,
  22. )
  23. _LOGGER = logging.getLogger(__name__)
  24. DEFAULT_OPTIONS = {
  25. CONF_MOWER_DURATION: DEFAULT_MOWER_DURATION,
  26. CONF_SMART_IRRIGATION_DURATION: DEFAULT_SMART_IRRIGATION_DURATION,
  27. CONF_SMART_WATERING_DURATION: DEFAULT_SMART_WATERING_DURATION,
  28. }
  29. class GardenaSmartSystemConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
  30. """Handle a config flow for Gardena."""
  31. VERSION = 1
  32. CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_PUSH
  33. async def _show_setup_form(self, errors=None):
  34. """Show the setup form to the user."""
  35. errors = {}
  36. fields = OrderedDict()
  37. fields[vol.Required(CONF_CLIENT_ID)] = str
  38. fields[vol.Required(CONF_CLIENT_SECRET)] = str
  39. return self.async_show_form(
  40. step_id="user", data_schema=vol.Schema(fields), errors=errors
  41. )
  42. async def async_step_user(self, user_input=None):
  43. """Handle the initial step."""
  44. if user_input is None:
  45. return await self._show_setup_form()
  46. errors = {}
  47. # try:
  48. # await try_connection(
  49. # user_input[CONF_CLIENT_ID],
  50. # user_input[CONF_CLIENT_SECRET])
  51. # except Exception: # pylint: disable=broad-except
  52. # _LOGGER.exception("Unexpected exception")
  53. # errors["base"] = "unknown"
  54. # return await self._show_setup_form(errors)
  55. unique_id = user_input[CONF_CLIENT_ID]
  56. await self.async_set_unique_id(unique_id)
  57. self._abort_if_unique_id_configured()
  58. return self.async_create_entry(
  59. title="",
  60. data={
  61. CONF_ID: unique_id,
  62. CONF_CLIENT_ID: user_input[CONF_CLIENT_ID],
  63. CONF_CLIENT_SECRET: user_input[CONF_CLIENT_SECRET]
  64. })
  65. @staticmethod
  66. @callback
  67. def async_get_options_flow(config_entry):
  68. return GardenaSmartSystemOptionsFlowHandler(config_entry)
  69. class GardenaSmartSystemOptionsFlowHandler(config_entries.OptionsFlow):
  70. def __init__(self, config_entry):
  71. """Initialize Gardena Smart Sytem options flow."""
  72. self.config_entry = config_entry
  73. async def async_step_init(self, user_input=None):
  74. """Manage the options."""
  75. return await self.async_step_user()
  76. async def async_step_user(self, user_input=None):
  77. """Handle a flow initialized by the user."""
  78. errors = {}
  79. if user_input is not None:
  80. # TODO: Validate options (min, max values)
  81. return self.async_create_entry(title="", data=user_input)
  82. fields = OrderedDict()
  83. fields[vol.Optional(
  84. CONF_MOWER_DURATION,
  85. default=self.config_entry.options.get(
  86. CONF_MOWER_DURATION, DEFAULT_MOWER_DURATION))] = cv.positive_int
  87. fields[vol.Optional(
  88. CONF_SMART_IRRIGATION_DURATION,
  89. default=self.config_entry.options.get(
  90. CONF_SMART_IRRIGATION_DURATION, DEFAULT_SMART_IRRIGATION_DURATION))] = cv.positive_int
  91. fields[vol.Optional(
  92. CONF_SMART_WATERING_DURATION,
  93. default=self.config_entry.options.get(
  94. CONF_SMART_WATERING_DURATION, DEFAULT_SMART_WATERING_DURATION))] = cv.positive_int
  95. return self.async_show_form(step_id="user", data_schema=vol.Schema(fields), errors=errors)
  96. async def try_connection(client_id, client_secret):
  97. _LOGGER.debug("Trying to connect to Gardena during setup")
  98. smart_system = SmartSystem(client_id=client_id, client_secret=client_secret)
  99. await smart_system.authenticate()
  100. await smart_system.update_locations()
  101. await smart_system.quit()
  102. _LOGGER.debug("Successfully connected to Gardena during setup")