__init__.py 6.8 KB

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