__init__.py 9.7 KB

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