config_flow.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. self.hass.config_entries.async_update_entry(
  45. existing_entry, title=title, options=user_input
  46. )
  47. return self.async_abort(reason="imported")
  48. else:
  49. await self.async_set_unique_id(user_input[CONF_DEVICE_ID])
  50. return self.async_create_entry(title=title, data=user_input)
  51. @staticmethod
  52. @callback
  53. def async_get_options_flow(config_entry):
  54. return OptionsFlowHandler(config_entry)
  55. class OptionsFlowHandler(config_entries.OptionsFlow):
  56. def __init__(self, config_entry):
  57. """Initialize options flow."""
  58. self.config_entry = config_entry
  59. async def async_step_init(self, user_input=None):
  60. if self.config_entry.data.get(config_entries.SOURCE_IMPORT, False):
  61. return await self.async_step_imported(user_input)
  62. else:
  63. return await self.async_step_user(user_input)
  64. async def async_step_user(self, user_input=None):
  65. """Manage the options."""
  66. errors = {}
  67. config = {**self.config_entry.data, **self.config_entry.options}
  68. if user_input is not None:
  69. config = {**config, **user_input}
  70. connect_success = await async_test_connection(config, self.hass)
  71. if connect_success:
  72. return self.async_create_entry(title="", data=user_input)
  73. else:
  74. errors["base"] = "connection"
  75. return self.async_show_form(
  76. step_id="user",
  77. data_schema=vol.Schema(
  78. individual_config_schema(defaults=config, options_only=True)
  79. ),
  80. errors=errors,
  81. )
  82. async def async_step_imported(self, user_input=None):
  83. return {**self.async_abort(reason="imported"), "data": {}}
  84. async def async_test_connection(config: dict, hass: HomeAssistant):
  85. device = TuyaLocalDevice(
  86. "Test", config[CONF_DEVICE_ID], config[CONF_HOST], config[CONF_LOCAL_KEY], hass
  87. )
  88. await device.async_refresh()
  89. return device.get_property("1") is not None or device.get_property("3") is not None