__init__.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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_POLL_ONLY,
  17. CONF_PROTOCOL_VERSION,
  18. CONF_TYPE,
  19. DOMAIN,
  20. )
  21. from .device import setup_device, async_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. "Unable to determine type for device %s",
  36. config[CONF_DEVICE_ID],
  37. )
  38. return False
  39. entry.data = {
  40. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  41. CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
  42. CONF_HOST: config[CONF_HOST],
  43. }
  44. entry.version = 2
  45. if entry.version == 2:
  46. # CONF_TYPE is not configurable, move from options to main config.
  47. config = {**entry.data, **entry.options, "name": entry.title}
  48. opts = {**entry.options}
  49. # Ensure type has been migrated. Some users are reporting errors which
  50. # suggest it was removed completely. But that is probably due to
  51. # overwriting options without CONF_TYPE.
  52. if config.get(CONF_TYPE, CONF_TYPE_AUTO) == CONF_TYPE_AUTO:
  53. device = setup_device(hass, config)
  54. config[CONF_TYPE] = await device.async_inferred_type()
  55. if config[CONF_TYPE] is None:
  56. _LOGGER.error(
  57. "Unable to determine type for device %s",
  58. config[CONF_DEVICE_ID],
  59. )
  60. return False
  61. entry.data = {
  62. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  63. CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
  64. CONF_HOST: config[CONF_HOST],
  65. CONF_TYPE: config[CONF_TYPE],
  66. }
  67. opts.pop(CONF_TYPE, None)
  68. entry.options = {**opts}
  69. entry.version = 3
  70. if entry.version == 3:
  71. # Migrate to filename based config_type, to avoid needing to
  72. # parse config files to find the right one.
  73. config = {**entry.data, **entry.options, "name": entry.title}
  74. config_type = get_config(config[CONF_TYPE]).config_type
  75. # Special case for kogan_switch. Consider also v2.
  76. if config_type == "smartplugv1":
  77. device = setup_device(hass, config)
  78. config_type = await device.async_inferred_type()
  79. if config_type != "smartplugv2":
  80. config_type = "smartplugv1"
  81. entry.data = {
  82. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  83. CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
  84. CONF_HOST: config[CONF_HOST],
  85. CONF_TYPE: config_type,
  86. }
  87. entry.version = 4
  88. if entry.version <= 5:
  89. # Migrate unique ids of existing entities to new format
  90. old_id = entry.unique_id
  91. conf_file = get_config(entry.data[CONF_TYPE])
  92. if conf_file is None:
  93. _LOGGER.error(
  94. "Configuration file for %s not found",
  95. entry.data[CONF_TYPE],
  96. )
  97. return False
  98. @callback
  99. def update_unique_id(entity_entry):
  100. """Update the unique id of an entity entry."""
  101. e = conf_file.primary_entity
  102. if e.entity != entity_entry.platform:
  103. for e in conf_file.secondary_entities():
  104. if e.entity == entity_entry.platform:
  105. break
  106. if e.entity == entity_entry.platform:
  107. new_id = e.unique_id(old_id)
  108. if new_id != old_id:
  109. _LOGGER.info(
  110. "Migrating %s unique_id %s to %s",
  111. e.entity,
  112. old_id,
  113. new_id,
  114. )
  115. return {
  116. "new_unique_id": entity_entry.unique_id.replace(
  117. old_id,
  118. new_id,
  119. )
  120. }
  121. await async_migrate_entries(hass, entry.entry_id, update_unique_id)
  122. entry.version = 6
  123. if entry.version <= 8:
  124. # Deprecated entities are removed, trim the config back to required
  125. # config only
  126. conf = {**entry.data, **entry.options}
  127. entry.data = {
  128. CONF_DEVICE_ID: conf[CONF_DEVICE_ID],
  129. CONF_LOCAL_KEY: conf[CONF_LOCAL_KEY],
  130. CONF_HOST: conf[CONF_HOST],
  131. CONF_TYPE: conf[CONF_TYPE],
  132. }
  133. entry.options = {}
  134. entry.version = 9
  135. if entry.version <= 9:
  136. # Added protocol_version, default to auto
  137. conf = {**entry.data, **entry.options}
  138. entry.data = {
  139. CONF_DEVICE_ID: conf[CONF_DEVICE_ID],
  140. CONF_LOCAL_KEY: conf[CONF_LOCAL_KEY],
  141. CONF_HOST: conf[CONF_HOST],
  142. CONF_TYPE: conf[CONF_TYPE],
  143. CONF_PROTOCOL_VERSION: "auto",
  144. }
  145. entry.options = {}
  146. entry.version = 10
  147. if entry.version <= 10:
  148. conf = entry.data | entry.options
  149. entry.data = {
  150. CONF_DEVICE_ID: conf[CONF_DEVICE_ID],
  151. CONF_LOCAL_KEY: conf[CONF_LOCAL_KEY],
  152. CONF_HOST: conf[CONF_HOST],
  153. CONF_TYPE: conf[CONF_TYPE],
  154. CONF_PROTOCOL_VERSION: "auto",
  155. CONF_POLL_ONLY: False,
  156. }
  157. entry.options = {}
  158. entry.version = 11
  159. if entry.version <= 11:
  160. # Migrate unique ids of existing entities to new format
  161. device_id = entry.unique_id
  162. conf_file = get_config(entry.data[CONF_TYPE])
  163. if conf_file is None:
  164. _LOGGER.error(
  165. "Configuration file for %s not found",
  166. entry.data[CONF_TYPE],
  167. )
  168. return False
  169. @callback
  170. def update_unique_id12(entity_entry):
  171. """Update the unique id of an entity entry."""
  172. old_id = entity_entry.unique_id
  173. platform = entity_entry.entity_id.split(".", 1)[0]
  174. e = conf_file.primary_entity
  175. if e.name:
  176. expect_id = f"{device_id}-{e.name}"
  177. else:
  178. expect_id = device_id
  179. if e.entity != platform or expect_id != old_id:
  180. for e in conf_file.secondary_entities():
  181. if e.name:
  182. expect_id = f"{device_id}-{e.name}"
  183. else:
  184. expect_id = device_id
  185. if e.entity == platform and expect_id == old_id:
  186. break
  187. if e.entity == platform and expect_id == old_id:
  188. new_id = e.unique_id(device_id)
  189. if new_id != old_id:
  190. _LOGGER.info(
  191. "Migrating %s unique_id %s to %s",
  192. e.entity,
  193. old_id,
  194. new_id,
  195. )
  196. return {
  197. "new_unique_id": entity_entry.unique_id.replace(
  198. old_id,
  199. new_id,
  200. )
  201. }
  202. await async_migrate_entries(hass, entry.entry_id, update_unique_id12)
  203. entry.version = 12
  204. return True
  205. async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
  206. _LOGGER.debug(
  207. "Setting up entry for device: %s",
  208. entry.data[CONF_DEVICE_ID],
  209. )
  210. config = {**entry.data, **entry.options, "name": entry.title}
  211. setup_device(hass, config)
  212. device_conf = get_config(entry.data[CONF_TYPE])
  213. if device_conf is None:
  214. _LOGGER.error("Configuration file for %s not found", config[CONF_TYPE])
  215. return False
  216. entities = set()
  217. e = device_conf.primary_entity
  218. entities.add(e.entity)
  219. for e in device_conf.secondary_entities():
  220. entities.add(e.entity)
  221. await hass.config_entries.async_forward_entry_setups(entry, entities)
  222. entry.add_update_listener(async_update_entry)
  223. return True
  224. async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
  225. _LOGGER.debug("Unloading entry for device: %s", entry.data[CONF_DEVICE_ID])
  226. config = entry.data
  227. data = hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  228. device_conf = get_config(config[CONF_TYPE])
  229. if device_conf is None:
  230. _LOGGER.error("Configuration file for %s not found", config[CONF_TYPE])
  231. return False
  232. entities = {}
  233. e = device_conf.primary_entity
  234. if e.config_id in data:
  235. entities[e.entity] = True
  236. for e in device_conf.secondary_entities():
  237. if e.config_id in data:
  238. entities[e.entity] = True
  239. for e in entities:
  240. await hass.config_entries.async_forward_entry_unload(entry, e)
  241. await async_delete_device(hass, config)
  242. del hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  243. return True
  244. async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry):
  245. _LOGGER.debug("Updating entry for device: %s", entry.data[CONF_DEVICE_ID])
  246. await async_unload_entry(hass, entry)
  247. await async_setup_entry(hass, entry)