__init__.py 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. """
  2. Integration 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.exceptions import ConfigEntryNotReady
  13. from homeassistant.helpers.entity_registry import (
  14. async_get as async_get_entity_registry,
  15. )
  16. from homeassistant.helpers.entity_registry import async_migrate_entries
  17. from homeassistant.util import slugify
  18. from .const import (
  19. CONF_DEVICE_CID,
  20. CONF_DEVICE_ID,
  21. CONF_LOCAL_KEY,
  22. CONF_POLL_ONLY,
  23. CONF_PROTOCOL_VERSION,
  24. CONF_TYPE,
  25. DOMAIN,
  26. )
  27. from .device import async_delete_device, get_device_id, setup_device
  28. from .helpers.device_config import get_config
  29. from .services import async_setup_services
  30. _LOGGER = logging.getLogger(__name__)
  31. NOT_FOUND = "Configuration file for %s not found"
  32. def replace_unique_ids(entity_entry, device_id, conf_file, replacements):
  33. """Update the unique id of an entry based on a table of replacements."""
  34. old_id = entity_entry.unique_id
  35. platform = entity_entry.entity_id.split(".", 1)[0]
  36. for suffix, new_suffix in replacements.items():
  37. if old_id.endswith(suffix):
  38. # If the entity still exists in the config, do not migrate
  39. for e in conf_file.all_entities():
  40. if e.unique_id(device_id) == old_id:
  41. return None
  42. for e in conf_file.all_entities():
  43. new_id = e.unique_id(device_id)
  44. if e.entity == platform and not e.name and new_id.endswith(new_suffix):
  45. break
  46. if e.entity == platform and not e.name and new_id.endswith(new_suffix):
  47. _LOGGER.info(
  48. "Migrating %s unique_id %s to %s",
  49. e.entity,
  50. old_id,
  51. new_id,
  52. )
  53. return {
  54. "new_unique_id": entity_entry.unique_id.replace(
  55. old_id,
  56. new_id,
  57. )
  58. }
  59. def get_device_unique_id(entry: ConfigEntry):
  60. """Get the unique id for the device from the config entry."""
  61. return (
  62. entry.unique_id
  63. or entry.data.get(CONF_DEVICE_CID)
  64. or entry.data.get(CONF_DEVICE_ID)
  65. )
  66. def cleanup_failed_device(hass: HomeAssistant, device_id: str):
  67. """Drop cached device objects left behind by failed setup."""
  68. domain_data = hass.data.get(DOMAIN, {})
  69. stale = domain_data.pop(device_id, None)
  70. if not stale:
  71. return
  72. api = stale.get("tuyadevice")
  73. if api:
  74. api.set_socketPersistent(False)
  75. if api.parent:
  76. api.parent.set_socketPersistent(False)
  77. async def async_migrate_entry(hass, entry: ConfigEntry):
  78. """Migrate to latest config format."""
  79. CONF_TYPE_AUTO = "auto"
  80. if entry.version == 1:
  81. # Removal of Auto detection.
  82. config = {**entry.data, **entry.options, "name": entry.title}
  83. if config[CONF_TYPE] == CONF_TYPE_AUTO:
  84. device = await hass.async_add_executor_job(
  85. setup_device,
  86. hass,
  87. config,
  88. )
  89. config[CONF_TYPE] = await device.async_inferred_type()
  90. if config[CONF_TYPE] is None:
  91. _LOGGER.error(
  92. "Unable to determine type for device %s",
  93. config[CONF_DEVICE_ID],
  94. )
  95. return False
  96. hass.config_entries.async_update_entry(
  97. entry,
  98. data={
  99. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  100. CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
  101. CONF_HOST: config[CONF_HOST],
  102. },
  103. version=2,
  104. )
  105. if entry.version == 2:
  106. # CONF_TYPE is not configurable, move from options to main config.
  107. config = {**entry.data, **entry.options, "name": entry.title}
  108. opts = {**entry.options}
  109. # Ensure type has been migrated. Some users are reporting errors which
  110. # suggest it was removed completely. But that is probably due to
  111. # overwriting options without CONF_TYPE.
  112. if config.get(CONF_TYPE, CONF_TYPE_AUTO) == CONF_TYPE_AUTO:
  113. device = await hass.async_add_executor_job(
  114. setup_device,
  115. hass,
  116. config,
  117. )
  118. config[CONF_TYPE] = await device.async_inferred_type()
  119. if config[CONF_TYPE] is None:
  120. _LOGGER.error(
  121. "Unable to determine type for device %s",
  122. config[CONF_DEVICE_ID],
  123. )
  124. return False
  125. opts.pop(CONF_TYPE, None)
  126. hass.config_entries.async_update_entry(
  127. entry,
  128. data={
  129. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  130. CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
  131. CONF_HOST: config[CONF_HOST],
  132. CONF_TYPE: config[CONF_TYPE],
  133. },
  134. options={**opts},
  135. version=3,
  136. )
  137. if entry.version == 3:
  138. # Migrate to filename based config_type, to avoid needing to
  139. # parse config files to find the right one.
  140. config = {**entry.data, **entry.options, "name": entry.title}
  141. config_yaml = await hass.async_add_executor_job(
  142. get_config,
  143. config[CONF_TYPE],
  144. )
  145. config_type = config_yaml.config_type
  146. # Special case for kogan_switch. Consider also v2.
  147. if config_type == "smartplugv1":
  148. device = await hass.async_add_executor_job(
  149. setup_device,
  150. hass,
  151. config,
  152. )
  153. config_type = await device.async_inferred_type()
  154. if config_type != "smartplugv2":
  155. config_type = "smartplugv1"
  156. hass.config_entries.async_update_entry(
  157. entry,
  158. data={
  159. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  160. CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
  161. CONF_HOST: config[CONF_HOST],
  162. CONF_TYPE: config_type,
  163. },
  164. version=4,
  165. )
  166. if entry.version <= 5:
  167. # Migrate unique ids of existing entities to new format
  168. old_id = get_device_unique_id(entry)
  169. conf_file = await hass.async_add_executor_job(
  170. get_config,
  171. entry.data[CONF_TYPE],
  172. )
  173. if conf_file is None:
  174. _LOGGER.error(NOT_FOUND, entry.data[CONF_TYPE])
  175. return False
  176. @callback
  177. def update_unique_id(entity_entry):
  178. """Update the unique id of an entity entry."""
  179. for e in conf_file.all_entities():
  180. if e.entity == entity_entry.platform:
  181. break
  182. if e.entity == entity_entry.platform:
  183. new_id = e.unique_id(old_id)
  184. if new_id != old_id:
  185. _LOGGER.info(
  186. "Migrating %s unique_id %s to %s",
  187. e.entity,
  188. old_id,
  189. new_id,
  190. )
  191. return {
  192. "new_unique_id": entity_entry.unique_id.replace(
  193. old_id,
  194. new_id,
  195. )
  196. }
  197. await async_migrate_entries(hass, entry.entry_id, update_unique_id)
  198. hass.config_entries.async_update_entry(entry, version=6)
  199. if entry.version <= 8:
  200. # Deprecated entities are removed, trim the config back to required
  201. # config only
  202. conf = {**entry.data, **entry.options}
  203. hass.config_entries.async_update_entry(
  204. entry,
  205. data={
  206. CONF_DEVICE_ID: conf[CONF_DEVICE_ID],
  207. CONF_LOCAL_KEY: conf[CONF_LOCAL_KEY],
  208. CONF_HOST: conf[CONF_HOST],
  209. CONF_TYPE: conf[CONF_TYPE],
  210. },
  211. options={},
  212. version=9,
  213. )
  214. if entry.version <= 9:
  215. # Added protocol_version, default to auto
  216. conf = {**entry.data, **entry.options}
  217. hass.config_entries.async_update_entry(
  218. entry,
  219. data={
  220. CONF_DEVICE_ID: conf[CONF_DEVICE_ID],
  221. CONF_LOCAL_KEY: conf[CONF_LOCAL_KEY],
  222. CONF_HOST: conf[CONF_HOST],
  223. CONF_TYPE: conf[CONF_TYPE],
  224. CONF_PROTOCOL_VERSION: "auto",
  225. },
  226. options={},
  227. version=10,
  228. )
  229. if entry.version <= 10:
  230. conf = entry.data | entry.options
  231. hass.config_entries.async_update_entry(
  232. entry,
  233. data={
  234. CONF_DEVICE_ID: conf[CONF_DEVICE_ID],
  235. CONF_LOCAL_KEY: conf[CONF_LOCAL_KEY],
  236. CONF_HOST: conf[CONF_HOST],
  237. CONF_TYPE: conf[CONF_TYPE],
  238. CONF_PROTOCOL_VERSION: conf[CONF_PROTOCOL_VERSION],
  239. CONF_POLL_ONLY: False,
  240. },
  241. options={},
  242. version=11,
  243. )
  244. if entry.version <= 11:
  245. # Migrate unique ids of existing entities to new format
  246. device_id = get_device_unique_id(entry)
  247. conf_file = await hass.async_add_executor_job(
  248. get_config,
  249. entry.data[CONF_TYPE],
  250. )
  251. if conf_file is None:
  252. _LOGGER.error(
  253. NOT_FOUND,
  254. entry.data[CONF_TYPE],
  255. )
  256. return False
  257. @callback
  258. def update_unique_id12(entity_entry):
  259. """Update the unique id of an entity entry."""
  260. old_id = entity_entry.unique_id
  261. platform = entity_entry.entity_id.split(".", 1)[0]
  262. for e in conf_file.all_entities():
  263. if e.name:
  264. expect_id = f"{device_id}-{slugify(e.name)}"
  265. else:
  266. expect_id = device_id
  267. if e.entity == platform and expect_id == old_id:
  268. break
  269. if e.entity == platform and expect_id == old_id:
  270. new_id = e.unique_id(device_id)
  271. if new_id != old_id:
  272. _LOGGER.info(
  273. "Migrating %s unique_id %s to %s",
  274. e.entity,
  275. old_id,
  276. new_id,
  277. )
  278. return {
  279. "new_unique_id": entity_entry.unique_id.replace(
  280. old_id,
  281. new_id,
  282. )
  283. }
  284. await async_migrate_entries(hass, entry.entry_id, update_unique_id12)
  285. hass.config_entries.async_update_entry(entry, version=12)
  286. if entry.version <= 12:
  287. # Migrate unique ids of existing entities to new format taking into
  288. # account device_class if name is missing.
  289. device_id = get_device_unique_id(entry)
  290. conf_file = await hass.async_add_executor_job(
  291. get_config,
  292. entry.data[CONF_TYPE],
  293. )
  294. if conf_file is None:
  295. _LOGGER.error(
  296. NOT_FOUND,
  297. entry.data[CONF_TYPE],
  298. )
  299. return False
  300. @callback
  301. def update_unique_id13(entity_entry):
  302. """Update the unique id of an entity entry."""
  303. old_id = entity_entry.unique_id
  304. platform = entity_entry.entity_id.split(".", 1)[0]
  305. # if unique_id ends with platform name, then this may have
  306. # changed with the addition of device_class.
  307. if old_id.endswith(platform):
  308. for e in conf_file.all_entities():
  309. if e.entity == platform and not e.name:
  310. break
  311. if e.entity == platform and not e.name:
  312. new_id = e.unique_id(device_id)
  313. if new_id != old_id:
  314. _LOGGER.info(
  315. "Migrating %s unique_id %s to %s",
  316. e.entity,
  317. old_id,
  318. new_id,
  319. )
  320. return {
  321. "new_unique_id": entity_entry.unique_id.replace(
  322. old_id,
  323. new_id,
  324. )
  325. }
  326. else:
  327. replacements = {
  328. "sensor_co2": "sensor_carbon_dioxide",
  329. "sensor_co": "sensor_carbon_monoxide",
  330. "sensor_pm2_5": "sensor_pm25",
  331. "sensor_pm_10": "sensor_pm10",
  332. "sensor_pm_1_0": "sensor_pm1",
  333. "sensor_pm_2_5": "sensor_pm25",
  334. "sensor_tvoc": "sensor_volatile_organic_compounds",
  335. "sensor_current_humidity": "sensor_humidity",
  336. "sensor_current_temperature": "sensor_temperature",
  337. }
  338. return replace_unique_ids(
  339. entity_entry, device_id, conf_file, replacements
  340. )
  341. await async_migrate_entries(hass, entry.entry_id, update_unique_id13)
  342. hass.config_entries.async_update_entry(entry, version=13)
  343. if entry.version == 13 and entry.minor_version < 2:
  344. # Migrate unique ids of existing entities to new id taking into
  345. # account translation_key, and standardising naming
  346. device_id = get_device_unique_id(entry)
  347. conf_file = await hass.async_add_executor_job(
  348. get_config,
  349. entry.data[CONF_TYPE],
  350. )
  351. if conf_file is None:
  352. _LOGGER.error(
  353. NOT_FOUND,
  354. entry.data[CONF_TYPE],
  355. )
  356. return False
  357. @callback
  358. def update_unique_id13_2(entity_entry):
  359. """Update the unique id of an entity entry."""
  360. # Standardistion of entity naming to use translation_key
  361. replacements = {
  362. # special meaning of None to handle _full and _empty variants
  363. "binary_sensor_tank": None,
  364. "binary_sensor_tank_full_or_missing": "binary_sensor_tank_full",
  365. "binary_sensor_water_tank_full": "binary_sensor_tank_full",
  366. "binary_sensor_low_water": "binary_sensor_tank_empty",
  367. "binary_sensor_water_tank_empty": "binary_sensor_tank_empty",
  368. "binary_sensor_fault": "binary_sensor_problem",
  369. "binary_sensor_error": "binary_sensor_problem",
  370. "binary_sensor_fault_alarm": "binary_sensor_problem",
  371. "binary_sensor_errors": "binary_sensor_problem",
  372. "binary_sensor_defrosting": "binary_sensor_defrost",
  373. "binary_sensor_anti_frost": "binary_sensor_defrost",
  374. "binary_sensor_anti_freeze": "binary_sensor_defrost",
  375. "binary_sensor_low_battery": "binary_sensor_battery",
  376. "binary_sensor_low_battery_alarm": "binary_sensor_battery",
  377. "select_temperature_units": "select_temperature_unit",
  378. "select_display_temperature_unit": "select_temperature_unit",
  379. "select_display_unit": "select_temperature_unit",
  380. "select_display_units": "select_temperature_unit",
  381. "select_temperature_display_units": "select_temperature_unit",
  382. "switch_defrost": "switch_anti_frost",
  383. "switch_frost_protection": "switch_anti_frost",
  384. }
  385. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  386. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_2)
  387. hass.config_entries.async_update_entry(entry, minor_version=2)
  388. if entry.version == 13 and entry.minor_version < 3:
  389. # Migrate unique ids of existing entities to new id taking into
  390. # account translation_key, and standardising naming
  391. device_id = get_device_unique_id(entry)
  392. conf_file = await hass.async_add_executor_job(
  393. get_config,
  394. entry.data[CONF_TYPE],
  395. )
  396. if conf_file is None:
  397. _LOGGER.error(
  398. NOT_FOUND,
  399. entry.data[CONF_TYPE],
  400. )
  401. return False
  402. @callback
  403. def update_unique_id13_3(entity_entry):
  404. """Update the unique id of an entity entry."""
  405. # Standardistion of entity naming to use translation_key
  406. replacements = {
  407. "light_front_display": "light_display",
  408. "light_lcd_brightness": "light_display",
  409. "light_coal_bed": "light_logs",
  410. "light_ember": "light_embers",
  411. "light_led_indicator": "light_indicator",
  412. "light_status_indicator": "light_indicator",
  413. "light_indicator_light": "light_indicator",
  414. "light_indicators": "light_indicator",
  415. "light_night_light": "light_nightlight",
  416. "number_tiemout_period": "number_timeout_period",
  417. "sensor_remaining_time": "sensor_time_remaining",
  418. "sensor_timer_remain": "sensor_time_remaining",
  419. "sensor_timer": "sensor_time_remaining",
  420. "sensor_timer_countdown": "sensor_time_remaining",
  421. "sensor_timer_remaining": "sensor_time_remaining",
  422. "sensor_time_left": "sensor_time_remaining",
  423. "sensor_timer_minutes_left": "sensor_time_remaining",
  424. "sensor_timer_time_left": "sensor_time_remaining",
  425. "sensor_auto_shutoff_time_remaining": "sensor_time_remaining",
  426. "sensor_warm_time_remaining": "sensor_time_remaining",
  427. "sensor_run_time_remaining": "sensor_time_remaining",
  428. "switch_ioniser": "switch_ionizer",
  429. "switch_run_uv_cycle": "switch_uv_sterilization",
  430. "switch_uv_light": "switch_uv_sterilization",
  431. "switch_ihealth": "switch_uv_sterilization",
  432. "switch_uv_lamp": "switch_uv_sterilization",
  433. "switch_anti_freeze": "switch_anti_frost",
  434. }
  435. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  436. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_3)
  437. hass.config_entries.async_update_entry(entry, minor_version=3)
  438. if entry.version == 13 and entry.minor_version < 4:
  439. conf = entry.data | entry.options
  440. conf_type = conf[CONF_TYPE]
  441. # Duolicate config removal - make sure the correct one is used
  442. if conf_type == "ble-yl01_waterquality_tester":
  443. conf_type = "ble_yl01_watertester"
  444. hass.config_entries.async_update_entry(
  445. entry,
  446. data={**entry.data, CONF_TYPE: conf_type},
  447. options={**entry.options},
  448. minor_version=4,
  449. )
  450. if entry.version == 13 and entry.minor_version < 5:
  451. # Migrate unique ids of existing entities to new id taking into
  452. # account translation_key, and standardising naming
  453. device_id = get_device_unique_id(entry)
  454. conf_file = await hass.async_add_executor_job(
  455. get_config,
  456. entry.data[CONF_TYPE],
  457. )
  458. if conf_file is None:
  459. _LOGGER.error(
  460. NOT_FOUND,
  461. entry.data[CONF_TYPE],
  462. )
  463. return False
  464. @callback
  465. def update_unique_id13_5(entity_entry):
  466. """Update the unique id of an entity entry."""
  467. # Standardistion of entity naming to use translation_key
  468. replacements = {
  469. "number_countdown": "number_timer",
  470. "select_countdown": "select_timer",
  471. "sensor_countdown": "sensor_time_remaining",
  472. "sensor_countdown_timer": "sensor_time_remaining",
  473. "fan": "fan_aroma_diffuser",
  474. }
  475. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  476. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_5)
  477. if entry.version == 13 and entry.minor_version < 6:
  478. # Migrate unique ids of existing entities to new id taking into
  479. # account translation_key, and standardising naming
  480. device_id = get_device_unique_id(entry)
  481. conf_file = await hass.async_add_executor_job(
  482. get_config,
  483. entry.data[CONF_TYPE],
  484. )
  485. if conf_file is None:
  486. _LOGGER.error(
  487. NOT_FOUND,
  488. entry.data[CONF_TYPE],
  489. )
  490. return False
  491. @callback
  492. def update_unique_id13_6(entity_entry):
  493. """Update the unique id of an entity entry."""
  494. # Standardistion of entity naming to use translation_key
  495. replacements = {
  496. "switch_sleep_mode": "switch_sleep",
  497. "switch_sleep_timer": "switch_sleep",
  498. "select_voice_language": "select_language",
  499. "select_restore_power_state": "select_initial_state",
  500. "select_power_on_state": "select_initial_state",
  501. "select_restart_status": "select_initial_state",
  502. "select_poweron_status": "select_initial_state",
  503. "select_mop_mode": "select_mopping",
  504. "select_mop_control": "select_mopping",
  505. "select_water_setting": "select_mopping",
  506. "select_water_mode": "select_mopping",
  507. "select_water_control": "select_mopping",
  508. "select_water_tank": "select_mopping",
  509. "light_light": "light",
  510. "light_lights": "light",
  511. }
  512. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  513. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_6)
  514. hass.config_entries.async_update_entry(entry, minor_version=6)
  515. if entry.version == 13 and entry.minor_version < 7:
  516. # Migrate unique ids of existing entities to new id taking into
  517. # account translation_key, and standardising naming
  518. device_id = get_device_unique_id(entry)
  519. conf_file = await hass.async_add_executor_job(
  520. get_config,
  521. entry.data[CONF_TYPE],
  522. )
  523. if conf_file is None:
  524. _LOGGER.error(
  525. NOT_FOUND,
  526. entry.data[CONF_TYPE],
  527. )
  528. return False
  529. @callback
  530. def update_unique_id13_7(entity_entry):
  531. """Update the unique id of an entity entry."""
  532. # Standardistion of entity naming to use translation_key
  533. replacements = {
  534. "sensor_charger_state": "sensor_status",
  535. }
  536. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  537. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_7)
  538. hass.config_entries.async_update_entry(entry, minor_version=7)
  539. if entry.version == 13 and entry.minor_version < 8:
  540. # Migrate unique ids of existing entities to new id taking into
  541. # account translation_key, and standardising naming
  542. device_id = get_device_unique_id(entry)
  543. conf_file = await hass.async_add_executor_job(
  544. get_config,
  545. entry.data[CONF_TYPE],
  546. )
  547. if conf_file is None:
  548. _LOGGER.error(
  549. NOT_FOUND,
  550. entry.data[CONF_TYPE],
  551. )
  552. return False
  553. @callback
  554. def update_unique_id13_8(entity_entry):
  555. """Update the unique id of an entity entry."""
  556. # Standardistion of entity naming to use translation_key
  557. replacements = {
  558. "sensor_forward_energy": "sensor_energy_consumed",
  559. "sensor_total_forward_energy": "sensor_energy_consumed",
  560. "sensor_energy_in": "sensor_energy_consumed",
  561. "sensor_reverse_energy": "sensor_energy_produced",
  562. "sensor_energy_out": "sensor_energy_produced",
  563. "sensor_forward_energy_a": "sensor_energy_consumed_a",
  564. "sensor_reverse_energy_a": "sensor_energy_produced_a",
  565. "sensor_forward_energy_b": "sensor_energy_consumed_b",
  566. "sensor_reverse_energy_b": "sensor_energy_produced_b",
  567. "sensor_energy_consumption_a": "sensor_energy_consumed_a",
  568. "sensor_energy_consumption_b": "sensor_energy_consumed_b",
  569. "number_timer_switch_1": "number_timer_1",
  570. "number_timer_switch_2": "number_timer_2",
  571. "number_timer_switch_3": "number_timer_3",
  572. "number_timer_switch_4": "number_timer_4",
  573. "number_timer_switch_5": "number_timer_5",
  574. "number_timer_socket_1": "number_timer_1",
  575. "number_timer_socket_2": "number_timer_2",
  576. "number_timer_socket_3": "number_timer_3",
  577. "number_timer_outlet_1": "number_timer_1",
  578. "number_timer_outlet_2": "number_timer_2",
  579. "number_timer_outlet_3": "number_timer_3",
  580. "number_timer_gang_1": "number_timer_1",
  581. "number_timer_gang_2": "number_timer_2",
  582. "number_timer_gang_3": "number_timer_3",
  583. }
  584. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  585. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_8)
  586. hass.config_entries.async_update_entry(entry, minor_version=8)
  587. if entry.version == 13 and entry.minor_version < 9:
  588. # Migrate unique ids of existing entities to new id taking into
  589. # account translation_key, and standardising naming
  590. device_id = get_device_unique_id(entry)
  591. conf_file = await hass.async_add_executor_job(
  592. get_config,
  593. entry.data[CONF_TYPE],
  594. )
  595. if conf_file is None:
  596. _LOGGER.error(
  597. NOT_FOUND,
  598. entry.data[CONF_TYPE],
  599. )
  600. return False
  601. @callback
  602. def update_unique_id13_9(entity_entry):
  603. """Update the unique id of an entity entry."""
  604. # Standardistion of entity naming to use translation_key
  605. replacements = {
  606. "number_delay_time": "number_delay",
  607. }
  608. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  609. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_9)
  610. hass.config_entries.async_update_entry(entry, minor_version=9)
  611. if entry.version == 13 and entry.minor_version < 10:
  612. # Migrate unique ids of existing entities to new id taking into
  613. # account translation_key, and standardising naming
  614. device_id = get_device_unique_id(entry)
  615. conf_file = await hass.async_add_executor_job(
  616. get_config,
  617. entry.data[CONF_TYPE],
  618. )
  619. if conf_file is None:
  620. _LOGGER.error(
  621. NOT_FOUND,
  622. entry.data[CONF_TYPE],
  623. )
  624. return False
  625. @callback
  626. def update_unique_id13_10(entity_entry):
  627. """Update the unique id of an entity entry."""
  628. # Standardistion of entity naming to use translation_key
  629. replacements = {
  630. "switch_disturb_switch": "switch_do_not_disturb",
  631. "number_temperature_correction": "number_temperature_calibration",
  632. "number_calibration_offset": "number_temperature_calibration",
  633. "number_high_temperature_limit": "number_maximum_temperature",
  634. "number_low_temperature_limit": "number_minimum_temperature",
  635. }
  636. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  637. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_10)
  638. hass.config_entries.async_update_entry(entry, minor_version=10)
  639. if entry.version == 13 and entry.minor_version < 12:
  640. # at some point HA stopped populating unique_id for device entries,
  641. # and our migrations have been using the wrong value. Migration
  642. # to correct that
  643. unique_id = entry.unique_id
  644. device_id = get_device_unique_id(entry)
  645. if unique_id is None:
  646. hass.config_entries.async_update_entry(
  647. entry,
  648. unique_id=device_id,
  649. )
  650. @callback
  651. def update_unique_id3_12(entity_entry):
  652. """Update the unique id of badly migrated entities."""
  653. old_id = entity_entry.unique_id
  654. if old_id.startswith("None-"):
  655. # new_id = f"{device_id}-{old_id[5:]}"
  656. # return {
  657. # "new_unique_id": new_id,
  658. # }
  659. # Rather than update, delete the bogus entities, as HA will
  660. # recreate them correctly, and maybe already has so duplicates
  661. # could result if they are migrated.
  662. registry = async_get_entity_registry(hass)
  663. registry.async_remove(entity_entry.entity_id)
  664. await async_migrate_entries(hass, entry.entry_id, update_unique_id3_12)
  665. hass.config_entries.async_update_entry(entry, minor_version=12)
  666. if entry.version == 13 and entry.minor_version < 13:
  667. # Migrate unique ids of existing entities to new id taking into
  668. # account translation_key, and standardising naming
  669. device_id = get_device_unique_id(entry)
  670. conf_file = await hass.async_add_executor_job(
  671. get_config,
  672. entry.data[CONF_TYPE],
  673. )
  674. if conf_file is None:
  675. _LOGGER.error(
  676. NOT_FOUND,
  677. entry.data[CONF_TYPE],
  678. )
  679. return False
  680. @callback
  681. def update_unique_id13_13(entity_entry):
  682. """Update the unique id of an entity entry."""
  683. # Standardistion of entity naming to use translation_key
  684. replacements = {
  685. "switch_flip": "switch_flip_image",
  686. "number_feed": "number_manual_feed",
  687. "number_feed_portions": "number_manual_feed",
  688. "number_manual_amount": "number_manual_feed",
  689. }
  690. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  691. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_13)
  692. hass.config_entries.async_update_entry(entry, minor_version=13)
  693. # 13.14 was botched, so repeat as 13.15
  694. if entry.version == 13 and entry.minor_version < 15:
  695. # Migrate unique ids of existing entities to new id taking into
  696. # account translation_key, and standardising naming
  697. device_id = get_device_unique_id(entry)
  698. conf_file = await hass.async_add_executor_job(
  699. get_config,
  700. entry.data[CONF_TYPE],
  701. )
  702. if conf_file is None:
  703. _LOGGER.error(
  704. NOT_FOUND,
  705. entry.data[CONF_TYPE],
  706. )
  707. return False
  708. @callback
  709. def update_unique_id13_15(entity_entry):
  710. """Update the unique id of an entity entry."""
  711. # Standardistion of entity naming to use translation_key
  712. replacements = {
  713. "sensor_filter_lifetime": "sensor_filter_life",
  714. "sensor_filter": "sensor_filter_life",
  715. "sensor_filter_days": "sensor_filter_life",
  716. "sensor_filter_remaining": "sensor_filter_life",
  717. "sensor_filter_remain": "sensor_filter_life",
  718. "sensor_filter_replacement": "sensor_filter_life",
  719. "sensor_filter_days_left": "sensor_filter_life",
  720. "sensor_filter_left_days": "sensor_filter_life",
  721. "sensor_filter_left": "sensor_filter_life",
  722. "sensor_filter_hours_left": "sensor_filter_life",
  723. "sensor_replace_filter_in": "sensor_filter_life",
  724. "sensor_filter_change_due": "sensor_filter_life",
  725. "sensor_filter_usage": "sensor_filter_life",
  726. "sensor_filter_used": "sensor_filter_life",
  727. "sensor_filter_time": "sensor_filter_life",
  728. "button_reset_filter": "button_filter_reset",
  729. "button_replace_filter": "button_filter_reset",
  730. "button_filter_changed": "button_filter_reset",
  731. "button_filter_replaced": "button_filter_reset",
  732. "select_motion_detection": "select_motion_sensitivity",
  733. "select_motion_distance": "select_motion_sensitivity",
  734. }
  735. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  736. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_15)
  737. hass.config_entries.async_update_entry(entry, minor_version=15)
  738. if entry.version == 13 and entry.minor_version < 16:
  739. # Migrate unique ids of existing entities to new id taking into
  740. # account translation_key, and standardising naming
  741. device_id = get_device_unique_id(entry)
  742. conf_file = await hass.async_add_executor_job(
  743. get_config,
  744. entry.data[CONF_TYPE],
  745. )
  746. if conf_file is None:
  747. _LOGGER.error(
  748. NOT_FOUND,
  749. entry.data[CONF_TYPE],
  750. )
  751. return False
  752. @callback
  753. def update_unique_id13_16(entity_entry):
  754. """Update the unique id of an entity entry."""
  755. # Standardistion of entity naming to use translation_key
  756. replacements = {
  757. "switch_beep": "switch_sound",
  758. "switch_mute": "switch_sound",
  759. "switch_muffling": "switch_sound",
  760. "switch_mute_sound": "switch_sound",
  761. "switch_mute_voice": "switch_sound",
  762. }
  763. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  764. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_16)
  765. hass.config_entries.async_update_entry(entry, minor_version=16)
  766. if entry.version == 13 and entry.minor_version < 17:
  767. # Migrate unique ids of existing entities to new id taking into
  768. # account translation_key, and standardising naming
  769. device_id = get_device_unique_id(entry)
  770. conf_file = await hass.async_add_executor_job(
  771. get_config,
  772. entry.data[CONF_TYPE],
  773. )
  774. if conf_file is None:
  775. _LOGGER.error(
  776. NOT_FOUND,
  777. entry.data[CONF_TYPE],
  778. )
  779. return False
  780. @callback
  781. def update_unique_id13_17(entity_entry):
  782. """Update the unique id of an entity entry."""
  783. # Standardistion of entity naming to use translation_key
  784. replacements = {
  785. "sensor_light_intensity": "sensor_illuminance",
  786. "sensor_rain": "sensor_precipitation",
  787. "sensor_rainfall_rate": "sensor_precipitation_intensity",
  788. "text_regular_schedule": "text_schedule",
  789. "text_program": "text_schedule",
  790. "text_program_data": "text_schedule",
  791. "text_weekly_program": "text_schedule",
  792. "text_weekprogram": "text_schedule",
  793. }
  794. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  795. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_17)
  796. hass.config_entries.async_update_entry(entry, minor_version=17)
  797. if entry.version == 13 and entry.minor_version < 18:
  798. # Migrate unique ids of existing entities to new id taking into
  799. # account translation_key, and standardising naming
  800. device_id = get_device_unique_id(entry)
  801. conf_file = await hass.async_add_executor_job(
  802. get_config,
  803. entry.data[CONF_TYPE],
  804. )
  805. if conf_file is None:
  806. _LOGGER.error(
  807. NOT_FOUND,
  808. entry.data[CONF_TYPE],
  809. )
  810. return False
  811. @callback
  812. def update_unique_id13_18(entity_entry):
  813. """Update the unique id of an entity entry."""
  814. # Standardistion of entity naming to use translation_key
  815. replacements = {
  816. "sensor_air_temperature": "sensor_ambient_temperature",
  817. "sensor_current_temperature": "sensor_temperature",
  818. }
  819. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  820. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_18)
  821. hass.config_entries.async_update_entry(entry, minor_version=18)
  822. if entry.version == 13 and entry.minor_version < 19:
  823. # Migrate unique ids of existing entities to new id taking into
  824. # account translation_key, and standardising naming
  825. device_id = get_device_unique_id(entry)
  826. conf_file = await hass.async_add_executor_job(
  827. get_config,
  828. entry.data[CONF_TYPE],
  829. )
  830. if conf_file is None:
  831. _LOGGER.error(
  832. NOT_FOUND,
  833. entry.data[CONF_TYPE],
  834. )
  835. return False
  836. @callback
  837. def update_unique_id13_19(entity_entry):
  838. """Update the unique id of an entity entry."""
  839. # Standardistion of entity naming to use translation_key
  840. replacements = {
  841. "fan_fan_with_presets": "fan_air_purifier",
  842. "fan_purifier": "fan_air_purifier",
  843. "fan": "fan_air_purifier",
  844. "light_ambient": "light_ambient_light",
  845. "number_snooze_time": "number_snooze_duration",
  846. "select_display": "select_display_brightness",
  847. "select_snooze_type": "select_snooze_action",
  848. "switch_internet_time": "switch_network_time",
  849. }
  850. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  851. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_19)
  852. hass.config_entries.async_update_entry(entry, minor_version=19)
  853. if entry.version == 13 and entry.minor_version < 20:
  854. # Migrate unique ids of existing entities to new id taking into
  855. # account translation_key, and standardising naming
  856. device_id = get_device_unique_id(entry)
  857. conf_file = await hass.async_add_executor_job(
  858. get_config,
  859. entry.data[CONF_TYPE],
  860. )
  861. if conf_file is None:
  862. _LOGGER.error(
  863. NOT_FOUND,
  864. entry.data[CONF_TYPE],
  865. )
  866. return False
  867. @callback
  868. def update_unique_id13_20(entity_entry):
  869. """Update the unique id of an entity entry."""
  870. # Standardistion of entity naming to use translation_key
  871. replacements = {
  872. "select_nightvision": "select_night_vision",
  873. "switch_timer_set": "switch_timer",
  874. "switch_timer_start": "switch_timer",
  875. "select_record_mode": "select_recording_mode",
  876. "switch_osd_watermark": "switch_watermark",
  877. "switch_timestamp": "switch_watermark",
  878. }
  879. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  880. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_20)
  881. hass.config_entries.async_update_entry(entry, minor_version=20)
  882. if entry.version == 13 and entry.minor_version < 21:
  883. # Migrate unique ids of existing entities to new id taking into
  884. # account translation_key, and standardising naming
  885. device_id = get_device_unique_id(entry)
  886. conf_file = await hass.async_add_executor_job(
  887. get_config,
  888. entry.data[CONF_TYPE],
  889. )
  890. if conf_file is None:
  891. _LOGGER.error(
  892. NOT_FOUND,
  893. entry.data[CONF_TYPE],
  894. )
  895. return False
  896. @callback
  897. def update_unique_id13_21(entity_entry):
  898. """Update the unique id of an entity entry."""
  899. # Standardistion of entity naming to use translation_key
  900. replacements = {
  901. "switch_motion_enable": "switch_motion_detection",
  902. "switch_motion_sensing": "switch_motion_detection",
  903. "swtich_motion_notification": "switch_motion_detection",
  904. }
  905. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  906. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_21)
  907. hass.config_entries.async_update_entry(entry, minor_version=21)
  908. return True
  909. async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
  910. device_id = get_device_id(entry.data)
  911. _LOGGER.debug(
  912. "Setting up entry for device: %s",
  913. device_id,
  914. )
  915. config = {**entry.data, **entry.options, "name": entry.title}
  916. try:
  917. device = await hass.async_add_executor_job(setup_device, hass, config)
  918. await device.async_refresh()
  919. except Exception as e:
  920. cleanup_failed_device(hass, device_id)
  921. raise ConfigEntryNotReady("tuya-local device not ready") from e
  922. if not device.has_returned_state:
  923. cleanup_failed_device(hass, device_id)
  924. raise ConfigEntryNotReady("tuya-local device offline")
  925. device_conf = await hass.async_add_executor_job(
  926. get_config,
  927. entry.data[CONF_TYPE],
  928. )
  929. if device_conf is None:
  930. _LOGGER.error(NOT_FOUND, config[CONF_TYPE])
  931. return False
  932. entities = set()
  933. for e in device_conf.all_entities():
  934. entities.add(e.entity)
  935. await hass.config_entries.async_forward_entry_setups(entry, entities)
  936. await async_setup_services(hass, entities)
  937. entry.add_update_listener(async_update_entry)
  938. return True
  939. async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
  940. device_id = get_device_id(entry.data)
  941. _LOGGER.debug("Unloading entry for device: %s", device_id)
  942. config = entry.data
  943. domain_data = hass.data.get(DOMAIN, {})
  944. data = domain_data.get(device_id)
  945. if data is None:
  946. await async_delete_device(hass, config)
  947. return True
  948. device_conf = await hass.async_add_executor_job(
  949. get_config,
  950. config[CONF_TYPE],
  951. )
  952. if device_conf is None:
  953. _LOGGER.error(NOT_FOUND, config[CONF_TYPE])
  954. return False
  955. entities = {}
  956. for e in device_conf.all_entities():
  957. if e.config_id in data:
  958. entities[e.entity] = True
  959. for e in entities:
  960. await hass.config_entries.async_forward_entry_unload(entry, e)
  961. await async_delete_device(hass, config)
  962. domain_data.pop(device_id, None)
  963. return True
  964. async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry):
  965. _LOGGER.debug("Updating entry for device: %s", get_device_id(entry.data))
  966. await async_unload_entry(hass, entry)
  967. await async_setup_entry(hass, entry)