config_flow.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 .device import TuyaLocalDevice
  8. from .const import CONF_DEVICE_ID, CONF_LOCAL_KEY, CONF_TYPE
  9. from .helpers.device_config import config_for_legacy_use
  10. _LOGGER = logging.getLogger(__name__)
  11. class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
  12. VERSION = 3
  13. CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
  14. device = None
  15. data = {}
  16. async def async_step_user(self, user_input=None):
  17. errors = {}
  18. devid_opts = {}
  19. host_opts = {}
  20. key_opts = {}
  21. if user_input is not None:
  22. await self.async_set_unique_id(user_input[CONF_DEVICE_ID])
  23. self._abort_if_unique_id_configured()
  24. self.device = await async_test_connection(user_input, self.hass)
  25. if self.device:
  26. self.data = user_input
  27. return await self.async_step_select_type()
  28. else:
  29. errors["base"] = "connection"
  30. devid_opts["default"] = user_input[CONF_DEVICE_ID]
  31. host_opts["default"] = user_input[CONF_HOST]
  32. key_opts["default"] = user_input[CONF_LOCAL_KEY]
  33. return self.async_show_form(
  34. step_id="user",
  35. data_schema=vol.Schema(
  36. {
  37. vol.Required(CONF_DEVICE_ID, **devid_opts): str,
  38. vol.Required(CONF_HOST, **host_opts): str,
  39. vol.Required(CONF_LOCAL_KEY, **key_opts): str,
  40. }
  41. ),
  42. errors=errors,
  43. )
  44. async def async_step_select_type(self, user_input=None):
  45. if user_input is not None:
  46. self.data[CONF_TYPE] = user_input[CONF_TYPE]
  47. return await self.async_step_choose_entities()
  48. types = []
  49. async for type in self.device.async_possible_types():
  50. types.append(type.legacy_type)
  51. if types:
  52. return self.async_show_form(
  53. step_id="select_type",
  54. data_schema=vol.Schema({vol.Required(CONF_TYPE): vol.In(types)}),
  55. )
  56. else:
  57. return self.async_abort(reason="not_supported")
  58. async def async_step_choose_entities(self, user_input=None):
  59. if user_input is not None:
  60. title = user_input[CONF_NAME]
  61. del user_input[CONF_NAME]
  62. return self.async_create_entry(
  63. title=title, data={**self.data, **user_input}
  64. )
  65. config = config_for_legacy_use(self.data[CONF_TYPE])
  66. schema = {vol.Required(CONF_NAME, default=config.name): str}
  67. e = config.primary_entity
  68. schema[vol.Optional(e.entity, default=True)] = bool
  69. for e in config.secondary_entities():
  70. schema[vol.Optional(e.entity, default=not e.deprecated)] = bool
  71. return self.async_show_form(
  72. step_id="choose_entities",
  73. data_schema=vol.Schema(schema),
  74. )
  75. @staticmethod
  76. @callback
  77. def async_get_options_flow(config_entry):
  78. return OptionsFlowHandler(config_entry)
  79. class OptionsFlowHandler(config_entries.OptionsFlow):
  80. def __init__(self, config_entry):
  81. """Initialize options flow."""
  82. self.config_entry = config_entry
  83. async def async_step_init(self, user_input=None):
  84. return await self.async_step_user(user_input)
  85. async def async_step_user(self, user_input=None):
  86. """Manage the options."""
  87. errors = {}
  88. config = {**self.config_entry.data, **self.config_entry.options}
  89. if user_input is not None:
  90. config = {**config, **user_input}
  91. device = await async_test_connection(config, self.hass)
  92. if device:
  93. return self.async_create_entry(title="", data=user_input)
  94. else:
  95. errors["base"] = "connection"
  96. schema = {
  97. vol.Required(CONF_LOCAL_KEY, default=config.get(CONF_LOCAL_KEY, "")): str,
  98. vol.Required(CONF_HOST, default=config.get(CONF_HOST, "")): str,
  99. }
  100. cfg = config_for_legacy_use(config[CONF_TYPE])
  101. if cfg is None:
  102. return self.async_abort(reason="not_supported")
  103. e = cfg.primary_entity
  104. schema[vol.Optional(e.entity, default=config.get(e.entity, True))] = bool
  105. for e in cfg.secondary_entities():
  106. schema[vol.Optional(e.entity, default=config.get(e.entity, False))] = bool
  107. return self.async_show_form(
  108. step_id="user",
  109. data_schema=vol.Schema(schema),
  110. errors=errors,
  111. )
  112. async def async_test_connection(config: dict, hass: HomeAssistant):
  113. device = TuyaLocalDevice(
  114. "Test", config[CONF_DEVICE_ID], config[CONF_HOST], config[CONF_LOCAL_KEY], hass
  115. )
  116. await device.async_refresh()
  117. return device if device.has_returned_state else None