config_flow.py 3.8 KB

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