config_flow.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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, individual_config_schema
  7. from .device import TuyaLocalDevice
  8. from .const import CONF_DEVICE_ID, CONF_LOCAL_KEY
  9. _LOGGER = logging.getLogger(__name__)
  10. class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
  11. VERSION = 1
  12. CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
  13. async def async_step_user(self, user_input=None):
  14. errors = {}
  15. if user_input is not None:
  16. await self.async_set_unique_id(user_input[CONF_DEVICE_ID])
  17. self._abort_if_unique_id_configured()
  18. connect_success = await async_test_connection(user_input, self.hass)
  19. if connect_success:
  20. title = user_input[CONF_NAME]
  21. del user_input[CONF_NAME]
  22. return self.async_create_entry(title=title, data=user_input)
  23. else:
  24. errors["base"] = "connection"
  25. return self.async_show_form(
  26. step_id="user",
  27. data_schema=vol.Schema(individual_config_schema(user_input or {})),
  28. errors=errors,
  29. )
  30. async def async_step_import(self, user_input):
  31. title = user_input[CONF_NAME]
  32. del user_input[CONF_NAME]
  33. user_input[config_entries.SOURCE_IMPORT] = True
  34. current_entries = self.hass.config_entries.async_entries(DOMAIN)
  35. existing_entry = next(
  36. (
  37. entry
  38. for entry in current_entries
  39. if entry.data[CONF_DEVICE_ID] == user_input[CONF_DEVICE_ID]
  40. ),
  41. None,
  42. )
  43. if existing_entry is not None:
  44. return self.async_abort(reason="imported")
  45. else:
  46. await self.async_set_unique_id(user_input[CONF_DEVICE_ID])
  47. return self.async_create_entry(title=title, data=user_input)
  48. @staticmethod
  49. @callback
  50. def async_get_options_flow(config_entry):
  51. return OptionsFlowHandler(config_entry)
  52. class OptionsFlowHandler(config_entries.OptionsFlow):
  53. def __init__(self, config_entry):
  54. """Initialize options flow."""
  55. self.config_entry = config_entry
  56. async def async_step_init(self, user_input=None):
  57. return await self.async_step_user(user_input)
  58. async def async_step_user(self, user_input=None):
  59. """Manage the options."""
  60. errors = {}
  61. config = {**self.config_entry.data, **self.config_entry.options}
  62. if user_input is not None:
  63. config = {**config, **user_input}
  64. connect_success = await async_test_connection(config, self.hass)
  65. if connect_success:
  66. return self.async_create_entry(title="", data=user_input)
  67. else:
  68. errors["base"] = "connection"
  69. return self.async_show_form(
  70. step_id="user",
  71. data_schema=vol.Schema(
  72. individual_config_schema(defaults=config, options_only=True)
  73. ),
  74. errors=errors,
  75. )
  76. async def async_test_connection(config: dict, hass: HomeAssistant):
  77. device = TuyaLocalDevice(
  78. "Test", config[CONF_DEVICE_ID], config[CONF_HOST], config[CONF_LOCAL_KEY], hass
  79. )
  80. await device.async_refresh()
  81. return device.has_returned_state