__init__.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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. return True
  823. async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
  824. device_id = get_device_id(entry.data)
  825. _LOGGER.debug(
  826. "Setting up entry for device: %s",
  827. device_id,
  828. )
  829. config = {**entry.data, **entry.options, "name": entry.title}
  830. try:
  831. device = await hass.async_add_executor_job(setup_device, hass, config)
  832. await device.async_refresh()
  833. except Exception as e:
  834. cleanup_failed_device(hass, device_id)
  835. raise ConfigEntryNotReady("tuya-local device not ready") from e
  836. if not device.has_returned_state:
  837. cleanup_failed_device(hass, device_id)
  838. raise ConfigEntryNotReady("tuya-local device offline")
  839. device_conf = await hass.async_add_executor_job(
  840. get_config,
  841. entry.data[CONF_TYPE],
  842. )
  843. if device_conf is None:
  844. _LOGGER.error(NOT_FOUND, config[CONF_TYPE])
  845. return False
  846. entities = set()
  847. for e in device_conf.all_entities():
  848. entities.add(e.entity)
  849. await hass.config_entries.async_forward_entry_setups(entry, entities)
  850. await async_setup_services(hass, entities)
  851. entry.add_update_listener(async_update_entry)
  852. return True
  853. async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
  854. _LOGGER.debug("Unloading entry for device: %s", get_device_id(entry.data))
  855. config = entry.data
  856. data = hass.data[DOMAIN][get_device_id(config)]
  857. device_conf = await hass.async_add_executor_job(
  858. get_config,
  859. config[CONF_TYPE],
  860. )
  861. if device_conf is None:
  862. _LOGGER.error(NOT_FOUND, config[CONF_TYPE])
  863. return False
  864. entities = {}
  865. for e in device_conf.all_entities():
  866. if e.config_id in data:
  867. entities[e.entity] = True
  868. for e in entities:
  869. await hass.config_entries.async_forward_entry_unload(entry, e)
  870. await async_delete_device(hass, config)
  871. del hass.data[DOMAIN][get_device_id(config)]
  872. return True
  873. async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry):
  874. _LOGGER.debug("Updating entry for device: %s", get_device_id(entry.data))
  875. await async_unload_entry(hass, entry)
  876. await async_setup_entry(hass, entry)