config_flow.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import logging
  2. import voluptuous as vol
  3. from homeassistant import config_entries, data_entry_flow
  4. from homeassistant.const import CONF_HOST, CONF_NAME
  5. from homeassistant.core import HomeAssistant, callback
  6. from . import DOMAIN
  7. from .configuration import individual_config_schema
  8. from .device import TuyaLocalDevice
  9. from .const import CONF_DEVICE_ID, CONF_LOCAL_KEY, CONF_TYPE
  10. from .helpers.device_config import config_for_legacy_use
  11. _LOGGER = logging.getLogger(__name__)
  12. class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
  13. VERSION = 2
  14. CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
  15. async def async_step_user(self, user_input=None):
  16. errors = {}
  17. if user_input is not None:
  18. await self.async_set_unique_id(user_input[CONF_DEVICE_ID])
  19. self._abort_if_unique_id_configured()
  20. self.device = await async_test_connection(user_input, self.hass)
  21. if self.device:
  22. self.data = user_input
  23. return self.async_step_select_type()
  24. else:
  25. errors["base"] = "connection"
  26. return self.async_show_form(
  27. step_id="user",
  28. data_schema=vol.Schema(individual_config_schema(user_input or {})),
  29. errors=errors,
  30. )
  31. async def async_step_select_type(self, user_input=None):
  32. if user_input is not None:
  33. self.data[CONF_TYPE] = user_input[CONF_TYPE]
  34. types = []
  35. async for type in self.device.async_possible_types():
  36. types.append(type)
  37. if types:
  38. return self.async_show_form(
  39. step_id="type",
  40. data_schema=vol.Schema({vol.Required(CONF_TYPE): vol.In(types)}),
  41. )
  42. else:
  43. return self.async_abort(reason="not_supported")
  44. async def async_step_choose_entities(self, user_input=None):
  45. if user_input is not None:
  46. title = user_input[CONF_NAME]
  47. del user_input[CONF_NAME]
  48. return self.async_create_entry(
  49. title=title, data={**self.data, **user_input}
  50. )
  51. config = config_for_legacy_use(self.data[CONF_TYPE])
  52. schema = {vol.Required(CONF_NAME, default=config.name): str}
  53. e = config.primary_entity
  54. schema[vol.Optional(e.entity, default=True)] = bool
  55. for e in config.secondary_entities():
  56. schema[vol.Optional(e.entity, default=not e.deprecated)] = bool
  57. return self.async_show_form(
  58. step_id="entities",
  59. data_schema=vol.Schema(schema),
  60. )
  61. @staticmethod
  62. @callback
  63. def async_get_options_flow(config_entry):
  64. return OptionsFlowHandler(config_entry)
  65. class OptionsFlowHandler(config_entries.OptionsFlow):
  66. def __init__(self, config_entry):
  67. """Initialize options flow."""
  68. self.config_entry = config_entry
  69. async def async_step_init(self, user_input=None):
  70. return await self.async_step_user(user_input)
  71. async def async_step_user(self, user_input=None):
  72. """Manage the options."""
  73. errors = {}
  74. config = {**self.config_entry.data, **self.config_entry.options}
  75. if user_input is not None:
  76. config = {**config, **user_input}
  77. connect_success = await async_test_connection(config, self.hass)
  78. if connect_success:
  79. return self.async_create_entry(title="", data=user_input)
  80. else:
  81. errors["base"] = "connection"
  82. return self.async_show_form(
  83. step_id="user",
  84. data_schema=vol.Schema(
  85. individual_config_schema(defaults=config, options_only=True)
  86. ),
  87. errors=errors,
  88. )
  89. async def async_test_connection(config: dict, hass: HomeAssistant):
  90. device = TuyaLocalDevice(
  91. "Test", config[CONF_DEVICE_ID], config[CONF_HOST], config[CONF_LOCAL_KEY], hass
  92. )
  93. await device.async_refresh()
  94. return device if device.has_returned_state else None