config_flow.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. best_match = 0
  50. best_matching_type = None
  51. async for type in self.device.async_possible_types():
  52. types.append(type.legacy_type)
  53. q = type.match_quality(self.device._get_cached_state())
  54. if q > best_match:
  55. best_match = q
  56. best_matching_type = type.legacy_type
  57. if best_match < 100:
  58. best_match = int(best_match)
  59. dps = self.device._get_cached_state()
  60. _LOGGER.warning(
  61. f"Device matches {best_matching_type} with quality of {best_match}%. DPS: {dps}"
  62. )
  63. _LOGGER.warning(
  64. f"Report this to https://github.com/make-all/tuya-local/issues/"
  65. )
  66. if types:
  67. return self.async_show_form(
  68. step_id="select_type",
  69. data_schema=vol.Schema(
  70. {vol.Required(CONF_TYPE, default=best_matching_type): vol.In(types)}
  71. ),
  72. )
  73. else:
  74. return self.async_abort(reason="not_supported")
  75. async def async_step_choose_entities(self, user_input=None):
  76. if user_input is not None:
  77. title = user_input[CONF_NAME]
  78. del user_input[CONF_NAME]
  79. return self.async_create_entry(
  80. title=title, data={**self.data, **user_input}
  81. )
  82. config = config_for_legacy_use(self.data[CONF_TYPE])
  83. schema = {vol.Required(CONF_NAME, default=config.name): str}
  84. e = config.primary_entity
  85. schema[vol.Optional(e.entity, default=True)] = bool
  86. for e in config.secondary_entities():
  87. schema[vol.Optional(e.entity, default=not e.deprecated)] = bool
  88. return self.async_show_form(
  89. step_id="choose_entities",
  90. data_schema=vol.Schema(schema),
  91. )
  92. @staticmethod
  93. @callback
  94. def async_get_options_flow(config_entry):
  95. return OptionsFlowHandler(config_entry)
  96. class OptionsFlowHandler(config_entries.OptionsFlow):
  97. def __init__(self, config_entry):
  98. """Initialize options flow."""
  99. self.config_entry = config_entry
  100. async def async_step_init(self, user_input=None):
  101. return await self.async_step_user(user_input)
  102. async def async_step_user(self, user_input=None):
  103. """Manage the options."""
  104. errors = {}
  105. config = {**self.config_entry.data, **self.config_entry.options}
  106. if user_input is not None:
  107. config = {**config, **user_input}
  108. device = await async_test_connection(config, self.hass)
  109. if device:
  110. return self.async_create_entry(title="", data=user_input)
  111. else:
  112. errors["base"] = "connection"
  113. schema = {
  114. vol.Required(CONF_LOCAL_KEY, default=config.get(CONF_LOCAL_KEY, "")): str,
  115. vol.Required(CONF_HOST, default=config.get(CONF_HOST, "")): str,
  116. }
  117. cfg = config_for_legacy_use(config[CONF_TYPE])
  118. if cfg is None:
  119. return self.async_abort(reason="not_supported")
  120. e = cfg.primary_entity
  121. schema[vol.Optional(e.entity, default=config.get(e.entity, True))] = bool
  122. for e in cfg.secondary_entities():
  123. schema[vol.Optional(e.entity, default=config.get(e.entity, False))] = bool
  124. return self.async_show_form(
  125. step_id="user",
  126. data_schema=vol.Schema(schema),
  127. errors=errors,
  128. )
  129. async def async_test_connection(config: dict, hass: HomeAssistant):
  130. device = TuyaLocalDevice(
  131. "Test", config[CONF_DEVICE_ID], config[CONF_HOST], config[CONF_LOCAL_KEY], hass
  132. )
  133. await device.async_refresh()
  134. return device if device.has_returned_state else None