__init__.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. """
  2. Platform for Tuya WiFi-connected devices.
  3. Based on nikrolls/homeassistant-goldair-climate for Goldair branded devices.
  4. Based on sean6541/tuya-homeassistant for service call logic, and TarxBoy's
  5. investigation into Goldair's tuyapi statuses
  6. https://github.com/codetheweb/tuyapi/issues/31.
  7. """
  8. import logging
  9. from homeassistant.config_entries import ConfigEntry
  10. from homeassistant.const import CONF_HOST
  11. from homeassistant.core import HomeAssistant, callback
  12. from homeassistant.helpers.entity_registry import async_migrate_entries
  13. from .const import (
  14. CONF_DEVICE_ID,
  15. CONF_LIGHT,
  16. CONF_LOCAL_KEY,
  17. CONF_LOCK,
  18. CONF_TYPE,
  19. DOMAIN,
  20. )
  21. from .device import setup_device, delete_device
  22. from .helpers.device_config import get_config
  23. _LOGGER = logging.getLogger(__name__)
  24. async def async_migrate_entry(hass, entry: ConfigEntry):
  25. """Migrate to latest config format."""
  26. CONF_TYPE_AUTO = "auto"
  27. if entry.version == 1:
  28. # Removal of Auto detection.
  29. config = {**entry.data, **entry.options, "name": entry.title}
  30. if config[CONF_TYPE] == CONF_TYPE_AUTO:
  31. device = setup_device(hass, config)
  32. config[CONF_TYPE] = await device.async_inferred_type()
  33. if config[CONF_TYPE] is None:
  34. _LOGGER.error(
  35. f"Unable to determine type for device {config[CONF_DEVICE_ID]}."
  36. )
  37. return False
  38. entry.data = {
  39. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  40. CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
  41. CONF_HOST: config[CONF_HOST],
  42. }
  43. entry.version = 2
  44. if entry.version == 2:
  45. # CONF_TYPE is not configurable, move it from options to the main config.
  46. config = {**entry.data, **entry.options, "name": entry.title}
  47. opts = {**entry.options}
  48. # Ensure type has been migrated. Some users are reporting errors which
  49. # suggest it was removed completely. But that is probably due to
  50. # overwriting options without CONF_TYPE.
  51. if config.get(CONF_TYPE, CONF_TYPE_AUTO) == CONF_TYPE_AUTO:
  52. device = setup_device(hass, config)
  53. config[CONF_TYPE] = await device.async_inferred_type()
  54. if config[CONF_TYPE] is None:
  55. _LOGGER.error(
  56. f"Unable to determine type for device {config[CONF_DEVICE_ID]}."
  57. )
  58. return False
  59. entry.data = {
  60. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  61. CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
  62. CONF_HOST: config[CONF_HOST],
  63. CONF_TYPE: config[CONF_TYPE],
  64. }
  65. opts.pop(CONF_TYPE, None)
  66. entry.options = {**opts}
  67. entry.version = 3
  68. if entry.version == 3:
  69. # Migrate to filename based config_type, to avoid needing to
  70. # parse config files to find the right one.
  71. config = {**entry.data, **entry.options, "name": entry.title}
  72. config_type = get_config(config[CONF_TYPE]).config_type
  73. # Special case for kogan_switch. Consider also v2.
  74. if config_type == "smartplugv1":
  75. device = setup_device(hass, config)
  76. config_type = await device.async_inferred_type()
  77. if config_type != "smartplugv2":
  78. config_type = "smartplugv1"
  79. entry.data = {
  80. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  81. CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
  82. CONF_HOST: config[CONF_HOST],
  83. CONF_TYPE: config_type,
  84. }
  85. entry.version = 4
  86. if entry.version <= 5:
  87. # Migrate unique ids of existing entities to new format
  88. old_id = entry.unique_id
  89. conf_file = get_config(entry.data[CONF_TYPE])
  90. if conf_file is None:
  91. _LOGGER.error(f"Configuration file for {entry.data[CONF_TYPE]} not found.")
  92. return False
  93. @callback
  94. def update_unique_id(entity_entry):
  95. """Update the unique id of an entity entry."""
  96. e = conf_file.primary_entity
  97. if e.entity != entity_entry.platform:
  98. for e in conf_file.secondary_entities():
  99. if e.entity == entity_entry.platform:
  100. break
  101. if e.entity == entity_entry.platform:
  102. new_id = e.unique_id(old_id)
  103. if new_id != old_id:
  104. _LOGGER.info(
  105. f"Migrating {e.entity} unique_id {old_id} to {new_id}."
  106. )
  107. return {
  108. "new_unique_id": entity_entry.unique_id.replace(old_id, new_id)
  109. }
  110. await async_migrate_entries(hass, entry.entry_id, update_unique_id)
  111. entry.version = 6
  112. if entry.version <= 8:
  113. # Deprecated entities are removed, trim the config back to required
  114. # config only
  115. conf = {**entry.data, **entry.options}
  116. entry.data = {
  117. CONF_DEVICE_ID: conf[CONF_DEVICE_ID],
  118. CONF_LOCAL_KEY: conf[CONF_LOCAL_KEY],
  119. CONF_HOST: conf[CONF_HOST],
  120. CONF_TYPE: conf[CONF_TYPE],
  121. }
  122. entry.options = {}
  123. entry.version = 9
  124. return True
  125. async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
  126. _LOGGER.debug(f"Setting up entry for device: {entry.data[CONF_DEVICE_ID]}")
  127. config = {**entry.data, **entry.options, "name": entry.title}
  128. setup_device(hass, config)
  129. device_conf = get_config(entry.data[CONF_TYPE])
  130. if device_conf is None:
  131. _LOGGER.error(f"Configuration file for {config[CONF_TYPE]} not found.")
  132. return False
  133. entities = set()
  134. e = device_conf.primary_entity
  135. entities.add(e.entity)
  136. for e in device_conf.secondary_entities():
  137. entities.add(e.entity)
  138. await hass.config_entries.async_forward_entry_setups(entry, entities)
  139. entry.add_update_listener(async_update_entry)
  140. return True
  141. async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
  142. _LOGGER.debug(f"Unloading entry for device: {entry.data[CONF_DEVICE_ID]}")
  143. config = entry.data
  144. data = hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  145. device_conf = get_config(config[CONF_TYPE])
  146. if device_conf is None:
  147. _LOGGER.error(f"Configuration file for {config[CONF_TYPE]} not found.")
  148. return False
  149. entities = {}
  150. e = device_conf.primary_entity
  151. if e.config_id in data:
  152. entities[e.entity] = True
  153. for e in device_conf.secondary_entities():
  154. if e.config_id in data:
  155. entities[e.entity] = True
  156. for e in entities:
  157. await hass.config_entries.async_forward_entry_unload(entry, e)
  158. delete_device(hass, config)
  159. del hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  160. return True
  161. async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry):
  162. _LOGGER.debug(f"Updating entry for device: {entry.data[CONF_DEVICE_ID]}")
  163. await async_unload_entry(hass, entry)
  164. await async_setup_entry(hass, entry)